public List <Instruction> VisitWhile(object context, AstStatement.WhileStmt whileStmt)
        {
            /*
             * while ( condition() ) { statement(); }
             *
             * #labelStart
             * condition()
             * not[]
             * jumpIf #labelEnd
             * statement()
             * jump #labelStart
             * #labelEnd
             */
            var labelStart = this.GetNextLabel(whileStmt);
            var labelEnd   = this.GetNextLabel(whileStmt);

            var list = new List <Instruction>();

            list.Add(Label, new int[] { labelStart });
            list.AddRange(whileStmt.Condition.Accept(whileStmt.Scope, this));
            list.Add(Not);
            list.Add(JumpIf, new int[] { labelEnd });
            list.AddRange(whileStmt.Body.Accept(context, this));
            list.Add(Jump, new int[] { labelStart });
            list.Add(Label, new int[] { labelEnd });
            return(list);
        }
 public void VisitWhile(IndentWriter context, AstStatement.WhileStmt whileStmt)
 {
     context.WriteLine("while(");
     context.Indent();
     this.PrintExpression(context, whileStmt.Condition, whileStmt.Scope);
     context.Unindent();
     context.WriteLine(") {");
     context.Indent();
     whileStmt.Body.Accept(context, this);
     context.Unindent();
     context.WriteLine("}");
 }
 public AstStatement VisitWhile(Scope context, AstStatement.WhileStmt whileStmt)
 {
     whileStmt.Condition = whileStmt.Condition.Accept(context, this);
     whileStmt.Body      = whileStmt.Body.Accept(context, this);
     return(whileStmt);
 }
Example #4
0
 public Tree VisitWhile(object c, AstStatement.WhileStmt whileStmt)
 => new Tree("while", Expr(whileStmt.Condition), whileStmt.Body.Accept(c, this));