private bool ParseWhileStatement(TokenReader reader, out Statement statement) { Token start = reader.Peek(); statement = null; if (!this.Expect(reader, Keyword.While)) { return false; } WhileStatement whileStatement = new WhileStatement(start); Expression condition = null; if (!this.ParseExpression(reader, out condition)) { return false; } whileStatement.Condition = condition; if (!this.Expect(reader, Keyword.Do)) { return false; } Statement bodyStatement = null; if (!this.ParseStatement(reader, out bodyStatement)) { return false; } whileStatement.BodyStatement = bodyStatement; statement = whileStatement; return true; }
private bool TryEmitWhileStatement(WhileStatement whileStatement, CompilerContext context, Scope scope, MethodImpl method) { string topLabel = method.Module.GetNextJumpLabel(); method.Statements.Add(new AsmStatement { Instruction = "nop", Label = topLabel }); TypeDefinition conditionType = null; if (!this.TryEmitExpression(whileStatement.Condition, context, scope, method, out conditionType)) { return false; } if (!this.ValidateConditionType(whileStatement.Condition, conditionType)) { return false; } string endLabel = method.Module.GetNextJumpLabel(); method.Statements.Add(new AsmStatement { Instruction = "test al,al" }); method.Statements.Add(new AsmStatement { Instruction = "jz " + endLabel }); if (!this.TryEmitStatement(whileStatement.BodyStatement, context, scope, method)) { return false; } method.Statements.Add(new AsmStatement { Instruction = "jmp " + topLabel }); method.Statements.Add(new AsmStatement { Instruction = "nop", Label = endLabel }); return true; }