From 815db2277366a21f7efe67cbb90a2c59993d9f7a Mon Sep 17 00:00:00 2001 From: b3b00 Date: Wed, 29 Dec 2021 15:35:40 +0100 Subject: [PATCH] fix while bytecode generation and unit test --- ParserTests/samples/IndentedWhileTests.cs | 26 +++++++++++++++++++++++ samples/while/compiler/SemanticChecker.cs | 4 ++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/ParserTests/samples/IndentedWhileTests.cs b/ParserTests/samples/IndentedWhileTests.cs index 1c00f6c7..3965589d 100644 --- a/ParserTests/samples/IndentedWhileTests.cs +++ b/ParserTests/samples/IndentedWhileTests.cs @@ -203,6 +203,32 @@ public void TestIfThenElse() Assert.Equal("world", (elseAssign.Value as StringConstant).Value); } + [Fact] + public void TestNestedIfThenElse() + { + var program = @" +# TestIfThenElse +a := -111 +if true then + if true then + a := 1 + else + a := 2 +else + a := 3 + b := ""world"" +return a +"; + var compiler = new IndentedWhileCompiler(); + var func = compiler.CompileToFunction(program,true); + Assert.NotNull(func); + var f = func(); + Assert.Equal(1, f); + + + } + + [Fact] public void TestInfiniteWhile() { diff --git a/samples/while/compiler/SemanticChecker.cs b/samples/while/compiler/SemanticChecker.cs index 39f7e725..09f9380f 100644 --- a/samples/while/compiler/SemanticChecker.cs +++ b/samples/while/compiler/SemanticChecker.cs @@ -80,11 +80,11 @@ private void SemanticCheck(IfStatement ast, CompilerContext context) ast.CompilerScope = context.CurrentScope; context.OpenNewScope(); - SemanticCheck(ast.ThenStmt); + SemanticCheck(ast.ThenStmt,context); context.CloseScope(); context.OpenNewScope(); - SemanticCheck(ast.ElseStmt); + SemanticCheck(ast.ElseStmt,context); context.CloseScope(); }