Exemple #1
0
        public object Eval(string scriptCode)
        {
            object ret = null;

            try
            {
                this.SetParent((ScriptObject)engine.globalScope.GetObject());

                StackFrame sf = new StackFrame(this, null, new object[0], this);
                engine.Globals.ScopeStack.Push(sf);

                Context  context = new Context(new DocumentContext("eval code", engine), ((IConvertible)scriptCode).ToString());
                JSParser p       = new JSParser(context);

                Block b = p.ParseEvalBody();
                AST   a = b.PartiallyEvaluate();

                Completion result = (Completion)a.Evaluate();

                ret = ((Completion)result).value;
            }
            finally
            {
                engine.Globals.ScopeStack.Pop();
            }

            return(ret);
        }
Exemple #2
0
        public AST Get(AST i)
        {
            Tree.List list  = env.Get(this.listName);
            int       index = int.Parse(i.Evaluate(env).Value.ToString());

            return(list.Get(index));
        }
Exemple #3
0
        void Should_Evaluate_When_DivideOperator_Given()
        {
            var tokens = new List <Token>()
            {
                new Token(TokenType.Number, "6", 6),
                new Token(TokenType.DivideOperator, "/"),
                new Token(TokenType.Number, "2", 2),
            };
            var ast    = new AST(tokens);
            var actual = ast.Evaluate();

            Assert.Equal(3, actual);
        }
Exemple #4
0
        void Should_Evaluate_When_MultiplyOperator_Given()
        {
            var tokens = new List <Token>()
            {
                new Token(TokenType.Number, "3", 3),
                new Token(TokenType.MultiplyOperator, "*"),
                new Token(TokenType.Number, "2", 2),
            };
            var ast    = new AST(tokens);
            var actual = ast.Evaluate();

            Assert.Equal(6, actual);
        }
Exemple #5
0
        void Should_Evaluate_When_PlusOperator_Given()
        {
            var tokens = new List <Token>()
            {
                new Token(TokenType.Number, "1", 1),
                new Token(TokenType.PlusOperator, "+"),
                new Token(TokenType.Number, "2", 2),
            };
            var ast    = new AST(tokens);
            var actual = ast.Evaluate();

            Assert.Equal(3, actual);
        }
Exemple #6
0
        public int Run(string input)
        {
            var tokens = _lexer.Scan(input);

            var ast = new AST(tokens);

            ast.EvaluationCallBack = (token) => { _printer.Print(token); };

            var result = ast.Evaluate() ?? 0;

            _printer.Print(result);

            //_printer.Print(tokens, result);

            return(result);
        }
Exemple #7
0
        void Should_Evaluate_When_Multiple_Operators_Given()
        {
            var tokens = new List <Token>()
            {
                new Token(TokenType.Number, "1", 1),
                new Token(TokenType.MinusOperator, "-"),
                new Token(TokenType.Number, "2", 2),
                new Token(TokenType.MultiplyOperator, "*"),
                new Token(TokenType.Number, "3", 3),
                new Token(TokenType.PlusOperator, "+"),
                new Token(TokenType.Number, "10", 10),
                new Token(TokenType.DivideOperator, "/"),
                new Token(TokenType.Number, "5", 5),
            };
            var ast    = new AST(tokens);
            var actual = ast.Evaluate();

            Assert.Equal(-3, actual);
        }
Exemple #8
0
        public override dynamic Evaluate(Env.Environment env)
        {
            var res = new Tree.Boolean(condition.Evaluate(env)).Value;

            if (res)
            {
                Function f = thenBlock as Function;
                if (f != null)
                {
                    this.Value = f.Solve(env);
                }
                else
                {
                    this.Value = thenBlock.Evaluate(env);
                }
                return(this.Value);
            }
            else
            {
                if (elseBlock != null)
                {
                    Function f = elseBlock as Function;
                    if (f != null)
                    {
                        this.Value = f.Solve(env);
                    }
                    else
                    {
                        this.Value = elseBlock.Evaluate(env);
                    }
                    return(this.Value);
                }
                else
                {
                    this.Value = new Tree.Null();
                    return(this.Value);
                }
            }
        }