public static int Main(string[] args) { HashSet <string> preSymbols = new HashSet <string>(); preSymbols.Add("true"); List <string> inputFiles = new List <string>(); List <string> searchDirs = new List <string>(); string lang = null; string outputFile = null; string namespace_ = null; for (int i = 0; i < args.Length; i++) { string arg = args[i]; if (arg[0] == '-') { switch (arg) { case "--help": Usage(); return(0); case "--version": Console.WriteLine("cito 0.4.0"); return(0); case "-l": lang = args[++i]; break; case "-o": outputFile = args[++i]; break; case "-n": namespace_ = args[++i]; break; case "-D": string symbol = args[++i]; if (symbol == "true" || symbol == "false") { throw new ArgumentException(symbol + " is reserved"); } preSymbols.Add(symbol); break; case "-I": searchDirs.Add(args[++i]); break; default: throw new ArgumentException("Unknown option: " + arg); } } else { inputFiles.Add(arg); } } if (lang == null && outputFile != null) { string ext = Path.GetExtension(outputFile); if (ext.Length >= 2) { lang = ext.Substring(1); } } if (lang == null || outputFile == null || inputFiles.Count == 0) { Usage(); return(1); } CiParser parser = new CiParser(); parser.PreSymbols = preSymbols; foreach (string inputFile in inputFiles) { try { parser.Parse(inputFile, File.OpenText(inputFile)); } catch (Exception ex) { Console.Error.WriteLine("{0}({1}): ERROR: {2}", inputFile, parser.InputLineNo, ex.Message); parser.PrintMacroStack(); if (parser.CurrentMethod != null) { Console.Error.WriteLine(" in method {0}", parser.CurrentMethod.Name); } return(1); // throw; } } CiProgram program = parser.Program; CiResolver resolver = new CiResolver(); resolver.SearchDirs = searchDirs; try { resolver.Resolve(program); } catch (Exception ex) { if (resolver.CurrentClass != null) { Console.Error.Write(resolver.CurrentClass.SourceFilename); Console.Error.Write(": "); } Console.Error.WriteLine("ERROR: {0}", ex.Message); if (resolver.CurrentMethod != null) { Console.Error.WriteLine(" in method {0}", resolver.CurrentMethod.Name); } return(1); // throw; } SourceGenerator gen; switch (lang) { case "c": gen = new GenC89(); break; case "c99": gen = new GenC(); break; case "java": gen = new GenJava(namespace_); break; case "cs": gen = new GenCs(namespace_); break; case "js": gen = new GenJs(); break; case "js-ta": gen = new GenJsWithTypedArrays(); break; case "as": gen = new GenAs(namespace_); break; case "d": gen = new GenD(); break; case "pm": gen = new GenPerl58(namespace_); break; case "pm510": gen = new GenPerl510(namespace_); break; default: throw new ArgumentException("Unknown language: " + lang); } gen.OutputFile = outputFile; gen.Write(program); return(0); }
void Translate() { try { CiParser parser = new CiParser(); foreach (TabPage page in this.CiGroup.TabPages) parser.Parse(page.Text, new StringReader(page.Controls[0].Text)); CiProgram program = parser.Program; CiResolver resolver = new CiResolver(); resolver.SearchDirs = this.SearchDirs; resolver.Resolve(program); this.CGroup.Load(program, new GenC89 { OutputFile = "hello.c" }, new GenC { OutputFile = "hello99.c" }); this.DGroup.Load(program, new GenD { OutputFile = "hello.d" }); this.JavaGroup.Load(program, new GenJava(null) { OutputFile = "." }); this.CsGroup.Load(program, new GenCs(null) { OutputFile = "hello.cs" }); this.PerlGroup.Load(program, new GenPerl58(null) { OutputFile = "hello.pm" }, new GenPerl510(null) { OutputFile = "hello-5.10.pm" }); this.JsGroup.Load(program, new GenJs() { OutputFile = "hello.js" }, new GenJsWithTypedArrays() { OutputFile = "hello-Typed-Arrays.js" }); this.AsGroup.Load(program, new GenAs(null) { OutputFile = "." }); this.Messages.BackColor = SystemColors.Window; this.Messages.Text = "OK"; } catch (Exception ex) { this.Messages.BackColor = Color.LightCoral; this.Messages.Text = ex.Message; } }
public static int Main(string[] args) { HashSet<string> preSymbols = new HashSet<string>(); preSymbols.Add("true"); List<string> inputFiles = new List<string>(); List<string> searchDirs = new List<string>(); string lang = null; string outputFile = null; string namespace_ = null; for (int i = 0; i < args.Length; i++) { string arg = args[i]; if (arg[0] == '-') { switch (arg) { case "--help": Usage(); return 0; case "--version": Console.WriteLine("cito 0.4.0"); return 0; case "-l": lang = args[++i]; break; case "-o": outputFile = args[++i]; break; case "-n": namespace_ = args[++i]; break; case "-D": string symbol = args[++i]; if (symbol == "true" || symbol == "false") throw new ArgumentException(symbol + " is reserved"); preSymbols.Add(symbol); break; case "-I": searchDirs.Add(args[++i]); break; default: throw new ArgumentException("Unknown option: " + arg); } } else { inputFiles.Add(arg); } } if (lang == null && outputFile != null) { string ext = Path.GetExtension(outputFile); if (ext.Length >= 2) lang = ext.Substring(1); } if (lang == null || outputFile == null || inputFiles.Count == 0) { Usage(); return 1; } CiParser parser = new CiParser(); parser.PreSymbols = preSymbols; foreach (string inputFile in inputFiles) { try { parser.Parse(inputFile, File.OpenText(inputFile)); } catch (Exception ex) { Console.Error.WriteLine("{0}({1}): ERROR: {2}", inputFile, parser.InputLineNo, ex.Message); parser.PrintMacroStack(); if (parser.CurrentMethod != null) Console.Error.WriteLine(" in method {0}", parser.CurrentMethod.Name); return 1; // throw; } } CiProgram program = parser.Program; CiResolver resolver = new CiResolver(); resolver.SearchDirs = searchDirs; try { resolver.Resolve(program); } catch (Exception ex) { if (resolver.CurrentClass != null) { Console.Error.Write(resolver.CurrentClass.SourceFilename); Console.Error.Write(": "); } Console.Error.WriteLine("ERROR: {0}", ex.Message); if (resolver.CurrentMethod != null) Console.Error.WriteLine(" in method {0}", resolver.CurrentMethod.Name); return 1; // throw; } SourceGenerator gen; switch (lang) { case "c": gen = new GenC89(); break; case "c99": gen = new GenC(); break; case "java": gen = new GenJava(namespace_); break; case "cs": gen = new GenCs(namespace_); break; case "js": gen = new GenJs(); break; case "js-ta": gen = new GenJsWithTypedArrays(); break; case "as": gen = new GenAs(namespace_); break; case "d": gen = new GenD(); break; case "pm": gen = new GenPerl58(namespace_); break; case "pm510": gen = new GenPerl510(namespace_); break; default: throw new ArgumentException("Unknown language: " + lang); } gen.OutputFile = outputFile; gen.Write(program); return 0; }