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); }
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; }