Skip to content

Commit

Permalink
initial pass at TS in REPL
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Nov 7, 2018
1 parent d67733c commit d8050a6
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
25 changes: 25 additions & 0 deletions js/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,31 @@ export class DenoCompiler
return moduleMetaData.outputCode;
}

/**
* TODO:
* Incremental compilation of code used in REPL.
*/
incrementalCompile(
sourceCode: SourceCode,
previousOutput?: OutputCode
): { diagnostics: ts.Diagnostic[], outputCode: OutputCode, additionalCode: OutputCode } {

console.log('incrementalCompile', sourceCode, previousOutput);

// const service = this._service;

// TODO:
// 1. transpile code to JS
// 2. diff code with 'previousOutput'
// 3. return new lines and diagnostics

return {
diagnostics: [],
outputCode: sourceCode,
additionalCode: '',
};
}

/** Given a fileName, return what was generated by the compiler. */
getGeneratedContents = (fileName: string): string | RawSourceMap => {
this._log("compiler.getGeneratedContents", fileName);
Expand Down
2 changes: 1 addition & 1 deletion js/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,6 @@ export default function denoMain() {
if (inputFn) {
compiler.run(inputFn, `${cwd}/`);
} else {
replLoop();
replLoop(`${cwd}/`);
}
}
34 changes: 33 additions & 1 deletion js/repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { close } from "./files";
import * as dispatch from "./dispatch";
import { exit } from "./os";
import { window } from "./globals";
import { DenoCompiler } from "./compiler";

function startRepl(historyFile: string): number {
const builder = flatbuffers.createBuilder();
Expand Down Expand Up @@ -46,6 +47,21 @@ export function readline(rid: number, prompt: string): string {
return line || "";
}


interface ReplContext {
lines: string[];
previousOutput: string;
}

/**
* Eval helpers.
*/
// const EVAL_FILENAME = `[eval].ts`
const REPL_CONTEXT: ReplContext = {
lines: [],
previousOutput: '',
};

// @internal
export function replLoop(): void {
window.deno = deno; // FIXME use a new scope (rather than window).
Expand Down Expand Up @@ -78,8 +94,17 @@ export function replLoop(): void {

function evaluate(code: string): void {
try {
const result = eval.call(window, code); // FIXME use a new scope.
// TODO:
// 1. preserve context between calls
// 2. run only incremental lines, not full output
// 3. output diagnostics
REPL_CONTEXT.lines.push(code);
const compiledCode = compileReplCode(REPL_CONTEXT);
console.log('compiledCode', compiledCode);

const result = eval.call(window, compiledCode.outputCode); // FIXME use a new scope.
console.log(result);
REPL_CONTEXT.previousOutput = compiledCode.outputCode;
} catch (err) {
if (err instanceof Error) {
console.error(`${err.constructor.name}: ${err.message}`);
Expand All @@ -89,6 +114,13 @@ function evaluate(code: string): void {
}
}

const compiler = DenoCompiler.instance();

function compileReplCode(context: ReplContext) {
const sourceCode = context.lines.join('\n');
return compiler.incrementalCompile(sourceCode, context.previousOutput);
}

function readBlock(
rid: number,
prompt: string,
Expand Down

0 comments on commit d8050a6

Please sign in to comment.