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 VarDeclarationTests() { VariableEnvironment variables = new VariableEnvironment(); FunctionEnvironment functionEnvironment = new FunctionEnvironment(); string txt = "int var"; AntlrInputStream inputStream = new AntlrInputStream(txt); BogieLangLexer lexer = new BogieLangLexer(inputStream); CommonTokenStream commonTokenStream = new CommonTokenStream(lexer); BogieLangParser parser = new BogieLangParser(commonTokenStream); BogieLangParser.VarDeclarationContext varDeclarationContext = parser.varDeclaration(); BogieLangBaseVisitor <object> visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(varDeclarationContext); VarDeclaration varDeclaration = VarDeclaration.Compile(varDeclarationContext); varDeclaration.Execute(functionEnvironment, variables); Assert.True(variables["var"].BogieLangType == BogieLangType.INTEGER); Assert.True(variables["var"].Value == null); Assert.True(variables["var"].Identifer == "var"); variables.Clear(); txt = "int var=123"; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); varDeclarationContext = parser.varDeclaration(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(varDeclarationContext); varDeclaration = VarDeclaration.Compile(varDeclarationContext); varDeclaration.Execute(functionEnvironment, variables); Assert.True(variables["var"].BogieLangType == BogieLangType.INTEGER); Assert.True((int)variables["var"].Value == 123); Assert.True(variables["var"].Identifer == "var"); }
public void FunctionDefinitionTests() { VariableEnvironment variableEnvironment = new VariableEnvironment(); FunctionEnvironment functionEnvironment = new FunctionEnvironment(); string txt = "void funcName(){}"; AntlrInputStream inputStream = new AntlrInputStream(txt); BogieLangLexer lexer = new BogieLangLexer(inputStream); CommonTokenStream commonTokenStream = new CommonTokenStream(lexer); BogieLangParser parser = new BogieLangParser(commonTokenStream); BogieLangParser.FunctionDefinitionContext functionDefinitionContext = parser.functionDefinition(); BogieLangBaseVisitor <object> visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(functionDefinitionContext); FunctionDefinition functionDefinition = FunctionDefinition.Compile(functionDefinitionContext); functionEnvironment.FunctionDefinitions.Add("funcName", functionDefinition); functionDefinition.Execute(functionEnvironment, variableEnvironment); functionEnvironment.Clear(); variableEnvironment.DeclaredVariables = new Dictionary <string, BogieLangTypeInstance>() { { "abc", new BogieLangTypeInstance() { BogieLangType = BogieLangType.INTEGER, Identifer = "abc", Value = 1 } }, { "str", new BogieLangTypeInstance() { BogieLangType = BogieLangType.STRING, Identifer = "str", Value = "string" } }, { "lol", new BogieLangTypeInstance() { BogieLangType = BogieLangType.VOID, Identifer = "lol", Value = null } } }; txt = "void funcName(int abc,string str,void lol){}"; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); functionDefinitionContext = parser.functionDefinition(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(functionDefinitionContext); functionDefinition = FunctionDefinition.Compile(functionDefinitionContext); functionEnvironment.FunctionDefinitions.Add("funcName", functionDefinition); functionDefinition.Execute(functionEnvironment, variableEnvironment); txt = "int funcName(int abc,string str,bool b){int intvar" + "\n intvar=123" + "\nintvar=1" + "\nreturn funcCall(b)" + "\n}"; string txt2 = "\nint funcCall(bool b)" + "\n{" + "\nif(b)" + "\n{" + "\n return 100" + "\n}" + "\nreturn 0" + "}"; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); functionDefinitionContext = parser.functionDefinition(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(functionDefinitionContext); FunctionDefinition functionDefinition1 = FunctionDefinition.Compile(functionDefinitionContext); inputStream = new AntlrInputStream(txt2); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); functionDefinitionContext = parser.functionDefinition(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(functionDefinitionContext); FunctionDefinition functionDefinition2 = FunctionDefinition.Compile(functionDefinitionContext); variableEnvironment.Clear(); functionEnvironment.Clear(); variableEnvironment.DeclaredVariables = new Dictionary <string, BogieLangTypeInstance>() { { "abc", new BogieLangTypeInstance() { BogieLangType = BogieLangType.INTEGER, Identifer = "abc", Value = 1 } }, { "str", new BogieLangTypeInstance() { BogieLangType = BogieLangType.STRING, Identifer = "str", Value = "string" } }, { "b", new BogieLangTypeInstance() { BogieLangType = BogieLangType.BOOL, Identifer = "b", Value = true } } }; functionEnvironment.DefineFunction("funcName", functionDefinition1); functionEnvironment.DefineFunction("funcCall", functionDefinition2); Assert.True((int)functionDefinition1.Execute(functionEnvironment, variableEnvironment) == 100); }
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); }