Skip to content

Commit

Permalink
Remove need for prompt sync
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver committed Sep 4, 2021
1 parent ab02e2d commit 808d35c
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 88 deletions.
32 changes: 3 additions & 29 deletions package-lock.json

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

7 changes: 1 addition & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,12 @@
"devDependencies": {
"@ava/typescript": "^1.1.1",
"@types/node": "^15.6.2",
"@types/prompt-sync": "^4.1.0",
"@types/prompt-sync-history": "^1.0.1",
"ava": "^3.15.0",
"prettier": "^2.3.0",
"ts-node": "^10.0.0",
"typescript": "^4.3.2"
},
"dependencies": {
"prompt-sync": "^4.2.0",
"prompt-sync-history": "^1.0.1"
},
"dependencies": {},
"ava": {
"extensions": {
"ts": "module"
Expand Down
66 changes: 38 additions & 28 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env node
import PromptSync from "prompt-sync";
import History from "prompt-sync-history";
import { createInterface, ReadLine } from "readline";

import { handleError, redText } from "./libzug/builtins/imports";
// @ts-ignore
Expand All @@ -12,42 +11,53 @@ import { LErr, lvalRead } from "./libzug/lval";
main();

function main() {
const history = History(".zug-history", 1000);
const promptSync = PromptSync({
sigint: false,
// @ts-ignore (seems to be misisng from typing)
eot: true,
history: history,
// Set up line reader
const readline = createInterface({
input: process.stdin,
output: process.stdout,
terminal: true,
});
readline.setPrompt("zug> ");

const interpreter = new Interpreter();

let res = loadFile(interpreter.env, "prelude", true);
if (res instanceof LErr) {
throw new Error("Failed to load prelude: " + res.s);
}

console.log("zug version 0.2.0");
console.log("press ctrl+d to exit");

while (true) {
const line = promptSync("zug> ");
if (!line) {
continue;
}

try {
var tree = parser.parse(line);
} catch (err) {
handleError(err, line);
continue;
readline.prompt();

readline.on("line", (line) => {
if (line) {
try {
var tree = parser.parse(line);
} catch (err) {
handleError(err, line);
readline.prompt();
return;
}

let result = interpreter.interpret(lvalRead(tree));
if (result.type === "ERROR") {
console.log(redText(result.print()));
} else {
console.log(result.print());
}
}
readline.prompt();
});

let result = interpreter.interpret(lvalRead(tree));
if (result.type === "ERROR") {
console.log(redText(result.print()));
} else {
console.log(result.print());
}
}
readline.on("SIGINT", () => {
// @ts-ignore
readline.clearLine();
readline.prompt();
});

history.save();
readline.on("close", () => {
console.log("goodbye!");
process.exit(0);
});
}
14 changes: 7 additions & 7 deletions src/libzug/builtins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { def, doc, lambda, let_, set } from "./defn";
import { import_ } from "./imports";
import { Builtin, LCallable, LObject } from "../lval";
import { getattr, keys, object, values } from "./object";
import { env as getenv, exit, input, print } from "./sys";
import { env as getenv, exit, print } from "./sys";
import { list, head, tail, join, evaluate } from "./array";

export default function seedEnv(env?: LObject): LObject {
Expand Down Expand Up @@ -165,12 +165,12 @@ export default function seedEnv(env?: LObject): LObject {
print,
"print [..strings]\n" + "Print the strings to stdout."
);
newBuiltin(
env,
"input",
input,
"input [string?]\n" + "Receive input from stdin, possibly with a prompt string."
);
// newBuiltin(
// env,
// "input",
// input,
// "input [string?]\n" + "Receive input from stdin, possibly with a prompt string."
// );
newBuiltin(
env,
"import",
Expand Down
35 changes: 17 additions & 18 deletions src/libzug/builtins/sys.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
import PromptSync from "prompt-sync";
import { LVal, LObject, LErr, LNum, NIL, LNil, LString } from "../lval";

export function print(env: LObject, args: Array<LVal>): LNil {
console.log(args.map((a) => a.print()).join(" "));
return NIL;
}

export function input(env: LObject, args: Array<LVal>): LVal {

let prompt: string;
if (args.length === 0) {
prompt = "";
} else if (args.length === 1) {
const arg = args[0];
if (!(arg instanceof LString)) {
return new LErr(`TypeError: input takes STRING, not ${arg.type}`);
}
prompt = arg.s;
} else {
return new LErr(`TypeError: input takes at most one argument. Received ${args.length}`)
}

return new LString(PromptSync()(prompt))
}
// export function input(env: LObject, args: Array<LVal>): LVal {
//
// let prompt: string;
// if (args.length === 0) {
// prompt = "";
// } else if (args.length === 1) {
// const arg = args[0];
// if (!(arg instanceof LString)) {
// return new LErr(`TypeError: input takes STRING, not ${arg.type}`);
// }
// prompt = arg.s;
// } else {
// return new LErr(`TypeError: input takes at most one argument. Received ${args.length}`)
// }
//
// return new LString(PromptSync()(prompt))
// }


export function env(env: LObject, args: Array<LVal>) {
Expand Down

0 comments on commit 808d35c

Please sign in to comment.