import type { TracingLogger } from './utils'; import * as readline from 'node:readline/promises'; import { stdin as input, stdout as output } from 'node:process'; import { peggyParse } from './parser'; import { evaluate } from './interpreter'; // cool asci logo for CPS const LOGO = `\x1b[33m simponic's _______ ________ ______ _______ __ / \\ / | / \\ / \\ / | $$$$$$$ |$$$$$$$$/ /$$$$$$ |$$$$$$$ |$$ | $$ |__$$ |$$ |__ $$ | $$/ $$ |__$$ |$$ | $$ $$< $$ | $$ | $$ $$/ $$ | $$$$$$$ |$$$$$/ $$ | __ $$$$$$$/ $$ | $$ | $$ |$$ |_____ $$ \\__/ |$$ | $$ |_____ $$ | $$ |$$ |$$ $$/ $$ | $$ | $$/ $$/ $$$$$$$$/ $$$$$$/ $$/ $$$$$$$$/ \x1b[0m`; const HELP = ` This is the CPS REPL. You can enter CPS programs and view the result of evaluating them. This REPL supports multi-line input. To end a multi-line input, enter an empty line. Commands: help - Show this message exit - Exit the REPL About: Read "Compiling With Continuations" by Andrew W. Appel for more information about this Intermediate Representation. Example: \x1b[34m ~>\x1b[0m\x1b[32m PRIMOP(+, [INT 1, INT 2], [result], [ | APP(LABEL id, [VAR result]) | ])\x1b[0m `; export const doRepl = async ( logger: TracingLogger, prompt = 0, rl = readline.createInterface({ input, output }), ): Promise => { if (prompt === 0) { logger.info( 'welcome to recpl (read eval continue print loop) ʕ•́ᴥ•̀ʔっ' + LOGO, ); } const promptString = `[ ${prompt} ] ~> `; const coloredPrompt = `\x1b[0m[\x1b[33m ${prompt} \x1b[0m]\x1b[34m ~> \x1b[0m\x1b[32m`; const lines: string[] = [await rl.question(coloredPrompt)]; while (lines.at(-1)) { const line = lines.at(-1)!; if (lines.length === 1 && line === 'help') { process.stdout.write('\x1b[0m'); logger.info(HELP); return doRepl(logger, prompt + 1, rl); } if (line === 'exit') { process.stdout.write('\x1b[0m'); logger.info('Exiting REPL...'); rl.close(); return; } lines.push( await rl.question(`|`.padStart(promptString.length - 1, ' ') + ' '), ); } const program = lines.slice(0, -1).join('\n'); process.stdout.write('\x1b[0m'); try { const ast = peggyParse(program); logger.debug('AST: ' + JSON.stringify(ast, null, 2)); const result = await evaluate(ast, logger.createChild('evaluate')); logger.info( 'Result: \n\x1b[36m' + JSON.stringify(result, null, 2) + '\x1b[0m\n', ); } catch (e) { logger.error(e!.toString() + '\n'); } return doRepl(logger, prompt + 1, rl); };