public void FunctionReturnTests() { VariableEnvironment variables = new VariableEnvironment(); FunctionEnvironment functionEnvironment = new FunctionEnvironment(); variables.DeclareVariable("abc", BogieLangType.BOOL); variables.DefineVariable("abc", true); string txt = "return abc"; AntlrInputStream inputStream = new AntlrInputStream(txt); BogieLangLexer lexer = new BogieLangLexer(inputStream); CommonTokenStream commonTokenStream = new CommonTokenStream(lexer); BogieLangParser parser = new BogieLangParser(commonTokenStream); BogieLangParser.FunctionReturnContext functionReturnContext = parser.functionReturn(); BogieLangBaseVisitor <object> visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(functionReturnContext); FunctionReturn functionReturn = FunctionReturn.Compile(functionReturnContext); Assert.True((bool)functionReturn.Execute(functionEnvironment, variables) == true); variables.Clear(); txt = "return 10.0"; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); functionReturnContext = parser.functionReturn(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(functionReturnContext); functionReturn = FunctionReturn.Compile(functionReturnContext); Assert.True((double)functionReturn.Execute(functionEnvironment, variables) == 10.0); }
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 VarDefinitionTests() { VariableEnvironment variables = new VariableEnvironment(); FunctionEnvironment functionEnvironment = new FunctionEnvironment(); variables.DeclareVariable("var", BogieLangType.INTEGER); string txt = "var=123"; AntlrInputStream inputStream = new AntlrInputStream(txt); BogieLangLexer lexer = new BogieLangLexer(inputStream); CommonTokenStream commonTokenStream = new CommonTokenStream(lexer); BogieLangParser parser = new BogieLangParser(commonTokenStream); BogieLangParser.VarDefinitionContext varDefinitionContext = parser.varDefinition(); BogieLangBaseVisitor <object> visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(varDefinitionContext); VarDefinition varDefinition = VarDefinition.Compile(varDefinitionContext); varDefinition.Execute(functionEnvironment, variables); Assert.True(variables["var"].BogieLangType == BogieLangType.INTEGER); Assert.True(variables["var"].Value is int); Assert.True((int)variables["var"].Value == 123); }
public void ExpressionTests() { VariableEnvironment variableEnvironment = new VariableEnvironment(); FunctionEnvironment functionEnvironment = new FunctionEnvironment(); string txt = "1"; AntlrInputStream inputStream = new AntlrInputStream(txt); BogieLangLexer lexer = new BogieLangLexer(inputStream); CommonTokenStream commonTokenStream = new CommonTokenStream(lexer); BogieLangParser parser = new BogieLangParser(commonTokenStream); BogieLangParser.ExpressionContext expressionContext = parser.expression(); BogieLangBaseVisitor <object> visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(expressionContext); Expression expression = Expression.Compile(expressionContext, null); Assert.True(expression.Execute(functionEnvironment, new VariableEnvironment()) is int); Assert.True(expression.ParentExpression == null); Assert.True((int)expression.Execute(functionEnvironment, new VariableEnvironment()) == 1); txt = "1.0"; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); expressionContext = parser.expression(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(expressionContext); expression = Expression.Compile(expressionContext, null); Assert.True(expression.Execute(functionEnvironment, new VariableEnvironment()) is double); Assert.True(expression.ParentExpression == null); Assert.True((double)expression.Execute(functionEnvironment, new VariableEnvironment()) == 1.0); txt = "false"; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); expressionContext = parser.expression(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(expressionContext); expression = Expression.Compile(expressionContext, null); Assert.True(expression.Execute(functionEnvironment, new VariableEnvironment()) is bool); Assert.True(expression.ParentExpression == null); Assert.True((bool)expression.Execute(functionEnvironment, new VariableEnvironment()) == false); txt = "\"asd899asd\""; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); expressionContext = parser.expression(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(expressionContext); expression = Expression.Compile(expressionContext, null); Assert.True(expression.Execute(functionEnvironment, new VariableEnvironment()) is string); Assert.True((string)expression.Execute(functionEnvironment, new VariableEnvironment()) == "asd899asd"); txt = "VarName"; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); expressionContext = parser.expression(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(expressionContext); expression = Expression.Compile(expressionContext, null); variableEnvironment.DeclareVariable(txt, BogieLangType.INTEGER); variableEnvironment.DefineVariable(txt, 1); Assert.True(expression.Execute(functionEnvironment, variableEnvironment) is int); Assert.True(expression.ParentExpression == null); Assert.True((int)expression.Execute(functionEnvironment, variableEnvironment) == 1); }
public void BodyTests() { VariableEnvironment variables = new VariableEnvironment(); FunctionEnvironment functionEnvironment = new FunctionEnvironment(); string txt = "int b=true"; AntlrInputStream inputStream = new AntlrInputStream(txt); BogieLangLexer lexer = new BogieLangLexer(inputStream); CommonTokenStream commonTokenStream = new CommonTokenStream(lexer); BogieLangParser parser = new BogieLangParser(commonTokenStream); BogieLangParser.BodyContext bodyContext = parser.body(); BogieLangBaseVisitor <object> visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(bodyContext); Body body = Body.Compile(bodyContext); try { body.Execute(functionEnvironment, variables); Assert.True(false); } catch (Exception e) { Assert.True(true);//todo: test for a specific exception types once they are created } variables.Clear(); txt = "int b=098"; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); bodyContext = parser.body(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(bodyContext); body = Body.Compile(bodyContext); body.Execute(functionEnvironment, variables); Assert.True(variables["b"].BogieLangType == BogieLangType.INTEGER); Assert.True(variables["b"].Identifer == "b"); Assert.True((int)variables["b"].Value == 98); txt = "abc=123"; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); bodyContext = parser.body(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(bodyContext); body = Body.Compile(bodyContext); try { Assert.True(body.Execute(functionEnvironment, variables) == null); Assert.True(false); } catch { Assert.True(true);//todo: test for specific exception types once they are created } variables.DeclareVariable("abc", BogieLangType.INTEGER); txt = "abc=123"; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); bodyContext = parser.body(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(bodyContext); body = Body.Compile(bodyContext); body.Execute(functionEnvironment, variables); Assert.True(variables["abc"].BogieLangType == BogieLangType.INTEGER); Assert.True(variables["abc"].Identifer == "abc"); Assert.True((int)variables["abc"].Value == 123); txt = "return 0"; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); bodyContext = parser.body(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(bodyContext); body = Body.Compile(bodyContext); Assert.True(body.Execute(functionEnvironment, variables) is int); Assert.True((int)body.Execute(functionEnvironment, variables) == 0); txt = "if(1){int b}"; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); bodyContext = parser.body(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(bodyContext); body = Body.Compile(bodyContext); body.Execute(functionEnvironment, variables); txt = "if(true){int b}"; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); bodyContext = parser.body(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(bodyContext); body = Body.Compile(bodyContext); body.Execute(functionEnvironment, variables); }