Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

Commit

Permalink
stdout adoption
Browse files Browse the repository at this point in the history
  • Loading branch information
eoussama committed Mar 21, 2024
1 parent dbdbea1 commit c18d8d1
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions bin/langkama.js
Original file line number Diff line number Diff line change
Expand Up @@ -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...`))
Expand All @@ -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();
Expand All @@ -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);
}
});
Expand Down

0 comments on commit c18d8d1

Please sign in to comment.