From 8050c2e40b3a15b92bc7e3c9be4db4c0637e797d Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Wed, 23 Nov 2022 23:29:39 -0600 Subject: Se añaden definiciones de AST MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- compilador/ast/decl.py | 26 ++++++++++++ compilador/ast/expr.py | 108 ++++++++++++++++++++++++++++++++++++++++++++++++ compilador/ast/ident.py | 3 ++ compilador/ast/type.py | 11 +++++ 4 files changed, 148 insertions(+) create mode 100644 compilador/ast/decl.py create mode 100644 compilador/ast/expr.py create mode 100644 compilador/ast/ident.py create mode 100644 compilador/ast/type.py 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 -- cgit v1.2.3