aboutsummaryrefslogtreecommitdiff
path: root/compilador/astree
diff options
context:
space:
mode:
authorIván Ávalos <avalos@disroot.org>2022-11-25 12:11:08 -0600
committerIván Ávalos <avalos@disroot.org>2022-11-25 12:11:08 -0600
commiteb4a3019bc0251e5b2b8229679e3c65d61d55336 (patch)
treef6b2a89dd35374272bd671933bfe87da4a587215 /compilador/astree
parent6b4e9a4e95eb511c194200e38ee323091dc5d7d2 (diff)
downloadjavanol-eb4a3019bc0251e5b2b8229679e3c65d61d55336.tar.gz
javanol-eb4a3019bc0251e5b2b8229679e3c65d61d55336.tar.bz2
javanol-eb4a3019bc0251e5b2b8229679e3c65d61d55336.zip
Buen progreso, pero se cicla
Diffstat (limited to 'compilador/astree')
-rw-r--r--compilador/astree/decl.py27
-rw-r--r--compilador/astree/expr.py110
-rw-r--r--compilador/astree/ident.py3
-rw-r--r--compilador/astree/type.py29
-rw-r--r--compilador/astree/unit.py9
5 files changed, 178 insertions, 0 deletions
diff --git a/compilador/astree/decl.py b/compilador/astree/decl.py
new file mode 100644
index 0000000..1e6de1d
--- /dev/null
+++ b/compilador/astree/decl.py
@@ -0,0 +1,27 @@
+from dataclasses import dataclass
+from typing import Optional
+
+from astree.type import Type
+from astree.ident import Ident
+from astree.expr import Expr
+
+# A global declaration.
+#
+# entero a = 0;
+@dataclass
+class DeclGlobal:
+ ident: Ident
+ _type: Type
+ init: Expr
+
+# A function declaration.
+#
+# funcion vacio main() { ... }
+@dataclass
+class DeclFunc:
+ ident: Ident
+ prototype: Type
+ body: Optional[Expr]
+
+# A Javañol declaration
+Decl = DeclGlobal | DeclFunc
diff --git a/compilador/astree/expr.py b/compilador/astree/expr.py
new file mode 100644
index 0000000..1bb7f4f
--- /dev/null
+++ b/compilador/astree/expr.py
@@ -0,0 +1,110 @@
+from dataclasses import dataclass
+from enum import Enum, auto
+from typing import List, Optional
+
+from astree.type import Type
+
+Expr = None
+
+# 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/astree/ident.py b/compilador/astree/ident.py
new file mode 100644
index 0000000..936b745
--- /dev/null
+++ b/compilador/astree/ident.py
@@ -0,0 +1,3 @@
+from typing import List
+
+Ident = str
diff --git a/compilador/astree/type.py b/compilador/astree/type.py
new file mode 100644
index 0000000..5389702
--- /dev/null
+++ b/compilador/astree/type.py
@@ -0,0 +1,29 @@
+from dataclasses import dataclass
+from typing import List
+from enum import Enum
+
+from tabla import Token
+
+Type = None
+
+# A built-in primitive type (int, bool, str, etc).
+class BuiltinType(Enum):
+ BOOLEAN = Token.BOOLEAN
+ STRING = Token.STRING
+ CHAR = Token.CHAR
+ INT = Token.INT
+ VOID = Token.VOID
+
+# A parameter to a function type.
+@dataclass
+class FuncParam:
+ name: str
+ _type: Type
+
+# funcion vacio ... (a: int, b: int ...)
+@dataclass
+class FuncType:
+ result: Type
+ params: List[FuncParam]
+
+Type = BuiltinType
diff --git a/compilador/astree/unit.py b/compilador/astree/unit.py
new file mode 100644
index 0000000..8ffdf19
--- /dev/null
+++ b/compilador/astree/unit.py
@@ -0,0 +1,9 @@
+from dataclasses import dataclass
+from typing import List
+
+from astree.decl import Decl
+
+# A single compilation unit, representing all of the members of a namespace.
+@dataclass
+class Unit:
+ decls: List[Decl]