-
Notifications
You must be signed in to change notification settings - Fork 0
/
bf.mjs
executable file
·31 lines (26 loc) · 907 Bytes
/
bf.mjs
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
#!/usr/bin/env node
import fs from "node:fs/promises";
import {brainfuck} from "./lib/brainfuck.mjs";
async function readfile(pathname, encoding) {
// NB: Frankly, I think it's *shocking* that fs.readFile can't read from a
// ReadableStream.
if(pathname === undefined || pathname === "" || pathname === "-") {
let source = "";
for await(const chunk of process.stdin) {
source += chunk.toString(encoding);
}
return source;
}
else {
return await fs.readFile(pathname, encoding);
}
}
const input = process.argv[2]?.match(/[1-9][0-9]*/g) ?? [];
for(let i = 0; i < input.length; i++) { input[i] = +input[i]; }
const pathname = process.argv[3];
let source = await readfile(pathname, "ascii");
if(source.startsWith("#!")) {
source = source.slice(source.indexOf("\n") + 1);
}
const {output} = brainfuck(source, input, true);
console.log("Output: %s", output.join(" "));