Esempio n. 1
0
        public void DoubleFunctionDefinition()
        {
            //First parse the initial file and then request the rest
            //Let's lexicalize the file
            StreamReader sourceStream = new StreamReader("../../../../TestChecker/WaebricTestFiles/doublefunctiondefinition.wae");
            WaebricLexer lexer = new WaebricLexer(sourceStream);

            lexer.LexicalizeStream();
            TokenIterator tokens = lexer.GetTokenIterator();

            //Lets parse the file
            WaebricParser parser = new WaebricParser(tokens);
            parser.Parse();

            SyntaxTree parsedTree = parser.GetTree();

            //Initialize ModuleCache with correct DirectoryPath
            ModuleCache.Instance.SetDirectoryPath("../../../../TestChecker/WaebricTestFiles/");

            //Lets check the tree
            WaebricChecker checker = new WaebricChecker();
            List<Exception> checkerExceptions = checker.CheckSyntaxTree(parsedTree);

            //Exception function already defined should be in list
            Assert.AreEqual(1, checkerExceptions.Count);
            Assert.AreEqual(typeof(FunctionAlreadyDefined), checkerExceptions.ToArray()[0].GetType());
        }
Esempio n. 2
0
        private static String Path; //Path of file to compile

        #endregion Fields

        #region Methods

        public static void Main(string[] args)
        {
            Console.WriteLine("Waebric Compiler/Interpreter v1.0");
            Console.WriteLine("---------------------------------");

            if (args.Length == 1)
            {   //There is one file specified.
                Path = args[0];
            }
            else
            {
                Console.WriteLine("WeabricCompiler: no input file specified.");
                Console.Read(); //Testing purposes only
                return;
            }

            //Let's lexicalize the file
            StreamReader sourceStream = new StreamReader(Path);
            WaebricLexer lexer = new WaebricLexer(sourceStream);

            lexer.LexicalizeStream();
            TokenIterator tokens = lexer.GetTokenIterator();

            if (tokens.GetSize() == 0)
            {   //Not tokens parsed
                Console.WriteLine("WaebricCompiler: Empty file or comments only.");
                return; //Nothing to compile so end program
            }

            //Lets parse the file
            WaebricParser parser = new WaebricParser(tokens);
            parser.Parse();

            SyntaxTree parsedTree = parser.GetTree();

            //Initialize ModuleCache with correct DirectoryPath
            ModuleCache.Instance.SetDirectoryPath(GetDirectoryPath());

            //Lets check the tree
            WaebricChecker checker = new WaebricChecker();
            checker.CheckSyntaxTree(parsedTree);

            //Lets interpret the tree and generate XHTML
            WaebricInterpreter interpreter = new WaebricInterpreter();
            interpreter.InterpretAST(parsedTree);
        }
Esempio n. 3
0
        public void ModuleParserComplexModuleNameTest()
        {
            SyntaxTree tree;

            //Create lexer to tokenize stream
            WaebricLexer lexer = new WaebricLexer(new StringReader("module test.test2.test3"));
            lexer.LexicalizeStream();

            //Retrieve tokenIterator from lexer and lets parse it
            WaebricParser parser = new WaebricParser(lexer.GetTokenIterator());
            parser.Parse();

            //Test if root is modulelist and it contains the right module
            tree = parser.GetTree();

            Module module = tree.GetRoot();
            Assert.AreEqual(3, module.GetModuleId().GetIdentifiers().Count);
            Assert.AreEqual("test.test2.test3", module.GetModuleId().ToString());
        }
Esempio n. 4
0
        public void WaebricCheckerImportTest()
        {
            //First parse the initial file and then request the rest
            //Let's lexicalize the file
            StreamReader sourceStream = new StreamReader("../../../../TestChecker/WaebricTestFiles/home.wae");
            WaebricLexer lexer = new WaebricLexer(sourceStream);

            lexer.LexicalizeStream();
            TokenIterator tokens = lexer.GetTokenIterator();

            //Lets parse the file
            WaebricParser parser = new WaebricParser(tokens);
            parser.Parse();

            SyntaxTree parsedTree = parser.GetTree();

            //Initialize ModuleCache with correct DirectoryPath
            ModuleCache.Instance.SetDirectoryPath("../../../../TestChecker/WaebricTestFiles/");

            //Lets check the tree
            WaebricChecker checker = new WaebricChecker();
            List<Exception> checkerExceptions = checker.CheckSyntaxTree(parsedTree);

            //Test output
            Assert.AreEqual(0, checkerExceptions.Count);

            //Test if all modules except tree root are in cache
            Assert.IsTrue(ModuleCache.Instance.ContainsModule("first"));
            Assert.IsTrue(ModuleCache.Instance.ContainsModule("second"));
            Assert.IsTrue(ModuleCache.Instance.ContainsModule("common"));
        }
Esempio n. 5
0
        /// <summary>
        /// Request an specified module
        /// </summary>
        /// <param name="indentifier">ModuleId of requested module</param>
        /// <returns>Requested module if available</returns>
        public Module RequestModule(ModuleId identifier)
        {
            if (ModuleTable.ContainsKey(identifier))
            {   //Module already loaded so return instance of module
                return (Module) ModuleTable[identifier];
            }

            //Module not cached, so load it
            StreamReader moduleStream = new StreamReader(GetPath(identifier));

            //Lexicalize and parse it
            WaebricLexer lexer = new WaebricLexer(moduleStream);
            lexer.LexicalizeStream();
            WaebricParser parser = new WaebricParser(lexer.GetTokenIterator());
            parser.Parse();

            //Get module of tree
            SyntaxTree tree = parser.GetTree();

            //Add module to hashtable
            Module requestedModule = tree.GetRoot();
            ModuleTable.Add(identifier, requestedModule);

            return requestedModule;
        }
Esempio n. 6
0
        public void ModuleParserImportTest()
        {
            SyntaxTree tree;

            //Create lexer to tokenize stream
            WaebricLexer lexer = new WaebricLexer(new StringReader("module test\n\nimport importtest"));
            lexer.LexicalizeStream();

            //Test if stream is lexicalized into 4 tokens
            Assert.IsTrue(lexer.GetTokenIterator().GetSize() == 4);

            //Retrieve tokenIterator from lexer and lets parse it
            WaebricParser parser = new WaebricParser(lexer.GetTokenIterator());
            parser.Parse();

            //Test tree structure
            tree = parser.GetTree();

            Module module = tree.GetRoot();
        }
Esempio n. 7
0
        public void ModuleParserSingleModuleTest()
        {
            SyntaxTree tree;

            //Create lexer to tokenize stream
            WaebricLexer lexer = new WaebricLexer(new StringReader("module test"));
            lexer.LexicalizeStream();

            //Retrieve tokenIterator from lexer and lets parse it
            WaebricParser parser = new WaebricParser(lexer.GetTokenIterator());
            parser.Parse();

            //Test if root is modulelist and it contains the right module
            tree = parser.GetTree();

            Module module = tree.GetRoot();
            String[] identifiers = module.GetModuleId().GetIdentifiers().ToArray();
            Assert.AreEqual(1, identifiers.Length);
            Assert.AreEqual("test", identifiers[0]);
        }