colored repl

This commit is contained in:
Lizzy Hunt 2024-02-28 15:18:38 -07:00
parent 55c00566b0
commit 3d3942a80f
No known key found for this signature in database
GPG Key ID: E835BD4B08CCAF96
3 changed files with 20 additions and 10 deletions

View File

@ -2,10 +2,12 @@ const argv = require('minimist')(process.argv.slice(2));
export type Args = { export type Args = {
devMode: boolean; devMode: boolean;
debug: boolean;
repl: boolean; repl: boolean;
}; };
export const args: Args = { export const args: Args = {
repl: argv.repl ?? false, repl: argv.repl ?? false,
devMode: argv.dev ?? false, devMode: argv.dev ?? false,
debug: argv.debug ?? false,
}; };

View File

@ -31,7 +31,7 @@ const devMode = async (logger: TracingLogger) => {
}; };
export const main = async (args: Args) => { export const main = async (args: Args) => {
if (args.devMode) { if (args.devMode || args.debug) {
LOG_LEVELS.push('debug'); LOG_LEVELS.push('debug');
} }

View File

@ -5,7 +5,8 @@ import { peggyParse } from './parser';
import { evaluate } from './interpreter'; import { evaluate } from './interpreter';
// cool asci logo for CPS // cool asci logo for CPS
const LOGO = ` const LOGO = `\x1b[33m
simponic's
_______ ________ ______ _______ __ _______ ________ ______ _______ __
/ \\ / | / \\ / \\ / | / \\ / | / \\ / \\ / |
$$$$$$$ |$$$$$$$$/ /$$$$$$ |$$$$$$$ |$$ | $$$$$$$ |$$$$$$$$/ /$$$$$$ |$$$$$$$ |$$ |
@ -15,11 +16,10 @@ $$$$$$$ |$$$$$/ $$ | __ $$$$$$$/ $$ |
$$ | $$ |$$ |_____ $$ \\__/ |$$ | $$ |_____ $$ | $$ |$$ |_____ $$ \\__/ |$$ | $$ |_____
$$ | $$ |$$ |$$ $$/ $$ | $$ | $$ | $$ |$$ |$$ $$/ $$ | $$ |
$$/ $$/ $$$$$$$$/ $$$$$$/ $$/ $$$$$$$$/ $$/ $$/ $$$$$$$$/ $$$$$$/ $$/ $$$$$$$$/
\x1b[0m`;
`;
const HELP = ` const HELP = `
This is the CPS REPL. You can enter CPS programs and see the result of evaluating them. 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. This REPL supports multi-line input. To end a multi-line input, enter an empty line.
Commands: Commands:
@ -31,9 +31,9 @@ const HELP = `
this Intermediate Representation. this Intermediate Representation.
Example: Example:
~> PRIMOP(+, [INT 1, INT 2], [result], [ \x1b[34m ~>\x1b[0m\x1b[32m PRIMOP(+, [INT 1, INT 2], [result], [
| APP(LABEL id, [VAR result]) | APP(LABEL id, [VAR result])
| ]) | ])\x1b[0m
`; `;
export const doRepl = async ( export const doRepl = async (
@ -42,19 +42,24 @@ export const doRepl = async (
rl = readline.createInterface({ input, output }), rl = readline.createInterface({ input, output }),
): Promise<any> => { ): Promise<any> => {
if (prompt === 0) { if (prompt === 0) {
logger.info('welcome to recpl (read eval continue print loop) :)' + LOGO); logger.info(
'welcome to recpl (read eval continue print loop) ʕ•́ᴥ•̀ʔっ' + LOGO,
);
} }
const promptString = `[ ${prompt} ] ~> `; const promptString = `[ ${prompt} ] ~> `;
const lines: string[] = [await rl.question(promptString)]; 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)) { while (lines.at(-1)) {
const line = lines.at(-1)!; const line = lines.at(-1)!;
if (lines.length === 1 && line === 'help') { if (lines.length === 1 && line === 'help') {
process.stdout.write('\x1b[0m');
logger.info(HELP); logger.info(HELP);
return doRepl(logger, prompt + 1, rl); return doRepl(logger, prompt + 1, rl);
} }
if (line === 'exit') { if (line === 'exit') {
process.stdout.write('\x1b[0m');
logger.info('Exiting REPL...'); logger.info('Exiting REPL...');
rl.close(); rl.close();
return; return;
@ -66,12 +71,15 @@ export const doRepl = async (
} }
const program = lines.slice(0, -1).join('\n'); const program = lines.slice(0, -1).join('\n');
process.stdout.write('\x1b[0m');
try { try {
const ast = peggyParse(program); const ast = peggyParse(program);
logger.debug('AST: ' + JSON.stringify(ast, null, 2)); logger.debug('AST: ' + JSON.stringify(ast, null, 2));
const result = await evaluate(ast, logger.createChild('evaluate')); const result = await evaluate(ast, logger.createChild('evaluate'));
logger.info('Result: ' + JSON.stringify(result, null, 2) + '\n'); logger.info(
'Result: \n\x1b[36m' + JSON.stringify(result, null, 2) + '\x1b[0m\n',
);
} catch (e) { } catch (e) {
logger.error(e!.toString() + '\n'); logger.error(e!.toString() + '\n');
} }