-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchaiparse.py
233 lines (184 loc) · 6.95 KB
/
chaiparse.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
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
# chaiparse.py
from sly import Parser
from chailex import ChaiLex
from scope.chaivars import *
from scope.chaiflow import *
import logging
class ChaiParse(Parser):
"""
Defines grammar's rules and actions
"""
# scope = ChaiScope()
tokens = ChaiLex.tokens
# debugfile = 'parser.out'
def __init__(self):
self.global_variables = {}
self.memory_indexes = {}
self.next_free_memory_index = 0
self.iterator_num = 0
def declare_global_variable(self, variable):
"""
Declares global variable in global variables dictionary
:param variable:
:return:
"""
if variable.pidentifier not in self.global_variables.keys():
self.global_variables[variable.pidentifier] = variable
self.memory_indexes[variable.pidentifier] = self.next_free_memory_index
if isinstance(variable, Int):
self.next_free_memory_index += 1
elif isinstance(variable, IntArray):
self.next_free_memory_index += variable.length
else:
raise Exception("chaiparse declare_global_variable: variable with pidentifier {} was already "
"declared, line {}".format(variable.pidentifier, variable.lineno))
def get_global_variable(self, pidentifier):
if pidentifier in self.global_variables.keys():
return self.global_variables[pidentifier]
else:
raise Exception("chaiparse get_global_variable: invalid access to variable '{}'".format(pidentifier))
def solve_iterator_collision(self):
num = self.iterator_num
self.iterator_num += 1
return num
# Declares precedence
precedence = (
('left', PLUS, MINUS),
('left', TIMES, DIVIDE),
)
# program:
# Handles the program division into declarations
@_('DECLARE declarations IN commands END')
def program(self, p):
return [p[1], p[3]]
# declarations:
# Handles the variable declarations
@_('declarations PIDENTIFIER SEMICOLON')
def declarations(self, p):
p[0] = list(p[0]) if p[0] else []
integer = Int(pidentifier=p[1], lineno=p.lineno)
self.declare_global_variable(integer)
p[0].append(integer)
return p[0]
@_('declarations PIDENTIFIER LPAREN NUMBER COLON NUMBER RPAREN SEMICOLON')
def declarations(self, p):
p[0] = list(p[0]) if p[0] else []
array = IntArray(pidentifier=p[1], lineno=p.lineno, from_val=p[3], to_val=p[5])
self.declare_global_variable(array)
p[0].append(array)
return p[0]
@_('empty')
def declarations(self, p):
return []
# commands:
@_('commands command')
def commands(self, p):
p[0] = list(p[0]) if p[0] else []
p[0].append(p[1])
return p[0]
@_('command')
def commands(self, p):
return list((p[0],))
# command:
# Handles command
@_('identifier ASSIGN expression SEMICOLON')
def command(self, p):
return Assign(identifier=p[0], expression=p[2])
@_('IF condition THEN commands ENDIF')
def command(self, p):
# return ('if', p[1], p[3])
return If(condition=p[1], commands=p[3])
@_('IF condition THEN commands ELSE commands ENDIF')
def command(self, p):
# return ('if_else', p[1], p[3], p[5])
return IfElse(condition=p[1], commands=p[3], alt_commands=p[5])
@_('WHILE condition DO commands ENDWHILE')
def command(self, p):
# return ('while', p[1], p[3])
return While(condition=p[1], commands=p[3])
@_('DO commands WHILE condition ENDDO')
def command(self, p):
# return ('do_while', p[1], p[3])
return DoWhile(condition=p[3], commands=p[1])
@_('FOR PIDENTIFIER FROM value TO value DO commands ENDFOR')
def command(self, p):
# pidentifier = ('int', p[1], p.lineno)
# return ('for', pidentifier, p[3], p[5], p[7])
# if p[1] in self.global_variables.keys():
# p[1] = p[1] + '_{}'.format(self.solve_iterator_collision())
iterator = Int(pidentifier=p[1], lineno=p.lineno)
iterator.set_as_iterator()
# iterator.set_value_has_been_set()
if iterator.pidentifier not in self.global_variables.keys():
self.declare_global_variable(iterator)
return For(iterator=iterator, from_val=p[3], to_val=p[5], commands=p[7])
@_('FOR PIDENTIFIER FROM value DOWNTO value DO commands ENDFOR')
def command(self, p):
# if p[1] in self.global_variables.keys():
# p[1] = p[1] + '_{}'.format(self.solve_iterator_collision())
iterator = Int(pidentifier=p[1], lineno=p.lineno)
iterator.set_as_iterator()
# iterator.set_value_has_been_set()
if iterator.pidentifier not in self.global_variables.keys():
self.declare_global_variable(iterator)
return ForDownTo(iterator=iterator, from_val=p[3], to_val=p[5], commands=p[7])
@_('READ identifier SEMICOLON')
def command(self, p):
# return ('read', p[1])
return Read(to_variable=p[1])
@_('WRITE value SEMICOLON')
def command(self, p):
# return ('write', p[1])
return Write(from_variable=p[1])
# unwrap_expression:
# Handles expressions
@_('value')
def expression(self, p):
# return p[0]
return Value(a=p[0])
@_('value PLUS value',
'value MINUS value',
'value TIMES value',
'value DIVIDE value',
'value MODULO value')
def expression(self, p):
# return ('unwrap_expression', p[1], p[0], p[2])
return Operation(a=p[0], operand=p[1], b=p[2])
# condition:
# Handles conditional statements
@_('value LESSER_EQUALS value',
'value GREATER_EQUALS value',
'value EQUALS value',
'value NOT_EQUALS value',
'value LESSER_THAN value',
'value GREATER_THAN value',)
def condition(self, p):
# return ('condition', p[1], p[0], p[2])
return Condition(p[0], p[1], p[2])
# value:
# Handles value statements
@_('identifier')
def value(self, p):
return p[0]
@_('NUMBER')
def value(self, p):
return int(p[0])
# identifier:
# Handles identifier statements
@_('PIDENTIFIER')
def identifier(self, p):
return Int(pidentifier=p[0], lineno=p.lineno)
@_('PIDENTIFIER LPAREN PIDENTIFIER RPAREN')
def identifier(self, p):
i = Int(pidentifier=p[2], lineno=p.lineno)
return IntArrayElement(array=self.get_global_variable(pidentifier=p[0]), value_holder=i, lineno=p.lineno)
@_('PIDENTIFIER LPAREN NUMBER RPAREN')
def identifier(self, p):
return IntArrayElement(array=self.get_global_variable(pidentifier=p[0]), value_holder=int(p[2]),
lineno=p.lineno)
@_('')
def empty(self, p):
pass
# Error handling
def error(self, p):
raise Exception("chaiparse: unknown input '{}' in line {}".format(p.value, p.lineno))