public void TestWhile() { var while_ = new While(); while_.Init(new Rel(new Token('>'), new Constant(42), new Constant(99)), new Stmt()); while_.Gen(10, 88); //output: // iffalse 42 > 99 goto L88 //L1: goto L 10 }
/// <summary> /// /// </summary> /// <returns></returns> public Stmt Stmt() { Expr expr; Stmt s1, s2, savedStmt; switch (_look.TagValue) { case ';': this.Move(); return(Sara.Stmt.Null); case Tag.IF: this.Match(Tag.IF); this.Match('('); expr = this.Bool(); this.Match(')'); s1 = this.Stmt(); if (_look.TagValue != Tag.ELSE) { return(new If(expr, s1)); } this.Match(Tag.ELSE); s2 = this.Stmt(); return(new IfElse(expr, s1, s2)); case Tag.WHILE: var whileNode = new While(); savedStmt = Sara.Stmt.Enclosing; Sara.Stmt.Enclosing = whileNode; this.Match(Tag.WHILE); this.Match('('); expr = this.Bool(); this.Match(')'); s1 = this.Stmt(); whileNode.Init(expr, s1); Sara.Stmt.Enclosing = savedStmt; return(whileNode); case Tag.DO: var doNode = new Do(); savedStmt = Sara.Stmt.Enclosing; Sara.Stmt.Enclosing = doNode; this.Match(Tag.DO); s1 = this.Stmt(); this.Match(Tag.WHILE); this.Match('('); expr = this.Bool(); this.Match(')'); this.Match(';'); doNode.Init(s1, expr); Sara.Stmt.Enclosing = savedStmt; return(doNode); case Tag.BREAK: this.Match(Tag.BREAK); this.Match(';'); return(new Break()); case '{': return(this.Block()); default: return(this.Assign()); } }
/// <summary> /// /// </summary> /// <returns></returns> public Stmt Stmt() { Expr expr; Stmt s1, s2, savedStmt; switch(_look.TagValue) { case ';': this.Move(); return Sara.Stmt.Null; case Tag.IF: this.Match(Tag.IF); this.Match('('); expr = this.Bool(); this.Match(')'); s1 = this.Stmt(); if (_look.TagValue != Tag.ELSE) return new If(expr, s1); this.Match(Tag.ELSE); s2 = this.Stmt(); return new IfElse(expr, s1, s2); case Tag.WHILE: var whileNode = new While(); savedStmt = Sara.Stmt.Enclosing; Sara.Stmt.Enclosing = whileNode; this.Match(Tag.WHILE); this.Match('('); expr = this.Bool(); this.Match(')'); s1 = this.Stmt(); whileNode.Init(expr, s1); Sara.Stmt.Enclosing = savedStmt; return whileNode; case Tag.DO: var doNode = new Do(); savedStmt = Sara.Stmt.Enclosing; Sara.Stmt.Enclosing = doNode; this.Match(Tag.DO); s1 = this.Stmt(); this.Match(Tag.WHILE); this.Match('('); expr = this.Bool(); this.Match(')'); this.Match(';'); doNode.Init(s1, expr); Sara.Stmt.Enclosing = savedStmt; return doNode; case Tag.BREAK: this.Match(Tag.BREAK); this.Match(';'); return new Break(); case '{': return this.Block(); default: return this.Assign(); } }