Skip to content

Commit

Permalink
Semantic check for function declaration and function invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
L0P0P committed Jun 26, 2024
1 parent fc712f9 commit 9e6c17c
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 27 deletions.
4 changes: 3 additions & 1 deletion progs/test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
def unibo(a):
print("u")
if a == 3:
print("UNIBO")

a = 3
unibo(a)
14 changes: 12 additions & 2 deletions src/ast/nodes/ArglistNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,19 @@ public ArglistNode(ArrayList<Node> arguments) {
@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;
Expand Down
9 changes: 6 additions & 3 deletions src/ast/nodes/AtomNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ public String getId() {
@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;
Expand Down
31 changes: 12 additions & 19 deletions src/ast/nodes/ExprNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,30 +106,23 @@ public String getId() {
@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;
}

Expand Down
5 changes: 4 additions & 1 deletion src/ast/nodes/FuncdefNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/ParamlistNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
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;
Expand Down
6 changes: 6 additions & 0 deletions src/semanticanalysis/STentry.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,10 @@ public String getLabel() {
return label;
}

@Override
public String toString() {
// Print all the fields of the STentry
return "Type: " + type + ", Offset: " + offset + ", Nesting: " + nesting + ", Label: " + label;
}

}
15 changes: 15 additions & 0 deletions src/semanticanalysis/SymbolTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,19 @@ public void increaseoffset() {
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;
}

}

0 comments on commit 9e6c17c

Please sign in to comment.