Skip to content

Commit

Permalink
run cli without compiling, improve cli input, fix bitwise and, fix la…
Browse files Browse the repository at this point in the history
…st line not read
  • Loading branch information
voliva committed Oct 5, 2024
1 parent 7d2e4de commit 42abedc
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 12 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"build-debug": "tsc -b && vite build --minify false",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview",
"cli": "node dist/cli.js"
"cli": "bun src/cli.ts"
},
"dependencies": {
"@commander-js/extra-typings": "^12.1.0",
"bun": "^1.1.29",
"commander": "^12.1.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
Expand Down
84 changes: 84 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ program
await execute(loadProgram(parseWhitespaceProgram(program)), {
input: callbackInput(() => {
return new Promise<string>((resolve) =>
process.stdin.once("data", (v) => resolve(v.toString()))
process.stdin.once("data", (v) => {
const value = v.toString();
if (value === "\r\n" || value === "\n") {
resolve("\n");
}
resolve(value.replace(/\r?\n$/, ""));
})
);
}),
output: callbackOutput((v) => process.stdout.write(v)),
Expand Down
2 changes: 0 additions & 2 deletions src/wsa/lib/bitwise.wsa
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ label bitwise_and
retrieve
div 2
dup
jumpz bitwise_and_end
dup
add 1
jumpz bitwise_and_end
; [&a, &b, r, p, &b, b]
Expand Down
15 changes: 7 additions & 8 deletions src/wsa/wsa.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import lib_io from "./lib/io.wsa?raw";
import lib_memory from "./lib/memory.wsa?raw";
import lib_bitwise from "./lib/bitwise.wsa?raw";
import lib_bitwise_extensions from "./lib/bitwise.extensions.wsa?raw";
import lib_math from "./lib/math.wsa?raw";
import lib_io from "./lib/io.wsa?raw" with { type: "text" };
import lib_memory from "./lib/memory.wsa?raw" with { type: "text" };
import lib_bitwise from "./lib/bitwise.wsa?raw" with { type: "text" };
import lib_bitwise_extensions from "./lib/bitwise.extensions.wsa?raw" with { type: "text" };
import lib_math from "./lib/math.wsa?raw" with { type: "text" };

type Opcode =
| { params: "none"; constr: () => string }
Expand Down Expand Up @@ -237,7 +237,6 @@ async function include(
if (includedFiles.has(filename)) return "";
includedFiles.add(filename);

console.log("include", filename);
if (filename === "io") {
return compile(stringToLineStream(lib_io), getIncludedStream);
}
Expand Down Expand Up @@ -391,7 +390,7 @@ function unescape(stringLiteral: string, quote: string) {

function parseArgs(opcode: string, args: Token[]): string {
if (!(opcode in opcodes)) {
throw "invalid opcode";
throw new Error(`invalid opcode ${opcode}`);
}
const op = opcodes[opcode];
let arg;
Expand Down Expand Up @@ -525,7 +524,7 @@ export async function compile(

let prevInclude: Promise<string> = Promise.resolve("");
inputStream((line) => {
if (!line) {
if (line == null) {
onEnd();
return;
}
Expand Down

0 comments on commit 42abedc

Please sign in to comment.