Skip to content

Commit

Permalink
I/O VM refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
conartist6 committed Jul 7, 2024
1 parent aa3392a commit 6080369
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
36 changes: 23 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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();
Expand All @@ -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);
}
Expand All @@ -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));
};
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit 6080369

Please sign in to comment.