diff options
author | geno <gabriele.genovese2@studio.unibo.it> | 2024-06-26 14:53:05 +0200 |
---|---|---|
committer | geno <gabriele.genovese2@studio.unibo.it> | 2024-06-26 14:53:05 +0200 |
commit | 06671f5aed68753435a762bc3be43e83094156d1 (patch) | |
tree | 56f251596cf57f6f13a7921e7dad6e795989329d /src/ast/nodes/ExprNode.java | |
parent | e09358f3648445bbf9747f40497af6221d933a99 (diff) |
exercise 1 completed
1.b now works
1.c implened error for "function takes N positional arguments but M were given"
Diffstat (limited to 'src/ast/nodes/ExprNode.java')
-rw-r--r-- | src/ast/nodes/ExprNode.java | 45 |
1 files changed, 32 insertions, 13 deletions
diff --git a/src/ast/nodes/ExprNode.java b/src/ast/nodes/ExprNode.java index 4bb67f7..eea69f8 100644 --- a/src/ast/nodes/ExprNode.java +++ b/src/ast/nodes/ExprNode.java @@ -3,6 +3,7 @@ package ast.nodes; import ast.types.*; import java.util.ArrayList; import java.util.Arrays; +import semanticanalysis.STentry; import semanticanalysis.SemanticError; import semanticanalysis.SymbolTable; @@ -100,29 +101,47 @@ public class ExprNode implements Node { } public String getId() { - return ((AtomNode) this.atom).getId(); + if (atom != null) { + return ((AtomNode) this.atom).getId(); + } else { + return null; + } } @Override public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) { ArrayList<SemanticError> errors = new ArrayList<SemanticError>(); - - 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 (atom != null && !trailers.isEmpty()) { + // function call + if (!Arrays.asList(bif).contains(atom.getId())) { + errors.addAll(atom.checkSemantics(ST, _nesting)); + Node trailer = trailers.get(0); + String funName = atom.getId(); + STentry s = ST.lookup(funName); + if (s != null) { + FunctionType ft = (FunctionType) s.getType(); + int paramNumber = ft.getParamNumber(); + int argNumber = ((TrailerNode) trailer).getArgumentNumber(); + if (paramNumber != argNumber) { + errors.add(new SemanticError(funName + "() takes " + String.valueOf(paramNumber) + " positional arguments but " + String.valueOf(argNumber) + " were given")); + } + } + } else { + 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; } @@ -157,11 +176,11 @@ public class ExprNode implements Node { for (var expr : exprs) { str += expr.toPrint(prefix); - } + } for (var trailer : trailers) { str += trailer.toPrint(prefix); - } + } if (op != null) { str += prefix + "Op(" + op + ")\n"; |