Skip to content

Commit

Permalink
use readline better
Browse files Browse the repository at this point in the history
  • Loading branch information
danvk committed Jan 17, 2024
1 parent c7ad7c4 commit 522031c
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,23 @@ export function resetErrors() {
hadRuntimeError = false;
}

export async function runPrompt(interpreter: Interpreter) {
process.stdout.write("> ");
for await (const line of createInterface({ input: process.stdin })) {
export function runPrompt(interpreter: Interpreter) {
const rl = createInterface({
input: process.stdin,
output: process.stdout,
prompt: "> ",
});
rl.prompt();
rl.on("line", (line) => {
const expr = maybeParseAsExpression(line);
if (expr) {
console.log(stringify(interpreter.evaluate(expr)));
} else {
run(interpreter, line);
}
resetErrors();
process.stdout.write("> ");
}
rl.prompt();
});
}

let hadError = false;
Expand Down Expand Up @@ -111,6 +116,6 @@ export async function main() {
if (args.length == 1) {
await runFile(interpreter, args[0]);
} else {
await runPrompt(interpreter);
runPrompt(interpreter);
}
}

0 comments on commit 522031c

Please sign in to comment.