Ejemplo n.º 1
0
        private bool TryParseFunction(out AstNode node)
        {
            node = null;

            if (IsNext(TokenType.Identifier))
            {
                var token   = _lexer.Peek();
                var stEntry = _symbolTable.Get(token.Value);

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

                if (stEntry.Type == EntryType.Function)
                {
                    node = new FunctionIdentifierAstNode(Accept());
                    Expect(TokenType.OpenParen);
                    Accept();

                    if (TryParseFuncArgs(out var args))
                    {
                        (node as FunctionIdentifierAstNode).ArgumentNodes.AddRange(args);
                    }

                    Expect(TokenType.CloseParen);
                    Accept();
                }
            }

            return(node != null);
        }
Ejemplo n.º 2
0
        private double Evaluate(FunctionIdentifierAstNode node)
        {
            var entry = _symbolTable.Get(node.Name);

            if (entry is null || entry.Type != EntryType.Function)
            {
                throw new Exception($"Error evaluating expression. Function {node.Name}");
            }

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