|
13 | 13 | // limitations under the License. |
14 | 14 | package wyjs; |
15 | 15 |
|
| 16 | +import java.io.BufferedReader; |
16 | 17 | import java.io.File; |
| 18 | +import java.io.FileInputStream; |
17 | 19 | import java.io.FileOutputStream; |
18 | 20 | import java.io.IOException; |
| 21 | +import java.io.InputStream; |
| 22 | +import java.io.InputStreamReader; |
| 23 | +import java.io.Reader; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
19 | 28 |
|
| 29 | +import wycc.util.OptArg; |
20 | 30 | import wycc.util.Trie; |
21 | | -import wyil.io.WyilFileWriter; |
22 | | -import wyil.lang.WyilFile; |
23 | 31 | import wyjs.core.JavaScriptFile; |
| 32 | +import wyjs.core.JavaScriptFile.NativeDeclaration; |
| 33 | +import wyjs.core.JavaScriptFile.Standard; |
24 | 34 | import wyjs.io.JavaScriptFilePrinter; |
| 35 | +import wyjs.tasks.JavaScriptCompileTask; |
25 | 36 |
|
26 | 37 | public class Main { |
| 38 | + /** |
| 39 | + * Determine the JavaScript standard to use. |
| 40 | + */ |
| 41 | + private Standard standard = Standard.ES6; |
| 42 | + /** |
| 43 | + * Destination directory of Wyil files. |
| 44 | + */ |
| 45 | + private File wyildir = new File("."); |
| 46 | + /** |
| 47 | + * Destination directory of Wyil files. |
| 48 | + */ |
| 49 | + private File jsdir = new File("."); |
| 50 | + /** |
| 51 | + * List of source files. |
| 52 | + */ |
| 53 | + private List<Trie> sources = new ArrayList<>(); |
| 54 | + /** |
| 55 | + * Determine target filename. |
| 56 | + */ |
| 57 | + private Trie target = Trie.fromString("main"); |
| 58 | + /** |
| 59 | + * List of JavaScript files to include. |
| 60 | + */ |
| 61 | + private List<File> includes = new ArrayList<>(); |
| 62 | + |
| 63 | + public Main addSource(Trie source) { |
| 64 | + this.sources.add(source); |
| 65 | + return this; |
| 66 | + } |
27 | 67 |
|
| 68 | + public Main setStandard(Standard standard) { |
| 69 | + this.standard = standard; |
| 70 | + return this; |
| 71 | + } |
28 | 72 |
|
29 | | -// public JavaScriptFile readJavaScriptFile(Trie p, InputStream inputStream, Content.Registry registry) throws IOException { |
30 | | -// // NOTE: this is strictly a hack at this time as its unclear what the best |
31 | | -// // alternative option is. Specifically, parsing JavaScriptFiles is not something |
32 | | -// // I'm contemplating right now :) |
33 | | -// Reader reader = new InputStreamReader(inputStream); |
34 | | -// BufferedReader in = new BufferedReader(reader); |
35 | | -// |
36 | | -// StringBuilder text = new StringBuilder(); |
37 | | -// int len = 0; |
38 | | -// char[] buf = new char[1024]; |
39 | | -// while ((len = in.read(buf)) != -1) { |
40 | | -// text.append(buf, 0, len); |
41 | | -// } |
42 | | -// // Finally, construct the native declaration |
43 | | -// NativeDeclaration d = new NativeDeclaration(text.toString()); |
44 | | -// // |
45 | | -// JavaScriptFile js = new JavaScriptFile(p, Collections.EMPTY_LIST, true, Standard.ES6); |
46 | | -// // Append our native declarations. |
47 | | -// js.declarations.add(d); |
48 | | -// return js; |
49 | | -// } |
| 73 | + public Main setTarget(Trie target) { |
| 74 | + this.target = target; |
| 75 | + return this; |
| 76 | + } |
50 | 77 |
|
| 78 | + public Main setWyilDir(File wyildir) { |
| 79 | + this.wyildir = wyildir; |
| 80 | + return this; |
| 81 | + } |
| 82 | + |
| 83 | + public Main setJsDir(File jsdir) { |
| 84 | + this.jsdir = jsdir; |
| 85 | + return this; |
| 86 | + } |
| 87 | + |
| 88 | + public boolean run() throws IOException { |
| 89 | + // Construct compile task |
| 90 | + JavaScriptCompileTask task = new JavaScriptCompileTask().setTarget(target).setStandard(standard); |
| 91 | + // Add sources |
| 92 | + for(Trie source : sources) { |
| 93 | + // Extract source file |
| 94 | + task.addSource(wyc.Compiler.readWyilFile(wyildir, source)); |
| 95 | + } |
| 96 | + for(File include : includes) { |
| 97 | + FileInputStream fin = new FileInputStream(include); |
| 98 | + JavaScriptFile jsf = readJavaScriptFile(fin); |
| 99 | + fin.close(); |
| 100 | + task.addInclude(jsf); |
| 101 | + } |
| 102 | + JavaScriptFile target = task.run(); |
| 103 | + // Write out binary target |
| 104 | + wyjs.Main.writeJavaScriptFile(this.target, target, jsdir); |
| 105 | + // Unsure how it can fail! |
| 106 | + return true; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Command-line options |
| 111 | + */ |
| 112 | + private static final OptArg[] OPTIONS = { |
| 113 | + // Standard options |
| 114 | + new OptArg("verbose","v","set verbose output"), |
| 115 | + new OptArg("standard","s",OptArg.STRING,"set JavaScript standard","ES6"), |
| 116 | + new OptArg("output","o",OptArg.STRING,"set output file","main"), |
| 117 | + new OptArg("wyildir", OptArg.FILEDIR, "Specify where to place binary (WyIL) files", new File(".")), |
| 118 | + new OptArg("jsdir", OptArg.FILEDIR, "Specify where to place JavaScript files", new File(".")) |
| 119 | + }; |
| 120 | + // |
| 121 | + public static void main(String[] _args) throws IOException { |
| 122 | + List<String> args = new ArrayList<>(Arrays.asList(_args)); |
| 123 | + Map<String, Object> options = OptArg.parseOptions(args, OPTIONS); |
| 124 | + // |
| 125 | + File wyildir = (File) options.get("wyildir"); |
| 126 | + File jsdir = (File) options.get("jsdir"); |
| 127 | + Trie target = Trie.fromString((String) options.get("output")); |
| 128 | + Standard standard = Standard.valueOf((String) options.get("standard")); |
| 129 | + // Construct Main object |
| 130 | + Main main = new Main().setStandard(standard).setWyilDir(wyildir).setJsDir(jsdir).setTarget(target); |
| 131 | + // Add source files |
| 132 | + for (String s : args) { |
| 133 | + main.addSource(Trie.fromString(s)); |
| 134 | + } |
| 135 | + // Run the compiler! |
| 136 | + boolean result = main.run(); |
| 137 | + // Produce exit code |
| 138 | + System.exit(result ? 0 : 1); |
| 139 | + } |
| 140 | + |
| 141 | + public JavaScriptFile readJavaScriptFile(InputStream inputStream) throws IOException { |
| 142 | + // NOTE: this is strictly a hack at this time as its unclear what the best |
| 143 | + // alternative option is. Specifically, parsing JavaScriptFiles is not something |
| 144 | + // I'm contemplating right now :) |
| 145 | + Reader reader = new InputStreamReader(inputStream); |
| 146 | + BufferedReader in = new BufferedReader(reader); |
| 147 | + |
| 148 | + StringBuilder text = new StringBuilder(); |
| 149 | + int len = 0; |
| 150 | + char[] buf = new char[1024]; |
| 151 | + while ((len = in.read(buf)) != -1) { |
| 152 | + text.append(buf, 0, len); |
| 153 | + } |
| 154 | + // Finally, construct the native declaration |
| 155 | + NativeDeclaration d = new NativeDeclaration(text.toString()); |
| 156 | + // |
| 157 | + JavaScriptFile js = new JavaScriptFile(true, Standard.ES6); |
| 158 | + // Append our native declarations. |
| 159 | + js.getDeclarations().add(d); |
| 160 | + return js; |
| 161 | + } |
51 | 162 |
|
52 | 163 | /** |
53 | 164 | * Write a given WyilFile to disk using the given directory as a root. |
|
0 commit comments