Esempio n. 1
0
        public void VarDefinitionTests()
        {
            string            txt               = "var=123";
            AntlrInputStream  inputStream       = new AntlrInputStream(txt);
            BogieLangLexer    lexer             = new BogieLangLexer(inputStream);
            CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
            BogieLangParser   parser            = new BogieLangParser(commonTokenStream);

            BogieLangParser.VarDefinitionContext varDefinitionContext = parser.varDefinition();
            BogieLangBaseVisitor <object>        visitor = new BogieLangBaseVisitor <object>();

            visitor.Visit(varDefinitionContext);
            VarDefinition varDefinition = VarDefinition.Compile(varDefinitionContext);

            Assert.True(varDefinition.Identifier == "var");
            Assert.True(varDefinition.Expression.Literal.Integer == 123);


            txt                  = "var=funcCall(\"arg\")";
            inputStream          = new AntlrInputStream(txt);
            lexer                = new BogieLangLexer(inputStream);
            commonTokenStream    = new CommonTokenStream(lexer);
            parser               = new BogieLangParser(commonTokenStream);
            varDefinitionContext = parser.varDefinition();
            visitor              = new BogieLangBaseVisitor <object>();
            visitor.Visit(varDefinitionContext);
            varDefinition = VarDefinition.Compile(varDefinitionContext);
            Assert.True(varDefinition.Identifier == "var");
            Assert.True(varDefinition.Expression.FunctionCall.Identifier == "funcCall");
        }
Esempio n. 2
0
        public void VarDefinitionTests()
        {
            string           txt         = "var=123";
            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.VarDefinitionContext VarDefinitionContext = parser.varDefinition();
            BogieLangBaseVisitor <object>        visitor = new BogieLangBaseVisitor <object>();

            visitor.Visit(VarDefinitionContext);
            Assert.True(parser.NumberOfSyntaxErrors == 0);


            txt         = "var=funcCall(\"arg\")";
            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>());
            VarDefinitionContext = parser.varDefinition();
            visitor = new BogieLangBaseVisitor <object>();
            visitor.Visit(VarDefinitionContext);
            Assert.True(parser.NumberOfSyntaxErrors == 0);


            txt         = " \t     \nvar \t     \n= \t     \nfuncCall(123) \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>());
            VarDefinitionContext = parser.varDefinition();
            visitor = new BogieLangBaseVisitor <object>();
            visitor.Visit(VarDefinitionContext);
            Assert.True(parser.NumberOfSyntaxErrors == 0);
        }
Esempio n. 3
0
        public void VarDefinitionTests()
        {
            VariableEnvironment variables           = new VariableEnvironment();
            FunctionEnvironment functionEnvironment = new FunctionEnvironment();

            variables.DeclareVariable("var", BogieLangType.INTEGER);
            string            txt               = "var=123";
            AntlrInputStream  inputStream       = new AntlrInputStream(txt);
            BogieLangLexer    lexer             = new BogieLangLexer(inputStream);
            CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
            BogieLangParser   parser            = new BogieLangParser(commonTokenStream);

            BogieLangParser.VarDefinitionContext varDefinitionContext = parser.varDefinition();
            BogieLangBaseVisitor <object>        visitor = new BogieLangBaseVisitor <object>();

            visitor.Visit(varDefinitionContext);
            VarDefinition varDefinition = VarDefinition.Compile(varDefinitionContext);

            varDefinition.Execute(functionEnvironment, variables);
            Assert.True(variables["var"].BogieLangType == BogieLangType.INTEGER);
            Assert.True(variables["var"].Value is int);
            Assert.True((int)variables["var"].Value == 123);
        }