-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmojo_lexer.py
142 lines (129 loc) · 2.74 KB
/
mojo_lexer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# -----------------------------------------------------------------------------
# mojo_lexer.py
#
# A simple lexer for the mojo language.
# -----------------------------------------------------------------------------
import ply.lex as lex
# Reserved words
reserved = {
'program': 'PROGRAM',
'if': 'IF',
'else': 'ELSE',
'var': 'VAR',
'int': 'INT',
'float': 'FLOAT',
'string': 'STRING',
'bool': 'BOOLEAN',
'void': 'VOID',
'and' : 'AND',
'or' : 'OR',
'not' : 'NOT',
'input' : 'READ',
'print' : 'PRINT',
'def' : 'DEF',
'while' : 'WHILE',
'main' : 'MAIN',
'return' : 'RETURN',
'True' : 'TRUE',
'False' : 'FALSE',
# Predefined functions
'create_turtle' : 'CREATE_TURTLE',
'reset' : 'RESET',
'finish_drawing' : 'FINISH_DRAWING',
'pen_up' : 'PEN_UP',
'pen_down' : 'PEN_DOWN',
'begin_fill' : 'BEGIN_FILL',
'end_fill' : 'END_FILL',
'pen_color' : 'PEN_COLOR',
'fill_color' : 'FILL_COLOR',
'pen_width' : 'PEN_WIDTH',
'move_forward' : 'MOVE_FORWARD',
'move_right' : 'MOVE_RIGHT',
'move_left' : 'MOVE_LEFT',
'turn_right' : 'TURN_RIGHT',
'turn_left' : 'TURN_LEFT',
'draw_square' : 'DRAW_SQUARE',
'draw_triangle' : 'DRAW_TRIANGLE',
'draw_circle' : 'DRAW_CIRCLE',
'draw_rectangle' : 'DRAW_RECTANGLE',
'set_position' : 'SET_POSITION',
'set_speed' : 'SET_SPEED'
}
# Token list (Tuple)
tokens = list(reserved.values()) + [
'ID',
'PLUS',
'MINUS',
'TIMES',
'DIVIDE',
'LPAREN',
'RPAREN',
'LBRACE',
'RBRACE',
'LBRACKET',
'RBRACKET',
'COLON',
'SEMICOLON',
'COMMA',
'GT',
'LT',
'LE',
'GE',
'EQ',
'NE',
'ASSIGN',
'ICONST',
'FCONST',
'SCONST'
]
# Regular expressions for each token
t_PLUS = r'\+'
t_MINUS = r'\-'
t_TIMES = r'\*'
t_DIVIDE = r'/'
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_LBRACE = r'\{'
t_RBRACE= r'\}'
t_LBRACKET = r'\['
t_RBRACKET = r'\]'
t_COLON = r':'
t_SEMICOLON = r';'
t_COMMA = r','
t_GT = r'>'
t_LT = r'<'
t_GE = r'>='
t_LE = r'<='
t_EQ = r'=='
t_NE = r'!='
t_ASSIGN = r'='
t_SCONST = r'\".*\" | \'.*\''
# Characteres that will be ignored
t_ignore = ' \t'
def t_ID(t):
r'[a-zA-Z_][a-zA-Z0-9_]*'
# Verifies that is not a reserved word
t.type = reserved.get(t.value, 'ID')
return t
def t_FCONST(t):
r'-?[0-9]+\.[0-9][0-9]*'
return t
def t_ICONST(t):
r'-?[0-9]+'
return t
def t_newline(t):
r'\n+'
t.lexer.lineno += len(t.value)
# Error lexico
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
# Runs lex
mojo_lexer = lex.lex()
input_data = ''
mojo_lexer.input(input_data)
while True:
token = mojo_lexer.token()
if not token:
break
print(token)