summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorL0P0P <grassoemanuele@live.com>2024-06-26 10:06:12 +0200
committerL0P0P <grassoemanuele@live.com>2024-06-26 10:06:12 +0200
commitfc712f94a7ed8554d8d44f4965be367354a7e670 (patch)
tree46345272a4cbbc6cc7c868e1bf9288bb44dbfc7f
parent9bb71e36feb60d886f1eb04f03daaa7bcf343355 (diff)
Using child
-rw-r--r--progs/test.py8
-rw-r--r--src/ast/Python3VisitorImpl.java40
-rw-r--r--src/ast/nodes/AtomNode.java2
-rw-r--r--src/ast/nodes/BlockNode.java11
-rw-r--r--src/ast/nodes/ExprNode.java4
-rw-r--r--src/ast/nodes/FuncdefNode.java1
-rw-r--r--src/ast/nodes/RootNode.java27
7 files changed, 46 insertions, 47 deletions
diff --git a/progs/test.py b/progs/test.py
index ab0901a..72729e1 100644
--- a/progs/test.py
+++ b/progs/test.py
@@ -1,4 +1,4 @@
-x = 1
-
-if x == 1:
- print("a")
+def unibo(a):
+ print("u")
+
+unibo(a)
diff --git a/src/ast/Python3VisitorImpl.java b/src/ast/Python3VisitorImpl.java
index 604c8d2..f5b369d 100644
--- a/src/ast/Python3VisitorImpl.java
+++ b/src/ast/Python3VisitorImpl.java
@@ -21,17 +21,19 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor<Node> {
* ```
*/
public Node visitRoot(RootContext ctx) {
- ArrayList<Node> stmts = new ArrayList<Node>();
- ArrayList<Node> compStmts = new ArrayList<Node>();
-
- for (Simple_stmtsContext stm : ctx.simple_stmts()) {
- stmts.add(visit(stm));
- }
- for (Compound_stmtContext stm : ctx.compound_stmt()) {
- compStmts.add(visit(stm));
+ ArrayList<Node> childs = new ArrayList<Node>();
+
+ for (int i = 0; i < ctx.getChildCount(); i++){
+ var child = ctx.getChild(i);
+
+ if (child instanceof Simple_stmtsContext) {
+ childs.add(visit((Simple_stmtsContext) child));
+ } else if (child instanceof Compound_stmtContext) {
+ childs.add(visit((Compound_stmtContext) child));
+ }
}
- return new RootNode(stmts, compStmts);
+ return new RootNode(childs);
}
/**
@@ -331,17 +333,19 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor<Node> {
* ```
*/
public Node visitBlock(BlockContext ctx) {
- ArrayList<Node> stmts = new ArrayList<Node>();
- ArrayList<Node> compStmts = new ArrayList<Node>();
-
- for (Simple_stmtsContext s : ctx.simple_stmts()) {
- stmts.add(visit(s));
- }
- for (Compound_stmtContext s : ctx.compound_stmt()) {
- compStmts.add(visit(s));
+ ArrayList<Node> childs = new ArrayList<Node>();
+
+ for (int i = 0; i < ctx.getChildCount(); i++){
+ var child = ctx.getChild(i);
+
+ if (child instanceof Simple_stmtsContext) {
+ childs.add(visit((Simple_stmtsContext) child));
+ } else if (child instanceof Compound_stmtContext) {
+ childs.add(visit((Compound_stmtContext) child));
+ }
}
- return new BlockNode(stmts, compStmts);
+ return new BlockNode(childs);
}
/**
diff --git a/src/ast/nodes/AtomNode.java b/src/ast/nodes/AtomNode.java
index 4c9a807..0a3c765 100644
--- a/src/ast/nodes/AtomNode.java
+++ b/src/ast/nodes/AtomNode.java
@@ -26,7 +26,7 @@ public class AtomNode implements Node {
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
var errors = new ArrayList<SemanticError>();
// System.out.println("[ATOM] id: " + getId() + " ns: " + _nesting + " top_lookup" + ST.top_lookup(this.getId()));
- if ((this.typeCheck() instanceof AtomType) && !ST.top_lookup(this.getId())) {
+ if ((this.typeCheck() instanceof AtomType) && ST.nslookup(this.getId()) < 0) {
// System.out.println(!(this.typeCheck() instanceof IntType) + " " + !ST.top_lookup(this.getId()));
errors.add(new SemanticError("Undefined name `" + this.getId() + "`"));
}
diff --git a/src/ast/nodes/BlockNode.java b/src/ast/nodes/BlockNode.java
index 6b07f49..2c85025 100644
--- a/src/ast/nodes/BlockNode.java
+++ b/src/ast/nodes/BlockNode.java
@@ -9,8 +9,8 @@ import ast.types.*;
* It extends the `RootNode`.
*/
public class BlockNode extends RootNode {
- public BlockNode(ArrayList<Node> stmts, ArrayList<Node> compoundStmts) {
- super(stmts, compoundStmts);
+ public BlockNode(ArrayList<Node> childs) {
+ super(childs);
}
@Override
@@ -23,11 +23,8 @@ public class BlockNode extends RootNode {
String str = prefix + "Block\n";
prefix += " ";
- for (Node stmt : stmts) {
- str += stmt.toPrint(prefix);
- }
- for (Node stmt : compoundStmts) {
- str += stmt.toPrint(prefix);
+ for (Node child : childs) {
+ str += child.toPrint(prefix);
}
return str;
diff --git a/src/ast/nodes/ExprNode.java b/src/ast/nodes/ExprNode.java
index 13b6619..344f3c0 100644
--- a/src/ast/nodes/ExprNode.java
+++ b/src/ast/nodes/ExprNode.java
@@ -17,7 +17,9 @@ public class ExprNode implements Node {
private ArrayList<Node> exprs;
private ArrayList<Node> trailers;
- private static final String[] bif = {"abs",
+ // built-in functions
+ private static final String[] bif = {
+ "abs",
"aiter",
"all",
"anext",
diff --git a/src/ast/nodes/FuncdefNode.java b/src/ast/nodes/FuncdefNode.java
index 341a28d..742f670 100644
--- a/src/ast/nodes/FuncdefNode.java
+++ b/src/ast/nodes/FuncdefNode.java
@@ -26,6 +26,7 @@ public class FuncdefNode implements Node {
@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
ArrayList<SemanticError> errors = new ArrayList<SemanticError>();
+
ST.insert(this.name.toString(), this.block.typeCheck(), _nesting, "");
HashMap<String, STentry> HM = new HashMap<String, STentry>();
diff --git a/src/ast/nodes/RootNode.java b/src/ast/nodes/RootNode.java
index 45d20db..4b29e8d 100644
--- a/src/ast/nodes/RootNode.java
+++ b/src/ast/nodes/RootNode.java
@@ -12,30 +12,28 @@ 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;
+ protected ArrayList<Node> childs;
- public RootNode(ArrayList<Node> stmts, ArrayList<Node> compoundStmts) {
- this.stmts = stmts;
- this.compoundStmts = compoundStmts;
+ public RootNode(ArrayList<Node> childs) {
+ this.childs = childs;
}
@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
ArrayList<SemanticError> errors = new ArrayList<SemanticError>();
+ // Create a new HashMap for the current scope
HashMap<String, STentry> HM = new HashMap<String, STentry>();
+ // Add the HashMap to the SymbolTable
ST.add(HM);
- for (Node stmt : stmts) {
- errors.addAll(stmt.checkSemantics(ST, _nesting));
- }
-
- for (Node stmt : compoundStmts) {
- errors.addAll(stmt.checkSemantics(ST, _nesting));
+ // Check semantics for each child
+ for (Node child : childs) {
+ errors.addAll(child.checkSemantics(ST, _nesting));
}
+ // Remove the HashMap from the SymbolTable
ST.remove();
return errors;
@@ -58,11 +56,8 @@ public class RootNode implements Node {
prefix += " ";
- for (Node stmt : stmts) {
- str += stmt.toPrint(prefix);
- }
- for (Node stmt : compoundStmts) {
- str += stmt.toPrint(prefix);
+ for (Node child : childs) {
+ str += child.toPrint(prefix);
}
return str;