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
248
249
250
251
252
253
254
255
256
257
|
from typing import NoReturn, Optional, cast
from tabla import Token, LexToken
from parse.base import BaseParser
from parse.ident import ParseIdent
from errors import Error
from astree.expr import Expr, BinarithmOp, ConstantExpr, NumberConstant, CallExpr, PrintExpr, BinarithmExpr, CompoundExpr, ReadExpr, AccessExpr, AssignExpr
class ParseExpr:
def __init__(self, parser: BaseParser):
self.parser = parser
def expr(self) -> (Expr | Error):
obj = self.binarithm(None, 0)
# =
tok = self.parser._try(Token.EQUAL)
if not tok:
return obj
error = self.parser.synassert(
isinstance(obj, AccessExpr),
"Se esperaba un objeto como destino de la asignación.")
if type(error) is Error:
return error
# Expresión
expr = self.expr()
if type(expr) is Error:
return expr
return AssignExpr(_object = obj,
value = expr)
# WIP
def binarithm(self, lvalue: Expr, i: int) -> (Expr | Error):
print(f'binarithm({lvalue}, {i})')
_lvalue = lvalue
if not lvalue:
_lvalue = self.cast(lvalue)
if type(_lvalue) is Error:
return _lvalue
print(f'lvalue = {_lvalue}')
tok = self.parser.lex()
print(f'tok = {tok}')
j: int = self.precedence(tok.tipo)
while j >= i:
op = self.binop_for_tok(tok.tipo)
rvalue = self.cast(_lvalue)
if type(rvalue) is Error:
return rvalue
tok = self.parser.lex()
k: int = self.precedence(tok.tipo)
while k > j:
self.parser.unlex()
rvalue = self.binarithm(rvalue, k)
if type(rvalue) is Error:
return rvalue
tok = self.parser.lex()
k = self.precedence(tok.tipo)
_lvalue = BinarithmExpr(op = op,
lvalue = _lvalue,
rvalue = rvalue)
j = self.precedence(tok.tipo)
self.parser.unlex()
return _lvalue
def call(self, lvalue: Expr) -> (Expr | Error):
args: List[Expr] = []
while not self.parser._try(Token.R_PAREN):
# Expresión
expr = self.expr()
if type(expr) is Error:
return epr
args.append(expr)
# ,
if self.parser._try(Token.COMMA):
continue
# )
if self.parser._try(Token.R_PAREN):
break
return CallExpr(lvalue = lvalue,
args = args)
def compound_expr(self) -> (Expr | Error):
items: List[Expr] = []
# {
lbracket = self.parser.want(Token.L_BRACKET)
if type(lbracket) is Error:
return lbracket
while True:
# }
item = self.parser.peek(Token.R_BRACKET)
if item: break
item = self.expr()
if type(item) is Error:
return item
items.append(item)
# ;
semicolon = self.parser.want(Token.SEMICOLON)
if type(semicolon) is Error:
return semicolon
# }
rbracket = self.parser.want(Token.R_BRACKET)
if type(rbracket) is Error:
return rbracket
return CompoundExpr(exprs = items)
def builtin(self) -> (Expr | Error):
tok: LexToken = self.parser.peek(Token.PRINT, Token.READ)
if not tok:
return self.postfix(None)
if tok.tipo == Token.PRINT:
return self.print_expr()
elif tok.tipo == Token.READ:
return self.read_expr()
def postfix(self, lvalue: Optional[Expr]) -> (Expr | Error):
_lvalue: Optional[Expr] = lvalue
if not lvalue:
_lvalue = self.plain_expression()
if type(_lvalue) == Error:
return _lvalue
tok: LexToken = self.parser._try(Token.L_PAREN)
if not tok:
return _lvalue
_next: Optional[LexToken] = None
if tok == Token.L_PAREN:
return self.call(_lvalue)
return self.postfix(_next)
def print_expr(self) -> (Expr | Error):
_print = self.parser.want(Token.PRINT)
if type(_print) is Error:
return _print
lparen = self.parser.want(Token.L_PAREN)
if type(lparen) is Error:
return lparen
expr = self.expr()
if type(expr) is Error:
return expr
rparen = self.parser.want(Token.R_PAREN)
if type(rparen) is Error:
return rparen
return PrintExpr(expr = expr)
def read_expr(self) -> (Expr | Error):
_read = self.parser.want(Token.READ)
if type(_read) is Error:
return _read
ident = ParseIdent(self.parser).ident()
if type(ident) is Error:
return ident
return ReadExpr(expr = ident)
# WIP
def cast(self, lvalue: Optional[Expr]) -> (Expr | Error):
return self.unarithm()
# WIP
def constant(self) -> (Expr | Error):
tok: LexToken = self.parser.lex()
expr: Optional[ConstantExpr] = None
if tok.tipo == Token.STRING_LIT:
expr: str = tok.valor
elif tok.tipo == Token.INT_LIT:
expr = NumberConstant(value = tok.valor)
elif tok.tipo == Token.BOOLEAN_LIT:
expr: bool = tok.valor
else:
return Error(msg = "Se esperaba una constante.", numlinea = tok.numlinea)
return expr
# WIP
def plain_expression(self) -> (Expr | Error):
tok: LexToken = self.parser.peek()
if tok.tipo in [Token.BOOLEAN_LIT, Token.CHAR_LIT, Token.INT_LIT, Token.STRING_LIT]:
return self.constant()
elif tok.tipo == Token.L_PAREN:
lparen = self.parser.want(Token.L_PAREN)
if type(lparen) is Error:
return lparen
expr = self.expr()
if type(expr) is Error:
return expr
rparen = self.parser.want(Token.R_PAREN)
if type(rparen) is Error:
return rparen
elif tok.tipo == Token.IDENT:
ident = ParseIdent(self.parser).ident()
if type(ident) is Error:
return ident
return ident
def unarithm(self) -> (Expr | Error):
return self.builtin()
def binop_for_tok(self, tok: Token) -> (BinarithmOp | NoReturn):
if tok is Token.SLASH:
return BinarithmOp.DIV
elif tok is Token.GT:
return BinarithmOp.GT
elif tok is Token.GEQ:
return BinarithmOp.GTEQ
elif tok is Token.EQEQ:
return BinarithmOp.LEQUAL
elif tok is Token.LT:
return BinarithmOp.LESS
elif tok is Token.LEQ:
return BinarithmOp.LESSEQ
elif tok is Token.MINUS:
return BinarithmOp.MINUS
elif tok is Token.NOTEQ:
return BinarithmOp.NEQUAL
elif tok is Token.PLUS:
return BinarithmOp.PLUS
elif tok is Token.TIMES:
return BinarithmOp.TIMES
def precedence(self, tok: Token) -> int:
if tok in [Token.EQEQ, Token.NOTEQ]:
return 0
elif tok in [Token.PLUS, Token.MINUS]:
return 1
elif tok in [Token.TIMES, Token.SLASH]:
return 2
return -1
|