Exemplo n.º 1
0
        public static TargetCppResult Compile(string input, string file_name)
        {
            if (file_name.LastIndexOf("/") != -1)
            {
                file_name = file_name.Substring(file_name.LastIndexOf("/") + 1);
            }
            if (file_name.LastIndexOf("\\") != -1)
            {
                file_name = file_name.Substring(file_name.LastIndexOf("\\") + 1);
            }
            input = input.Replace("\r", "");
            ANTLRStringStream Input  = new ANTLRStringStream(input);
            SugarCppLexer     lexer  = new SugarCppLexer(Input);
            CommonTokenStream tokens = new CommonTokenStream(lexer);

            SugarCppParser parser = new SugarCppParser(tokens);

            AstParserRuleReturnScope <CommonTree, IToken> t = parser.root();
            CommonTree ct = (CommonTree)t.Tree;

            if (parser.errors.Count() > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var error in parser.errors)
                {
                    sb.Append(error);
                    sb.Append("\n");
                }
                throw new Exception(sb.ToString());
            }

            CommonTreeNodeStream nodes  = new CommonTreeNodeStream(ct);
            SugarWalker          walker = new SugarWalker(nodes);

            Root ast = walker.root();

            TargetCppHeader         header         = new TargetCppHeader();
            TargetCppImplementation implementation = new TargetCppImplementation();

            implementation.HeaderFileName = string.Format("{0}.h", file_name);

            TargetCppResult result = new TargetCppResult();

            result.Header         = ast.Accept(header).Render();
            result.Implementation = ast.Accept(implementation).Render();

            return(result);
        }
Exemplo n.º 2
0
        public static TargetCppResult Compile(string input, string file_name)
        {
            if (file_name.LastIndexOf("/") != -1) file_name = file_name.Substring(file_name.LastIndexOf("/") + 1);
            if (file_name.LastIndexOf("\\") != -1) file_name = file_name.Substring(file_name.LastIndexOf("\\") + 1);
            input = input.Replace("\r", "");
            ANTLRStringStream Input = new ANTLRStringStream(input);
            SugarCppLexer lexer = new SugarCppLexer(Input);
            CommonTokenStream tokens = new CommonTokenStream(lexer);

            SugarCppParser parser = new SugarCppParser(tokens);

            AstParserRuleReturnScope<CommonTree, IToken> t = parser.root();
            CommonTree ct = (CommonTree)t.Tree;

            if (parser.errors.Count() > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var error in parser.errors)
                {
                    sb.Append(error);
                    sb.Append("\n");
                }
                throw new Exception(sb.ToString());
            }

            CommonTreeNodeStream nodes = new CommonTreeNodeStream(ct);
            SugarWalker walker = new SugarWalker(nodes);

            Root ast = walker.root();

            TargetCppHeader header = new TargetCppHeader();
            TargetCppImplementation implementation = new TargetCppImplementation();
            implementation.HeaderFileName = string.Format("{0}.h", file_name);

            TargetCppResult result = new TargetCppResult();
            result.Header = ast.Accept(header).Render();
            result.Implementation = ast.Accept(implementation).Render();

            return result;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Compile code from input.
        /// </summary>
        private static void Compile()
        {
            string input = File.ReadAllText(inputFileName);
            int dot_pos = inputFileName.LastIndexOf(".");
            string header_name = inputFileName.Substring(0, dot_pos) + ".h";
            string implementation_name = inputFileName.Substring(0, dot_pos) + ".cpp";
            ANTLRStringStream inputStream = new ANTLRStringStream(input);
            SugarCppLexer lexer = new SugarCppLexer(inputStream);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            if (printTokens)
            {
                PrintTokens(tokens);
            }

            SugarCppParser parser = new SugarCppParser(tokens);
            AstParserRuleReturnScope<CommonTree, IToken> t = parser.root();
            CommonTree ct = (CommonTree)t.Tree;
            if (printAST)
            {
                PrintAST(ct);
            }

            if (printCode)
            {
                CommonTreeNodeStream nodes = new CommonTreeNodeStream(ct);
                SugarWalker walker = new SugarWalker(nodes);
                Root x = walker.root();
                TargetCppHeader header = new TargetCppHeader();
                TargetCppImplementation implementation = new TargetCppImplementation();
                string include_name = header_name;
                if (include_name.LastIndexOf('/') != -1) include_name = include_name.Substring(include_name.LastIndexOf('/') + 1);
                if (include_name.LastIndexOf('\\') != -1) include_name = include_name.Substring(include_name.LastIndexOf('\\') + 1);
                implementation.HeaderFileName = include_name;
                string header_code = x.Accept(header).Render();
                string implementation_code = x.Accept(implementation).Render();
                File.WriteAllText(header_name, header_code);
                File.WriteAllText(implementation_name, implementation_code);
                Console.WriteLine();
            }
        }