private void EmitIfStatement(Statement.If ifStmt, BlockCollector blockCollector) { var ifStmtElseIfs = (IAstList <ElseIf>)ifStmt.ElseIfs; var elseBody = ifStmt.Else.HasValue ? EmitStatement(ifStmt.Else.Value.Body).ToBlockExpression() : null; Expression elseIfExpr = null; // else if statements if (ifStmt.ElseIfs != null && ifStmtElseIfs.Count > 0) { var lastElseIf = ifStmtElseIfs[ifStmtElseIfs.Count - 1]; Expression lastElseIfExpr = elseBody == null ? Expression.IfThen(EmitExpression(lastElseIf.Condition), EmitStatement(lastElseIf.Body).ToBlockExpression()) : Expression.IfThenElse(EmitExpression(lastElseIf.Condition), EmitStatement(lastElseIf.Body).ToBlockExpression(), elseBody); for (int i = ifStmtElseIfs.Count - 2; i >= 0; i--) { var currentElseIf = ifStmtElseIfs[i]; elseIfExpr = Expression.IfThenElse( EmitExpression(currentElseIf.Condition), EmitStatement(currentElseIf.Body).ToBlockExpression(), Expression.Block(lastElseIfExpr)); lastElseIfExpr = elseIfExpr; } elseBody = Expression.Block(elseIfExpr ?? lastElseIfExpr); } blockCollector.AddExpression(elseBody == null ? Expression.IfThen(EmitExpression(ifStmt.Condition), EmitStatement(ifStmt.Body).ToBlockExpression()) : Expression.IfThenElse(EmitExpression(ifStmt.Condition), EmitStatement(ifStmt.Body).ToBlockExpression(), elseBody)); }
public object VisitIfStatement(Statement.If statement) { Resolve(statement.condition); Resolve(statement.thenBranch); if (statement.elseBranch != null) { Resolve(statement.elseBranch); } return(null); }
public object VisitIfStatement(Statement.If statement) { if (IsTruthy(Evaluate(statement.condition))) { Execute(statement.thenBranch); } else if (statement.elseBranch != null) { Execute(statement.elseBranch); } return(null); }
public override Statement VisitSTMR_If([NotNull] S_ScriptParser.STMR_IfContext context) { Statement.If x = new Statement.If(this._Host, this._Master, context.K_ELSE() != null); foreach (S_ScriptParser.StatementContext s in context.statement()) { x.AddChild(this.Visit(s)); } foreach (S_ScriptParser.ExpressionContext e in context.expression()) { x.Parameters.Add("F" + x.Parameters.Count.ToString(), this._expr.Render(e)); } this._Master = x; return(x); }
Expr IStatementVisitor <Expr> .Visit(Statement.If statement) { var testExpr = statement.Test.Visit(this); var bodyExpr = Visit(statement.Body); var elseExpr = statement.ElseBody != null?Visit(statement.ElseBody) : Expr.Default(typeof(void)); var elseifExprs = statement.Elseifs.Aggregate(elseExpr, ElseifCombiner); return (Expr.IfThenElse( Expr.Dynamic( Context.DynamicCache.GetConvertBinder(typeof(bool)), typeof(bool), testExpr), bodyExpr, elseifExprs)); }
public object VisitIfStatement(Statement.If stmt) { // TODO fix returningBranch spaget returningBranch = false; stmt.Then.Accept(this); var thenReturning = returningBranch; returningBranch = false; if (stmt.Else != null) { stmt.Else.Accept(this); } stmt.Returning = thenReturning && returningBranch; return(null); }
public ByteCodeChunk VisitIf(Statement.If statement) { var chunk = new ByteCodeChunk(); chunk.AddRange(VisitExpression(statement.Condition)); var thenBlock = VisitBlock(statement.ThenBlock); chunk.AddInstruction(Instruction.JmpIfZero, thenBlock.Count); chunk.AddRange(thenBlock); if (statement.ElseBlock == null) { return(chunk); } var elseBlock = VisitBlock(statement.ElseBlock); chunk.AddInstruction(Instruction.JmpShort, elseBlock.Count); chunk.AddRange(elseBlock); return(chunk); }
public object VisitIfStatement(Statement.If stmt) { stmt.Condition.Accept(this); addInstruction(0x04); // if addInstruction(0x40); // if returns void stmt.Then.Accept(this); if (stmt.Else != null) { addInstruction(0x05); stmt.Else.Accept(this); } addInstruction(0x0b); // end // Add a dummy return value if all branches of if return a value // This return value will never be reached, but wasm doesn't know it if (stmt.Returning) { addInstruction(ZERO); } return(null); }