public void VarDeclarationTests() { string txt = "int var"; 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.VarDeclarationContext VarDeclarationContext = parser.varDeclaration(); BogieLangBaseVisitor <object> visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(VarDeclarationContext); Assert.True(parser.NumberOfSyntaxErrors == 0); txt = "int var=123"; 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>()); VarDeclarationContext = parser.varDeclaration(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(VarDeclarationContext); Assert.True(parser.NumberOfSyntaxErrors == 0); txt = "int var=funcCall()"; 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>()); VarDeclarationContext = parser.varDeclaration(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(VarDeclarationContext); Assert.True(parser.NumberOfSyntaxErrors == 0); txt = " \t \nint \t \nvar \t \n= \t \nfuncCall() \t \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>()); VarDeclarationContext = parser.varDeclaration(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(VarDeclarationContext); Assert.True(parser.NumberOfSyntaxErrors == 0); }
public void VarDeclarationTests() { 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); Assert.True(varDeclaration.BogieLangType == BogieLangType.INTEGER); Assert.True(varDeclaration.Identifier == "var"); 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); Assert.True(varDeclaration.BogieLangType == BogieLangType.INTEGER); Assert.True(varDeclaration.Identifier == "var"); Assert.True(varDeclaration.Expression.Literal.Integer == 123); txt = "int var=funcCall()"; 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); Assert.True(varDeclaration.BogieLangType == BogieLangType.INTEGER); Assert.True(varDeclaration.Identifier == "var"); Assert.True(varDeclaration.Expression.FunctionCall.Identifier == "funcCall"); }
public static VarDeclaration Compile(BogieLangParser.VarDeclarationContext varDeclarationContext) { VarDeclaration result = new VarDeclaration(); if (varDeclarationContext.TYPE() != null) { result.BogieLangType = Runtime.BogieLangTypeHelpr.StringToType(varDeclarationContext.TYPE().GetText()); } if (varDeclarationContext.IDENTIFIER() != null) { result.Identifier = varDeclarationContext.IDENTIFIER().GetText(); } if (varDeclarationContext.expression() != null) { result.Expression = Expression.Compile(varDeclarationContext.expression(), null); } return(result); }
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"); }