diff options
author | Iván Ávalos <avalos@disroot.org> | 2022-11-23 23:29:39 -0600 |
---|---|---|
committer | Iván Ávalos <avalos@disroot.org> | 2022-11-23 23:29:39 -0600 |
commit | 8050c2e40b3a15b92bc7e3c9be4db4c0637e797d (patch) | |
tree | 1bd0a97bded9a2e97a633765e891bf9cd84a752e /compilador/ast/expr.py | |
parent | 549857003fd3c4411946db8a2ced846d8cbb825e (diff) | |
download | javanol-8050c2e40b3a15b92bc7e3c9be4db4c0637e797d.tar.gz javanol-8050c2e40b3a15b92bc7e3c9be4db4c0637e797d.tar.bz2 javanol-8050c2e40b3a15b92bc7e3c9be4db4c0637e797d.zip |
Se añaden definiciones de AST
Diffstat (limited to 'compilador/ast/expr.py')
-rw-r--r-- | compilador/ast/expr.py | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/compilador/ast/expr.py b/compilador/ast/expr.py new file mode 100644 index 0000000..646733f --- /dev/null +++ b/compilador/ast/expr.py @@ -0,0 +1,108 @@ +from dataclasses import dataclass +from enum import Enum, auto +from typing import List, Optional + +from type import Type + +# 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 +PrintExpr = Expr + +# A read statement. +# +# leer a +ReadExpr = Expr + +# A return statement. +# +# return a +ReturnExpr = Optional[Expr] + +# A Javañol expression +Expr = (AssignExpr | BinarithmExpr | CallExpr | ConstantExpr | + ForExpr | IfExpr | CompoundExpr | PrintExpr | + ReadExpr | ReturnExpr) |