-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre_parser.py
More file actions
245 lines (198 loc) · 6.9 KB
/
pre_parser.py
File metadata and controls
245 lines (198 loc) · 6.9 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
from sly import Parser
from definitions.compiler_exception import CompilerException
from lexer import CompilerLexer
from definitions.variable import *
class CompilerPreParser(Parser):
tokens = CompilerLexer.tokens
def __init__(self):
self.variables = {}
self.in_loop = False
self.range_of_loop = None
def declare_variable(self, name, line):
if name in self.variables.keys():
raise CompilerException("{} already defined".format(name), line)
new_variable = Identifier(name, None)
self.variables[name] = new_variable
def declare_iterator(self, name, line, range=None):
if name in self.variables.keys() and not isinstance(self.variables[name], Iterator):
raise CompilerException("{} cannot be used as an iterator. Iterator is local for each loop".format(name),
line)
if name not in self.variables.keys():
new_variable = Iterator(name, None)
if range is not None:
new_variable.occurrences = range
else:
new_variable.occurrences = 10
self.variables[name] = new_variable
def count_variable(self, variable, line):
if variable not in self.variables.keys():
raise CompilerException("{} not declared".format(variable), line)
else:
self.variables[variable].occurrences += 1
if self.in_loop:
if self.range_of_loop is not None:
self.variables[variable].occurrences += self.range_of_loop
else:
self.variables[variable].occurrences += 10
self.variables[variable].in_loop = True
def count_number(self, number):
if number not in self.variables.keys():
new_variable = Number(number, number, None)
new_variable.occurrences = 1
if self.in_loop:
new_variable.in_loop = True
self.variables[number] = new_variable
else:
self.variables[number].occurrences += 1
if self.in_loop:
if self.range_of_loop is not None:
self.variables[number].occurrences += self.range_of_loop
else:
self.variables[number].occurrences += 10
self.variables[number].in_loop = True
def declare_array(self, name, start, end):
variable = Array(name, start, end, 0)
self.variables[name] = variable
'''
program
'''
@_('DECLARE declarations BEGIN commands END')
def program(self, p):
return self.variables
@_('BEGIN commands END')
def program(self, p):
return self.variables
'''
declarations
'''
@_('declarations COMMA PIDENTIFIER')
def declarations(self, p):
self.declare_variable(p[2], p.lineno)
@_('declarations COMMA PIDENTIFIER LEFTBRACKET NUMBER COLON NUMBER RIGHTBRACKET')
def declarations(self, p):
self.declare_array(p[2], p[4], p[6])
@_('PIDENTIFIER')
def declarations(self, p):
self.declare_variable(p[0], p.lineno)
@_('PIDENTIFIER LEFTBRACKET NUMBER COLON NUMBER RIGHTBRACKET')
def declarations(self, p):
self.declare_array(p[0], p[2], p[4])
'''
commands
'''
@_('commands command')
def commands(self, p):
pass
@_('command')
def commands(self, p):
pass
'''
command
'''
@_('identifier ASSIGN expression SEMICOLON')
def command(self, p):
pass
@_('IF condition THEN commands ELSE begin_else_if commands ENDIF')
def command(self, p):
pass
@_('')
def begin_else_if(self, p):
pass
@_('IF condition THEN commands ENDIF')
def command(self, p):
pass
@_('WHILE begin_while condition DO commands ENDWHILE')
def command(self, p):
self.in_loop = False
self.range_of_loop = None
@_('REPEAT begin_while commands UNTIL condition SEMICOLON')
def command(self, p):
self.in_loop = False
self.range_of_loop = None
@_('')
def begin_while(self, p):
self.in_loop = True
@_('FOR PIDENTIFIER FROM value TO value DO begin_for_to commands ENDFOR')
def command(self, p):
self.in_loop = False
self.range_of_loop = None
@_('')
def begin_for_to(self, p):
self.in_loop = True
if isinstance(p[-4], Number) and isinstance(p[-2], Number):
if self.range_of_loop is not None:
self.range_of_loop *= abs(p[-2].value - p[-4].value)
else:
self.range_of_loop = p[-2].value - p[-4].value
self.declare_iterator(p[-6], -1, self.range_of_loop)
self.declare_iterator(p[-6], -1)
@_('FOR PIDENTIFIER FROM value DOWNTO value DO begin_for_downto commands ENDFOR')
def command(self, p):
self.in_loop = False
self.range_of_loop = None
@_('')
def begin_for_downto(self, p):
self.in_loop = True
if isinstance(p[-4], Number) and isinstance(p[-2], Number):
if self.range_of_loop is not None:
self.range_of_loop *= abs(p[-2].value - p[-4].value)
else:
self.range_of_loop = abs(p[-4].value - p[-2].value)
self.declare_iterator(p[-6], -1, self.range_of_loop)
self.declare_iterator(p[-6], -1)
@_('READ identifier SEMICOLON')
def command(self, p):
pass
@_('WRITE value SEMICOLON')
def command(self, p):
if isinstance(p[1], Number):
self.variables[p[1].name].in_write = True
'''
expression
'''
@_('value')
def expression(self, p):
pass
@_('value ADDITION value',
'value SUBTRACTION value',
'value MULTIPLICATION value',
'value DIVIDE value',
'value MODULO value')
def expression(self, p):
pass
@_('value EQUAL value',
'value NOTEQUAL value',
'value LOWERTHAN value',
'value GREATERTHAN value',
'value LOWEREQUALTHAN value',
'value GREATEREQUALTHAN value')
def condition(self, p):
pass
'''
value
'''
@_('NUMBER')
def value(self, p):
self.count_number(p[0])
return self.variables[p[0]]
@_('identifier')
def value(self, p):
return p[0]
'''
identifier
'''
@_('PIDENTIFIER')
def identifier(self, p):
self.count_variable(p[0], p.lineno)
return self.variables[p[0]]
@_('PIDENTIFIER LEFTBRACKET PIDENTIFIER RIGHTBRACKET')
def identifier(self, p):
self.count_variable(p[0], p.lineno)
self.count_variable(p[2], p.lineno)
@_('PIDENTIFIER LEFTBRACKET NUMBER RIGHTBRACKET')
def identifier(self, p):
self.count_variable(p[0], p.lineno)
def error(self, token):
if token is not None:
raise CompilerException("Syntax error", token.lineno)
raise CompilerException("End of file", -1)