public void ExpressionTests() { string txt = "1"; AntlrInputStream inputStream = new AntlrInputStream(txt); BogieLangLexer lexer = new BogieLangLexer(inputStream); CommonTokenStream commonTokenStream = new CommonTokenStream(lexer); BogieLangParser parser = new BogieLangParser(commonTokenStream); BogieLangParser.ExpressionContext expressionContext = parser.expression(); BogieLangBaseVisitor <object> visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(expressionContext); Expression expression = Expression.Compile(expressionContext, null); Assert.True(expression.Identifier == null); Assert.True(expression.Literal.Integer == 1); Assert.True(expression.FunctionCall == null); Assert.True(expression.Operator == null); Assert.True(expression.SubExpression == null); Assert.True(expression.ParentExpression == null); txt = "1.0"; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); expressionContext = parser.expression(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(expressionContext); expression = Expression.Compile(expressionContext, null); Assert.True(expression.Identifier == null); Assert.True(expression.Literal.Real == 1.0); Assert.True(expression.FunctionCall == null); Assert.True(expression.Operator == null); Assert.True(expression.SubExpression == null); Assert.True(expression.ParentExpression == null); txt = "false"; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); expressionContext = parser.expression(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(expressionContext); expression = Expression.Compile(expressionContext, null); Assert.True(expression.Identifier == null); Assert.True(expression.Literal.Bool == false); Assert.True(expression.FunctionCall == null); Assert.True(expression.Operator == null); Assert.True(expression.SubExpression == null); Assert.True(expression.ParentExpression == null); txt = "\"asd899asd\""; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); expressionContext = parser.expression(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(expressionContext); expression = Expression.Compile(expressionContext, null); Assert.True(expression.Identifier == null); Assert.True(expression.Literal.String == "asd899asd"); Assert.True(expression.FunctionCall == null); Assert.True(expression.Operator == null); Assert.True(expression.SubExpression == null); Assert.True(expression.ParentExpression == null); txt = "VarName"; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); expressionContext = parser.expression(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(expressionContext); expression = Expression.Compile(expressionContext, null); Assert.True(expression.Identifier == "VarName"); Assert.True(expression.Literal == null); Assert.True(expression.FunctionCall == null); Assert.True(expression.Operator == null); Assert.True(expression.SubExpression == null); Assert.True(expression.ParentExpression == null); txt = "VarName + 1+ true*0"; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); expressionContext = parser.expression(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(expressionContext); expression = Expression.Compile(expressionContext, null); Assert.True(expression.Identifier == "VarName"); Assert.True(expression.Literal == null); Assert.True(expression.FunctionCall == null); Assert.True(expression.Operator == "+"); Assert.True(expression.SubExpression.Literal.Integer == 1); Assert.True(expression.SubExpression.Operator == "+"); Assert.True(expression.SubExpression.SubExpression.Literal.Bool == true); Assert.True(expression.SubExpression.SubExpression.Operator == "*"); Assert.True(expression.SubExpression.SubExpression.SubExpression.Literal.Integer == 0); Assert.True(expression.ParentExpression == null); Assert.True(expression.SubExpression.ParentExpression == expression); Assert.True(expression.SubExpression.SubExpression.ParentExpression == expression.SubExpression); Assert.True(expression.SubExpression.SubExpression.SubExpression.ParentExpression == expression.SubExpression.SubExpression); txt = "funcCall()"; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); expressionContext = parser.expression(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(expressionContext); expression = Expression.Compile(expressionContext, null); Assert.True(expression.Identifier == null); Assert.True(expression.Literal == null); Assert.True(expression.FunctionCall.Identifier == "funcCall"); Assert.True(expression.Operator == null); Assert.True(expression.SubExpression == null); Assert.True(expression.ParentExpression == null); }
public void ExpressionTests() { VariableEnvironment variableEnvironment = new VariableEnvironment(); FunctionEnvironment functionEnvironment = new FunctionEnvironment(); string txt = "1"; AntlrInputStream inputStream = new AntlrInputStream(txt); BogieLangLexer lexer = new BogieLangLexer(inputStream); CommonTokenStream commonTokenStream = new CommonTokenStream(lexer); BogieLangParser parser = new BogieLangParser(commonTokenStream); BogieLangParser.ExpressionContext expressionContext = parser.expression(); BogieLangBaseVisitor <object> visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(expressionContext); Expression expression = Expression.Compile(expressionContext, null); Assert.True(expression.Execute(functionEnvironment, new VariableEnvironment()) is int); Assert.True(expression.ParentExpression == null); Assert.True((int)expression.Execute(functionEnvironment, new VariableEnvironment()) == 1); txt = "1.0"; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); expressionContext = parser.expression(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(expressionContext); expression = Expression.Compile(expressionContext, null); Assert.True(expression.Execute(functionEnvironment, new VariableEnvironment()) is double); Assert.True(expression.ParentExpression == null); Assert.True((double)expression.Execute(functionEnvironment, new VariableEnvironment()) == 1.0); txt = "false"; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); expressionContext = parser.expression(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(expressionContext); expression = Expression.Compile(expressionContext, null); Assert.True(expression.Execute(functionEnvironment, new VariableEnvironment()) is bool); Assert.True(expression.ParentExpression == null); Assert.True((bool)expression.Execute(functionEnvironment, new VariableEnvironment()) == false); txt = "\"asd899asd\""; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); expressionContext = parser.expression(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(expressionContext); expression = Expression.Compile(expressionContext, null); Assert.True(expression.Execute(functionEnvironment, new VariableEnvironment()) is string); Assert.True((string)expression.Execute(functionEnvironment, new VariableEnvironment()) == "asd899asd"); txt = "VarName"; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); expressionContext = parser.expression(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(expressionContext); expression = Expression.Compile(expressionContext, null); variableEnvironment.DeclareVariable(txt, BogieLangType.INTEGER); variableEnvironment.DefineVariable(txt, 1); Assert.True(expression.Execute(functionEnvironment, variableEnvironment) is int); Assert.True(expression.ParentExpression == null); Assert.True((int)expression.Execute(functionEnvironment, variableEnvironment) == 1); }
public void AddTests() { VariableEnvironment variableEnvironment = new VariableEnvironment(); FunctionEnvironment functionEnvironment = new FunctionEnvironment(); string txt = "1+1"; AntlrInputStream inputStream = new AntlrInputStream(txt); BogieLangLexer lexer = new BogieLangLexer(inputStream); CommonTokenStream commonTokenStream = new CommonTokenStream(lexer); BogieLangParser parser = new BogieLangParser(commonTokenStream); BogieLangParser.ExpressionContext expressionContext = parser.expression(); BogieLangBaseVisitor <object> visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(expressionContext); Expression expression = Expression.Compile(expressionContext, null); Assert.True(expression.ParentExpression == null); Assert.True(expression.SubExpression.ParentExpression == expression); Assert.True((int)expression.Execute(functionEnvironment, variableEnvironment) == 2); txt = "1+1+1"; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); expressionContext = parser.expression(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(expressionContext); expression = Expression.Compile(expressionContext, null); Assert.True(expression.ParentExpression == null); Assert.True(expression.SubExpression.ParentExpression == expression); Assert.True(expression.SubExpression.SubExpression.ParentExpression == expression.SubExpression); Assert.True((int)expression.Execute(functionEnvironment, variableEnvironment) == 3); txt = "1+1.1"; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); expressionContext = parser.expression(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(expressionContext); expression = Expression.Compile(expressionContext, null); Assert.True(expression.ParentExpression == null); Assert.True(expression.SubExpression.ParentExpression == expression); Assert.True((double)expression.Execute(functionEnvironment, variableEnvironment) == 2.1); txt = "1+\"one\""; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); expressionContext = parser.expression(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(expressionContext); expression = Expression.Compile(expressionContext, null); Assert.True(expression.ParentExpression == null); Assert.True(expression.SubExpression.ParentExpression == expression); Assert.True((string)expression.Execute(functionEnvironment, variableEnvironment) == "1one"); txt = "1+true"; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); expressionContext = parser.expression(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(expressionContext); expression = Expression.Compile(expressionContext, null); Assert.True(expression.ParentExpression == null); Assert.True(expression.SubExpression.ParentExpression == expression); Assert.True((int)expression.Execute(functionEnvironment, variableEnvironment) == 2); txt = "1+1.1+true+\"str\""; inputStream = new AntlrInputStream(txt); lexer = new BogieLangLexer(inputStream); commonTokenStream = new CommonTokenStream(lexer); parser = new BogieLangParser(commonTokenStream); expressionContext = parser.expression(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(expressionContext); expression = Expression.Compile(expressionContext, null); Assert.True(expression.ParentExpression == null); Assert.True(expression.SubExpression.ParentExpression == expression); Assert.True(expression.SubExpression.SubExpression.ParentExpression == expression.SubExpression); Assert.True(expression.SubExpression.SubExpression.SubExpression.ParentExpression == expression.SubExpression.SubExpression); Assert.True((string)expression.Execute(functionEnvironment, variableEnvironment) == "3.1str"); }
public void ExpressionTests() { string txt = "1"; 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.ExpressionContext expressionContext = parser.expression(); BogieLangBaseVisitor <object> visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(expressionContext); Assert.True(parser.NumberOfSyntaxErrors == 0); txt = "1.0"; 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>()); expressionContext = parser.expression(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(expressionContext); Assert.True(parser.NumberOfSyntaxErrors == 0); txt = "false"; 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>()); expressionContext = parser.expression(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(expressionContext); Assert.True(parser.NumberOfSyntaxErrors == 0); txt = "\"asd899asd\""; 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>()); expressionContext = parser.expression(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(expressionContext); Assert.True(parser.NumberOfSyntaxErrors == 0); txt = "VarName"; 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>()); expressionContext = parser.expression(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(expressionContext); Assert.True(parser.NumberOfSyntaxErrors == 0); txt = "VarName + 1+ true*0"; 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>()); expressionContext = parser.expression(); visitor = new BogieLangBaseVisitor <object>(); visitor.Visit(expressionContext); Assert.True(parser.NumberOfSyntaxErrors == 0); }