summaryrefslogtreecommitdiff
path: root/src/ast/nodes
diff options
context:
space:
mode:
Diffstat (limited to 'src/ast/nodes')
-rw-r--r--src/ast/nodes/AssignmentNode.java21
-rw-r--r--src/ast/nodes/AtomNode.java22
-rw-r--r--src/ast/nodes/ExprListNode.java61
-rw-r--r--src/ast/nodes/ForStmtNode.java12
-rw-r--r--src/ast/nodes/ParamdefNode.java2
-rw-r--r--src/ast/nodes/TestlistCompNode.java61
6 files changed, 161 insertions, 18 deletions
diff --git a/src/ast/nodes/AssignmentNode.java b/src/ast/nodes/AssignmentNode.java
index 3d597ef..07966c3 100644
--- a/src/ast/nodes/AssignmentNode.java
+++ b/src/ast/nodes/AssignmentNode.java
@@ -10,14 +10,14 @@ import semanticanalysis.SymbolTable;
*/
public class AssignmentNode implements Node {
- private ExprNode lhr;
+ private ExprListNode lhr;
private Node assign;
- private ExprNode rhr;
+ private ExprListNode rhr;
public AssignmentNode(Node lhr, Node assign, Node rhr) {
- this.lhr = (ExprNode) lhr;
+ this.lhr = (ExprListNode) lhr;
this.assign = assign;
- this.rhr = (ExprNode) rhr;
+ this.rhr = (ExprListNode) rhr;
}
@Override
@@ -28,7 +28,18 @@ public class AssignmentNode implements Node {
errors.addAll(assign.checkSemantics(ST, _nesting));
errors.addAll(rhr.checkSemantics(ST, _nesting));
- ST.insert(lhr.getId(), rhr.typeCheck(), _nesting, "");
+ int lsize = lhr.getSize();
+ int rsize = rhr.getSize();
+
+ if (lsize == rsize) {
+ for (int i = 0; i < lsize; i++) {
+ ExprNode latom = (ExprNode) lhr.getElem(i);
+ ExprNode ratom = (ExprNode) rhr.getElem(i);
+ ST.insert(latom.getId(), ratom.typeCheck(), _nesting, "");
+ }
+ } else {
+ errors.add(new SemanticError("ValueError: different size of left or right side assignment"));
+ }
return errors;
}
diff --git a/src/ast/nodes/AtomNode.java b/src/ast/nodes/AtomNode.java
index 3c2b1eb..592aac4 100644
--- a/src/ast/nodes/AtomNode.java
+++ b/src/ast/nodes/AtomNode.java
@@ -13,9 +13,11 @@ import semanticanalysis.SymbolTable;
public class AtomNode implements Node {
protected String val;
+ protected TestlistCompNode exprlist;
- public AtomNode(String val) {
+ public AtomNode(String val, Node exprlist) {
this.val = val;
+ this.exprlist = (TestlistCompNode) exprlist;
}
public String getId() {
@@ -26,10 +28,16 @@ public class AtomNode implements Node {
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
var errors = new ArrayList<SemanticError>();
- if ((this.typeCheck() instanceof AtomType) && ST.nslookup(this.getId()) < 0) {
- errors.add(new SemanticError("name '" + this.getId() + "' is not defined."));
- } else {
- // System.out.println("exist " + this.getId());
+ if (val != null) {
+ if ((this.typeCheck() instanceof AtomType) && ST.nslookup(this.getId()) < 0) {
+ errors.add(new SemanticError("name '" + this.getId() + "' is not defined."));
+ } else {
+ // System.out.println("exist " + this.typeCheck());
+ }
+ }
+
+ if (exprlist != null) {
+ errors.addAll(exprlist.checkSemantics(ST, _nesting));
}
return errors;
@@ -38,6 +46,10 @@ public class AtomNode implements Node {
// ENHANCE: return more specific types
@Override
public Type typeCheck() {
+ if (this.val == null) {
+ return new VoidType();
+ }
+
Pattern booleanVariable = Pattern.compile("^(True|False)$");
Pattern continueBreakVariable = Pattern.compile("^(continue|break)$");
// this regex should match every possible atom name written in this format: CHAR (CHAR | DIGIT)*
diff --git a/src/ast/nodes/ExprListNode.java b/src/ast/nodes/ExprListNode.java
new file mode 100644
index 0000000..c1a2ebc
--- /dev/null
+++ b/src/ast/nodes/ExprListNode.java
@@ -0,0 +1,61 @@
+package ast.nodes;
+
+import ast.types.*;
+import java.util.ArrayList;
+import semanticanalysis.SemanticError;
+import semanticanalysis.SymbolTable;
+
+/**
+ * Node for the `expr_list` statement of the grammar.
+ */
+public class ExprListNode implements Node {
+
+ private final ArrayList<Node> exprs;
+
+ public ExprListNode(ArrayList<Node> _exprs) {
+ this.exprs = _exprs;
+ }
+
+ @Override
+ public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
+ ArrayList<SemanticError> errors = new ArrayList();
+
+ for (var param : exprs) {
+ errors.addAll(param.checkSemantics(ST, _nesting));
+ }
+
+ return errors;
+ }
+
+ public int getSize() {
+ return exprs.size();
+ }
+
+ public Node getElem(int i) {
+ return exprs.get(i);
+ }
+
+ @Override
+ public Type typeCheck() {
+ return new VoidType();
+ }
+
+ // TODO: code generation for expr list
+ @Override
+ public String codeGeneration() {
+ return "";
+ }
+
+ @Override
+ public String toPrint(String prefix) {
+ String str = prefix + "Paramlist\n";
+
+ prefix += " ";
+ for (var param : exprs) {
+ str += param.toPrint(prefix);
+ }
+
+ return str;
+ }
+
+}
diff --git a/src/ast/nodes/ForStmtNode.java b/src/ast/nodes/ForStmtNode.java
index 28fe783..610d0a0 100644
--- a/src/ast/nodes/ForStmtNode.java
+++ b/src/ast/nodes/ForStmtNode.java
@@ -1,20 +1,20 @@
package ast.nodes;
+import ast.types.*;
import java.util.ArrayList;
-
import semanticanalysis.SemanticError;
import semanticanalysis.SymbolTable;
-import ast.types.*;
/**
* Node for the `for_stmt` statement of the grammar.
*/
public class ForStmtNode implements Node {
- private Node exprList;
+
+ private ExprListNode exprList;
private Node block;
public ForStmtNode(Node exprList, Node block) {
- this.exprList = exprList;
+ this.exprList = (ExprListNode) exprList;
this.block = block;
}
@@ -22,9 +22,7 @@ public class ForStmtNode implements Node {
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
ArrayList<SemanticError> errors = new ArrayList<SemanticError>();
- ExprNode expr = (ExprNode) exprList;
-
- ST.insert(expr.getId(), expr.typeCheck(), _nesting, "");
+ // ST.insert(expr.getId(), expr.typeCheck(s), _nesting, "");
errors.addAll(exprList.checkSemantics(ST, _nesting));
errors.addAll(block.checkSemantics(ST, _nesting));
diff --git a/src/ast/nodes/ParamdefNode.java b/src/ast/nodes/ParamdefNode.java
index 5164537..adadf72 100644
--- a/src/ast/nodes/ParamdefNode.java
+++ b/src/ast/nodes/ParamdefNode.java
@@ -12,7 +12,7 @@ import semanticanalysis.SymbolTable;
public class ParamdefNode extends AtomNode {
public ParamdefNode(String val) {
- super(val);
+ super(val, null);
}
@Override
diff --git a/src/ast/nodes/TestlistCompNode.java b/src/ast/nodes/TestlistCompNode.java
new file mode 100644
index 0000000..070b1a1
--- /dev/null
+++ b/src/ast/nodes/TestlistCompNode.java
@@ -0,0 +1,61 @@
+package ast.nodes;
+
+import ast.types.*;
+import java.util.ArrayList;
+import semanticanalysis.SemanticError;
+import semanticanalysis.SymbolTable;
+
+/**
+ * Node for the `testlist_comp` statement of the grammar.
+ */
+public class TestlistCompNode implements Node {
+
+ private final ArrayList<Node> exprs;
+
+ public TestlistCompNode(ArrayList<Node> _exprs) {
+ this.exprs = _exprs;
+ }
+
+ @Override
+ public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
+ ArrayList<SemanticError> errors = new ArrayList();
+
+ for (var param : exprs) {
+ errors.addAll(param.checkSemantics(ST, _nesting));
+ }
+
+ return errors;
+ }
+
+ public int getSize() {
+ return exprs.size();
+ }
+
+ public Node getElem(int i) {
+ return exprs.get(i);
+ }
+
+ @Override
+ public Type typeCheck() {
+ return new VoidType();
+ }
+
+ // TODO: code generation for expr list
+ @Override
+ public String codeGeneration() {
+ return "";
+ }
+
+ @Override
+ public String toPrint(String prefix) {
+ String str = prefix + "Testlist_comp\n";
+
+ prefix += " ";
+ for (var param : exprs) {
+ str += param.toPrint(prefix);
+ }
+
+ return str;
+ }
+
+}