From eb7fb04d2dfadd004fa86f28da42681e0038df06 Mon Sep 17 00:00:00 2001 From: geno Date: Thu, 27 Jun 2024 18:20:34 +0200 Subject: left, right = 1, 2 assignment covered sfaccimm ho fatto refactoring dell exprlist --- src/ast/nodes/TestlistCompNode.java | 61 +++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/ast/nodes/TestlistCompNode.java (limited to 'src/ast/nodes/TestlistCompNode.java') 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 exprs; + + public TestlistCompNode(ArrayList _exprs) { + this.exprs = _exprs; + } + + @Override + public ArrayList checkSemantics(SymbolTable ST, int _nesting) { + ArrayList 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; + } + +} -- cgit v1.2.3-18-g5258 From fd85e9980c0305c6dfb91aaeb199430a89163c3e Mon Sep 17 00:00:00 2001 From: geno Date: Thu, 27 Jun 2024 20:49:56 +0200 Subject: fixed comp_for and added reseved word type --- src/ast/nodes/TestlistCompNode.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'src/ast/nodes/TestlistCompNode.java') diff --git a/src/ast/nodes/TestlistCompNode.java b/src/ast/nodes/TestlistCompNode.java index 070b1a1..bcaca83 100644 --- a/src/ast/nodes/TestlistCompNode.java +++ b/src/ast/nodes/TestlistCompNode.java @@ -11,17 +11,28 @@ import semanticanalysis.SymbolTable; public class TestlistCompNode implements Node { private final ArrayList exprs; + private final CompForNode comp; - public TestlistCompNode(ArrayList _exprs) { - this.exprs = _exprs; + public TestlistCompNode(ArrayList exprs, Node comp) { + this.exprs = exprs; + this.comp = (CompForNode) comp; } @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { ArrayList errors = new ArrayList(); - for (var param : exprs) { - errors.addAll(param.checkSemantics(ST, _nesting)); + if (comp != null) { + // if comp is set, then we save the atom in the ST (we assume the first expr is an atom) + String id = ((ExprNode) exprs.get(0)).getId(); + Type t = ((ExprNode) exprs.get(0)).typeCheck(); + ST.insert(id, t, _nesting, ""); + // errors.addAll(comp.checkSemantics(ST, _nesting)); + } else { + // if comp is not set, then exprs is a list of 1 or more element + for (var param : exprs) { + errors.addAll(param.checkSemantics(ST, _nesting)); + } } return errors; -- cgit v1.2.3-18-g5258 From 3f29e5101d9c6e670dd05715297f2060707dc6c7 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Thu, 27 Jun 2024 22:18:55 +0200 Subject: Fix trailer with parentheeses `for (key, val) in ...` Resolves test `a228.py` --- src/ast/nodes/TestlistCompNode.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/ast/nodes/TestlistCompNode.java') diff --git a/src/ast/nodes/TestlistCompNode.java b/src/ast/nodes/TestlistCompNode.java index bcaca83..4ae77c9 100644 --- a/src/ast/nodes/TestlistCompNode.java +++ b/src/ast/nodes/TestlistCompNode.java @@ -23,7 +23,8 @@ public class TestlistCompNode implements Node { ArrayList errors = new ArrayList(); if (comp != null) { - // if comp is set, then we save the atom in the ST (we assume the first expr is an atom) + // if comp is set, then we save the atom in the ST (we assume the first expr is + // an atom) String id = ((ExprNode) exprs.get(0)).getId(); Type t = ((ExprNode) exprs.get(0)).typeCheck(); ST.insert(id, t, _nesting, ""); @@ -31,6 +32,8 @@ public class TestlistCompNode implements Node { } else { // if comp is not set, then exprs is a list of 1 or more element for (var param : exprs) { + var exp = (ExprNode) param; + ST.insert(exp.getId(), exp.typeCheck(), _nesting, ""); errors.addAll(param.checkSemantics(ST, _nesting)); } } -- cgit v1.2.3-18-g5258 From 37665fb6d0bc1eb29396ae949354cf7d6f9d54ca Mon Sep 17 00:00:00 2001 From: geno Date: Fri, 28 Jun 2024 12:23:28 +0200 Subject: resolving all the comments santo made --- src/ast/nodes/TestlistCompNode.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src/ast/nodes/TestlistCompNode.java') diff --git a/src/ast/nodes/TestlistCompNode.java b/src/ast/nodes/TestlistCompNode.java index 4ae77c9..244f4ef 100644 --- a/src/ast/nodes/TestlistCompNode.java +++ b/src/ast/nodes/TestlistCompNode.java @@ -10,8 +10,8 @@ import semanticanalysis.SymbolTable; */ public class TestlistCompNode implements Node { - private final ArrayList exprs; - private final CompForNode comp; + private ArrayList exprs; + private CompForNode comp; public TestlistCompNode(ArrayList exprs, Node comp) { this.exprs = exprs; @@ -45,7 +45,14 @@ public class TestlistCompNode implements Node { return exprs.size(); } + /** + * Returns the i-th expressions of `exprs` field. If the index is greater or + * equals than the size return `null`. + */ public Node getElem(int i) { + if (i >= this.exprs.size()) { + return null; + } return exprs.get(i); } -- cgit v1.2.3-18-g5258 From aaa97e2e41c0aa17a6f99099dd39bb73a935fe02 Mon Sep 17 00:00:00 2001 From: L0P0P Date: Sat, 29 Jun 2024 09:54:08 +0200 Subject: Fixed some warnings --- src/ast/nodes/TestlistCompNode.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/ast/nodes/TestlistCompNode.java') diff --git a/src/ast/nodes/TestlistCompNode.java b/src/ast/nodes/TestlistCompNode.java index 244f4ef..cba056c 100644 --- a/src/ast/nodes/TestlistCompNode.java +++ b/src/ast/nodes/TestlistCompNode.java @@ -20,7 +20,7 @@ public class TestlistCompNode implements Node { @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { - ArrayList errors = new ArrayList(); + ArrayList errors = new ArrayList<>(); if (comp != null) { // if comp is set, then we save the atom in the ST (we assume the first expr is -- cgit v1.2.3-18-g5258 From afff6a80cd58f7787efa1398f7c8cbdce8989323 Mon Sep 17 00:00:00 2001 From: geno Date: Sun, 30 Jun 2024 13:36:37 +0200 Subject: fixed warnings, better formatting, final added where possible --- src/ast/nodes/TestlistCompNode.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/ast/nodes/TestlistCompNode.java') diff --git a/src/ast/nodes/TestlistCompNode.java b/src/ast/nodes/TestlistCompNode.java index cba056c..32049f5 100644 --- a/src/ast/nodes/TestlistCompNode.java +++ b/src/ast/nodes/TestlistCompNode.java @@ -10,8 +10,8 @@ import semanticanalysis.SymbolTable; */ public class TestlistCompNode implements Node { - private ArrayList exprs; - private CompForNode comp; + private final ArrayList exprs; + private final CompForNode comp; public TestlistCompNode(ArrayList exprs, Node comp) { this.exprs = exprs; -- cgit v1.2.3-18-g5258