summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorL0P0P <grassoemanuele@live.com>2024-06-26 11:44:58 +0200
committerL0P0P <grassoemanuele@live.com>2024-06-26 11:44:58 +0200
commit9e6c17cb44bc165e315ec039a0e09183716d2037 (patch)
tree4a64fd37fa9b2483b88a5f33a37c6b9b51812d81
parentfc712f94a7ed8554d8d44f4965be367354a7e670 (diff)
Semantic check for function declaration and function invocation
-rw-r--r--progs/test.py4
-rw-r--r--src/ast/nodes/ArglistNode.java14
-rw-r--r--src/ast/nodes/AtomNode.java9
-rw-r--r--src/ast/nodes/ExprNode.java31
-rw-r--r--src/ast/nodes/FuncdefNode.java5
-rw-r--r--src/ast/nodes/ParamlistNode.java2
-rw-r--r--src/semanticanalysis/STentry.java6
-rw-r--r--src/semanticanalysis/SymbolTable.java15
8 files changed, 59 insertions, 27 deletions
diff --git a/progs/test.py b/progs/test.py
index 72729e1..97e316f 100644
--- a/progs/test.py
+++ b/progs/test.py
@@ -1,4 +1,6 @@
def unibo(a):
- print("u")
+ if a == 3:
+ print("UNIBO")
+a = 3
unibo(a)
diff --git a/src/ast/nodes/ArglistNode.java b/src/ast/nodes/ArglistNode.java
index f0c33e1..71bcdce 100644
--- a/src/ast/nodes/ArglistNode.java
+++ b/src/ast/nodes/ArglistNode.java
@@ -19,9 +19,19 @@ public class ArglistNode implements Node {
@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
ArrayList<SemanticError> errors = new ArrayList<SemanticError>();
-
+
for (var arg : arguments) {
- errors.addAll(arg.checkSemantics(ST, _nesting));
+ ExprNode argExpr = (ExprNode) arg;
+ String argName = argExpr.getId();
+
+ // TODO: check fucking IntType for params
+ // TODO: remove fucking comments
+ if (!ST.top_lookup(argName) && argExpr.typeCheck() instanceof AtomType){
+ // System.out.println(!(this.typeCheck() instanceof IntType) + " " + !ST.top_lookup(this.getId()));
+ errors.add(new SemanticError("'" + argName + "' is not defined."));
+ } else {
+ errors.addAll(arg.checkSemantics(ST, _nesting));
+ }
}
return errors;
diff --git a/src/ast/nodes/AtomNode.java b/src/ast/nodes/AtomNode.java
index 0a3c765..0704bc4 100644
--- a/src/ast/nodes/AtomNode.java
+++ b/src/ast/nodes/AtomNode.java
@@ -25,10 +25,13 @@ public class AtomNode implements Node {
@Override
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.nslookup(this.getId()) < 0) {
+
+ // Print the symbol table
+ System.out.println(ST);
+
+ if ((this.typeCheck() instanceof AtomType) && !ST.top_lookup(this.getId())/*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() + "`"));
+ errors.add(new SemanticError("'" + this.getId() + "' is not defined."));
}
return errors;
diff --git a/src/ast/nodes/ExprNode.java b/src/ast/nodes/ExprNode.java
index 344f3c0..4bb67f7 100644
--- a/src/ast/nodes/ExprNode.java
+++ b/src/ast/nodes/ExprNode.java
@@ -106,30 +106,23 @@ public class ExprNode implements Node {
@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
ArrayList<SemanticError> errors = new ArrayList<SemanticError>();
- if (atom != null && !trailers.isEmpty()) {
- // function call
- if (!Arrays.asList(bif).contains(atom.getId())) {
- errors.addAll(atom.checkSemantics(ST, _nesting));
- }
- } else {
- // butto tutto quello che c'era prima nell'else così non rischio di perdere niente di utile
- if (atom != null) {
- errors.addAll(atom.checkSemantics(ST, _nesting));
- }
-
- if (compOp != null) {
- errors.addAll(compOp.checkSemantics(ST, _nesting));
- }
-
- for (var expr : exprs) {
- errors.addAll(expr.checkSemantics(ST, _nesting));
- }
-
+
+ if (atom != null && !Arrays.asList(bif).contains(atom.getId())) {
+ errors.addAll(atom.checkSemantics(ST, _nesting));
+
for (var trailer : trailers) {
errors.addAll(trailer.checkSemantics(ST, _nesting));
}
}
+
+ if (compOp != null) {
+ errors.addAll(compOp.checkSemantics(ST, _nesting));
+ }
+ for (var expr : exprs) {
+ errors.addAll(expr.checkSemantics(ST, _nesting));
+ }
+
return errors;
}
diff --git a/src/ast/nodes/FuncdefNode.java b/src/ast/nodes/FuncdefNode.java
index 742f670..f3be1d9 100644
--- a/src/ast/nodes/FuncdefNode.java
+++ b/src/ast/nodes/FuncdefNode.java
@@ -37,10 +37,13 @@ public class FuncdefNode implements Node {
errors.addAll(paramlist.checkSemantics(ST, _nesting + 1));
}
+ // TODO: think to the fucking offset
// Offset is increased for the possible return value
ST.increaseoffset();
- errors.addAll(block.checkSemantics(ST, _nesting));
+ errors.addAll(block.checkSemantics(ST, _nesting + 1));
+
+ ST.remove();
return errors;
}
diff --git a/src/ast/nodes/ParamlistNode.java b/src/ast/nodes/ParamlistNode.java
index a097c19..144eb13 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 + 1));
+ errors.addAll(param.checkSemantics(ST, _nesting));
}
return errors;
diff --git a/src/semanticanalysis/STentry.java b/src/semanticanalysis/STentry.java
index 0e4b00e..07b62c8 100644
--- a/src/semanticanalysis/STentry.java
+++ b/src/semanticanalysis/STentry.java
@@ -52,4 +52,10 @@ public class STentry {
return label;
}
+ @Override
+ public String toString() {
+ // Print all the fields of the STentry
+ return "Type: " + type + ", Offset: " + offset + ", Nesting: " + nesting + ", Label: " + label;
+ }
+
}
diff --git a/src/semanticanalysis/SymbolTable.java b/src/semanticanalysis/SymbolTable.java
index 617b48a..6756ec4 100644
--- a/src/semanticanalysis/SymbolTable.java
+++ b/src/semanticanalysis/SymbolTable.java
@@ -153,4 +153,19 @@ public class SymbolTable {
this.offset.add(offs);
}
+ @Override
+ public String toString() {
+ // Print the symbol table
+ String str = "";
+ for (int i = 0; i < this.symbolTable.size(); i++) {
+ str += "Level " + i + "\n";
+ HashMap<String, STentry> H = this.symbolTable.get(i);
+ for (String key : H.keySet()) {
+ STentry T = H.get(key);
+ str += key + " -> " + T.toString() + "\n";
+ }
+ }
+ return str;
+ }
+
}