public bool TryParse(Tokens tokens, IScope scope, out IStatement statement) { if ( tokens.PeekToken () == IfTag ) { tokens.RemoveNextToken ( IfTag ); If iff = new If ( _executorFactory.GetIfExecutor ()); iff.Test = _expressionParser.ParseExpression ( scope, tokens ); iff.Body = _blockParser.GetBlock ( scope, tokens ); statement = iff; return true; } statement = null; return false; }
/// <summary> /// Returns a variable to execute if it was the tail call of the if block. /// </summary> public ITailCallExecution Execute( If info ) { _debug.PrintDebugInfo ( "Checking If" ); Dynamic test = info.Test.Evaluate (); if ( test.BoolValue ) { _debug.PrintDebugInfo ( "If passed" ); return info.Body.ExecuteBlockWithTailCallElimination (); } return null; }
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 ); }