Repository navigation
Expand file tree
/
Copy pathcli.ts
More file actions
47 lines (42 loc) · 1.42 KB
/
Copy pathcli.ts
File metadata and controls
47 lines (42 loc) · 1.42 KB
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
import { Command } from "commander";
import { version } from "../package.json";
import MiniDecaf = require("./minidecaf");
import fs from "fs";
const program = new Command();
program
.version(version)
.name("minidecaf")
.arguments("<input_file>")
.action((file) => {
program.input = file;
})
.option("-s, --asm", "generate the RISC-V assembly code instead of executing")
.option("-r, --ir", "generate the intermediate representation (IR)")
.option("-t, --timeout <second>", "set execution timeout (in seconds)")
.option("-o, --output <output_file>", "save the output to file")
.option("-c, --trunc", "truncate the return code to 8-bit integer")
.option("-d, --debug", "debug mode");
program.parse(process.argv);
let input = fs.readFileSync(program.input).toString();
let option: MiniDecaf.CompilerOption = {
target: program.asm
? MiniDecaf.CompilerTarget.Riscv32Asm
: program.ir
? MiniDecaf.CompilerTarget.Ir
: MiniDecaf.CompilerTarget.Executed,
truncateReturnCode: program.trunc,
};
if (program.timeout) {
let t = parseInt(program.timeout);
if (t > 0) option.timeoutSecond = t;
}
try {
let output = MiniDecaf.compile(input, option).toString();
if (program.output) {
fs.writeFileSync(program.output, output);
} else {
console.log(output);
}
} catch (err) {
console.error(program.debug ? err.stack : err.message);
}