Introduce TermOperator helper and refactor execute()s to use replaceSubjectMatchingTerm

This commit is contained in:
2026-02-09 23:49:30 -06:00
parent feba84051a
commit f36621c646
25 changed files with 243 additions and 159 deletions

View File

@@ -1,4 +1,4 @@
import {Command, ParseContext, unwrapString, wrapDestructured} from "./command.js";
import {Command, ParseContext} from "./command.js";
import {LexInput} from "../lexer.js";
import {StrVM} from "../vm.js";
import {Awaitable} from "../../util/types.js";
@@ -17,17 +17,16 @@ export class Words extends Command<{}> {
}
execute(vm: StrVM): Awaitable<StrVM> {
return vm.tapInPlace(ctx =>
ctx.replaceSubject(sub => {
const val = unwrapString(sub)
const separators = [...val.matchAll(/\s+/sg)]
const parts = val.split(/\s+/sg)
return vm.replaceContextMatchingTerm({
destructure: sub => {
const separators = [...sub.matchAll(/\s+/sg)]
const parts = sub.split(/\s+/sg)
return wrapDestructured(
parts.map((part, idx) => ({
prefix: idx ? separators[idx - 1][0] : undefined,
value: part,
})))
}))
return parts.map((part, idx) => ({
prefix: idx ? separators[idx - 1][0] : undefined,
value: part,
}))
}
})
}
}