diff options
author | Santo Cariotti <santo@dcariotti.me> | 2024-06-25 11:33:41 +0200 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2024-06-25 11:33:41 +0200 |
commit | 6bdf1fc6c1b7afe18ffcae05f8fb11eca0f51258 (patch) | |
tree | 2cfbae97d45388bac38defc9907c77e7b275b207 | |
parent | 54b2c77e6875c1abb3d73fdfd0de924e75633087 (diff) |
wip
-rw-r--r-- | progs/test.py | 19 | ||||
-rw-r--r-- | src/ast/nodes/AssignmentNode.java | 12 | ||||
-rw-r--r-- | src/ast/nodes/AtomNode.java | 22 | ||||
-rw-r--r-- | src/ast/nodes/ExprNode.java | 9 | ||||
-rw-r--r-- | src/ast/nodes/FuncdefNode.java | 12 | ||||
-rw-r--r-- | src/ast/nodes/ParamdefNode.java | 7 | ||||
-rw-r--r-- | src/ast/nodes/ParamlistNode.java | 2 | ||||
-rw-r--r-- | src/ast/nodes/RootNode.java | 12 | ||||
-rw-r--r-- | src/semanticanalysis/SymbolTable.java | 9 |
9 files changed, 72 insertions, 32 deletions
diff --git a/progs/test.py b/progs/test.py index 718885d..ab0901a 100644 --- a/progs/test.py +++ b/progs/test.py @@ -1,17 +1,4 @@ -import math -from re import f4, r3 +x = 1 -from abs import * - - -def find_first_duplicate(nums, x): - num_set = set() - no_duplicate = -1 - - for i in range(len(nums)): - if nums[i] in num_set: - return nums[i] - else: - num_set.add(nums[i]) - - return no_duplicate +if x == 1: + print("a") diff --git a/src/ast/nodes/AssignmentNode.java b/src/ast/nodes/AssignmentNode.java index 627e842..b0310b5 100644 --- a/src/ast/nodes/AssignmentNode.java +++ b/src/ast/nodes/AssignmentNode.java @@ -10,24 +10,26 @@ import ast.types.*; * Node for the `assignment` statement of the grammar. */ public class AssignmentNode implements Node { - private Node lhr; + private ExprNode lhr; private Node assign; - private Node rhr; + private ExprNode rhr; public AssignmentNode(Node lhr, Node assign, Node rhr) { - this.lhr = lhr; + this.lhr = (ExprNode) lhr; this.assign = assign; - this.rhr = rhr; + this.rhr = (ExprNode) rhr; } @Override public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) { ArrayList<SemanticError> errors = new ArrayList<SemanticError>(); - errors.addAll(lhr.checkSemantics(ST, _nesting)); + // errors.addAll(lhr.checkSemantics(ST, _nesting)); errors.addAll(assign.checkSemantics(ST, _nesting)); errors.addAll(rhr.checkSemantics(ST, _nesting)); + ST.insert(lhr.getId(), rhr.typeCheck(), _nesting, ""); + return errors; } diff --git a/src/ast/nodes/AtomNode.java b/src/ast/nodes/AtomNode.java index 4dcb926..7dc38fb 100644 --- a/src/ast/nodes/AtomNode.java +++ b/src/ast/nodes/AtomNode.java @@ -16,15 +16,33 @@ public class AtomNode implements Node { this.val = val; } + public String getId() { + return this.val; + } + @Override public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) { - return new ArrayList<SemanticError>(); + var errors = new ArrayList<SemanticError>(); + System.out.println(getId() + " " + _nesting + " " + ST.nslookup(getId())); + if (!(this.typeCheck() instanceof IntType) && !ST.top_lookup(this.getId())) { + System.out.println(!(this.typeCheck() instanceof IntType) + " " + !ST.top_lookup(this.getId())); + errors.add(new SemanticError("Undefined name `" + this.getId() + "`")); + } + + return errors; } // FIXME: this type for atom @Override public Type typeCheck() { - return new AtomType(); + try { + Integer.parseInt(this.val); + System.out.println(this.val + " is int"); + return new IntType(); + } catch (NumberFormatException e) { + System.out.println(this.val + " is atom"); + return new AtomType(); + } } // TODO: add code generation for atom node diff --git a/src/ast/nodes/ExprNode.java b/src/ast/nodes/ExprNode.java index 25c2016..39f397e 100644 --- a/src/ast/nodes/ExprNode.java +++ b/src/ast/nodes/ExprNode.java @@ -24,10 +24,13 @@ public class ExprNode implements Node { this.trailers = trailers; } + public String getId() { + return ((AtomNode) this.atom).getId(); + } + @Override public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) { ArrayList<SemanticError> errors = new ArrayList<SemanticError>(); - if (atom != null) { errors.addAll(atom.checkSemantics(ST, _nesting)); } @@ -50,6 +53,10 @@ public class ExprNode implements Node { // FIXME: type for the expr @Override public Type typeCheck() { + if (this.atom != null) { + return this.atom.typeCheck(); + } + return new VoidType(); } diff --git a/src/ast/nodes/FuncdefNode.java b/src/ast/nodes/FuncdefNode.java index 67bc772..e50e35c 100644 --- a/src/ast/nodes/FuncdefNode.java +++ b/src/ast/nodes/FuncdefNode.java @@ -1,6 +1,7 @@ package ast.nodes; import java.util.ArrayList; +import java.util.HashMap; import semanticanalysis.SemanticError; import semanticanalysis.SymbolTable; @@ -24,11 +25,20 @@ 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>(); + ArrayList<Type> partypes = new ArrayList<Type>(); + + ST.add(HM); if (paramlist != null) { - errors.addAll(paramlist.checkSemantics(ST, _nesting)); + errors.addAll(paramlist.checkSemantics(ST, _nesting + 1)); } + // Offset is increased for the possible return value + ST.increaseoffset(); + errors.addAll(block.checkSemantics(ST, _nesting)); return errors; diff --git a/src/ast/nodes/ParamdefNode.java b/src/ast/nodes/ParamdefNode.java index 481282e..3ad82dd 100644 --- a/src/ast/nodes/ParamdefNode.java +++ b/src/ast/nodes/ParamdefNode.java @@ -11,6 +11,13 @@ public class ParamdefNode extends AtomNode { super(val); } + @Override + public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) { + ST.insert(this.getId(), this.typeCheck(), _nesting, ""); + + return new ArrayList<SemanticError>(); + } + // FIXME: it should returns the param' type @Override public Type typeCheck() { diff --git a/src/ast/nodes/ParamlistNode.java b/src/ast/nodes/ParamlistNode.java index 144eb13..a097c19 100644 --- a/src/ast/nodes/ParamlistNode.java +++ b/src/ast/nodes/ParamlistNode.java @@ -21,7 +21,7 @@ public class ParamlistNode implements Node { ArrayList<SemanticError> errors = new ArrayList<SemanticError>(); for (var param : params) { - errors.addAll(param.checkSemantics(ST, _nesting)); + errors.addAll(param.checkSemantics(ST, _nesting + 1)); } return errors; diff --git a/src/ast/nodes/RootNode.java b/src/ast/nodes/RootNode.java index e0989e8..fd33e30 100644 --- a/src/ast/nodes/RootNode.java +++ b/src/ast/nodes/RootNode.java @@ -1,6 +1,7 @@ package ast.nodes; import java.util.ArrayList; +import java.util.HashMap; import semanticanalysis.SemanticError; import semanticanalysis.SymbolTable; @@ -24,13 +25,20 @@ public class RootNode implements Node { public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) { ArrayList<SemanticError> errors = new ArrayList<SemanticError>(); - for (Node stmt : stmts) { + HashMap<String, STentry> HM = new HashMap<String, STentry>(); + + ST.add(HM); + + for (Node stmt : compoundStmts) { errors.addAll(stmt.checkSemantics(ST, _nesting)); } - for (Node stmt : compoundStmts) { + + for (Node stmt : stmts) { errors.addAll(stmt.checkSemantics(ST, _nesting)); } + ST.remove(); + return errors; } diff --git a/src/semanticanalysis/SymbolTable.java b/src/semanticanalysis/SymbolTable.java index f2876db..8193d5e 100644 --- a/src/semanticanalysis/SymbolTable.java +++ b/src/semanticanalysis/SymbolTable.java @@ -40,8 +40,8 @@ public class SymbolTable { HashMap<String, STentry> H = this.symbolTable.get(n); T = H.get(id); if (T != null) { - found = true; - }else { + found = true; + } else { n = n - 1; } } @@ -60,8 +60,8 @@ public class SymbolTable { while ((n >= 0) && !found) { HashMap<String, STentry> H = this.symbolTable.get(n); if (H.get(id) != null) { - found = true; - }else { + found = true; + } else { n = n - 1; } } @@ -125,6 +125,7 @@ public class SymbolTable { // We always increment the offset by 1 otherwise we need ad-hoc bytecode // operations + // FIXME: wtf is that? if (type.getClass().equals((new BoolType()).getClass())) { offs = offs + 1; } else if (type.getClass().equals((new IntType()).getClass())) { |