aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--compilador/ast/decl.py26
-rw-r--r--compilador/ast/expr.py108
-rw-r--r--compilador/ast/ident.py3
-rw-r--r--compilador/ast/type.py11
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