Skip to content

Commit

Permalink
Using child
Browse files Browse the repository at this point in the history
  • Loading branch information
L0P0P committed Jun 26, 2024
1 parent 9bb71e3 commit fc712f9
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 47 deletions.
8 changes: 4 additions & 4 deletions progs/test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
x = 1

if x == 1:
print("a")
def unibo(a):
print("u")
unibo(a)
40 changes: 22 additions & 18 deletions src/ast/Python3VisitorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -331,17 +333,19 @@ public Node visitFor_stmt(For_stmtContext ctx) {
* ```
*/
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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/AtomNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public String getId() {
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() + "`"));
}
Expand Down
11 changes: 4 additions & 7 deletions src/ast/nodes/BlockNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
* 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
Expand All @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/ast/nodes/ExprNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/ast/nodes/FuncdefNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public FuncdefNode(TerminalNode name, Node paramlist, Node block) {
@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>();
Expand Down
27 changes: 11 additions & 16 deletions src/ast/nodes/RootNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit fc712f9

Please sign in to comment.