예제 #1
0
        public void WhileTests()
        {
            string           txt         = "while(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.WhileControlContext WhileControlContext = parser.whileControl();
            BogieLangBaseVisitor <object>       visitor             = new BogieLangBaseVisitor <object>();

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


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


            txt         = @"while(true)
{
    int b=0
    b = 1
    funcCall(b)
    while(false){return 1}
    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>());
            WhileControlContext = parser.whileControl();
            visitor             = new BogieLangBaseVisitor <object>();
            visitor.Visit(WhileControlContext);
            Assert.True(parser.NumberOfSyntaxErrors == 0);
        }
예제 #2
0
        public static WhileControl Compile(BogieLangParser.WhileControlContext whileControlContext)
        {
            WhileControl result = new WhileControl();

            if (whileControlContext.expression() != null)
            {
                result.Expression = Expression.Compile(whileControlContext.expression(), null);
            }
            if (whileControlContext.body().Length != 0)
            {
                foreach (BogieLangParser.BodyContext context in whileControlContext.body())
                {
                    result.Body.Add(Runtime.Body.Compile(context));
                }
            }

            return(result);
        }
예제 #3
0
        public void WhileTests()
        {
            VariableEnvironment variables           = new VariableEnvironment();
            FunctionEnvironment functionEnvironment = new FunctionEnvironment();

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

            string            txt               = "while(loop){loop = false}";
            AntlrInputStream  inputStream       = new AntlrInputStream(txt);
            BogieLangLexer    lexer             = new BogieLangLexer(inputStream);
            CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
            BogieLangParser   parser            = new BogieLangParser(commonTokenStream);

            BogieLangParser.WhileControlContext whileControlContext = parser.whileControl();
            BogieLangBaseVisitor <object>       visitor             = new BogieLangBaseVisitor <object>();

            visitor.Visit(whileControlContext);
            WhileControl whileControl = WhileControl.Compile(whileControlContext);

            Assert.True(whileControl.Execute(functionEnvironment, variables) == null);
            Assert.True((bool)variables["loop"].Value == false);



            txt                 = " while(loop){loop = false int b=0}";
            inputStream         = new AntlrInputStream(txt);
            lexer               = new BogieLangLexer(inputStream);
            commonTokenStream   = new CommonTokenStream(lexer);
            parser              = new BogieLangParser(commonTokenStream);
            whileControlContext = parser.whileControl();
            visitor             = new BogieLangBaseVisitor <object>();
            visitor.Visit(whileControlContext);
            whileControl = WhileControl.Compile(whileControlContext);
            Assert.True(whileControl.Execute(functionEnvironment, variables) == null);
            Assert.True((bool)variables["loop"].Value == false);
            Assert.True(!variables.IsVariableDeclared("b"));
        }
예제 #4
0
        public void WhileTests()
        {
            string            txt               = "while(true){}";
            AntlrInputStream  inputStream       = new AntlrInputStream(txt);
            BogieLangLexer    lexer             = new BogieLangLexer(inputStream);
            CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
            BogieLangParser   parser            = new BogieLangParser(commonTokenStream);

            BogieLangParser.WhileControlContext whileControlContext = parser.whileControl();
            BogieLangBaseVisitor <object>       visitor             = new BogieLangBaseVisitor <object>();

            visitor.Visit(whileControlContext);
            WhileControl whileControl = WhileControl.Compile(whileControlContext);

            Assert.True(whileControl.Expression.Literal.Bool == true);
            Assert.True(whileControl.Body.Count == 0);


            txt                 = "while(true){int b=0}";
            inputStream         = new AntlrInputStream(txt);
            lexer               = new BogieLangLexer(inputStream);
            commonTokenStream   = new CommonTokenStream(lexer);
            parser              = new BogieLangParser(commonTokenStream);
            whileControlContext = parser.whileControl();
            visitor             = new BogieLangBaseVisitor <object>();
            visitor.Visit(whileControlContext);
            whileControl = WhileControl.Compile(whileControlContext);
            Assert.True(whileControl.Expression.Literal.Bool == true);
            Assert.True(whileControl.Body[0].VarDeclaration.BogieLangType == BogieLangType.INTEGER);
            Assert.True(whileControl.Body[0].VarDeclaration.Identifier == "b");
            Assert.True(whileControl.Body[0].VarDeclaration.Expression.Literal.Integer == 0);


            txt                 = @"while(true)
{
    int b=0
    b = 1
    funcCall(b)
    while(false){return 1}
    return 0
}";
            inputStream         = new AntlrInputStream(txt);
            lexer               = new BogieLangLexer(inputStream);
            commonTokenStream   = new CommonTokenStream(lexer);
            parser              = new BogieLangParser(commonTokenStream);
            whileControlContext = parser.whileControl();
            visitor             = new BogieLangBaseVisitor <object>();
            visitor.Visit(whileControlContext);
            whileControl = WhileControl.Compile(whileControlContext);
            Assert.True(whileControl.Expression.Literal.Bool == true);
            Assert.True(whileControl.Body[0].VarDeclaration.BogieLangType == BogieLangType.INTEGER);
            Assert.True(whileControl.Body[0].VarDeclaration.Identifier == "b");
            Assert.True(whileControl.Body[0].VarDeclaration.Expression.Literal.Integer == 0);

            Assert.True(whileControl.Body[1].VarDefinition.Identifier == "b");
            Assert.True(whileControl.Body[1].VarDefinition.Expression.Literal.Integer == 1);

            Assert.True(whileControl.Body[2].FunctionCall.Identifier == "funcCall");
            Assert.True(whileControl.Body[2].FunctionCall.Arguments[0].Identifier == "b");

            Assert.True(whileControl.Body[3].WhileControl.Expression.Literal.Bool == false);
            Assert.True(whileControl.Body[3].WhileControl.Body[0].FunctionReturn.Expression.Literal.Integer == 1);

            Assert.True(whileControl.Body[4].FunctionReturn.Expression.Literal.Integer == 0);
        }