Esempio n. 1
0
        public void FunctionDefinitionTests()
        {
            VariableEnvironment variableEnvironment = new VariableEnvironment();
            FunctionEnvironment functionEnvironment = new FunctionEnvironment();

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

            BogieLangParser.FunctionDefinitionContext functionDefinitionContext = parser.functionDefinition();
            BogieLangBaseVisitor <object>             visitor = new BogieLangBaseVisitor <object>();

            visitor.Visit(functionDefinitionContext);
            FunctionDefinition functionDefinition = FunctionDefinition.Compile(functionDefinitionContext);

            functionEnvironment.FunctionDefinitions.Add("funcName", functionDefinition);
            functionDefinition.Execute(functionEnvironment, variableEnvironment);

            functionEnvironment.Clear();
            variableEnvironment.DeclaredVariables = new Dictionary <string, BogieLangTypeInstance>()
            {
                { "abc", new BogieLangTypeInstance()
                  {
                      BogieLangType = BogieLangType.INTEGER, Identifer = "abc", Value = 1
                  } },
                { "str", new BogieLangTypeInstance()
                  {
                      BogieLangType = BogieLangType.STRING, Identifer = "str", Value = "string"
                  } },
                { "lol", new BogieLangTypeInstance()
                  {
                      BogieLangType = BogieLangType.VOID, Identifer = "lol", Value = null
                  } }
            };
            txt                       = "void funcName(int abc,string str,void lol){}";
            inputStream               = new AntlrInputStream(txt);
            lexer                     = new BogieLangLexer(inputStream);
            commonTokenStream         = new CommonTokenStream(lexer);
            parser                    = new BogieLangParser(commonTokenStream);
            functionDefinitionContext = parser.functionDefinition();
            visitor                   = new BogieLangBaseVisitor <object>();
            visitor.Visit(functionDefinitionContext);
            functionDefinition = FunctionDefinition.Compile(functionDefinitionContext);
            functionEnvironment.FunctionDefinitions.Add("funcName", functionDefinition);
            functionDefinition.Execute(functionEnvironment, variableEnvironment);

            txt = "int funcName(int abc,string str,bool b){int intvar" +
                  "\n intvar=123" +
                  "\nintvar=1" +
                  "\nreturn funcCall(b)" +
                  "\n}";

            string txt2 = "\nint funcCall(bool b)" +
                          "\n{" +
                          "\nif(b)" +
                          "\n{" +
                          "\n return 100" +
                          "\n}" +
                          "\nreturn 0" +
                          "}";

            inputStream               = new AntlrInputStream(txt);
            lexer                     = new BogieLangLexer(inputStream);
            commonTokenStream         = new CommonTokenStream(lexer);
            parser                    = new BogieLangParser(commonTokenStream);
            functionDefinitionContext = parser.functionDefinition();
            visitor                   = new BogieLangBaseVisitor <object>();
            visitor.Visit(functionDefinitionContext);
            FunctionDefinition functionDefinition1 = FunctionDefinition.Compile(functionDefinitionContext);

            inputStream               = new AntlrInputStream(txt2);
            lexer                     = new BogieLangLexer(inputStream);
            commonTokenStream         = new CommonTokenStream(lexer);
            parser                    = new BogieLangParser(commonTokenStream);
            functionDefinitionContext = parser.functionDefinition();
            visitor                   = new BogieLangBaseVisitor <object>();
            visitor.Visit(functionDefinitionContext);
            FunctionDefinition functionDefinition2 = FunctionDefinition.Compile(functionDefinitionContext);

            variableEnvironment.Clear();
            functionEnvironment.Clear();

            variableEnvironment.DeclaredVariables = new Dictionary <string, BogieLangTypeInstance>()
            {
                { "abc", new BogieLangTypeInstance()
                  {
                      BogieLangType = BogieLangType.INTEGER, Identifer = "abc", Value = 1
                  } },
                { "str", new BogieLangTypeInstance()
                  {
                      BogieLangType = BogieLangType.STRING, Identifer = "str", Value = "string"
                  } },
                { "b", new BogieLangTypeInstance()
                  {
                      BogieLangType = BogieLangType.BOOL, Identifer = "b", Value = true
                  } }
            };
            functionEnvironment.DefineFunction("funcName", functionDefinition1);
            functionEnvironment.DefineFunction("funcCall", functionDefinition2);
            Assert.True((int)functionDefinition1.Execute(functionEnvironment, variableEnvironment) == 100);
        }