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 | |
parent | 549857003fd3c4411946db8a2ced846d8cbb825e (diff) | |
download | javanol-8050c2e40b3a15b92bc7e3c9be4db4c0637e797d.tar.gz javanol-8050c2e40b3a15b92bc7e3c9be4db4c0637e797d.tar.bz2 javanol-8050c2e40b3a15b92bc7e3c9be4db4c0637e797d.zip |
Se añaden definiciones de AST
-rw-r--r-- | compilador/ast/decl.py | 26 | ||||
-rw-r--r-- | compilador/ast/expr.py | 108 | ||||
-rw-r--r-- | compilador/ast/ident.py | 3 | ||||
-rw-r--r-- | compilador/ast/type.py | 11 |
4 files changed, 148 insertions, 0 deletions
diff --git a/compilador/ast/decl.py b/compilador/ast/decl.py new file mode 100644 index 0000000..1c12162 --- /dev/null +++ b/compilador/ast/decl.py @@ -0,0 +1,26 @@ +from typing import Optional + +from type import Type +from ident import Ident +from expr import Expr + +# A global declaration. +# +# entero a = 0; +@dataclass +class DeclGlobal: + ident: Ident + _type: Type + init: Optional[Expr] + +# A function declaration. +# +# vacio main() { ... } +@dataclass +class DeclFunc: + ident: Ident + prototype: Type + body: Optional[Expr] + +# A Javañol declaration +Decl = DeclGlobal | DeclFunc 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) diff --git a/compilador/ast/ident.py b/compilador/ast/ident.py new file mode 100644 index 0000000..b6a3acb --- /dev/null +++ b/compilador/ast/ident.py @@ -0,0 +1,3 @@ +from typing import List + +Ident = List[str] diff --git a/compilador/ast/type.py b/compilador/ast/type.py new file mode 100644 index 0000000..62a56e9 --- /dev/null +++ b/compilador/ast/type.py @@ -0,0 +1,11 @@ +from enum import Enum + +# A built-in primitive type (int, bool, str, etc). +class BuiltinType(Enum): + BOOLEAN = 'booleano' + STRING = 'cadena' + CHAR = 'caracter' + INT = 'entero' + VOID = 'vacio' + +Type = BuiltinType |