public bool TryParse(Tokens tokens, IScope scope, out IStatement statement) { // This is a block call Variable variable = new Variable ( _executorFactory.GetVariableExecutor() ); variable.Scope = scope; variable.Ident = tokens.PullToken (); // Check if there is an indexer into the variable if (tokens.PeekToken () == "[") { tokens.PullToken (); variable.Indexer = _expressionParser.ParseExpression ( scope, tokens ); tokens.RemoveNextToken ("]"); } if ( tokens.NextTokenIs("(") ) { // Parse the parameters.. variable.Parameters = _expressionParser.ParseArray( scope, tokens ) as ArrayExpr; } statement = variable; return true; }
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 ); }
/// <summary> /// Value is a constant string, number or variable name. /// </summary> /// <param name="tokens"></param> /// <returns></returns> public Expression ParseValue( IScope scope, Tokens tokens ) { Expression value = null; if ( tokens.NextTokenIs ( BeginBlock ) ) { value = _blockParser.GetBlock ( scope, tokens ); } else if ( tokens.PeekToken().StartsWith ( "'" ) && tokens.PeekToken().EndsWith ( "'" ) ) { string literal = tokens.PullToken().Trim ( '\'' ); value = new StringLiteral { Value = literal }; } else if ( tokens.PeekToken().IsNumber () ) { value = new NumberLiteral { Value = double.Parse ( tokens.PullToken() ) }; } else if ( tokens.NextTokenIs(BeginArray) ) { return this.ParseArray ( scope, tokens ); } else { var variable = new Variable ( _executorFactory.GetVariableExecutor () ); variable.Scope = scope; variable.Ident = tokens.PullToken(); // Check if there is an indexer into the variable if (tokens.PeekToken () == "[") { tokens.PullToken (); variable.Indexer = ParseExpression ( scope, tokens ); tokens.RemoveNextToken ("]"); } value = variable; } return value; }
/// <summary> /// Calls a block that this variable is assigned to. /// Returns a Variable which holds a block if that was the /// tail call of this block. /// </summary> public ITailCallExecution Execute( Variable variable, Dynamic parameters ) { Dynamic dynamic = Scope.GetDynamic ( variable.Ident ); if (dynamic.Type == DynamicType.arrayType) { Dynamic index = variable.Indexer.Evaluate(); dynamic = dynamic.ArrayValue[(Int32)Math.Truncate (index.NumberValue)]; } if ( dynamic.BlockValue != null ) { _debug.PrintDebugInfo ( "Executing block : " + variable.Ident ); dynamic.BlockValue.Scope.AddDynamic ( "$", parameters ); ITailCallExecution tailCall = dynamic.BlockValue.ExecuteBlockWithTailCallElimination (); if (tailCall != null) _debug.PrintDebugInfo ( "Tail call returned : " + tailCall.ToString() ); return tailCall; } 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 ); }