public static Program Compile(BogieLangParser.ProgramContext programContext) { Program result = new Program(); foreach (BogieLangParser.FunctionDefinitionContext functionDefinitionContext in programContext.functionDefinition()) { FunctionDefinition functionDefinition = FunctionDefinition.Compile(functionDefinitionContext); result.FunctionEnvironment.DefineFunction(functionDefinition.Identifier, functionDefinition); } return(result); }
public void ProgramTests() { string txt = "void funcName(){}"; AntlrInputStream inputStream = new AntlrInputStream(txt); BogieLangLexer lexer = new BogieLangLexer(inputStream); lexer.AddErrorListener(new ParserErrorHandler <int>()); CommonTokenStream commonTokenStream = new CommonTokenStream(lexer); BogieLangParser parser = new BogieLangParser(commonTokenStream); parser.AddErrorListener(new ParserErrorHandler <object>()); BogieLangParser.ProgramContext ProgramContext = parser.program(); BogieLangBaseVisitor <object> visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(ProgramContext); Assert.True(parser.NumberOfSyntaxErrors == 0); txt = "void funcName(int abc,string str,void lol){}"; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); lexer.AddErrorListener(new ParserErrorHandler <int>()); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); parser.AddErrorListener(new ParserErrorHandler <object>()); ProgramContext = parser.program(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(ProgramContext); Assert.True(parser.NumberOfSyntaxErrors == 0); txt = "void funcName(){}" + "void funcName(int abc,string str,void lol){}" + "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); lexer.AddErrorListener(new ParserErrorHandler <int>()); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); parser.AddErrorListener(new ParserErrorHandler <object>()); ProgramContext = parser.program(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(ProgramContext); Assert.True(parser.NumberOfSyntaxErrors == 0); }
public void ProgramTests() { string txt = @" bool Main() { return func(true) } bool func(bool b) { if(b){return false} return true }"; AntlrInputStream inputStream = new AntlrInputStream(txt); BogieLangLexer lexer = new BogieLangLexer(inputStream); CommonTokenStream commonTokenStream = new CommonTokenStream(lexer); BogieLangParser parser = new BogieLangParser(commonTokenStream); BogieLangParser.ProgramContext programContext = parser.program(); BogieLangBaseVisitor <object> visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(programContext); Program program = Program.Compile(programContext); Assert.True((bool)program.Execute() == false); txt = @" int Main() { return fibonacci(8) } int fibonacci(int n) { if(n==0){return 0} if(n==1){return 1} return fibonacci(n-2)+fibonacci(n-1) }"; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); programContext = parser.program(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(programContext); program = Program.Compile(programContext); Assert.True((int)program.Execute() == 21); }
public void ProgramTests() { string txt = "void Main(){}"; AntlrInputStream inputStream = new AntlrInputStream(txt); BogieLangLexer lexer = new BogieLangLexer(inputStream); CommonTokenStream commonTokenStream = new CommonTokenStream(lexer); BogieLangParser parser = new BogieLangParser(commonTokenStream); BogieLangParser.ProgramContext programContext = parser.program(); BogieLangBaseVisitor <object> visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(programContext); Program program = Program.Compile(programContext); Assert.True(program.FunctionEnvironment.IsFunctionDefined("Main")); Assert.True(program.FunctionEnvironment["Main"].ReturnBogieLangType == BogieLangType.VOID); Assert.True(program.FunctionEnvironment["Main"].Parameters.Count == 0); txt = "void Main(int abc,string str,void lol){}"; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); programContext = parser.program(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(programContext); program = Program.Compile(programContext); Assert.True(program.FunctionEnvironment.IsFunctionDefined("Main")); Assert.True(program.FunctionEnvironment["Main"].ReturnBogieLangType == BogieLangType.VOID); Assert.True(program.FunctionEnvironment["Main"].Parameters[0].Item1 == BogieLangType.INTEGER); Assert.True(program.FunctionEnvironment["Main"].Parameters[0].Item2 == "abc"); Assert.True(program.FunctionEnvironment["Main"].Parameters[1].Item1 == BogieLangType.STRING); Assert.True(program.FunctionEnvironment["Main"].Parameters[1].Item2 == "str"); Assert.True(program.FunctionEnvironment["Main"].Parameters[2].Item1 == BogieLangType.VOID); Assert.True(program.FunctionEnvironment["Main"].Parameters[2].Item2 == "lol"); Assert.True(program.FunctionEnvironment["Main"].Body.Count == 0); txt = "void Main(){}" + "void funcName1(int abc,string str,void lol){}" + "void funcName2(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); programContext = parser.program(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(programContext); program = Program.Compile(programContext); Assert.True(program.FunctionEnvironment.IsFunctionDefined("Main")); Assert.True(program.FunctionEnvironment["Main"].ReturnBogieLangType == BogieLangType.VOID); Assert.True(program.FunctionEnvironment["Main"].Parameters.Count == 0); Assert.True(program.FunctionEnvironment.IsFunctionDefined("funcName1")); Assert.True(program.FunctionEnvironment["funcName1"].ReturnBogieLangType == BogieLangType.VOID); Assert.True(program.FunctionEnvironment["funcName1"].Parameters[0].Item1 == BogieLangType.INTEGER); Assert.True(program.FunctionEnvironment["funcName1"].Parameters[0].Item2 == "abc"); Assert.True(program.FunctionEnvironment["funcName1"].Parameters[1].Item1 == BogieLangType.STRING); Assert.True(program.FunctionEnvironment["funcName1"].Parameters[1].Item2 == "str"); Assert.True(program.FunctionEnvironment["funcName1"].Parameters[2].Item1 == BogieLangType.VOID); Assert.True(program.FunctionEnvironment["funcName1"].Parameters[2].Item2 == "lol"); Assert.True(program.FunctionEnvironment["funcName1"].Body.Count == 0); Assert.True(program.FunctionEnvironment.IsFunctionDefined("funcName2")); Assert.True(program.FunctionEnvironment["funcName2"].ReturnBogieLangType == BogieLangType.VOID); Assert.True(program.FunctionEnvironment["funcName2"].Parameters[0].Item1 == BogieLangType.INTEGER); Assert.True(program.FunctionEnvironment["funcName2"].Parameters[0].Item2 == "abc"); Assert.True(program.FunctionEnvironment["funcName2"].Parameters[1].Item1 == BogieLangType.STRING); Assert.True(program.FunctionEnvironment["funcName2"].Parameters[1].Item2 == "str"); Assert.True(program.FunctionEnvironment["funcName2"].Parameters[2].Item1 == BogieLangType.VOID); Assert.True(program.FunctionEnvironment["funcName2"].Parameters[2].Item2 == "lol"); Assert.True(program.FunctionEnvironment["funcName2"].Body[0].VarDeclaration.BogieLangType == BogieLangType.INTEGER); Assert.True(program.FunctionEnvironment["funcName2"].Body[0].VarDeclaration.Identifier == "intvar"); Assert.True(program.FunctionEnvironment["funcName2"].Body[1].VarDeclaration.BogieLangType == BogieLangType.INTEGER); Assert.True(program.FunctionEnvironment["funcName2"].Body[1].VarDeclaration.Identifier == "intvar"); Assert.True(program.FunctionEnvironment["funcName2"].Body[1].VarDeclaration.Expression.Literal.Integer == 123); Assert.True(program.FunctionEnvironment["funcName2"].Body[2].VarDefinition.Identifier == "intvar"); Assert.True(program.FunctionEnvironment["funcName2"].Body[2].VarDefinition.Expression.Literal.Real == 0.1); Assert.True(program.FunctionEnvironment["funcName2"].Body[3].FunctionCall.Identifier == "funcCall"); Assert.True(program.FunctionEnvironment["funcName2"].Body[3].FunctionCall.Arguments[0].Identifier == "lol"); Assert.True(program.FunctionEnvironment["funcName2"].Body[4].FunctionReturn.Expression.FunctionCall.Identifier == "funcCall"); }