UNPKG

@tsukiroku/tiny

Version:
118 lines (117 loc) 4.75 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const prompt_sync_1 = __importDefault(require("prompt-sync")); const colors_1 = __importDefault(require("colors")); const Tiny = __importStar(require("../../index")); const defaultFilename = '<REPL>'; class default_1 { env; option; promptSync = (0, prompt_sync_1.default)({ sigint: true }); mode = 'repl'; constructor(env, option) { this.env = env; this.option = option; colors_1.default.enabled = true; } executeCommand(input, lexer, parsed, env) { const [command, ...args] = input.split(' '); const commands = new Map([ [ '//mode', (...args) => { if (!['repl', 'parser', 'lexer', 'env'].includes(args[0]) || args.length <= 0) return 'Invalid mode. valid modes are `repl`, `parser`, `lexer`, and `env`'; this.mode = args[0]; return `Switched to '${this.mode}' mode`; }, ], ['//exit', () => process.exit(0)], ]); if (commands.has(command)) return commands.get(command)(...args); else { const result = new Tiny.Evaluator(parsed, env, { ...this.option, stdio: { stdin: this.promptSync, stdout: Tiny.stdout, stderr: Tiny.stderr, }, filename: defaultFilename, root: './', }).eval(); switch (this.mode) { case 'repl': return Tiny.objectStringify(result).gray; case 'parser': return JSON.stringify(parsed, null, 2); case 'lexer': const tokens = []; let peekToken; while ((peekToken = lexer.nextToken()).type !== Tiny.TokenType.EOF) tokens.push(peekToken); return tokens; case 'env': return env; } } } start() { if (this.option.useStdLibAutomatically) new Tiny.Evaluator(new Tiny.Parser(new Tiny.Lexer(`import('@std/lib');`, { ...this.option, stderr: Tiny.stderr, }, defaultFilename), this.option).parseProgram(), this.env, { ...this.option, stdio: { stdin: this.promptSync, stdout: Tiny.stdout, stderr: Tiny.stderr, }, filename: defaultFilename, root: './', }).eval(); while (true) { const input = this.promptSync(`${`[${this.mode.toUpperCase()}]`.white.bgBlack} ${`${this.env.store.size} Env(s)`.gray} ${'➜'.red} `); const parser = new Tiny.Parser(new Tiny.Lexer(input, { ...this.option, stderr: Tiny.stderr, }, defaultFilename), this.option); const executed = this.executeCommand(input, new Tiny.Lexer(input, { ...this.option, stderr: Tiny.stderr, }, defaultFilename), parser.parseProgram(), this.env); if (executed && parser.errors.length > 0) parser.errors.forEach((error) => Tiny.printError(error, defaultFilename, Tiny.stderr, this.option)); console.log(executed, '\n'); } } } exports.default = default_1;