예제 #1
0
        public void ASTTreeDeclaredMainWithoutParameters_SemanticsCheckCalled_SemanticsExceptionIsCalled()
        {
            // prepare
            var program = new Program {
                Functions = new List <Function> {
                    new Function {
                        Identifier   = "not_main",
                        Arguments    = new List <string>(),
                        Instructions = new List <IInstruction> {
                            new ValueOf {
                                VariableName = "model",
                                NestedValue  = new ValueOf {
                                    VariableName = "nested_val"
                                }
                            }
                        }
                    }
                }
            };

            // act
            var exception_thrown = false;

            try {
                var sem_checker = new SemanticsChecker(program);
                var ir          = sem_checker.CheckAST();
            }
            catch (SemanticsException)
            {
                exception_thrown = true;
            }

            // validate
            Assert.True(exception_thrown);
        }
예제 #2
0
        public void ASTTreeDeclaredForFunction_SemanticsCheckCalled_SemanticsExceptionIsCalled()
        {
            // prepare
            var program = new Program {
                Functions = new List <Function> {
                    new Function {
                        Identifier = "main",
                        Arguments  = new List <string> {
                            "model"
                        },
                        Instructions = new List <IInstruction> {
                            new ForExpression {
                                Collection = new ValueOf {
                                    VariableName = "model",
                                    NestedValue  = new ValueOf {
                                        VariableName = "nested"
                                    }
                                },
                                ElementName  = "element",
                                Instructions = new List <IInstruction> {
                                    new ValueOf {
                                        VariableName = "element"
                                    }
                                }
                            }
                        }
                    }
                }
            };

            // act

            var sem_checker = new SemanticsChecker(program);
            var ir          = sem_checker.CheckAST();


            // validate
            Assert.Single(ir.First().Value.NestedBlocks);

            var for_instr = (ForInstruction)ir.First().Value.NestedBlocks.First();

            Assert.Single(for_instr.Block.ScopePrototype.Variables);
            Assert.NotNull(for_instr.Block.ScopePrototype.UpperScopePrototype);
        }
예제 #3
0
        private static void Execute(JToken model, string templatePath, string outputPath, bool addDeclaration)
        {
            var loggerFactory = LoggerFactory.Create(builder =>
            {
                builder
                .AddFilter("TKOM.Program", LogLevel.Debug)
                .AddConsole();
            });
            ILogger <Program> logger = loggerFactory.CreateLogger <Program>();

            var fileReader = new FileReader(templatePath);
            var scanner    = new Scanner(fileReader);
            var parser     = new Tools.Parser(scanner);

            try
            {
                logger.LogInformation("About to parse...");
                var tree = parser.Parse();

                var sem_checker   = new SemanticsChecker(tree);
                var functionsDict = sem_checker.CheckAST();

                var executor = new Executor(functionsDict);
                executor.Execute(model, outputPath, addDeclaration);
            }
            catch (ParsingException e)
            {
                logger.LogError($"{scanner.Token.Line}:{scanner.Token.Column} Parsing error:\n{e.Message}\n\nBuild failed. Try to compile after fixing the error.");
                return;
            }
            catch (SemanticsException e)
            {
                logger.LogError($"Semantics exception: \n{e.Message}\n\nBuild failed. Try to compile after fixing the error.");
                return;
            }
            catch (RuntimeException e)
            {
                logger.LogError($"Runtime exception: \n{e.Message}\n\nBuild failed. Try to compile after fixing the error.");
                return;
            }
            logger.LogInformation("The file has been parsed successfully!");
        }
예제 #4
0
        public void ASTTreeDeclaredWithMainAndValueCallInside_SemanticsCheckCalled_IRCorrect()
        {
            // prepare
            var program = new Program {
                Functions = new List <Function> {
                    new Function {
                        Identifier = "main",
                        Arguments  = new List <string> {
                            "model"
                        },
                        Instructions = new List <IInstruction> {
                            new ValueOf {
                                VariableName = "model",
                                NestedValue  = new ValueOf {
                                    VariableName = "nested_val"
                                }
                            }
                        }
                    }
                }
            };

            // act
            var sem_checker = new SemanticsChecker(program);
            var ir          = sem_checker.CheckAST();

            // validate
            Assert.Single(ir);
            var mainBlock = ir.First().Value;

            Assert.Single(mainBlock.NestedBlocks);
            Assert.Single(mainBlock.ScopePrototype.Variables);
            Assert.Equal("model", mainBlock.ScopePrototype.Variables.First());
            Assert.Single(mainBlock.NestedBlocks.First().ScopePrototype.Variables);
            Assert.Equal("model", mainBlock.NestedBlocks.First().ScopePrototype.Variables.First());
        }
예제 #5
0
        public void ASTTreeDeclaredWithTwoFunctions_SemanticsCheckCalled_IRCorrect()
        {
            // prepare
            var program = new Program {
                Functions = new List <Function> {
                    new Function {
                        Identifier = "main",
                        Arguments  = new List <string> {
                            "model"
                        },
                        Instructions = new List <IInstruction> {
                            new ValueOf {
                                VariableName = "model",
                                NestedValue  = new ValueOf {
                                    VariableName = "nested_val"
                                }
                            },
                            new FunctionCall {
                                FunctionName   = "function",
                                ArgumentValues = new List <Value> {
                                    new NumericValue {
                                        Integer  = true,
                                        IntValue = 5
                                    }
                                }
                            }
                        }
                    },
                    new Function {
                        Identifier = "function",
                        Arguments  = new List <string> {
                            "argument", "argument2"
                        },
                        Instructions = new List <IInstruction> {
                            new ValueOf {
                                VariableName = "argument",
                                NestedValue  = new ValueOf {
                                    VariableName = "nested_val"
                                }
                            }
                        }
                    }
                }
            };

            // act
            var sem_checker = new SemanticsChecker(program);
            var ir          = sem_checker.CheckAST();

            // validate
            Assert.Equal(2, ir.Count());
            var mainBlock     = ir.First().Value;
            var functionBlock = ir.Skip(1).First().Value;

            Assert.Equal(2, mainBlock.NestedBlocks.Count());
            Assert.Single(mainBlock.ScopePrototype.Variables);
            Assert.Equal("model", mainBlock.ScopePrototype.Variables.First());
            Assert.Single(mainBlock.NestedBlocks.First().ScopePrototype.Variables);
            Assert.Equal("model", mainBlock.NestedBlocks.First().ScopePrototype.Variables.First());

            Assert.Single(functionBlock.NestedBlocks);
            Assert.Equal(2, functionBlock.ScopePrototype.Variables.Count());
            Assert.Equal("argument", functionBlock.ScopePrototype.Variables.First());
        }