diff options
Diffstat (limited to 'src/ast/nodes')
| -rw-r--r-- | src/ast/nodes/AtomNode.java | 2 | ||||
| -rw-r--r-- | src/ast/nodes/BlockNode.java | 11 | ||||
| -rw-r--r-- | src/ast/nodes/ExprNode.java | 4 | ||||
| -rw-r--r-- | src/ast/nodes/FuncdefNode.java | 1 | ||||
| -rw-r--r-- | src/ast/nodes/RootNode.java | 27 | 
5 files changed, 20 insertions, 25 deletions
| 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; | 
