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

            variables.DeclareVariable("abc", BogieLangType.BOOL);
            variables.DefineVariable("abc", true);

            string            txt               = "return abc";
            AntlrInputStream  inputStream       = new AntlrInputStream(txt);
            BogieLangLexer    lexer             = new BogieLangLexer(inputStream);
            CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
            BogieLangParser   parser            = new BogieLangParser(commonTokenStream);

            BogieLangParser.FunctionReturnContext functionReturnContext = parser.functionReturn();
            BogieLangBaseVisitor <object>         visitor = new BogieLangBaseVisitor <object>();

            visitor.Visit(functionReturnContext);
            FunctionReturn functionReturn = FunctionReturn.Compile(functionReturnContext);

            Assert.True((bool)functionReturn.Execute(functionEnvironment, variables) == true);

            variables.Clear();
            txt                   = "return 10.0";
            inputStream           = new AntlrInputStream(txt);
            lexer                 = new BogieLangLexer(inputStream);
            commonTokenStream     = new CommonTokenStream(lexer);
            parser                = new BogieLangParser(commonTokenStream);
            functionReturnContext = parser.functionReturn();
            visitor               = new BogieLangBaseVisitor <object>();
            visitor.Visit(functionReturnContext);
            functionReturn = FunctionReturn.Compile(functionReturnContext);
            Assert.True((double)functionReturn.Execute(functionEnvironment, variables) == 10.0);
        }
Exemplo n.º 2
0
        public void FunctionReturnTests()
        {
            string            txt               = "return abc";
            AntlrInputStream  inputStream       = new AntlrInputStream(txt);
            BogieLangLexer    lexer             = new BogieLangLexer(inputStream);
            CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
            BogieLangParser   parser            = new BogieLangParser(commonTokenStream);

            BogieLangParser.FunctionReturnContext functionReturnContext = parser.functionReturn();
            BogieLangBaseVisitor <object>         visitor = new BogieLangBaseVisitor <object>();

            visitor.Visit(functionReturnContext);
            FunctionReturn functionReturn = FunctionReturn.Compile(functionReturnContext);

            Assert.True(functionReturn.Expression.Identifier == "abc");


            txt                   = "return 10.0";
            inputStream           = new AntlrInputStream(txt);
            lexer                 = new BogieLangLexer(inputStream);
            commonTokenStream     = new CommonTokenStream(lexer);
            parser                = new BogieLangParser(commonTokenStream);
            functionReturnContext = parser.functionReturn();
            visitor               = new BogieLangBaseVisitor <object>();
            visitor.Visit(functionReturnContext);
            functionReturn = FunctionReturn.Compile(functionReturnContext);
            Assert.True(functionReturn.Expression.Literal.Real == 10.0);
        }
Exemplo n.º 3
0
        public void FunctionReturnTests()
        {
            string           txt         = "return abc";
            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.FunctionReturnContext FunctionReturnContext = parser.functionReturn();
            BogieLangBaseVisitor <object>         visitor = new BogieLangBaseVisitor <object>();

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


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

            txt         = " \t     \nreturn \t     \nabc \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>());
            FunctionReturnContext = parser.functionReturn();
            visitor = new BogieLangBaseVisitor <object>();
            visitor.Visit(FunctionReturnContext);
            Assert.True(parser.NumberOfSyntaxErrors == 0);
        }