From fc712f94a7ed8554d8d44f4965be367354a7e670 Mon Sep 17 00:00:00 2001 From: L0P0P Date: Wed, 26 Jun 2024 10:06:12 +0200 Subject: [PATCH] Using child --- progs/test.py | 8 +++---- src/ast/Python3VisitorImpl.java | 40 ++++++++++++++++++--------------- src/ast/nodes/AtomNode.java | 2 +- src/ast/nodes/BlockNode.java | 11 ++++----- src/ast/nodes/ExprNode.java | 4 +++- src/ast/nodes/FuncdefNode.java | 1 + src/ast/nodes/RootNode.java | 27 +++++++++------------- 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 { * ``` */ public Node visitRoot(RootContext ctx) { - ArrayList stmts = new ArrayList(); - ArrayList compStmts = new ArrayList(); - - for (Simple_stmtsContext stm : ctx.simple_stmts()) { - stmts.add(visit(stm)); - } - for (Compound_stmtContext stm : ctx.compound_stmt()) { - compStmts.add(visit(stm)); + ArrayList childs = new ArrayList(); + + 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 Node visitFor_stmt(For_stmtContext ctx) { * ``` */ public Node visitBlock(BlockContext ctx) { - ArrayList stmts = new ArrayList(); - ArrayList compStmts = new ArrayList(); - - for (Simple_stmtsContext s : ctx.simple_stmts()) { - stmts.add(visit(s)); - } - for (Compound_stmtContext s : ctx.compound_stmt()) { - compStmts.add(visit(s)); + ArrayList childs = new ArrayList(); + + 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 String getId() { public ArrayList checkSemantics(SymbolTable ST, int _nesting) { var errors = new ArrayList(); // 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 @@ * It extends the `RootNode`. */ public class BlockNode extends RootNode { - public BlockNode(ArrayList stmts, ArrayList compoundStmts) { - super(stmts, compoundStmts); + public BlockNode(ArrayList childs) { + super(childs); } @Override @@ -23,11 +23,8 @@ 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); + 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 exprs; private ArrayList 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 FuncdefNode(TerminalNode name, Node paramlist, Node block) { @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { ArrayList errors = new ArrayList(); + ST.insert(this.name.toString(), this.block.typeCheck(), _nesting, ""); HashMap HM = new HashMap(); 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 stmts; - protected ArrayList compoundStmts; + protected ArrayList childs; - public RootNode(ArrayList stmts, ArrayList compoundStmts) { - this.stmts = stmts; - this.compoundStmts = compoundStmts; + public RootNode(ArrayList childs) { + this.childs = childs; } @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { ArrayList errors = new ArrayList(); + // Create a new HashMap for the current scope HashMap HM = new HashMap(); + // 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 String toPrint(String prefix) { 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;