Exemplo n.º 1
0
        public void BodyTests()
        {
            VariableEnvironment variables           = new VariableEnvironment();
            FunctionEnvironment functionEnvironment = new FunctionEnvironment();

            string            txt               = "int b=true";
            AntlrInputStream  inputStream       = new AntlrInputStream(txt);
            BogieLangLexer    lexer             = new BogieLangLexer(inputStream);
            CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
            BogieLangParser   parser            = new BogieLangParser(commonTokenStream);

            BogieLangParser.BodyContext   bodyContext = parser.body();
            BogieLangBaseVisitor <object> visitor     = new BogieLangBaseVisitor <object>();

            visitor.Visit(bodyContext);
            Body body = Body.Compile(bodyContext);

            try
            {
                body.Execute(functionEnvironment, variables);
                Assert.True(false);
            }
            catch (Exception e)
            {
                Assert.True(true);//todo: test for a specific exception types once they are created
            }

            variables.Clear();
            txt               = "int b=098";
            inputStream       = new AntlrInputStream(txt);
            lexer             = new BogieLangLexer(inputStream);
            commonTokenStream = new CommonTokenStream(lexer);
            parser            = new BogieLangParser(commonTokenStream);
            bodyContext       = parser.body();
            visitor           = new BogieLangBaseVisitor <object>();
            visitor.Visit(bodyContext);
            body = Body.Compile(bodyContext);
            body.Execute(functionEnvironment, variables);
            Assert.True(variables["b"].BogieLangType == BogieLangType.INTEGER);
            Assert.True(variables["b"].Identifer == "b");
            Assert.True((int)variables["b"].Value == 98);

            txt               = "abc=123";
            inputStream       = new AntlrInputStream(txt);
            lexer             = new BogieLangLexer(inputStream);
            commonTokenStream = new CommonTokenStream(lexer);
            parser            = new BogieLangParser(commonTokenStream);
            bodyContext       = parser.body();
            visitor           = new BogieLangBaseVisitor <object>();
            visitor.Visit(bodyContext);
            body = Body.Compile(bodyContext);
            try
            {
                Assert.True(body.Execute(functionEnvironment, variables) == null);
                Assert.True(false);
            }
            catch
            {
                Assert.True(true);//todo: test for specific exception types once they are created
            }


            variables.DeclareVariable("abc", BogieLangType.INTEGER);
            txt               = "abc=123";
            inputStream       = new AntlrInputStream(txt);
            lexer             = new BogieLangLexer(inputStream);
            commonTokenStream = new CommonTokenStream(lexer);
            parser            = new BogieLangParser(commonTokenStream);
            bodyContext       = parser.body();
            visitor           = new BogieLangBaseVisitor <object>();
            visitor.Visit(bodyContext);
            body = Body.Compile(bodyContext);
            body.Execute(functionEnvironment, variables);
            Assert.True(variables["abc"].BogieLangType == BogieLangType.INTEGER);
            Assert.True(variables["abc"].Identifer == "abc");
            Assert.True((int)variables["abc"].Value == 123);


            txt               = "return 0";
            inputStream       = new AntlrInputStream(txt);
            lexer             = new BogieLangLexer(inputStream);
            commonTokenStream = new CommonTokenStream(lexer);
            parser            = new BogieLangParser(commonTokenStream);
            bodyContext       = parser.body();
            visitor           = new BogieLangBaseVisitor <object>();
            visitor.Visit(bodyContext);
            body = Body.Compile(bodyContext);
            Assert.True(body.Execute(functionEnvironment, variables) is int);
            Assert.True((int)body.Execute(functionEnvironment, variables) == 0);

            txt               = "if(1){int b}";
            inputStream       = new AntlrInputStream(txt);
            lexer             = new BogieLangLexer(inputStream);
            commonTokenStream = new CommonTokenStream(lexer);
            parser            = new BogieLangParser(commonTokenStream);
            bodyContext       = parser.body();
            visitor           = new BogieLangBaseVisitor <object>();
            visitor.Visit(bodyContext);
            body = Body.Compile(bodyContext);
            body.Execute(functionEnvironment, variables);

            txt               = "if(true){int b}";
            inputStream       = new AntlrInputStream(txt);
            lexer             = new BogieLangLexer(inputStream);
            commonTokenStream = new CommonTokenStream(lexer);
            parser            = new BogieLangParser(commonTokenStream);
            bodyContext       = parser.body();
            visitor           = new BogieLangBaseVisitor <object>();
            visitor.Visit(bodyContext);
            body = Body.Compile(bodyContext);
            body.Execute(functionEnvironment, variables);
        }
Exemplo n.º 2
0
        public void BodyTests()
        {
            string            txt               = "int b=true";
            AntlrInputStream  inputStream       = new AntlrInputStream(txt);
            BogieLangLexer    lexer             = new BogieLangLexer(inputStream);
            CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
            BogieLangParser   parser            = new BogieLangParser(commonTokenStream);

            BogieLangParser.BodyContext   bodyContext = parser.body();
            BogieLangBaseVisitor <object> visitor     = new BogieLangBaseVisitor <object>();

            visitor.Visit(bodyContext);
            Body body = Body.Compile(bodyContext);

            Assert.True(body.VarDeclaration.BogieLangType == BogieLangType.INTEGER);
            Assert.True(body.VarDeclaration.Identifier == "b");
            Assert.True(body.VarDeclaration.Expression.Literal.Bool == true);


            txt               = "abc=123";
            inputStream       = new AntlrInputStream(txt);
            lexer             = new BogieLangLexer(inputStream);
            commonTokenStream = new CommonTokenStream(lexer);
            parser            = new BogieLangParser(commonTokenStream);
            bodyContext       = parser.body();
            visitor           = new BogieLangBaseVisitor <object>();
            visitor.Visit(bodyContext);
            body = Body.Compile(bodyContext);
            Assert.True(body.VarDefinition.Identifier == "abc");
            Assert.True(body.VarDefinition.Expression.Literal.Integer == 123);


            txt               = "func()";
            inputStream       = new AntlrInputStream(txt);
            lexer             = new BogieLangLexer(inputStream);
            commonTokenStream = new CommonTokenStream(lexer);
            parser            = new BogieLangParser(commonTokenStream);
            bodyContext       = parser.body();
            visitor           = new BogieLangBaseVisitor <object>();
            visitor.Visit(bodyContext);
            body = Body.Compile(bodyContext);
            Assert.True(body.FunctionCall.Identifier == "func");


            txt               = "return 0";
            inputStream       = new AntlrInputStream(txt);
            lexer             = new BogieLangLexer(inputStream);
            commonTokenStream = new CommonTokenStream(lexer);
            parser            = new BogieLangParser(commonTokenStream);
            bodyContext       = parser.body();
            visitor           = new BogieLangBaseVisitor <object>();
            visitor.Visit(bodyContext);
            body = Body.Compile(bodyContext);
            Assert.True(body.FunctionReturn.Expression.Literal.Integer == 0);


            txt               = "if(1){int b}";
            inputStream       = new AntlrInputStream(txt);
            lexer             = new BogieLangLexer(inputStream);
            commonTokenStream = new CommonTokenStream(lexer);
            parser            = new BogieLangParser(commonTokenStream);
            bodyContext       = parser.body();
            visitor           = new BogieLangBaseVisitor <object>();
            visitor.Visit(bodyContext);
            body = Body.Compile(bodyContext);
            Assert.True(body.IfControl.Expression.Literal.Integer == 1);
            Assert.True(body.IfControl.Body[0].VarDeclaration.BogieLangType == BogieLangType.INTEGER);
            Assert.True(body.IfControl.Body[0].VarDeclaration.Identifier == "b");


            txt               = "while(1){int b}";
            inputStream       = new AntlrInputStream(txt);
            lexer             = new BogieLangLexer(inputStream);
            commonTokenStream = new CommonTokenStream(lexer);
            parser            = new BogieLangParser(commonTokenStream);
            bodyContext       = parser.body();
            visitor           = new BogieLangBaseVisitor <object>();
            visitor.Visit(bodyContext);
            body = Body.Compile(bodyContext);
            Assert.True(body.WhileControl.Expression.Literal.Integer == 1);
            Assert.True(body.WhileControl.Body[0].VarDeclaration.BogieLangType == BogieLangType.INTEGER);
            Assert.True(body.WhileControl.Body[0].VarDeclaration.Identifier == "b");
        }
Exemplo n.º 3
0
        public void BodyTests()
        {
            string           txt         = "int b=true";
            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.BodyContext   BodyContext = parser.body();
            BogieLangBaseVisitor <object> visitor     = new BogieLangBaseVisitor <object>();

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


            txt         = "abc=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>());
            BodyContext = parser.body();
            visitor     = new BogieLangBaseVisitor <object>();
            visitor.Visit(BodyContext);
            Assert.True(parser.NumberOfSyntaxErrors == 0);


            txt         = "func()";
            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>());
            BodyContext = parser.body();
            visitor     = new BogieLangBaseVisitor <object>();
            visitor.Visit(BodyContext);
            Assert.True(parser.NumberOfSyntaxErrors == 0);


            txt         = "return 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>());
            BodyContext = parser.body();
            visitor     = new BogieLangBaseVisitor <object>();
            visitor.Visit(BodyContext);
            Assert.True(parser.NumberOfSyntaxErrors == 0);


            txt         = "if(1){int b}";
            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>());
            BodyContext = parser.body();
            visitor     = new BogieLangBaseVisitor <object>();
            visitor.Visit(BodyContext);
            Assert.True(parser.NumberOfSyntaxErrors == 0);


            txt         = "while(1){int b}";
            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>());
            BodyContext = parser.body();
            visitor     = new BogieLangBaseVisitor <object>();
            visitor.Visit(BodyContext);
            Assert.True(parser.NumberOfSyntaxErrors == 0);
        }