예제 #1
0
        /// <summary>
        /// Validates the syntax correctness of the token stream
        /// and returns the AST root node if success.
        /// </summary>
        /// <param name="tokens">Tokens of a whole program to be validated.</param>
        /// <returns>A syntax responce instance which contains the AST root node.</returns>
        public SyntaxRule.SyntaxResponse CheckSyntax(List <Token> tokens)
        {
            ASTNode root = new ASTNode
                               (null,
                               new List <ASTNode>(),
                               new Token(TokenType.NO_TOKEN, "no_token", new TokenPosition(0, 0)),
                               "root");

            SyntaxRule.SyntaxResponse sr = programRule.Verify(tokens, root);
            if (!sr.Success)
            {
                throw programRule.GetErrors();
            }
            return(sr);
        }
예제 #2
0
        ///<summary>
        /// Main compiler function.
        ///</summary>
        ///<returns>
        /// A string with the compiled source code. It is ready to be written to the file.
        ///</returns>
        public byte[] Compile(string sourceCode, CompilationMode mode)
        {
            switch (mode)
            {
            // Only Lexical Analyzer.
            // Returns a string with all tokens listed.
            case CompilationMode.LEXIS:
            {
                List <Token>  lst = lexis.GetTokenList(sourceCode);
                StringBuilder sb  = new StringBuilder();
                sb.Append("{\r\n\t[\r\n");
                foreach (Token t in lst)
                {
                    sb.Append(t.ToString()).Append(",\r\n");
                }
                sb.Remove(sb.ToString().LastIndexOf(','), 1);
                sb.Append("\t]\r\n}");
                return(Encoding.ASCII.GetBytes(sb.ToString()));
            }

            // Lexical and Syntax Analyzers.
            // Returns a string with AST traversion listed.
            case CompilationMode.SYNTAX:
            {
                List <Token> lst = lexis.GetTokenList(sourceCode);
                SyntaxRule.SyntaxResponse synResp = syntax.CheckSyntax(lst);
                ASTNode ast = synResp.AstNode;
                return(Encoding.ASCII.GetBytes(ast.ToString()));
            }

            // Lexical, Syntax, and Semantic Analyzers.
            // Returns a string with AAST traversion listed.
            case CompilationMode.SEMANTICS:
            {
                List <Token> lst = lexis.GetTokenList(sourceCode);
                SyntaxRule.SyntaxResponse synResp = syntax.CheckSyntax(lst);
                ASTNode  ast  = synResp.AstNode;
                AASTNode aast = semantics.BuildAAST(ast);
                return(Encoding.ASCII.GetBytes(aast.ToString()));
            }

            // Full compilation.
            // Returns actual assembly generated code.
            case CompilationMode.GENERATION:
            {
                List <Token> lst = lexis.GetTokenList(sourceCode);
                SyntaxRule.SyntaxResponse synResp = syntax.CheckSyntax(lst);
                ASTNode  ast  = synResp.AstNode;
                AASTNode aast = semantics.BuildAAST(ast);
                if (Program.config.ConvertToAsmCode)
                {
                    return(Encoding.ASCII.GetBytes(Generator.GetProgramCodeNodeRoot(aast).ToString()));
                }
                LinkedList <byte> bytes    = CollectBytes(Generator.GetProgramCodeNodeRoot(aast));
                byte[]            toReturn = new byte[bytes.Count];
                int i = 0;
                foreach (byte b in bytes)
                {
                    toReturn[i] = b;
                    i++;
                }
                return(toReturn);
            }

            default:
                return(System.Array.Empty <byte>());
            }
        }