diff options
author | L0P0P <grassoemanuele@live.com> | 2024-06-27 12:02:35 +0200 |
---|---|---|
committer | L0P0P <grassoemanuele@live.com> | 2024-06-27 12:02:35 +0200 |
commit | 3c4229fc9e0ec6da9a7f60b57b9e93c49d1b6b6c (patch) | |
tree | aca198eca62a0260a50ea59003c44a942e98f521 /src/ast/nodes/ExprNode.java | |
parent | fb89b8c0885ee4e289cfb577bbabf0ee66b05024 (diff) |
Fixed a lot of problems from all the progs we need to parse
Diffstat (limited to 'src/ast/nodes/ExprNode.java')
-rw-r--r-- | src/ast/nodes/ExprNode.java | 114 |
1 files changed, 30 insertions, 84 deletions
diff --git a/src/ast/nodes/ExprNode.java b/src/ast/nodes/ExprNode.java index 4fbe166..1b20e2e 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; @@ -18,80 +19,6 @@ public class ExprNode implements Node { private ArrayList<Node> exprs; private ArrayList<Node> trailers; - // built-in functions - private static final String[] bif = { - "abs", - "aiter", - "all", - "anext", - "any", - "ascii", - "bin", - "bool", - "breakpoint", - "bytearray", - "bytes", - "callable", - "chr", - "classmethod", - "compile", - "complex", - "delattr", - "dict", - "dir", - "divmod", - "enumerate", - "eval", - "exec", - "filter", - "float", - "format", - "frozenset", - "getattr", - "globals", - "hasattr", - "hash", - "help", - "hex", - "id", - "input", - "int", - "isinstance", - "issubclass", - "iter", - "len", - "list", - "locals", - "map", - "max", - "memoryview", - "min", - "next", - "object", - "oct", - "open", - "ord", - "pow", - "print", - "property", - "range", - "repr", - "reversed", - "round", - "set", - "setattr", - "slice", - "sorted", - "staticmethod", - "str", - "sum", - "super", - "tuple", - "type", - "vars", - "zip", - "__import__"}; - public ExprNode(Node atom, Node compOp, ArrayList<Node> exprs, String op, ArrayList<Node> trailers) { this.atom = (AtomNode) atom; this.compOp = compOp; @@ -112,19 +39,38 @@ public class ExprNode implements Node { public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) { ArrayList<SemanticError> errors = new ArrayList<SemanticError>(); + // check if the atom is a built-in function 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); + + TrailerNode trailer = (TrailerNode) 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")); + + // 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 { @@ -150,7 +96,7 @@ public class ExprNode implements Node { // FIXME: type for the expr @Override public Type typeCheck() { - if (this.atom != null) { + if (this.atom != null ) { return this.atom.typeCheck(); } |