public void FunctionDefinitionTests() { 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); Assert.True(functionDefinition.Identifier == "funcName"); Assert.True(functionDefinition.ReturnBogieLangType == BogieLangType.VOID); Assert.True(functionDefinition.Parameters.Count == 0); Assert.True(functionDefinition.Body.Count == 0); 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); Assert.True(functionDefinition.Identifier == "funcName"); Assert.True(functionDefinition.ReturnBogieLangType == BogieLangType.VOID); Assert.True(functionDefinition.Parameters[0].Item1 == BogieLangType.INTEGER); Assert.True(functionDefinition.Parameters[0].Item2 == "abc"); Assert.True(functionDefinition.Parameters[1].Item1 == BogieLangType.STRING); Assert.True(functionDefinition.Parameters[1].Item2 == "str"); Assert.True(functionDefinition.Parameters[2].Item1 == BogieLangType.VOID); Assert.True(functionDefinition.Parameters[2].Item2 == "lol"); Assert.True(functionDefinition.Body.Count == 0); txt = "void funcName(int abc,string str,void lol){int intvar" + "\nint intvar=123" + "\nintvar=0.1" + "\nfuncCall(lol)" + "\nreturn funcCall()" + "\n}"; 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); Assert.True(functionDefinition.Identifier == "funcName"); Assert.True(functionDefinition.ReturnBogieLangType == BogieLangType.VOID); Assert.True(functionDefinition.Parameters[0].Item1 == BogieLangType.INTEGER); Assert.True(functionDefinition.Parameters[0].Item2 == "abc"); Assert.True(functionDefinition.Parameters[1].Item1 == BogieLangType.STRING); Assert.True(functionDefinition.Parameters[1].Item2 == "str"); Assert.True(functionDefinition.Parameters[2].Item1 == BogieLangType.VOID); Assert.True(functionDefinition.Parameters[2].Item2 == "lol"); Assert.True(functionDefinition.Body[0].VarDeclaration.BogieLangType == BogieLangType.INTEGER); Assert.True(functionDefinition.Body[0].VarDeclaration.Identifier == "intvar"); Assert.True(functionDefinition.Body[1].VarDeclaration.BogieLangType == BogieLangType.INTEGER); Assert.True(functionDefinition.Body[1].VarDeclaration.Identifier == "intvar"); Assert.True(functionDefinition.Body[1].VarDeclaration.Expression.Literal.Integer == 123); Assert.True(functionDefinition.Body[2].VarDefinition.Identifier == "intvar"); Assert.True(functionDefinition.Body[2].VarDefinition.Expression.Literal.Real == 0.1); Assert.True(functionDefinition.Body[3].FunctionCall.Identifier == "funcCall"); Assert.True(functionDefinition.Body[3].FunctionCall.Arguments[0].Identifier == "lol"); Assert.True(functionDefinition.Body[4].FunctionReturn.Expression.FunctionCall.Identifier == "funcCall"); }
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); }