Exemplo n.º 1
0
        public void IllegalTensorMultiplicationTest()
        {
            // tensor a[2][1] = { [2.4], [5.1] }
            // tensor b[5][2] = { [1, 1],
            //                    [1, 1],
            //                    [1, 1],
            //                    [1, 1],
            //                    [1, 1] }
            // tensor c[2][2] = a*b;

            TensorInitNode aInit = new TensorInitNode();

            aInit.FirstRowElements = new List <ExpressionNode>()
            {
                new DoubleNode(2.4)
            };
            aInit.Elements = new List <ExpressionNode>()
            {
                new DoubleNode(5.1)
            };
            TensorDCLNode a = new TensorDCLNode("tensor", "a", 2, 1, aInit);

            TensorInitNode bInit = new TensorInitNode();

            bInit.FirstRowElements = new List <ExpressionNode>()
            {
                new DoubleNode(1), new DoubleNode(1)
            };
            bInit.Elements = new List <ExpressionNode>()
            {
                new DoubleNode(1), new DoubleNode(1),
                new DoubleNode(1), new DoubleNode(1),
                new DoubleNode(1), new DoubleNode(1),
                new DoubleNode(1), new DoubleNode(1)
            };
            TensorDCLNode b = new TensorDCLNode("tensor", "b", 5, 2, bInit);

            MultiplicationNode mulNode = new MultiplicationNode("*");

            mulNode.Left  = new IDNode("a");
            mulNode.Right = new IDNode("b");

            SymbolTable symbolTable             = new SymbolTable();
            TypeCheckSymbolTableVisitor visitor = new TypeCheckSymbolTableVisitor(symbolTable);

            visitor.Visit(a);
            visitor.Visit(b);
            Assert.ThrowsException <InvalidOperandsException>(() =>
            {
                visitor.Visit(mulNode);
            });
        }
Exemplo n.º 2
0
        public void DuplicateVariableDeclarationErrorTest()
        {
            VariableDCLNode dclNode1 = new VariableDCLNode("int", "var1", new IntNode(10));
            VariableDCLNode dclNode2 = new VariableDCLNode("int", "var1", new IntNode(20));

            SymbolTable symbolTable             = new SymbolTable();
            TypeCheckSymbolTableVisitor visitor = new TypeCheckSymbolTableVisitor(symbolTable);

            visitor.Visit(dclNode1);

            Assert.ThrowsException <VariableAlreadyDeclaredException>(() =>
            {
                visitor.Visit(dclNode2);
            });
        }
Exemplo n.º 3
0
        public void InvalidOperandstoOrOperation()
        {
            NotEqualNode node = new NotEqualNode("!=");

            node.Left  = new BoolNode(true);
            node.Right = new IntNode(2);

            SymbolTable symbolTable             = new SymbolTable();
            TypeCheckSymbolTableVisitor visitor = new TypeCheckSymbolTableVisitor(symbolTable);

            Assert.ThrowsException <InvalidOperandsException>(() =>
            {
                visitor.Visit(node);
            });
        }
Exemplo n.º 4
0
        public void AssigningDoubleToIntVariable()
        {
            //Arrange
            ExpressionNode  exprNode = new DoubleNode(25.5);
            VariableDCLNode node     = new VariableDCLNode("int", "a", exprNode);

            //Act
            SymbolTable symbolTable             = new SymbolTable();
            TypeCheckSymbolTableVisitor visitor = new TypeCheckSymbolTableVisitor(symbolTable);

            //Assert
            Assert.ThrowsException <VariableInitialisationException>(() =>
            {
                visitor.Visit(node);
            });
        }
Exemplo n.º 5
0
        public void MixedExpressionDoubleAndIntBecomesDoubleNode()
        {
            //Arrange
            InfixExpressionNode node = new AdditionNode("+");

            node.Left  = new DoubleNode(25.5);
            node.Right = new IntNode(1);

            SymbolTable symbolTable             = new SymbolTable();
            TypeCheckSymbolTableVisitor visitor = new TypeCheckSymbolTableVisitor(symbolTable);

            //Act
            visitor.Visit(node);

            //Assert
            Assert.AreEqual(node.Type, "double");
        }
Exemplo n.º 6
0
        public void DuplicateFunctionDeclarationErrorTest()
        {
            FunctionDCLNode funcDclNode1 = new FunctionDCLNode("void", "myFunction");

            funcDclNode1.Body = new LinesNode();
            FunctionDCLNode funcDclNode2 = new FunctionDCLNode("int", "myFunction");

            funcDclNode2.Body = new LinesNode();

            SymbolTable symbolTable             = new SymbolTable();
            TypeCheckSymbolTableVisitor visitor = new TypeCheckSymbolTableVisitor(symbolTable);

            visitor.Visit(funcDclNode1);

            Assert.ThrowsException <FunctionAlreadyDeclaredException>(() =>
            {
                visitor.Visit(funcDclNode2);
            });
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            try
            {
                if (string.IsNullOrEmpty(args[0]) || string.IsNullOrEmpty(args[1]))
                {
                    throw new Exception(
                              "No run arguments were found. Missing arguments: <source filename> <target filename>");
                }

                string sourceFilename = args[0].Trim();
                string targetFileName = args[1].Trim();
                string text           = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + sourceFilename + ".ML4D");

                // ANTLR Lexer, Paser, and AST
                var inputStream = new AntlrInputStream(new StringReader(text));
                var lexer       = new ML4DLexer(inputStream);
                var tokenStream = new CommonTokenStream(lexer);
                var parser      = new ML4DParser(tokenStream);
                var cst         = parser.lines();
                var ast         = new ASTBuilder().VisitLines(cst);

                // Symbol Table and Type check
                SymbolTable symbolTable = new SymbolTable();
                symbolTable.OpenScope(); // Global scope
                var typesymbolVisitor = new TypeCheckSymbolTableVisitor(symbolTable);
                typesymbolVisitor.Visit(ast);

                // Code generation
                symbolTable.OnlyGlobalScope();
                CodeGeneration codeGen = new CodeGeneration(symbolTable);
                codeGen.Visit(ast);
                codeGen.WriteToFile(targetFileName);
                SymbolTable.Clear();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Exemplo n.º 8
0
        public void ValidOperandsToOrOperation()
        {
            OrNode node = new OrNode("Or");

            EqualNode fourplus6equal10 = new EqualNode("=");

            InfixExpressionNode addNode = new AdditionNode("+");

            addNode.Left           = new IntNode(4);
            addNode.Right          = new IntNode(6);
            fourplus6equal10.Left  = addNode;
            fourplus6equal10.Right = new IntNode(10);

            node.Left  = fourplus6equal10;
            node.Right = new BoolNode(false);

            SymbolTable symbolTable             = new SymbolTable();
            TypeCheckSymbolTableVisitor visitor = new TypeCheckSymbolTableVisitor(symbolTable);

            visitor.Visit(node);

            Assert.AreEqual(node.Type, "bool");
        }