Пример #1
0
        public void IfTests()
        {
            VariableEnvironment variables           = new VariableEnvironment();
            FunctionEnvironment functionEnvironment = new FunctionEnvironment();

            string            txt               = "if(true){}";
            AntlrInputStream  inputStream       = new AntlrInputStream(txt);
            BogieLangLexer    lexer             = new BogieLangLexer(inputStream);
            CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
            BogieLangParser   parser            = new BogieLangParser(commonTokenStream);

            BogieLangParser.IfControlContext ifControlContext = parser.ifControl();
            BogieLangBaseVisitor <object>    visitor          = new BogieLangBaseVisitor <object>();

            visitor.Visit(ifControlContext);
            IfControl ifControl = IfControl.Compile(ifControlContext);

            Assert.True(ifControl.Execute(functionEnvironment, variables) == null);



            txt               = "if(true){int b=0}";
            inputStream       = new AntlrInputStream(txt);
            lexer             = new BogieLangLexer(inputStream);
            commonTokenStream = new CommonTokenStream(lexer);
            parser            = new BogieLangParser(commonTokenStream);
            ifControlContext  = parser.ifControl();
            visitor           = new BogieLangBaseVisitor <object>();
            visitor.Visit(ifControlContext);
            ifControl = IfControl.Compile(ifControlContext);
            Assert.True(ifControl.Execute(functionEnvironment, variables) == null);
            Assert.True(!variables.IsVariableDeclared("b"));
        }
Пример #2
0
        public void IfTests()
        {
            string            txt               = "if(true){}";
            AntlrInputStream  inputStream       = new AntlrInputStream(txt);
            BogieLangLexer    lexer             = new BogieLangLexer(inputStream);
            CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
            BogieLangParser   parser            = new BogieLangParser(commonTokenStream);

            BogieLangParser.IfControlContext ifControlContext = parser.ifControl();
            BogieLangBaseVisitor <object>    visitor          = new BogieLangBaseVisitor <object>();

            visitor.Visit(ifControlContext);
            IfControl ifControl = IfControl.Compile(ifControlContext);

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



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


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

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

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

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

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