public AstStatement VisitIf(Scope context, AstStatement.IfStmt ifStmt) { ifStmt.Condition = ifStmt.Condition.Accept(context, this); ifStmt.TrueBranch = ifStmt.TrueBranch.Accept(context, this); ifStmt.FalseBranch = ifStmt.FalseBranch?.Accept(context, this); return(ifStmt); }
public void VisitIf(IndentWriter context, AstStatement.IfStmt ifStmt) { context.WriteLine("if("); context.Indent(); this.PrintExpression(context, ifStmt.Condition, ifStmt.Scope); context.Unindent(); context.WriteLine(") {"); context.Indent(); ifStmt.TrueBranch.Accept(context, this); context.Unindent(); if (ifStmt.FalseBranch != null) { context.WriteLine("} else {"); context.Indent(); ifStmt.FalseBranch.Accept(context, this); context.Unindent(); } context.WriteLine("}"); }
public List <Instruction> VisitIf(object context, AstStatement.IfStmt ifStmt) { // var list = new List<Instruction>(); // list.AddRange(ifStmt.Condition.Accept(this)); // list.Add(IfElseBegin)); // list.AddRange(ifStmt.TrueBranch.Accept(this)); // list.Add(IfElseElse)); // if (ifStmt.FalseBranch != null) // { // list.AddRange(ifStmt.FalseBranch.Accept(this)); // } // list.Add(IfElseEnd)); // return list; /* * if (condition()) { statement(); } * 1) condition(); * jmpif 2 3 * 2) statement(); * jmp 3 * 3) ... */ /* * if (condition()) { statement1(); } else { statement2(); } * 1) condition(); * jmpif 2 3 * 2) statement1(); * jmp 4 * 3) statement2(); * jmp 4 * 4) ... * */ var scope = ifStmt.Scope; if (ifStmt.FalseBranch == null) { /* * if (condition()) { statement(); } * * condition(); * not[] * jumpif #label * statement(); * #label */ var label = this.GetNextLabel(ifStmt); var list = new List <Instruction>(); list.AddRange(ifStmt.Condition.Accept(scope, this)); list.Add(Not); list.Add(JumpIf, new int[] { label }); list.AddRange(ifStmt.TrueBranch.Accept(context, this)); list.Add(Label, new int[] { label }); return(list); } else { /* * if (condition()) { statement1(); } else { statement2(); } * * condition(); * jumpif #labelTrue * statement2(); * jump #labelEnd * #labelTrue * statement1(); * #labelEnd */ var labelTrue = this.GetNextLabel(ifStmt); var labelEnd = this.GetNextLabel(ifStmt); var list = new List <Instruction>(); list.AddRange(ifStmt.Condition.Accept(scope, this)); list.Add(JumpIf, new int[] { labelTrue }); list.AddRange(ifStmt.FalseBranch.Accept(context, this)); list.Add(Jump, new int[] { labelEnd }); list.Add(Label, new int[] { labelTrue }); list.AddRange(ifStmt.TrueBranch.Accept(context, this)); list.Add(Label, new int[] { labelEnd }); return(list); } }
public Tree VisitIf(object c, AstStatement.IfStmt ifStmt) => new Tree("if", Expr(ifStmt.Condition), ifStmt.TrueBranch.Accept(c, this), ifStmt.FalseBranch?.Accept(c, this));