2018-11-05 12:55:59 -05:00
|
|
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
|
|
|
import * as msg from "gen/msg_generated";
|
|
|
|
import * as flatbuffers from "./flatbuffers";
|
|
|
|
import { assert } from "./util";
|
|
|
|
import * as deno from "./deno";
|
|
|
|
import { close } from "./files";
|
|
|
|
import * as dispatch from "./dispatch";
|
|
|
|
import { exit } from "./os";
|
2018-11-08 19:09:18 -05:00
|
|
|
import { globalEval } from "./global_eval";
|
|
|
|
|
|
|
|
const window = globalEval("this");
|
2018-11-05 12:55:59 -05:00
|
|
|
|
|
|
|
function startRepl(historyFile: string): number {
|
|
|
|
const builder = flatbuffers.createBuilder();
|
|
|
|
const historyFile_ = builder.createString(historyFile);
|
|
|
|
|
|
|
|
msg.ReplStart.startReplStart(builder);
|
|
|
|
msg.ReplStart.addHistoryFile(builder, historyFile_);
|
|
|
|
const inner = msg.ReplStart.endReplStart(builder);
|
|
|
|
|
|
|
|
const baseRes = dispatch.sendSync(builder, msg.Any.ReplStart, inner);
|
|
|
|
assert(baseRes != null);
|
|
|
|
assert(msg.Any.ReplStartRes === baseRes!.innerType());
|
|
|
|
const innerRes = new msg.ReplStartRes();
|
|
|
|
assert(baseRes!.inner(innerRes) != null);
|
|
|
|
const rid = innerRes.rid();
|
|
|
|
return rid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// @internal
|
2018-11-28 04:07:22 -05:00
|
|
|
export async function readline(rid: number, prompt: string): Promise<string> {
|
2018-11-05 12:55:59 -05:00
|
|
|
const builder = flatbuffers.createBuilder();
|
|
|
|
const prompt_ = builder.createString(prompt);
|
|
|
|
msg.ReplReadline.startReplReadline(builder);
|
|
|
|
msg.ReplReadline.addRid(builder, rid);
|
|
|
|
msg.ReplReadline.addPrompt(builder, prompt_);
|
|
|
|
const inner = msg.ReplReadline.endReplReadline(builder);
|
|
|
|
|
2018-11-28 04:07:22 -05:00
|
|
|
const baseRes = await dispatch.sendAsync(
|
|
|
|
builder,
|
|
|
|
msg.Any.ReplReadline,
|
|
|
|
inner
|
|
|
|
);
|
2018-11-05 12:55:59 -05:00
|
|
|
|
|
|
|
assert(baseRes != null);
|
|
|
|
assert(msg.Any.ReplReadlineRes === baseRes!.innerType());
|
|
|
|
const innerRes = new msg.ReplReadlineRes();
|
|
|
|
assert(baseRes!.inner(innerRes) != null);
|
|
|
|
const line = innerRes.line();
|
|
|
|
assert(line !== null);
|
|
|
|
return line || "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// @internal
|
2018-11-28 04:07:22 -05:00
|
|
|
export async function replLoop(): Promise<void> {
|
2018-11-05 12:55:59 -05:00
|
|
|
window.deno = deno; // FIXME use a new scope (rather than window).
|
|
|
|
|
|
|
|
const historyFile = "deno_history.txt";
|
|
|
|
const rid = startRepl(historyFile);
|
|
|
|
|
2018-11-06 14:19:16 -05:00
|
|
|
let code = "";
|
2018-11-05 12:55:59 -05:00
|
|
|
while (true) {
|
|
|
|
try {
|
2018-11-28 04:07:22 -05:00
|
|
|
code = await readBlock(rid, "> ", " ");
|
2018-11-05 12:55:59 -05:00
|
|
|
} catch (err) {
|
|
|
|
if (err.message === "EOF") {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
console.error(err);
|
|
|
|
exit(1);
|
|
|
|
}
|
2018-11-06 14:19:16 -05:00
|
|
|
if (!code) {
|
2018-11-05 12:55:59 -05:00
|
|
|
continue;
|
2018-11-06 14:19:16 -05:00
|
|
|
} else if (code.trim() === ".exit") {
|
2018-11-05 12:55:59 -05:00
|
|
|
break;
|
|
|
|
}
|
2018-11-06 14:19:16 -05:00
|
|
|
|
|
|
|
evaluate(code);
|
2018-11-05 12:55:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
close(rid);
|
|
|
|
}
|
2018-11-06 14:19:16 -05:00
|
|
|
|
|
|
|
function evaluate(code: string): void {
|
|
|
|
try {
|
|
|
|
const result = eval.call(window, code); // FIXME use a new scope.
|
|
|
|
console.log(result);
|
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof Error) {
|
|
|
|
console.error(`${err.constructor.name}: ${err.message}`);
|
|
|
|
} else {
|
|
|
|
console.error("Thrown:", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-28 04:07:22 -05:00
|
|
|
async function readBlock(
|
2018-11-06 14:19:16 -05:00
|
|
|
rid: number,
|
|
|
|
prompt: string,
|
|
|
|
continuedPrompt: string
|
2018-11-28 04:07:22 -05:00
|
|
|
): Promise<string> {
|
2018-11-06 14:19:16 -05:00
|
|
|
let code = "";
|
|
|
|
do {
|
2018-11-28 04:07:22 -05:00
|
|
|
code += await readline(rid, prompt);
|
2018-11-06 14:19:16 -05:00
|
|
|
prompt = continuedPrompt;
|
|
|
|
} while (parenthesesAreOpen(code));
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
// modified from
|
|
|
|
// https://codereview.stackexchange.com/a/46039/148556
|
|
|
|
function parenthesesAreOpen(code: string): boolean {
|
|
|
|
const parentheses = "[]{}()";
|
|
|
|
const stack = [];
|
|
|
|
|
|
|
|
for (const ch of code) {
|
|
|
|
const bracePosition = parentheses.indexOf(ch);
|
|
|
|
|
|
|
|
if (bracePosition === -1) {
|
|
|
|
// not a paren
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bracePosition % 2 === 0) {
|
|
|
|
stack.push(bracePosition + 1); // push next expected brace position
|
|
|
|
} else {
|
|
|
|
if (stack.length === 0 || stack.pop() !== bracePosition) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return stack.length > 0;
|
|
|
|
}
|