summaryrefslogtreecommitdiff
path: root/src/ast/nodes
diff options
context:
space:
mode:
Diffstat (limited to 'src/ast/nodes')
-rw-r--r--src/ast/nodes/ArglistNode.java53
-rw-r--r--src/ast/nodes/AssignmentNode.java52
-rw-r--r--src/ast/nodes/AtomNode.java45
-rw-r--r--src/ast/nodes/AugassignNode.java42
-rw-r--r--src/ast/nodes/BlockNode.java38
-rw-r--r--src/ast/nodes/CompNode.java41
-rw-r--r--src/ast/nodes/CompoundNode.java84
-rw-r--r--src/ast/nodes/DottedNameNode.java51
-rw-r--r--src/ast/nodes/ExprNode.java90
-rw-r--r--src/ast/nodes/ForStmtNode.java47
-rw-r--r--src/ast/nodes/FuncdefNode.java63
-rw-r--r--src/ast/nodes/IfNode.java71
-rw-r--r--src/ast/nodes/ImportNode.java76
-rw-r--r--src/ast/nodes/Node.java36
-rw-r--r--src/ast/nodes/ParamdefNode.java29
-rw-r--r--src/ast/nodes/ParamlistNode.java53
-rw-r--r--src/ast/nodes/ReturnStmtNode.java57
-rw-r--r--src/ast/nodes/RootNode.java64
-rw-r--r--src/ast/nodes/SimpleStmtNode.java84
-rw-r--r--src/ast/nodes/SimpleStmtsNode.java54
-rw-r--r--src/ast/nodes/TrailerNode.java78
-rw-r--r--src/ast/nodes/WhileStmtNode.java46
22 files changed, 1254 insertions, 0 deletions
diff --git a/src/ast/nodes/ArglistNode.java b/src/ast/nodes/ArglistNode.java
new file mode 100644
index 0000000..8a5b4b9
--- /dev/null
+++ b/src/ast/nodes/ArglistNode.java
@@ -0,0 +1,53 @@
+package com.clp.project.ast.nodes;
+
+import java.util.ArrayList;
+
+import com.clp.project.semanticanalysis.SemanticError;
+import com.clp.project.semanticanalysis.SymbolTable;
+import com.clp.project.ast.types.*;
+
+/**
+ * Node for the `arglist` statement of the grammar.
+ */
+public class ArglistNode implements Node {
+ protected ArrayList<Node> arguments;
+
+ public ArglistNode(ArrayList<Node> arguments) {
+ this.arguments = arguments;
+ }
+
+ @Override
+ public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
+ ArrayList<SemanticError> errors = new ArrayList<SemanticError>();
+
+ for (var arg : arguments) {
+ errors.addAll(arg.checkSemantics(ST, _nesting));
+ }
+
+ return errors;
+ }
+
+ @Override
+ public Type typeCheck() {
+ return new VoidType();
+ }
+
+ // TODO: add code generation for arglist node
+ @Override
+ public String codeGeneration() {
+ return "";
+ }
+
+ @Override
+ public String toPrint(String prefix) {
+ String str = prefix + "ArglistNode\n";
+
+ prefix += " ";
+ for (Node arg : arguments) {
+ str += arg.toPrint(prefix);
+ }
+
+ return str;
+ }
+
+}
diff --git a/src/ast/nodes/AssignmentNode.java b/src/ast/nodes/AssignmentNode.java
new file mode 100644
index 0000000..d722f41
--- /dev/null
+++ b/src/ast/nodes/AssignmentNode.java
@@ -0,0 +1,52 @@
+package com.clp.project.ast.nodes;
+
+import java.util.ArrayList;
+
+import com.clp.project.semanticanalysis.SemanticError;
+import com.clp.project.semanticanalysis.SymbolTable;
+import com.clp.project.ast.types.*;
+
+/**
+ * Node for the `assignment` statement of the grammar.
+ */
+public class AssignmentNode implements Node {
+ private Node lhr;
+ private Node assign;
+ private Node rhr;
+
+ public AssignmentNode(Node lhr, Node assign, Node rhr) {
+ this.lhr = lhr;
+ this.assign = assign;
+ this.rhr = rhr;
+ }
+
+ @Override
+ public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
+ ArrayList<SemanticError> errors = new ArrayList<SemanticError>();
+
+ errors.addAll(lhr.checkSemantics(ST, _nesting));
+ errors.addAll(assign.checkSemantics(ST, _nesting));
+ errors.addAll(rhr.checkSemantics(ST, _nesting));
+
+ return errors;
+ }
+
+ // TODO: check it out for this type
+ @Override
+ public Type typeCheck() {
+ return rhr.typeCheck();
+ }
+
+ // TODO: add code generation for assignment
+ @Override
+ public String codeGeneration() {
+ return "";
+ }
+
+ @Override
+ public String toPrint(String prefix) {
+ return prefix + "Assignment\n" + lhr.toPrint(prefix + " ") + assign.toPrint(prefix + " ")
+ + rhr.toPrint(prefix + " ");
+ }
+
+}
diff --git a/src/ast/nodes/AtomNode.java b/src/ast/nodes/AtomNode.java
new file mode 100644
index 0000000..fa81f92
--- /dev/null
+++ b/src/ast/nodes/AtomNode.java
@@ -0,0 +1,45 @@
+package com.clp.project.ast.nodes;
+
+import java.util.ArrayList;
+
+import com.clp.project.semanticanalysis.SemanticError;
+import com.clp.project.semanticanalysis.SymbolTable;
+import com.clp.project.ast.types.*;
+
+/**
+ * Node for the `atom` statement of the grammar.
+ */
+public class AtomNode implements Node {
+ protected String val;
+
+ public AtomNode(String val) {
+ this.val = val;
+ }
+
+ @Override
+ public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
+ return new ArrayList<SemanticError>();
+ }
+
+ // FIXME: this type for atom
+ @Override
+ public Type typeCheck() {
+ return new AtomType();
+ }
+
+ // TODO: add code generation for atom node
+ @Override
+ public String codeGeneration() {
+ return "";
+ }
+
+ @Override
+ public String toPrint(String prefix) {
+ if (val != null) {
+ return prefix + "Atom(" + val + ")\n";
+ }
+
+ return prefix + "Atom(null)\n";
+
+ }
+}
diff --git a/src/ast/nodes/AugassignNode.java b/src/ast/nodes/AugassignNode.java
new file mode 100644
index 0000000..3519b50
--- /dev/null
+++ b/src/ast/nodes/AugassignNode.java
@@ -0,0 +1,42 @@
+package com.clp.project.ast.nodes;
+
+import java.util.ArrayList;
+
+import com.clp.project.semanticanalysis.SemanticError;
+import com.clp.project.semanticanalysis.SymbolTable;
+
+import com.clp.project.ast.types.*;
+import org.antlr.v4.runtime.tree.TerminalNode;
+
+/**
+ * Node for the `augassign` statement of the grammar.
+ */
+public class AugassignNode implements Node {
+ private TerminalNode val;
+
+ public AugassignNode(TerminalNode val) {
+ this.val = val;
+ }
+
+ @Override
+ public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
+ return new ArrayList<SemanticError>();
+ }
+
+ // FIXME: use the right type
+ @Override
+ public Type typeCheck() {
+ return new VoidType();
+ }
+
+ // TODO: add code generation for augassign node
+ @Override
+ public String codeGeneration() {
+ return "";
+ }
+
+ @Override
+ public String toPrint(String prefix) {
+ return prefix + "Augassign(" + val + ")\n";
+ }
+}
diff --git a/src/ast/nodes/BlockNode.java b/src/ast/nodes/BlockNode.java
new file mode 100644
index 0000000..1fdfcc3
--- /dev/null
+++ b/src/ast/nodes/BlockNode.java
@@ -0,0 +1,38 @@
+package com.clp.project.ast.nodes;
+
+import java.util.ArrayList;
+
+import com.clp.project.semanticanalysis.SemanticError;
+import com.clp.project.semanticanalysis.SymbolTable;
+import com.clp.project.ast.types.*;
+
+/**
+ * Node for `block` statement of the grammar.
+ * It extends the `RootNode`.
+ */
+public class BlockNode extends RootNode implements Node {
+ public BlockNode(ArrayList<Node> stmts, ArrayList<Node> compoundStmts) {
+ super(stmts, compoundStmts);
+ }
+
+ @Override
+ public Type typeCheck() {
+ return new VoidType();
+ }
+
+ @Override
+ public String toPrint(String prefix) {
+ String str = prefix + "Block\n";
+
+ prefix += " ";
+ for (Node stmt : stmts) {
+ str += stmt.toPrint(prefix);
+ }
+ for (Node stmt : compoundStmts) {
+ str += stmt.toPrint(prefix);
+ }
+
+ return str;
+ }
+
+}
diff --git a/src/ast/nodes/CompNode.java b/src/ast/nodes/CompNode.java
new file mode 100644
index 0000000..6deb51c
--- /dev/null
+++ b/src/ast/nodes/CompNode.java
@@ -0,0 +1,41 @@
+package com.clp.project.ast.nodes;
+
+import java.util.ArrayList;
+
+import com.clp.project.semanticanalysis.SemanticError;
+import com.clp.project.semanticanalysis.SymbolTable;
+import com.clp.project.ast.types.*;
+import org.antlr.v4.runtime.tree.TerminalNode;
+
+/**
+ * Node for the `comp_op` statement of the grammar.
+ */
+public class CompNode implements Node {
+ private TerminalNode op;
+
+ public CompNode(TerminalNode op) {
+ this.op = op;
+ }
+
+ @Override
+ public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
+ return new ArrayList<SemanticError>();
+ }
+
+ // TODO: it should be boolean, right?
+ @Override
+ public Type typeCheck() {
+ return new VoidType();
+ }
+
+ // TODO: add code generation for CompNode
+ @Override
+ public String codeGeneration() {
+ return "";
+ }
+
+ @Override
+ public String toPrint(String prefix) {
+ return prefix + "CompNode(" + op + ")\n";
+ }
+}
diff --git a/src/ast/nodes/CompoundNode.java b/src/ast/nodes/CompoundNode.java
new file mode 100644
index 0000000..bfa0523
--- /dev/null
+++ b/src/ast/nodes/CompoundNode.java
@@ -0,0 +1,84 @@
+package com.clp.project.ast.nodes;
+
+import java.util.ArrayList;
+
+import com.clp.project.semanticanalysis.SemanticError;
+import com.clp.project.semanticanalysis.SymbolTable;
+import com.clp.project.ast.types.*;
+
+/**
+ * Node for the `compound_node` statement of the grammar.
+ */
+public class CompoundNode implements Node {
+ private Node ifNode;
+ private Node funcDef;
+ private Node forStmt;
+ private Node whileStmt;
+
+ public CompoundNode(Node ifNode, Node funcDef, Node forStmt, Node whileStmt) {
+ this.ifNode = ifNode;
+ this.funcDef = funcDef;
+ this.forStmt = forStmt;
+ this.whileStmt = whileStmt;
+ }
+
+ @Override
+ public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
+ ArrayList<SemanticError> errors = new ArrayList<SemanticError>();
+
+ if (ifNode != null) {
+ errors.addAll(ifNode.checkSemantics(ST, _nesting));
+ }
+
+ if (funcDef != null) {
+ errors.addAll(funcDef.checkSemantics(ST, _nesting));
+ }
+
+ if (forStmt != null) {
+ errors.addAll(forStmt.checkSemantics(ST, _nesting));
+ }
+
+ if (whileStmt != null) {
+ errors.addAll(whileStmt.checkSemantics(ST, _nesting));
+ }
+
+ return errors;
+ }
+
+ @Override
+ public Type typeCheck() {
+ return new VoidType();
+ }
+
+ // TODO: add code generation for CompoundNode
+ @Override
+ public String codeGeneration() {
+ return "";
+ }
+
+ @Override
+ public String toPrint(String prefix) {
+ String str = prefix + "CompoundNode\n";
+
+ prefix += " ";
+
+ if (ifNode != null) {
+ str += ifNode.toPrint(prefix);
+ }
+
+ if (funcDef != null) {
+ str += funcDef.toPrint(prefix);
+ }
+
+ if (forStmt != null) {
+ str += forStmt.toPrint(prefix);
+ }
+
+ if (whileStmt != null) {
+ str += whileStmt.toPrint(prefix);
+ }
+
+ return str;
+ }
+
+}
diff --git a/src/ast/nodes/DottedNameNode.java b/src/ast/nodes/DottedNameNode.java
new file mode 100644
index 0000000..342950a
--- /dev/null
+++ b/src/ast/nodes/DottedNameNode.java
@@ -0,0 +1,51 @@
+package com.clp.project.ast.nodes;
+
+import java.util.ArrayList;
+
+import com.clp.project.semanticanalysis.SemanticError;
+import com.clp.project.semanticanalysis.SymbolTable;
+import com.clp.project.ast.types.*;
+import org.antlr.v4.runtime.tree.TerminalNode;
+
+/**
+ * Node for the `dooted_name` statement of the grammar.
+ */
+public class DottedNameNode implements Node {
+ protected ArrayList<TerminalNode> names;
+
+ public DottedNameNode(ArrayList<TerminalNode> names) {
+ this.names = names;
+ }
+
+ @Override
+ public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
+ ArrayList<SemanticError> errors = new ArrayList<SemanticError>();
+
+ return errors;
+ }
+
+ @Override
+ public Type typeCheck() {
+ return new VoidType();
+ }
+
+ // NOTE: we do not provide code generation for this node in the same way
+ // we do not want to do this for the import stm.
+ @Override
+ public String codeGeneration() {
+ return "";
+ }
+
+ @Override
+ public String toPrint(String prefix) {
+ String str = prefix + "DottedName\n";
+
+ prefix += " ";
+ for (var name : names) {
+ str += prefix + name.toString() + "\n";
+ }
+
+ return str;
+ }
+
+}
diff --git a/src/ast/nodes/ExprNode.java b/src/ast/nodes/ExprNode.java
new file mode 100644
index 0000000..3aa112f
--- /dev/null
+++ b/src/ast/nodes/ExprNode.java
@@ -0,0 +1,90 @@
+package com.clp.project.ast.nodes;
+
+import java.util.ArrayList;
+
+import com.clp.project.semanticanalysis.SemanticError;
+import com.clp.project.semanticanalysis.SymbolTable;
+import com.clp.project.ast.types.*;
+
+/**
+ * Node for the `expr` statement of the grammar.
+ */
+public class ExprNode implements Node {
+ private Node atom;
+ private Node compOp;
+ private String op;
+ private ArrayList<Node> exprs;
+ private ArrayList<Node> trailers;
+
+ public ExprNode(Node atom, Node compOp, ArrayList<Node> exprs, String op, ArrayList<Node> trailers) {
+ this.atom = atom;
+ this.compOp = compOp;
+ this.exprs = exprs;
+ this.op = op;
+ this.trailers = trailers;
+ }
+
+ @Override
+ public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
+ ArrayList<SemanticError> errors = new ArrayList<SemanticError>();
+
+ if (atom != null) {
+ errors.addAll(atom.checkSemantics(ST, _nesting));
+ }
+
+ if (compOp != null) {
+ errors.addAll(compOp.checkSemantics(ST, _nesting));
+ }
+
+ for (var expr : exprs) {
+ errors.addAll(expr.checkSemantics(ST, _nesting));
+ }
+
+ for (var trailer : trailers) {
+ errors.addAll(trailer.checkSemantics(ST, _nesting));
+ }
+
+ return errors;
+ }
+
+ // FIXME: type for the expr
+ @Override
+ public Type typeCheck() {
+ return new VoidType();
+ }
+
+ // TODO: add code generation for expr
+ @Override
+ public String codeGeneration() {
+ return "";
+ }
+
+ @Override
+ public String toPrint(String prefix) {
+ String str = prefix + "Expr\n";
+
+ prefix += " ";
+ if (atom != null) {
+ str += atom.toPrint(prefix);
+ }
+
+ if (compOp != null) {
+ str += compOp.toPrint(prefix);
+ }
+
+ for (var expr : exprs) {
+ str += expr.toPrint(prefix);
+ }
+
+ for (var trailer : trailers) {
+ str += trailer.toPrint(prefix);
+ }
+
+ if (op != null) {
+ str += prefix + "Op(" + op + ")\n";
+ }
+
+ return str;
+ }
+
+}
diff --git a/src/ast/nodes/ForStmtNode.java b/src/ast/nodes/ForStmtNode.java
new file mode 100644
index 0000000..69dee74
--- /dev/null
+++ b/src/ast/nodes/ForStmtNode.java
@@ -0,0 +1,47 @@
+package com.clp.project.ast.nodes;
+
+import java.util.ArrayList;
+
+import com.clp.project.semanticanalysis.SemanticError;
+import com.clp.project.semanticanalysis.SymbolTable;
+import com.clp.project.ast.types.*;
+
+/**
+ * Node for the `for_stmt` statement of the grammar.
+ */
+public class ForStmtNode implements Node {
+ private Node exprList;
+ private Node block;
+
+ public ForStmtNode(Node exprList, Node block) {
+ this.exprList = exprList;
+ this.block = block;
+ }
+
+ @Override
+ public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
+ ArrayList<SemanticError> errors = new ArrayList<SemanticError>();
+
+ errors.addAll(exprList.checkSemantics(ST, _nesting));
+ errors.addAll(block.checkSemantics(ST, _nesting));
+
+ return errors;
+ }
+
+ @Override
+ public Type typeCheck() {
+ return new VoidType();
+ }
+
+ // TODO: add code generation for while
+ @Override
+ public String codeGeneration() {
+ return "";
+ }
+
+ @Override
+ public String toPrint(String prefix) {
+ return prefix + "For\n" + exprList.toPrint(prefix + " ") + block.toPrint(prefix + " ");
+ }
+
+}
diff --git a/src/ast/nodes/FuncdefNode.java b/src/ast/nodes/FuncdefNode.java
new file mode 100644
index 0000000..c48eb1c
--- /dev/null
+++ b/src/ast/nodes/FuncdefNode.java
@@ -0,0 +1,63 @@
+package com.clp.project.ast.nodes;
+
+import java.util.ArrayList;
+
+import com.clp.project.semanticanalysis.SemanticError;
+import com.clp.project.semanticanalysis.SymbolTable;
+import com.clp.project.ast.types.*;
+import org.antlr.v4.runtime.tree.TerminalNode;
+
+/**
+ * Node for the `funcdef` statement of the grammar.
+ */
+public class FuncdefNode implements Node {
+ private TerminalNode name;
+ private Node paramlist;
+ private Node block;
+
+ public FuncdefNode(TerminalNode name, Node paramlist, Node block) {
+ this.name = name;
+ this.paramlist = paramlist;
+ this.block = block;
+ }
+
+ @Override
+ public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
+ ArrayList<SemanticError> errors = new ArrayList<SemanticError>();
+
+ if (paramlist != null) {
+ errors.addAll(paramlist.checkSemantics(ST, _nesting));
+ }
+
+ errors.addAll(block.checkSemantics(ST, _nesting));
+
+ return errors;
+ }
+
+ // FIXME: this type must be the same of the return stmt variable
+ @Override
+ public Type typeCheck() {
+ return new VoidType();
+ }
+
+ // TODO: code generation for funcdef
+ @Override
+ public String codeGeneration() {
+ return "";
+ }
+
+ public String toPrint(String prefix) {
+ String str = prefix + "Funcdef(" + name + ")\n";
+
+ prefix += " ";
+
+ if (paramlist != null) {
+ str += paramlist.toPrint(prefix);
+ }
+
+ str += block.toPrint(prefix);
+
+ return str;
+ }
+
+}
diff --git a/src/ast/nodes/IfNode.java b/src/ast/nodes/IfNode.java
new file mode 100644
index 0000000..a4f160d
--- /dev/null
+++ b/src/ast/nodes/IfNode.java
@@ -0,0 +1,71 @@
+package com.clp.project.ast.nodes;
+
+import java.util.ArrayList;
+
+import com.clp.project.semanticanalysis.SemanticError;
+import com.clp.project.semanticanalysis.SymbolTable;
+import com.clp.project.ast.types.*;
+
+/**
+ * Node for the `if` statement of the grammar.
+ */
+public class IfNode implements Node {
+ private Node guard;
+ private Node thenbranch;
+ private Node elsebranch;
+
+ public IfNode(Node guard, Node thenbranch, Node elsebranch) {
+ this.guard = guard;
+ this.thenbranch = thenbranch;
+ this.elsebranch = elsebranch;
+ }
+
+ @Override
+ public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
+ ArrayList<SemanticError> errors = new ArrayList<SemanticError>();
+
+ errors.addAll(guard.checkSemantics(ST, _nesting));
+ errors.addAll(thenbranch.checkSemantics(ST, _nesting));
+ if (elsebranch != null) {
+ errors.addAll(elsebranch.checkSemantics(ST, _nesting));
+ }
+
+ return errors;
+ }
+
+ // FIXME: fix the if statement
+ @Override
+ public Type typeCheck() {
+ if (guard.typeCheck() instanceof BoolType) {
+ Type thenexp = thenbranch.typeCheck();
+ Type elseexp = elsebranch.typeCheck();
+ if (thenexp.getClass().equals(elseexp.getClass()))
+ return thenexp;
+ else {
+ System.out.println("Type Error: incompatible types in then and else branches");
+ return new ErrorType();
+ }
+ } else {
+ System.out.println("Type Error: non boolean condition in if");
+ return new ErrorType();
+ }
+ }
+
+ // TODO: add code generation for if
+ @Override
+ public String codeGeneration() {
+ return "";
+ }
+
+ @Override
+ public String toPrint(String prefix) {
+ String str = prefix + "If\n" + guard.toPrint(prefix + " ") + thenbranch.toPrint(prefix + " ");
+
+ if (elsebranch != null) {
+ str += elsebranch.toPrint(prefix + " ");
+ }
+
+ return str;
+ }
+
+}
diff --git a/src/ast/nodes/ImportNode.java b/src/ast/nodes/ImportNode.java
new file mode 100644
index 0000000..e6ac8c7
--- /dev/null
+++ b/src/ast/nodes/ImportNode.java
@@ -0,0 +1,76 @@
+package com.clp.project.ast.nodes;
+
+import java.util.ArrayList;
+
+import com.clp.project.semanticanalysis.SemanticError;
+import com.clp.project.semanticanalysis.SymbolTable;
+import com.clp.project.ast.types.*;
+
+/**
+ * Node for the `import_stmt` statement of the grammar.
+ */
+public class ImportNode implements Node {
+ private Node dottedName;
+ private boolean isFrom;
+ private boolean importAs;
+ private boolean importAll;
+ private ArrayList<String> names;
+
+ public ImportNode(Node dottedName, boolean isFrom, boolean importAs, boolean importAll,
+ ArrayList<String> names) {
+ this.dottedName = dottedName;
+ this.isFrom = isFrom;
+ this.importAs = importAs;
+ this.importAll = importAll;
+ this.names = names;
+ }
+
+ @Override
+ public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
+ ArrayList<SemanticError> errors = new ArrayList<SemanticError>();
+
+ return errors;
+ }
+
+ @Override
+ public Type typeCheck() {
+ return new VoidType();
+ }
+
+ // NOTE: we do not want to provide a code generation for this statement
+ @Override
+ public String codeGeneration() {
+ return "";
+ }
+
+ @Override
+ public String toPrint(String prefix) {
+ String str = prefix + "Import\n";
+
+ prefix += " ";
+ if (isFrom) {
+ str += prefix + " From\n" + dottedName.toPrint(prefix + " ");
+ } else {
+ str += dottedName.toPrint(prefix);
+ }
+
+ if (importAs) {
+ str += prefix + " As " + names.get(0) + "\n";
+ }
+
+ if (importAll) {
+ str += prefix + " All\n";
+ }
+
+ for (int i = 0; i < names.size(); ++i) {
+ if (i == 0 && importAs)
+ continue;
+
+ str += prefix + names.get(i) + "\n";
+ }
+
+ str += "\n";
+ return str;
+ }
+
+}
diff --git a/src/ast/nodes/Node.java b/src/ast/nodes/Node.java
new file mode 100644
index 0000000..f807745
--- /dev/null
+++ b/src/ast/nodes/Node.java
@@ -0,0 +1,36 @@
+package com.clp.project.ast.nodes;
+
+import java.util.ArrayList;
+
+import com.clp.project.semanticanalysis.SemanticError;
+import com.clp.project.semanticanalysis.SymbolTable;
+import com.clp.project.ast.types.*;
+
+/**
+ * Base interface for a Node.
+ */
+public interface Node {
+ /**
+ * Checks semantics for a given node for a SymbolTable ST and a level of
+ * nesting.
+ * Returns a list of `SemanticError`.
+ */
+ ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting);
+
+ /**
+ * Checks the type for a given node. If there's any error, returns an
+ * `ErrorType`.
+ */
+ Type typeCheck();
+
+ /**
+ * Returns a string for the Python Virtual Machine.
+ */
+ String codeGeneration();
+
+ /**
+ * Returns a string for a given node with a prefix.
+ * It used when an AST wants to be visualized on screen.
+ */
+ String toPrint(String prefix);
+}
diff --git a/src/ast/nodes/ParamdefNode.java b/src/ast/nodes/ParamdefNode.java
new file mode 100644
index 0000000..d8e04f7
--- /dev/null
+++ b/src/ast/nodes/ParamdefNode.java
@@ -0,0 +1,29 @@
+package com.clp.project.ast.nodes;
+
+import java.util.ArrayList;
+
+import com.clp.project.semanticanalysis.SemanticError;
+import com.clp.project.semanticanalysis.SymbolTable;
+
+import com.clp.project.ast.types.*;
+
+/**
+ * Node for the `paramdef` statement of the grammar. Extends the `AtomNode`
+ * class.
+ */
+public class ParamdefNode extends AtomNode implements Node {
+ public ParamdefNode(String val) {
+ super(val);
+ }
+
+ // FIXME: it should returns the param' type
+ @Override
+ public Type typeCheck() {
+ return new VoidType();
+ }
+
+ @Override
+ public String toPrint(String prefix) {
+ return prefix + "Paramdef(" + val + ")\n";
+ }
+}
diff --git a/src/ast/nodes/ParamlistNode.java b/src/ast/nodes/ParamlistNode.java
new file mode 100644
index 0000000..d4f40b8
--- /dev/null
+++ b/src/ast/nodes/ParamlistNode.java
@@ -0,0 +1,53 @@
+package com.clp.project.ast.nodes;
+
+import java.util.ArrayList;
+
+import com.clp.project.semanticanalysis.SemanticError;
+import com.clp.project.semanticanalysis.SymbolTable;
+import com.clp.project.ast.types.*;
+
+/**
+ * Node for the `param_list` statement of the grammar.
+ */
+public class ParamlistNode implements Node {
+ private ArrayList<Node> params;
+
+ public ParamlistNode(ArrayList<Node> _params) {
+ params = _params;
+ }
+
+ @Override
+ public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
+ ArrayList<SemanticError> errors = new ArrayList<SemanticError>();
+
+ for (var param : params) {
+ errors.addAll(param.checkSemantics(ST, _nesting));
+ }
+
+ return errors;
+ }
+
+ @Override
+ public Type typeCheck() {
+ return new VoidType();
+ }
+
+ // TODO: code generation for param list
+ @Override
+ public String codeGeneration() {
+ return "";
+ }
+
+ @Override
+ public String toPrint(String prefix) {
+ String str = prefix + "Paramlist\n";
+
+ prefix += " ";
+ for (var param : params) {
+ str += param.toPrint(prefix);
+ }
+
+ return str;
+ }
+
+}
diff --git a/src/ast/nodes/ReturnStmtNode.java b/src/ast/nodes/ReturnStmtNode.java
new file mode 100644
index 0000000..0cfd2b4
--- /dev/null
+++ b/src/ast/nodes/ReturnStmtNode.java
@@ -0,0 +1,57 @@
+package com.clp.project.ast.nodes;
+
+import java.util.ArrayList;
+
+import com.clp.project.semanticanalysis.SemanticError;
+import com.clp.project.semanticanalysis.SymbolTable;
+import com.clp.project.ast.types.*;
+
+/**
+ * Node for the `return_stmt` statement of the grammar.
+ */
+public class ReturnStmtNode implements Node {
+ private Node exprList;
+
+ public ReturnStmtNode(Node exprList) {
+ this.exprList = exprList;
+ }
+
+ @Override
+ public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
+ ArrayList<SemanticError> errors = new ArrayList<SemanticError>();
+
+ if (this.exprList != null) {
+ errors.addAll(this.exprList.checkSemantics(ST, _nesting));
+ }
+
+ return errors;
+ }
+
+ @Override
+ public Type typeCheck() {
+ if (this.exprList != null) {
+ return this.exprList.typeCheck();
+ }
+
+ return new VoidType();
+ }
+
+ // TODO: add code generation for return stmt
+ @Override
+ public String codeGeneration() {
+ return "";
+ }
+
+ @Override
+ public String toPrint(String prefix) {
+ String str = prefix + "ReturnStmt\n";
+
+ prefix += " ";
+ if (this.exprList != null) {
+ str += this.exprList.toPrint(prefix);
+ }
+
+ return str;
+ }
+
+}
diff --git a/src/ast/nodes/RootNode.java b/src/ast/nodes/RootNode.java
new file mode 100644
index 0000000..12d2f22
--- /dev/null
+++ b/src/ast/nodes/RootNode.java
@@ -0,0 +1,64 @@
+package com.clp.project.ast.nodes;
+
+import java.util.ArrayList;
+
+import com.clp.project.semanticanalysis.SemanticError;
+import com.clp.project.semanticanalysis.SymbolTable;
+import com.clp.project.ast.types.*;
+
+/**
+ * Node for the `root` statement of the grammar.
+ */
+public class RootNode implements Node {
+ // stms and compundStmts are protected because they are reused for a
+ // BlockNode
+ protected ArrayList<Node> stmts;
+ protected ArrayList<Node> compoundStmts;
+
+ public RootNode(ArrayList<Node> stmts, ArrayList<Node> compoundStmts) {
+ this.stmts = stmts;
+ this.compoundStmts = compoundStmts;
+ }
+
+ @Override
+ public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
+ ArrayList<SemanticError> errors = new ArrayList<SemanticError>();
+
+ for (Node stmt : stmts) {
+ errors.addAll(stmt.checkSemantics(ST, _nesting));
+ }
+ for (Node stmt : compoundStmts) {
+ errors.addAll(stmt.checkSemantics(ST, _nesting));
+ }
+
+ return errors;
+ }
+
+ @Override
+ public Type typeCheck() {
+ return new VoidType();
+ }
+
+ // TODO: Code generation for RootNode
+ @Override
+ public String codeGeneration() {
+ return "";
+ }
+
+ @Override
+ public String toPrint(String prefix) {
+ String str = "Root\n";
+
+ prefix += " ";
+
+ for (Node stmt : stmts) {
+ str += stmt.toPrint(prefix);
+ }
+ for (Node stmt : compoundStmts) {
+ str += stmt.toPrint(prefix);
+ }
+
+ return str;
+ }
+
+}
diff --git a/src/ast/nodes/SimpleStmtNode.java b/src/ast/nodes/SimpleStmtNode.java
new file mode 100644
index 0000000..3f4dec3
--- /dev/null
+++ b/src/ast/nodes/SimpleStmtNode.java
@@ -0,0 +1,84 @@
+package com.clp.project.ast.nodes;
+
+import java.util.ArrayList;
+
+import com.clp.project.semanticanalysis.SemanticError;
+import com.clp.project.semanticanalysis.SymbolTable;
+import com.clp.project.ast.types.*;
+
+/**
+ * Node for the `simple_stmt` statement of the grammar.
+ */
+public class SimpleStmtNode implements Node {
+ private Node assignment;
+ private Node expr;
+ private Node returnStmt;
+ private Node importStmt;
+
+ public SimpleStmtNode(Node assignment, Node expr, Node returnStmt, Node importStmt) {
+ this.assignment = assignment;
+ this.expr = expr;
+ this.returnStmt = returnStmt;
+ this.importStmt = importStmt;
+ }
+
+ @Override
+ public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
+ ArrayList<SemanticError> errors = new ArrayList<SemanticError>();
+
+ if (assignment != null) {
+ errors.addAll(assignment.checkSemantics(ST, _nesting));
+ }
+
+ if (expr != null) {
+ errors.addAll(expr.checkSemantics(ST, _nesting));
+ }
+
+ if (returnStmt != null) {
+ errors.addAll(returnStmt.checkSemantics(ST, _nesting));
+ }
+
+ if (importStmt != null) {
+ errors.addAll(importStmt.checkSemantics(ST, _nesting));
+ }
+
+ return errors;
+ }
+
+ @Override
+ public Type typeCheck() {
+ return new VoidType();
+ }
+
+ // TODO: add code generation for SimpleStmtNode
+ @Override
+ public String codeGeneration() {
+ return "";
+ }
+
+ @Override
+ public String toPrint(String prefix) {
+ String str = prefix + "SimpleStmt\n";
+
+ prefix += " ";
+
+ if (assignment != null) {
+ str += assignment.toPrint(prefix);
+ }
+
+ if (expr != null) {
+ str += expr.toPrint(prefix);
+ }
+
+ if (returnStmt != null) {
+ str += returnStmt.toPrint(prefix);
+ }
+
+ if (importStmt != null) {
+ str += importStmt.toPrint(prefix);
+ }
+
+ return str;
+ }
+
+}
diff --git a/src/ast/nodes/SimpleStmtsNode.java b/src/ast/nodes/SimpleStmtsNode.java
new file mode 100644
index 0000000..c8013a3
--- /dev/null
+++ b/src/ast/nodes/SimpleStmtsNode.java
@@ -0,0 +1,54 @@
+package com.clp.project.ast.nodes;
+
+import java.util.ArrayList;
+
+import com.clp.project.semanticanalysis.SemanticError;
+import com.clp.project.semanticanalysis.SymbolTable;
+import com.clp.project.ast.types.*;
+
+/**
+ * Node for the `simple_stmts` statement of the grammar.
+ */
+public class SimpleStmtsNode implements Node {
+ private ArrayList<Node> stmts;
+
+ public SimpleStmtsNode(ArrayList<Node> stmts) {
+ this.stmts = stmts;
+ }
+
+ @Override
+ public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
+ ArrayList<SemanticError> errors = new ArrayList<SemanticError>();
+
+ for (Node stmt : stmts) {
+ errors.addAll(stmt.checkSemantics(ST, _nesting));
+ }
+
+ return errors;
+ }
+
+ @Override
+ public Type typeCheck() {
+ return new VoidType();
+ }
+
+ // TODO: Code generation for SimpleStmtsNode
+ @Override
+ public String codeGeneration() {
+ return "";
+ }
+
+ @Override
+ public String toPrint(String prefix) {
+ String str = prefix + "SimpleStmts\n";
+
+ prefix += " ";
+
+ for (Node stmt : stmts) {
+ str += stmt.toPrint(prefix);
+ }
+
+ return str;
+ }
+
+}
diff --git a/src/ast/nodes/TrailerNode.java b/src/ast/nodes/TrailerNode.java
new file mode 100644
index 0000000..fa7e1ed
--- /dev/null
+++ b/src/ast/nodes/TrailerNode.java
@@ -0,0 +1,78 @@
+package com.clp.project.ast.nodes;
+
+import java.util.ArrayList;
+
+import com.clp.project.semanticanalysis.SemanticError;
+import com.clp.project.semanticanalysis.SymbolTable;
+import com.clp.project.ast.types.*;
+import org.antlr.v4.runtime.tree.TerminalNode;
+
+/**
+ * Node for the `trailer` statement of the grammar.
+ */
+public class TrailerNode implements Node {
+ private Node arglist;
+ private ArrayList<Node> exprs;
+ private TerminalNode methodCall;
+ private boolean isEmpty;
+
+ public TrailerNode(Node arglist, ArrayList<Node> exprs, TerminalNode methodCall) {
+ this.arglist = arglist;
+ this.exprs = exprs;
+ this.methodCall = methodCall;
+
+ this.isEmpty = (this.arglist == null && this.exprs.size() == 0 && this.methodCall == null);
+ }
+
+ @Override
+ public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
+ ArrayList<SemanticError> errors = new ArrayList<SemanticError>();
+
+ if (arglist != null) {
+ errors.addAll(arglist.checkSemantics(ST, _nesting));
+ }
+
+ for (var expr : exprs) {
+ errors.addAll(expr.checkSemantics(ST, _nesting));
+ }
+
+ return errors;
+ }
+
+ @Override
+ public Type typeCheck() {
+ return new VoidType();
+ }
+
+ // TODO: add code generation for trailer node
+ @Override
+ public String codeGeneration() {
+ return "";
+ }
+
+ @Override
+ public String toPrint(String prefix) {
+ String str = prefix + "TrailerNode\n";
+
+ prefix += " ";
+
+ if (arglist != null) {
+ str += arglist.toPrint(prefix);
+ }
+
+ for (var expr : exprs) {
+ str += expr.toPrint(prefix);
+ }
+
+ if (methodCall != null) {
+ str += prefix + "Method(" + methodCall + ")\n";
+ }
+
+ if (isEmpty) {
+ str += prefix + "()\n";
+ }
+
+ return str;
+ }
+
+}
diff --git a/src/ast/nodes/WhileStmtNode.java b/src/ast/nodes/WhileStmtNode.java
new file mode 100644
index 0000000..a84d60e
--- /dev/null
+++ b/src/ast/nodes/WhileStmtNode.java
@@ -0,0 +1,46 @@
+package com.clp.project.ast.nodes;
+
+import java.util.ArrayList;
+
+import com.clp.project.semanticanalysis.SemanticError;
+import com.clp.project.semanticanalysis.SymbolTable;
+import com.clp.project.ast.types.*;
+
+/**
+ * Node for the `while_stmt` statement of the grammar.
+ */
+public class WhileStmtNode implements Node {
+ private Node expr;
+ private Node block;
+
+ public WhileStmtNode(Node expr, Node block) {
+ this.expr = expr;
+ this.block = block;
+ }
+
+ @Override
+ public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
+ ArrayList<SemanticError> errors = new ArrayList<SemanticError>();
+
+ errors.addAll(expr.checkSemantics(ST, _nesting));
+ errors.addAll(block.checkSemantics(ST, _nesting));
+
+ return errors;
+ }
+
+ @Override
+ public Type typeCheck() {
+ return new VoidType();
+ }
+
+ // TODO: add code generation for while
+ @Override
+ public String codeGeneration() {
+ return "";
+ }
+
+ @Override
+ public String toPrint(String prefix) {
+ return prefix + "While\n" + expr.toPrint(prefix + " ") + block.toPrint(prefix + " ");
+ }
+}