Exemplo n.º 1
0
        private Types.Object StructuredDoAssign(Node.Structured node)
        {
            Types.Object lvalue = Evaluate(node.V);
            Types.Object rvalue = Evaluate(node.C).Itself;

            if (lvalue.IsIdentifier)
            {
                this.Environment.SetLocal(lvalue as Types.Identifier, rvalue);
            }
            else
            {
                throw new Exception("Left-hand side is not a valid L-value");
            }
            return(rvalue);
        }
Exemplo n.º 2
0
        //////

        private Types.Object EvaluateStructured(Node.Structured node)
        {
            if (node.C == null)
            {
                return(Evaluate(node.V));
            }
            Type compoundType = node.C.GetType();

            if (compoundType == typeof(Node.Assign))
            {
                return(StructuredDoAssign(node));
            }
            else if (compoundType == typeof(Node.Call))
            {
                return(StructuredDoCall(node));
            }
            else
            {
                return(NYI(node));
            }
        }
Exemplo n.º 3
0
        private Types.Object StructuredDoCall(Node.Structured node)
        {
            Types.Object target     = Evaluate(node.V);
            Types.Object parameters = Evaluate(node.C);

            if (target.IsIdentifier)
            {
                if (target.Itself.IsClosure)
                {
                    return(CallClosure(target.Itself as Types.Closure, parameters));
                }
                else if (target.Itself.IsList)
                {
                    return((target.Itself as Types.List).At(((parameters as Types.List).First.Value as Types.Number).Value - 1));
                }
                else
                {
                    if (BuiltIns.Methods.ContainsKey((target as Types.Identifier).Name))
                    {
                        return(BuiltIns.Methods[(target as Types.Identifier).Name](parameters));
                    }
                    else
                    {
                        return(new Types.Undefined());
                    }
                }
            }
            else if (target.Itself.IsClosure)
            {
                return(CallClosure(target.Itself as Types.Closure, parameters));
            }
            else if (target.Itself.IsList)
            {
                return((target.Itself as Types.List).At(((parameters as Types.List).First.Value as Types.Number).Value - 1));
            }
            else
            {
                return(new Types.Undefined());
            }
        }
Exemplo n.º 4
0
            public static Node.Expression Parse()
            {
                Node.Expression n;

                if (IsLookahead(1, Tokens.Keyword, Lexemes.If))
                {
                    Match(Tokens.Keyword, Lexemes.If);
                    Node.Expression test = Parser.Expression.Parse();
                    Match(Tokens.Keyword, Lexemes.Then);
                    Node.Expression expIf = Parser.Expression.Parse();

                    if (IsLookahead(1, Tokens.Keyword, Lexemes.Else))
                    {
                        Match(Tokens.Keyword, Lexemes.Else);
                        Node.Expression expElse = Parser.Expression.Parse();

                        n = new Node.IfThenElse
                        {
                            Test = test,
                            If   = expIf,
                            Else = expElse
                        };
                    }
                    else
                    {
                        n = new Node.IfThen
                        {
                            Test = test,
                            If   = expIf
                        }
                    };
                }
                else if (IsLookahead(1, Tokens.Keyword, Lexemes.While))
                {
                    Match(Tokens.Keyword, Lexemes.While);
                    Node.Expression test = Parser.Expression.Parse();
                    Match(Tokens.Keyword, Lexemes.Loop);
                    Node.Expression exp = Parser.Expression.Parse();

                    n = new Node.WhileLoop
                    {
                        Test = test,
                        Loop = exp
                    };
                }
                else if (IsLookahead(1, Tokens.Keyword, Lexemes.Loop))
                {
                    Match(Tokens.Keyword, Lexemes.Loop);
                    Node.Expression exp = Parser.Expression.Parse();
                    Match(Tokens.Keyword, Lexemes.While);
                    Node.Expression test = Parser.Expression.Parse();

                    n = new Node.LoopWhile
                    {
                        Test = test,
                        Loop = exp
                    };
                }
                else if (IsLookahead(1, Tokens.Keyword, Lexemes.Do))
                {
                    Match(Tokens.Keyword, Lexemes.Do);

                    n = new Node.Do
                    {
                        E = Parser.Expression.Parse()
                    };
                }
                else if (IsLookahead(1, Tokens.Keyword, Lexemes.Async))
                {
                    Match(Tokens.Keyword, Lexemes.Async);

                    n = new Node.Async
                    {
                        E = Parser.Expression.Parse()
                    };
                }
                else if (IsLookahead(1, Tokens.Identifier) ||
                         IsLookahead(1, Tokens.Number) ||
                         IsLookahead(1, Tokens.Keyword, Lexemes.Function) ||
                         IsLookahead(1, Tokens.Operator, Lexemes.BrackOpen) ||
                         IsLookahead(1, Tokens.Operator, Lexemes.BraceOpen) ||
                         IsLookahead(1, Tokens.Keyword, Lexemes.Undefined))
                {
                    n = new Node.Structured
                    {
                        V = Parser.Value.Parse(),
                        C = Parser.Continuation.Parse()
                    };
                }
                else
                {
                    throw new Exceptions.Parser.UnexpectedLookahead("Parse(Expression)", Lexer.Lookahead(1), 1);
                }

                return(n);
            }