-
-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathgrain.js
executable file
·230 lines (206 loc) · 6.88 KB
/
grain.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env node
const commander = require("commander");
const exec = require("./exec.js");
const pkgJson = require("../package.json");
const stdlibPath = require("@grain/stdlib");
function list(val) {
return val.split(",");
}
function num(val) {
return Number.parseInt(val, 10);
}
class ForwardOption extends commander.Option {
// A ForwardOption is forwarded to the underlying program
forward = true;
toFlag(opts) {
const value = opts[this.attributeName()];
if (value instanceof Array && value.length > 0) {
return `${this.long || this.short} ${value.join(",")}`;
} else if (typeof value === "string" || typeof value === "number") {
return `${this.long || this.short} ${value}`;
} else if (
(this.negate && value === false) ||
(!this.negate && value === true)
) {
return this.long || this.short;
}
}
}
class ProfileOption extends commander.Option {
// Like ForwardOption, ProfileOption is forwarded to the underlying program
// but we convert the flag into a profile flag, i.e. `--release` becomes `--profile=release`
forward = true;
toFlag(opts) {
const attribute = this.attributeName();
if (opts[attribute]) {
return `--profile=${attribute}`;
}
}
}
class GrainHelp extends commander.Help {
visibleOptions(cmd) {
// If we are running `--help` at the root, we want to list options for `compile-and-run`
if (cmd.name() === "grain") {
return super.visibleOptions(
cmd.commands.find((command) => command.name() === "compile-and-run")
);
}
return super.visibleOptions(cmd);
}
}
const optionApplicator = (Option) =>
function (flags, description, parser, defaultValue) {
const option = new Option(flags, description);
if (parser) option.argParser(parser);
if (typeof defaultValue !== "undefined") option.default(defaultValue);
return this.addOption(option);
};
class GrainCommand extends commander.Command {
// Adds .forwardOption to commands. Similar to Commander's native .option,
// but will forward the flag to the underlying program.
forwardOption = optionApplicator(ForwardOption);
// Adds .profileOption to commands. Similar to Commander's native .option,
// but will convert the flag from the shorthand to the full form.
profileOption = optionApplicator(ProfileOption);
createHelp() {
return new GrainHelp();
}
createCommand(name) {
const cmd = new GrainCommand(name);
// Add global options to command
cmd.forwardOption(
"-I, --include-dirs <dirs>",
"add additional dependency include directories",
list,
[]
);
cmd.forwardOption(
"-S, --stdlib <path>",
"override the standard library with your own",
null,
stdlibPath
);
cmd.forwardOption(
"--initial-memory-pages <size>",
"initial number of WebAssembly memory pages",
num
);
cmd.forwardOption(
"--maximum-memory-pages <size>",
"maximum number of WebAssembly memory pages",
num
);
cmd.forwardOption("--import-memory", "import the memory from `env.memory`");
cmd.option("--dir <dir...>", "directory to preopen");
cmd.option("--env <env...>", "WASI environment variables");
cmd.forwardOption(
"--elide-type-info",
"don't include runtime type information used by toString/print"
);
cmd.profileOption(
"--release",
"compile using the release profile (production mode)"
);
cmd.forwardOption("--no-wasm-tail-call", "disables tail-call optimization");
cmd.forwardOption("--debug", "compile with debugging information");
cmd.forwardOption(
"--wat",
"additionally produce a WebAssembly Text (.wat) file"
);
cmd.forwardOption(
"--hide-locs",
"hide locations from intermediate trees. Only has an effect with `--verbose`"
);
cmd.forwardOption("--no-color", "disable colored output");
cmd.forwardOption(
"--no-gc",
"turn off reference counting garbage collection"
);
cmd.forwardOption(
"--no-bulk-memory",
"polyfill WebAssembly bulk memory instructions"
);
cmd.forwardOption(
"--wasi-polyfill <filename>",
"path to custom WASI implementation"
);
cmd.forwardOption(
"--no-pervasives",
"don't automatically import the Grain Pervasives module"
);
cmd.forwardOption(
"--memory-base <addr>",
"set the base address for the Grain heap"
);
cmd.forwardOption("--source-map", "generate source maps");
cmd.forwardOption("--strict-sequence", "enable strict sequencing");
cmd.forwardOption(
"--verbose",
"print critical information at various stages of compilation"
);
cmd.forwardOption(
"--ignore-warnings <warnings>",
"compiler warnings to ignore",
list,
[]
);
return cmd;
}
}
let endOptsI = process.argv.findIndex((x) => x === "--");
if (endOptsI === -1) {
endOptsI = Infinity;
}
const argsToProcess = process.argv.slice(0, endOptsI);
const unprocessedArgs = process.argv.slice(endOptsI + 1);
const program = new GrainCommand();
program
.description("Compile and run Grain programs. 🌾")
// Show the default usage without "compile-and-run"
.usage("[options] <file>")
.addHelpCommand(false)
// The default command that compiles & runs
.command("compile-and-run <file>", { isDefault: true, hidden: true })
// `--version` should only be available on the default command
.version(pkgJson.version, "-v, --version", "output the current version")
.forwardOption("-o <filename>", "output filename")
.action(function (file, options, program) {
const success = exec.grainc(file, options, program);
if (success) {
const outFile = options.o ?? file.replace(/\.gr$/, ".gr.wasm");
exec.grainrun(unprocessedArgs, outFile, options, program);
}
});
program
.command("compile <file>")
.description("compile a grain program into wasm")
.forwardOption("-o <filename>", "output filename")
.forwardOption(
"--use-start-section",
"replaces the _start export with a start section during linking"
)
.forwardOption("--no-link", "disable static linking")
.action(exec.grainc);
program
.command("run <file>")
.description("run a wasm file via grain's WASI runner")
.action((...args) => exec.grainrun(unprocessedArgs, ...args));
program
.command("lsp")
.description("start the Grain LSP server")
.action(exec.grainlsp);
program
.command("doc <file|dir>")
.description("generate documentation for a grain file")
.forwardOption(
"--current-version <version>",
"provide a version to use as current when generating markdown for `@since` and `@history` attributes"
)
.forwardOption("-o <file|dir>", "output file or directory")
.action(exec.graindoc);
program
.command("format <file|dir>")
.description("format a grain file")
.forwardOption("-o <file|dir>", "output file or directory")
.action(exec.grainformat);
program.parse(argsToProcess);