From c18d8d101f31cb4b33b63164a4e111a32fd2c2e3 Mon Sep 17 00:00:00 2001 From: eoussama Date: Thu, 21 Mar 2024 14:03:59 +0000 Subject: [PATCH] stdout adoption --- bin/langkama.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/bin/langkama.js b/bin/langkama.js index 0bf25c0..8c23a59 100755 --- a/bin/langkama.js +++ b/bin/langkama.js @@ -100,12 +100,13 @@ class Cmd { compiler .on(LangKamaEvent.Success, result => { - this.#info('LangKama script compiled!\n'); + this.#info('LangKama script interpreted!\n'); this.#info(chalk.green(JSON.stringify(result.value, null, 2))); process.exit(0); }) .on(LangKamaEvent.Error, error => { this.#error(error) }) + .on(LangKamaEvent.Stdout, stdout => { this.#info(stdout) }) .on(LangKamaEvent.Lexer, () => this.#info(`Tokenizing "${fileName}" script...`)) .on(LangKamaEvent.Parser, tokens => this.#info(`Parsing "${tokens.length}" tokens...`)) .on(LangKamaEvent.Interpreter, ast => this.#info(`Interpreting "${fileName}" script...`)) @@ -123,6 +124,15 @@ class Cmd { const rl = createInterface({ input: process.stdin, output: process.stdout }); const env = new Environment(); + const onSuccessCallback = result => { + this.#info(chalk.green(result.value)); + prompt(); + }; + + const onStdoutCallback = stdout => { + this.#info(stdout); + }; + const onErrorCallback = error => { this.#error(error, false); prompt(); @@ -140,11 +150,9 @@ class Cmd { env.setErrorCallback(errorManager.raise.bind(errorManager)); compiler - .on(LangKamaEvent.Success, result => { - this.#info(chalk.green(result.value)); - prompt(); - }) .on(LangKamaEvent.Error, onErrorCallback) + .on(LangKamaEvent.Stdout, onStdoutCallback) + .on(LangKamaEvent.Success, onSuccessCallback) .interpret(input, env); } });