Ejemplo n.º 1
0
        ///FUNCTION: FUNCTION_NAME '(' FUNC_ARGS ')'
        private bool TryParseFunction(out ASTNode node)
        {
            node = null;
            if (IsNext(Token.TokenType.Identifier))
            {
                var token   = _lexer.Peek();
                var stEntry = _symbolTable.Get(token.Value);

                if (stEntry == null)
                {
                    throw new Exception($"Undefined Identifier {token.Value} at position {token.Position}");
                }

                if (stEntry.Type == SymbolTableEntry.EntryType.Function)
                {
                    node = new FunctionASTNode(Accept( ));

                    Expect(Token.TokenType.OpenParen);
                    Accept( );

                    if (TryParseFuncArgs(out var arguments))
                    {
                        (node as FunctionASTNode).ArgumentNodes.AddRange(arguments);
                    }

                    Expect(Token.TokenType.CloseParen);
                    Accept( );
                }
            }
            return(node != null);
        }
Ejemplo n.º 2
0
        protected double Evaluate(FunctionASTNode node)
        {
            var entry = _symbolTable.Get(node.Name);

            if (entry == null || entry.Type != SymbolTableEntry.EntryType.Function)
            {
                throw new Exception($"Error Evaluating Expression.  Function {node.Name}");
            }

            return
                (( double )
                 (entry as FunctionSystemTableEntry)
                 .MethodInfo
                 .Invoke(null, node.ArgumentNodes.Select(arg => Evaluate(arg as dynamic)).ToArray( )));
        }