public void EmptyForStatement() { var source = @"for(var i = 0; i < 10; i = i + 1) {}"; var code = CreateCode(source); var expected = @" DECL i i = 0 LABEL #0 IF i < 10 JUMP #1 JUMP #2 LABEL #1 LABEL #3 i = i + 1 JUMP #0 LABEL #2"; Assert.AreEqual(10, InstructionCounter.Count(code)); Assert.AreEqual(expected.Trim(), CodeStringifier.Generate(code)); }
public void ConditionalExpressionInsideIfStatement() { var source = @"if((a == b ? 1 : 0) > 0) {}"; var code = _CreateCode(source); var expected = @" DECL $temp_0 IF a == b JUMP #0 $temp_0 = 0 JUMP #1 LABEL #0 $temp_0 = 1 LABEL #1 IF $temp_0 > 0 JUMP #2 JUMP #3 LABEL #2 LABEL #3"; Assert.AreEqual(11, InstructionCounter.Count(code)); Assert.AreEqual(expected.Trim(), CodeStringifier.Generate(code)); }
public void ForStatementWithMultipleVariables() { var source = @"for(i = 5, j = 0; i < 100; i = i + 2, j++) {}"; var code = CreateCode(source); var expected = @" i = 5 j = 0 LABEL #0 IF i < 100 JUMP #1 JUMP #2 LABEL #1 LABEL #3 i = i + 2 j = j + 1 JUMP #0 LABEL #2"; Assert.AreEqual(11, InstructionCounter.Count(code)); Assert.AreEqual(expected.Trim(), CodeStringifier.Generate(code)); }
public void ForStatementWithoutSuccessor() { var source = @"for(i = 5; i < 100; i = i + 2) { var x = 1; }"; var code = CreateCode(source); var expected = @" i = 5 LABEL #0 IF i < 100 JUMP #1 JUMP #2 LABEL #1 DECL x x = 1 LABEL #3 i = i + 2 JUMP #0 LABEL #2"; Assert.AreEqual(11, InstructionCounter.Count(code)); Assert.AreEqual(expected.Trim(), CodeStringifier.Generate(code)); }
public void ComplexCondition() { var source = @"if(x/10 + x%5 - 10 > 0.0) { x++; }"; var code = _CreateCode(source); var expected = @" DECL $temp_0 $temp_0 = x / 10 DECL $temp_1 $temp_1 = x % 5 DECL $temp_2 $temp_2 = $temp_0 + $temp_1 DECL $temp_3 $temp_3 = $temp_2 - 10 IF $temp_3 > 0 JUMP #0 JUMP #1 LABEL #0 x = x + 1 LABEL #1"; Assert.AreEqual(13, InstructionCounter.Count(code)); Assert.AreEqual(expected.Trim(), CodeStringifier.Generate(code)); }
public void ForStatementWithSuccessor() { var source = @"for(int i = 0; i < 100; i++) { var x = 1; } y = 5;"; var code = CreateCode(source); var expected = @" DECL i i = 0 LABEL #0 IF i < 100 JUMP #1 JUMP #2 LABEL #1 DECL x x = 1 LABEL #3 i = i + 1 JUMP #0 LABEL #2 y = 5"; Assert.AreEqual(13, InstructionCounter.Count(code)); Assert.AreEqual(expected.Trim(), CodeStringifier.Generate(code)); }