-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
247 lines (199 loc) · 6.01 KB
/
parser.py
File metadata and controls
247 lines (199 loc) · 6.01 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
246
247
# pyright: reportUndefinedVariable=false
from sly import Parser
from scanner import Scanner
from errors import ParserError
import AST
class Mparser(Parser):
tokens = Scanner.tokens
debugfile = "parser.out"
precedence = (
("nonassoc", "IFX"),
("nonassoc", "ELSE"),
("nonassoc", "<", ">", "LTE", "GTE", "NEQ", "EQ"),
("left", "+", "-", "DOT_ADD", "DOT_SUB"),
("left", "*", "/", "DOT_MUL", "DOT_DIV"),
("right", "UMINUS"),
("left", "'"),
)
# ---------------------------
# program to ciąg linijek (lines)
@_("lines")
def program(self, p):
return AST.Program(p.lineno, p.lines)
# ---------------------------
# lines
@_("line lines")
def lines(self, p):
return [p.line] + p.lines
@_("")
def lines(self, p):
return []
# ---------------------------
# line
@_(
'assignment ";"',
'statement ";"',
"if_statement",
"while_statement",
"for_statement",
)
def line(self, p):
return p[0]
# ---------------------------
# statements
# BREAK I CONTINUE DZIALA WSZEDZIE, A POWINIEN TYLKO W PETLI
@_("BREAK")
def statement(self, p):
return AST.BreakStatement(p.lineno)
@_("CONTINUE")
def statement(self, p):
return AST.ContinueStatement(p.lineno)
@_("RETURN")
def statement(self, p):
return AST.ReturnStatement(p.lineno)
@_("RETURN expr")
def statement(self, p):
return AST.ReturnStatement(p.lineno, p.expr)
@_("PRINT print_args")
def statement(self, p):
return AST.PrintStatement(p.lineno, p[1])
@_("print_args ',' print_arg")
def print_args(self, p):
return p.print_args + p.print_arg
@_("print_arg")
def print_args(self, p):
return p.print_arg
@_("expr")
def print_arg(self, p):
return [p.expr]
# @_("STRING")
# def print_arg(self, p):
# return [AST.String(p.lineno, p[0])]
# ---------------------------
# assignments
@_(
'var "=" expr',
"var ADD_ASSIGN expr",
"var SUB_ASSIGN expr",
"var MUL_ASSIGN expr",
"var DIV_ASSIGN expr",
)
def assignment(self, p):
return AST.Assignment(p.lineno, p[1], p.var, p.expr)
@_('matrix_reference "=" expr')
def assignment(self, p):
return AST.Assignment(p.lineno, p[1], p.matrix_reference, p.expr)
# ---------------------------
# warunki i petle
# pomysl: osobny if dla petli
@_('IF "(" condition ")" block %prec IFX')
def if_statement(self, p):
return AST.IfStatement(p.lineno, p.condition, p.block)
@_('IF "(" condition ")" block ELSE block')
def if_statement(self, p):
return AST.IfStatement(p.lineno, p.condition, p.block0, p.block1)
@_('WHILE "(" condition ")" block')
def while_statement(self, p):
return AST.WhileStatement(p.lineno, p.condition, p.block)
@_('FOR var "=" expr ":" expr block')
def for_statement(self, p):
return AST.ForStatement(p.lineno, p.var, p.expr0, p.expr1, p.block)
# ---------------------------
# blok
@_("line")
def block(self, p):
return AST.Block(p.lineno, [p.line])
@_('"{" lines "}"')
def block(self, p):
return AST.Block(p.lineno, p.lines)
# ---------------------------
# condition
@_('">"', '"<"', "EQ", "NEQ", "GTE", "LTE")
def comp_op(self, p):
return p[0]
@_("expr comp_op expr")
def condition(self, p):
return AST.Condition(p.lineno, p.comp_op, p.expr0, p.expr1)
# ---------------------------
# expressions
@_('"-" expr %prec UMINUS')
def expr(self, p):
return AST.UnaryExpr(p.lineno, p[0], p.expr)
@_(
'expr "+" expr',
'expr "-" expr',
'expr "*" expr',
'expr "/" expr',
"expr DOT_ADD expr",
"expr DOT_SUB expr",
"expr DOT_MUL expr",
"expr DOT_DIV expr",
)
def expr(self, p):
return AST.BinaryExpr(p.lineno, p[1], p[0], p[2])
@_('"(" expr ")"')
def expr(self, p):
return p.expr
@_("matrix")
def expr(self, p):
return p.matrix
@_("matrix_reference")
def expr(self, p):
return p.matrix_reference
@_("INTNUM")
def expr(self, p):
return AST.IntNum(p.lineno, p[0])
@_("FLOATNUM")
def expr(self, p):
return AST.FloatNum(p.lineno, p[0])
@_("STRING")
def expr(self, p):
#slicing so the quotation isn't part of the string, preventing e.g. ""str""
return AST.String(p.lineno, p[0][1:-1])
@_('expr "\'"')
def expr(self, p):
return AST.UnaryExpr(p.lineno, p[1], p.expr)
@_("var")
def expr(self, p):
return p.var
# ---------------------------
# variable
@_("ID")
def var(self, p):
return AST.Id(p.lineno, p[0])
# ---------------------------
# matrix
@_('"[" vectors "]"')
def matrix(self, p):
return AST.Matrix(p.lineno, p.vectors)
# matrix creation with functions
@_('ZEROS "(" INTNUM ")"', 'ONES "(" INTNUM ")"', 'EYE "(" INTNUM ")"')
def matrix(self, p):
return AST.FunctionCall(p.lineno, p[0], AST.IntNum(p.lineno, p[2]))
@_('vectors "," vector')
def vectors(self, p):
return p.vectors + [p.vector]
@_("vector")
def vectors(self, p):
return [p.vector]
@_('"[" row "]"')
def vector(self, p):
return AST.Vector(p.lineno, p.row)
# rows
@_('row "," expr')
def row(self, p):
return p.row + [p.expr]
@_("expr")
def row(self, p):
return [p.expr]
@_("var vector")
def matrix_reference(self, p):
return AST.MatrixRefference(p.lineno, p.var, p.vector)
def error(self, p):
if p:
print(
f"{'\033[91m'}Syntax error at token {p.type}, value={p.value!r}, line={p.lineno}{'\033[0m'}"
)
else:
print(f"{'\033[91m'}Syntax error at end of input{'\033[0m'}")
raise ParserError