public override DataType staticTypeCheck() { DataType type = base.staticTypeCheck(); // check if callable Console.WriteLine(type.ToString()); if (type != DataType.CallableType) { Log.Error(String.Format("{0} is not callable", Identifier), Filename, Line, Position); } CallableType callable = Scope.Callables[Identifier]; // check parameter count matches defined parameter count if (Parameters.Count != callable.ParameterList.Count) { Log.Error("Parameter count mismatch", Filename, Line, Position); } // check parameter types match for (int index = 0; index < Parameters.Count; index++) { DataType parameterType = Parameters[index].staticTypeCheck(); DataType definedType = callable.ParameterList[index].staticTypeCheck(); if (parameterType != callable.ParameterList[index].staticTypeCheck()) { Log.Error(String.Format("Parameter type mismatch {0}->{1}", definedType.ToString(), parameterType.ToString()), Filename, Line, Position); } } return(callable.ReturnType); }
private bool identifierDefinition() { int line = _line; int position = _position; if (!parameter()) { return(false); } IdentifierDefinitionNode identifier = (IdentifierDefinitionNode)_stack.Pop(); Node rvalue = null; // default value if (accept("=")) { expression(); rvalue = _stack.Pop(); identifier.Value = rvalue; } // function definition else if (accept("(")) { // create new scope here so that parameters don't leak out SymbolTable.EnterScope(); List <Node> parameters = parameterList(); block(); SymbolTable.ExitScope(); Node functionBody = _stack.Pop(); CallableType type = new CallableType(identifier.Type, parameters, functionBody); SymbolTable.CurrentScope.Callables[identifier.Symbol.Identifier] = type; FunctionDefinitionNode node = new FunctionDefinitionNode(type, identifier.Symbol, _filename, line, position); _stack.Push(node); return(true); } _stack.Push(identifier); return(true); }
public FunctionDefinitionNode(CallableType callableType, Node symbol, string filename, int line, int position) : base(DataType.CallableType, symbol, null, filename, line, position) { CallableType = callableType; }