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 ); }
public void ParseBlock( Block block, Tokens tokens ) { bool enclosed = false; if ( tokens.PeekToken () == BeginBlock ) { enclosed = true; tokens.RemoveNextToken ( BeginBlock ); } while ( !tokens.AtEnd () ) { if ( enclosed && tokens.PeekToken () == EndBlock ) { tokens.RemoveNextToken ( EndBlock ); break; } bool parsed = false; foreach ( IStatementParser parser in _parsers ) { IStatement nextStatement; if ( parser.TryParse( tokens, block.Scope, out nextStatement ) ) { block.Add ( nextStatement ); parsed = true; break; } } if (!parsed) throw new Exception("Unable to parse token " + tokens.PeekToken() ); } }
public void PrintExpressionTest() { IKernel kernel = TestModule.GetTestKernel (); IExecutorFactory factory = kernel.Get<IExecutorFactory> (); Print print = new Print ( factory.GetPrintExecutor() ); print.Expr = new ArithExpr { Left = new StringLiteral { Value = "Yo " }, Right = new StringLiteral { Value = "planet!" }, Op = ArithOp.Add }; Block statements = new Block (); statements.Add ( print ); new Executor ( statements ); StandardOutDummy output = kernel.Get<IStandardOut> () as StandardOutDummy; Assert.AreEqual ( "Yo planet!", output.Text ); }
public void ExecuteBlockTest() { IKernel kernel = TestModule.GetTestKernel (); Assign assign = kernel.Get<Assign> (); assign.Ident = "x"; Block block = new Block(); Print print = kernel.Get<Print> (); print.Expr = new StringLiteral { Value = "Yo planet!" }; block.Add(print); assign.Expr = block; Block statements = new Block (); statements.Add ( assign ); Variable variable = kernel.Get<Variable> (); variable.Ident = "x"; statements.Add ( variable ); Executor target = new Executor ( statements ); StandardOutDummy output = kernel.Get<IStandardOut> () as StandardOutDummy; Assert.AreEqual ( "Yo planet!", output.Text ); }
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 ); }
public void SimpleIfTest() { IKernel kernel = TestModule.GetTestKernel (); Block statements = new Block (); Assign assign = kernel.Get<Assign> (); assign.Ident = "x"; assign.Expr = new NumberLiteral { Value = 5 }; statements.Add ( assign ); If iif = kernel.Get<If> (); Variable variable = kernel.Get<Variable> (); ( variable as Variable ).Ident = "x"; iif.Test = new ArithExpr { Left = variable, Op = ArithOp.Equality, Right = new NumberLiteral { Value = 5 } }; iif.Body = new Block (); Print print = kernel.Get<Print> (); Variable call = kernel.Get<Variable> (); ( call as Variable ).Ident = "x"; print.Expr = call; iif.Body.Add( print ); statements.Add ( iif ); Executor target = new Executor ( statements ); StandardOutDummy output = kernel.Get<IStandardOut> () as StandardOutDummy; Assert.AreEqual ( "5", output.Text ); }
public void SimplePrintTest() { IKernel kernel = TestModule.GetTestKernel (); Print print = kernel.Get<Print> (); print.Expr = new StringLiteral { Value = "Yo planet!" }; Block statements = new Block (); statements.Add ( print ); Executor target = new Executor ( statements ); StandardOutDummy output = kernel.Get<IStandardOut> () as StandardOutDummy; Assert.AreEqual( "Yo planet!", output.Text ); }
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 ); }