diff options
Diffstat (limited to 'src/ast/nodes/ExprNode.java')
-rw-r--r-- | src/ast/nodes/ExprNode.java | 66 |
1 files changed, 58 insertions, 8 deletions
diff --git a/src/ast/nodes/ExprNode.java b/src/ast/nodes/ExprNode.java index 25c2016..1b20e2e 100644 --- a/src/ast/nodes/ExprNode.java +++ b/src/ast/nodes/ExprNode.java @@ -1,34 +1,84 @@ package ast.nodes; +import ast.types.*; import java.util.ArrayList; +import java.util.Arrays; +import semanticanalysis.STentry; import semanticanalysis.SemanticError; import semanticanalysis.SymbolTable; -import ast.types.*; /** * Node for the `expr` statement of the grammar. */ public class ExprNode implements Node { - private Node atom; + + private AtomNode atom; private Node compOp; private String op; private ArrayList<Node> exprs; private ArrayList<Node> trailers; public ExprNode(Node atom, Node compOp, ArrayList<Node> exprs, String op, ArrayList<Node> trailers) { - this.atom = atom; + this.atom = (AtomNode) atom; this.compOp = compOp; this.exprs = exprs; this.op = op; this.trailers = trailers; } + public String 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) { + // check if the atom is a built-in function + if (atom != null && !trailers.isEmpty()) { + + if (!Arrays.asList(bif).contains(atom.getId())) { + + errors.addAll(atom.checkSemantics(ST, _nesting)); + + TrailerNode trailer = (TrailerNode) trailers.get(0); + String funName = atom.getId(); + + // TODO: it isnt a function, it could be a variable + STentry fun = ST.lookup(funName); + + + + if (fun != null && !(fun.getType() instanceof ImportType)) { + if (!(fun.getType() instanceof FunctionType)) { + if (trailer.isParenthesis()) { + errors.add(new SemanticError("'" + funName + "' is not a function.")); + } else { + for (var t : trailers) { + errors.addAll(t.checkSemantics(ST, _nesting)); + } + } + } else { + FunctionType ft = (FunctionType) fun.getType(); + int paramNumber = ft.getParamNumber(); + int argNumber = 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)); + } + } + } else if (atom != null) { errors.addAll(atom.checkSemantics(ST, _nesting)); } @@ -40,16 +90,16 @@ public class ExprNode implements Node { errors.addAll(expr.checkSemantics(ST, _nesting)); } - for (var trailer : trailers) { - errors.addAll(trailer.checkSemantics(ST, _nesting)); - } - return errors; } // FIXME: type for the expr @Override public Type typeCheck() { + if (this.atom != null ) { + return this.atom.typeCheck(); + } + return new VoidType(); } |