public void WhileTests() { VariableEnvironment variables = new VariableEnvironment(); FunctionEnvironment functionEnvironment = new FunctionEnvironment(); variables.DeclareVariable("loop", BogieLangType.BOOL); variables.DefineVariable("loop", true); string txt = "while(loop){loop = false}"; AntlrInputStream inputStream = new AntlrInputStream(txt); BogieLangLexer lexer = new BogieLangLexer(inputStream); CommonTokenStream commonTokenStream = new CommonTokenStream(lexer); BogieLangParser parser = new BogieLangParser(commonTokenStream); BogieLangParser.WhileControlContext whileControlContext = parser.whileControl(); BogieLangBaseVisitor <object> visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(whileControlContext); WhileControl whileControl = WhileControl.Compile(whileControlContext); Assert.True(whileControl.Execute(functionEnvironment, variables) == null); Assert.True((bool)variables["loop"].Value == false); txt = " while(loop){loop = false int b=0}"; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); whileControlContext = parser.whileControl(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(whileControlContext); whileControl = WhileControl.Compile(whileControlContext); Assert.True(whileControl.Execute(functionEnvironment, variables) == null); Assert.True((bool)variables["loop"].Value == false); Assert.True(!variables.IsVariableDeclared("b")); }
public void WhileTests() { string txt = "while(true){}"; AntlrInputStream inputStream = new AntlrInputStream(txt); BogieLangLexer lexer = new BogieLangLexer(inputStream); CommonTokenStream commonTokenStream = new CommonTokenStream(lexer); BogieLangParser parser = new BogieLangParser(commonTokenStream); BogieLangParser.WhileControlContext whileControlContext = parser.whileControl(); BogieLangBaseVisitor <object> visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(whileControlContext); WhileControl whileControl = WhileControl.Compile(whileControlContext); Assert.True(whileControl.Expression.Literal.Bool == true); Assert.True(whileControl.Body.Count == 0); txt = "while(true){int b=0}"; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); whileControlContext = parser.whileControl(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(whileControlContext); whileControl = WhileControl.Compile(whileControlContext); Assert.True(whileControl.Expression.Literal.Bool == true); Assert.True(whileControl.Body[0].VarDeclaration.BogieLangType == BogieLangType.INTEGER); Assert.True(whileControl.Body[0].VarDeclaration.Identifier == "b"); Assert.True(whileControl.Body[0].VarDeclaration.Expression.Literal.Integer == 0); txt = @"while(true) { int b=0 b = 1 funcCall(b) while(false){return 1} return 0 }"; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); whileControlContext = parser.whileControl(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(whileControlContext); whileControl = WhileControl.Compile(whileControlContext); Assert.True(whileControl.Expression.Literal.Bool == true); Assert.True(whileControl.Body[0].VarDeclaration.BogieLangType == BogieLangType.INTEGER); Assert.True(whileControl.Body[0].VarDeclaration.Identifier == "b"); Assert.True(whileControl.Body[0].VarDeclaration.Expression.Literal.Integer == 0); Assert.True(whileControl.Body[1].VarDefinition.Identifier == "b"); Assert.True(whileControl.Body[1].VarDefinition.Expression.Literal.Integer == 1); Assert.True(whileControl.Body[2].FunctionCall.Identifier == "funcCall"); Assert.True(whileControl.Body[2].FunctionCall.Arguments[0].Identifier == "b"); Assert.True(whileControl.Body[3].WhileControl.Expression.Literal.Bool == false); Assert.True(whileControl.Body[3].WhileControl.Body[0].FunctionReturn.Expression.Literal.Integer == 1); Assert.True(whileControl.Body[4].FunctionReturn.Expression.Literal.Integer == 0); }