blob: c0745531dac2dd7603c94206d6bf3f409bea4718 (
plain)
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
|
from dataclasses import dataclass
from enum import Enum, auto
from typing import List, Optional
from astree.type import Type
from astree.ident import Ident
Expr = None
# An identifier access expression.
#
# a
AccessIdentifier = Ident
# An access expression.
AccessExpr = AccessIdentifier
# An assignment expression.
#
# a = 10
@dataclass
class AssignExpr:
_object: Expr
value: Expr
# A binary arithmetic operator.
class BinarithmOp(Enum):
BAND = '&'
BOR = '|'
DIV = '/'
GT = '>'
GTEQ = '>='
LAND = '&&'
LEQUAL = '=='
LESS = '<'
LESSEQ = '<='
LOR = '||'
MINUS = '-'
NEQUAL = '!='
PLUS = '+'
TIMES = '*'
# A binary arithmetic expression.
#
# a * b
@dataclass
class BinarithmExpr:
op: BinarithmOp
lvalue: Expr
rvalue: Expr
# A function call expression.
#
# foo(bar)
@dataclass
class CallExpr:
lvalue: Expr
args: List[Expr]
# A compound expression.
#
# {
# foo;
# bar;
# // ...
# }
@dataclass
class CompoundExpr:
exprs: List[Expr]
# A scalar value.
Value = bool | str | int | type(None)
# An integer constant.
@dataclass
class NumberConstant:
value: int
# A constant expression.
ConstantExpr = Value | NumberConstant
# A for loop.
#
# porcada (entero a = 0; a < b; a++) {}
@dataclass
class ForExpr:
bindings: Optional[Expr]
cond: Expr
afterthought: Optional[Expr]
body: Expr
# An if or if..else expression.
#
# si (a) { } sino { }
@dataclass
class IfExpr:
cond: Expr
tbranch: Expr
fbranch: Optional[Expr]
# A print statement.
#
# imprimir a
@dataclass
class PrintExpr:
expr: Expr
# A read statement.
#
# leer a
@dataclass
class ReadExpr:
expr: AccessExpr
# A return statement.
#
# retorna a
ReturnExpr = Optional[Expr]
# A while expression.
#
# mientras (cond) { }
@dataclass
class WhileExpr:
cond: Expr
body: Expr
# A Javañol expression.
Expr = (AccessExpr | AssignExpr | BinarithmExpr | CallExpr |
ConstantExpr | ForExpr | IfExpr | CompoundExpr |
PrintExpr | ReadExpr | ReturnExpr)
|