コード例 #1
0
ファイル: Executors.cs プロジェクト: mdcuesta/Ongle
        public void Execute( Assign info )
        {
            _debug.PrintDebugInfo ( "Assigning : " + info.Ident.Ident );

            if (Scope == null)
                Console.WriteLine ( "Scope is null" );

            if (info.Ident.Indexer != null )
                Scope.SetDynamic ( info.Ident.Ident, info.Ident.Indexer.Evaluate (), info.Expr.Evaluate () );
            else
                Scope.SetDynamic ( info.Ident.Ident, info.Expr.Evaluate () );
        }
コード例 #2
0
ファイル: VariableTests.cs プロジェクト: mdcuesta/Ongle
        public void TestAccessArray()
        {
            IKernel kernel = TestModule.GetTestKernel ();
            var factory = kernel.Get<IExecutorFactory>();

            Block statements = new Block ();
            statements.Scope = new Scope ();

            ArrayExpr array = new ArrayExpr(factory.GetArrayExecutor());
            array.Scope = statements.Scope;

            array.Elements.Add(new StringLiteral {
                Value = "yay"
            });

            array.Elements.Add(new StringLiteral {
                Value = "boo"
            });

            Assign assign = new Assign(factory.GetAssignExecutor())
            {
                Ident = new Variable ( factory.GetVariableExecutor ())
                {
                    Scope = statements.Scope,
                    Ident = "x"
                },
                Expr = array,
                Scope = statements.Scope
            };

            Print print = new Print(factory.GetPrintExecutor());
            print.Expr = new Variable( factory.GetVariableExecutor())
            {
                Scope = statements.Scope,
                Ident = "x",
                Indexer = new NumberLiteral()
                {
                    Value = 1
                }
            };

            statements.Add ( assign );
            statements.Add ( print );

            statements.Execute();

            StandardOutDummy output = kernel.Get<IStandardOut> () as StandardOutDummy;
            Assert.AreEqual ( "boo", output.Text );
        }
コード例 #3
0
ファイル: AssignmentParser.cs プロジェクト: mdcuesta/Ongle
        public bool TryParse(Tokens tokens, IScope scope, out IStatement statement)
        {
            tokens.SetMark ();

            Expression value = _expressionParser.ParseValue ( scope, tokens );

            // Look ahead to see if this is an assignment
            if ( value is Variable && tokens.PullTokenIfEqual ( "=" ) )
            {
                Assign assign = new Assign ( _executorFactory.GetAssignExecutor() );
                assign.Ident = value as Variable;

                Expression expression = _expressionParser.ParseExpression ( scope, tokens );
                assign.Expr = expression;

                statement = assign;
                return true;
            }

            // Failed to parse as assignment, rollback.
            tokens.RollbackToMark ();
            statement = null;
            return false;
        }
コード例 #4
0
ファイル: ExecutorTest.cs プロジェクト: mdcuesta/Ongle
        public void ExecuteBlockTest()
        {
            IKernel kernel = TestModule.GetTestKernel ();
            IExecutorFactory factory = kernel.Get<IExecutorFactory> ();
            Block statements = new Block ();
            statements.Scope = new Scope();

            Assign assign = new Assign ( factory.GetAssignExecutor () );
            assign.Ident = new Variable ( factory.GetVariableExecutor () )
            {
                Ident = "x"
            };
            assign.Scope = statements.Scope;

            Block block = new Block();
            block.Scope = statements.Scope;

            Print print = new Print ( factory.GetPrintExecutor () );
            print.Scope = block.Scope;
            print.Expr = new StringLiteral { Value = "Yo planet!" };
            block.Add(print);

            assign.Expr = block;

            Assert.IsTrue ( assign.Scope != null );
            statements.Add ( assign );
            Variable variable = new Variable ( factory.GetVariableExecutor () );
            variable.Ident = "x";
            variable.Scope = statements.Scope;
            statements.Add ( variable );

            new Executor ( statements );

            StandardOutDummy output = kernel.Get<IStandardOut> () as StandardOutDummy;
            Assert.AreEqual ( "Yo planet!", output.Text );
        }
コード例 #5
0
ファイル: ExecutorTest.cs プロジェクト: mdcuesta/Ongle
        public void SimpleIfTest()
        {
            IKernel kernel = TestModule.GetTestKernel ();
            IExecutorFactory factory = kernel.Get<IExecutorFactory> ();
            Block statements = new Block ();
            statements.Scope = new Scope ();

            Assign assign = new Assign ( factory.GetAssignExecutor () );
            assign.Scope = statements.Scope;
            assign.Ident = new Variable ( factory.GetVariableExecutor () )
            {
                Ident = "x"
            };
            assign.Expr = new NumberLiteral
            {
                Value = 5
            };
            statements.Add ( assign );

            If iif = new If ( factory.GetIfExecutor() );
            Variable variable = new Variable ( factory.GetVariableExecutor () );
            ( variable as Variable ).Ident = "x";

            iif.Scope = statements.Scope;
            iif.Test = new ArithExpr
            {
                Scope = iif.Scope,
                Left = variable,
                Op = ArithOp.Equality,
                Right = new NumberLiteral
                {
                    Value = 5
                }
            };

            iif.Body = new Block ();
            iif.Body.Scope = statements.Scope;

            Print print = new Print ( factory.GetPrintExecutor () );
            Variable call = new Variable ( factory.GetVariableExecutor () );
            ( call as Variable ).Ident = "x";
            print.Expr = call;

            print.Scope = iif.Body.Scope;
            call.Scope = iif.Body.Scope;
            iif.Body.Add( print );
            statements.Add ( iif );

            new Executor ( statements );

            StandardOutDummy output = kernel.Get<IStandardOut> () as StandardOutDummy;
            Assert.AreEqual ( "5", output.Text );
        }