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 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"); }