From 6080369ce4e0264187ef839da11e133711a31740 Mon Sep 17 00:00:00 2001 From: Conrad Buck Date: Sun, 7 Jul 2024 06:30:44 -0600 Subject: [PATCH] I/O VM refactor --- lib/index.js | 36 +++++++++++++++++++++++------------- package.json | 3 ++- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/lib/index.js b/lib/index.js index 71fcac8..5aef75b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,10 +1,12 @@ /* global console */ -import { printSource } from '@bablr/agast-helpers/tree'; +import { getCooked, printSource } from '@bablr/agast-helpers/tree'; import { StreamIterable, getStreamIterator, printTerminal } from '@bablr/agast-helpers/stream'; +import { buildCall, buildNumber, buildObject, buildString } from '@bablr/agast-vm-helpers'; +import { buildWriteEffect } from '@bablr/agast-helpers/builders'; import { Coroutine } from '@bablr/coroutine'; -function* wrapGeneratorWithEmitLogging(generator, indent, log) { +function* wrapGeneratorWithEmitLogging(generator, indent) { const co = new Coroutine(generator); co.advance(); @@ -16,15 +18,25 @@ function* wrapGeneratorWithEmitLogging(generator, indent, log) { if (co.done) break; - let instr = co.value; + let terminal = co.value; - log(indent + printTerminal(instr)); + if (terminal.type === 'Effect') { + yield terminal; + co.advance(); + continue; + } - co.advance(yield instr); + yield buildWriteEffect(indent + printTerminal(terminal)); + + co.advance(yield terminal); } } -function* wrapGeneratorWithLogging(generator, indent, log) { +const writeError = (text) => { + return buildCall('write', buildString(text), buildObject({ stream: buildNumber(2) })); +}; + +function* wrapGeneratorWithLogging(generator, indent) { const co = new Coroutine(generator); co.advance(); @@ -38,7 +50,9 @@ function* wrapGeneratorWithLogging(generator, indent, log) { const instr = co.value; - log(indent + printSource(instr)); + if (getCooked(instr.properties.verb) !== 'write') { + yield writeError(indent + printSource(instr)); + } co.advance(yield instr); } @@ -59,14 +73,10 @@ export const enhanceStrategyBuilderWithDebugLogging = ( }; }; -export const enhanceStrategyBuilderWithEmittedLogging = ( - strategyBuilder, - indent = '', - log = console.log, -) => { +export const enhanceStrategyBuilderWithEmittedLogging = (strategyBuilder, indent = '') => { return (...strategyBuilderArgs) => { const strategy = getStreamIterator(strategyBuilder(...strategyBuilderArgs)); - return new StreamIterable(wrapGeneratorWithEmitLogging(strategy, indent, log)); + return new StreamIterable(wrapGeneratorWithEmitLogging(strategy, indent)); }; }; diff --git a/package.json b/package.json index 83c362e..ae4805b 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,8 @@ }, "sideEffects": false, "dependencies": { - "@bablr/agast-helpers": "0.1.1", + "@bablr/agast-helpers": "0.1.6", + "@bablr/agast-vm-helpers": "0.1.5", "@bablr/coroutine": "0.1.0" }, "devDependencies": {