str/src/vm/commands/join.ts

35 lines
1.1 KiB
TypeScript
Raw Normal View History

import {Command, joinDestructured, ParseContext, StrTerm, unwrapDestructured, wrapString} from "./command.js";
2025-11-12 03:37:32 +00:00
import {LexInput} from "../lexer.js";
import {StrVM} from "../vm.js";
import {Awaitable} from "../../util/types.js";
2025-11-12 03:37:32 +00:00
export class Join extends Command<{ with?: StrTerm }> {
attemptParse(context: ParseContext): { with?: StrTerm } {
2025-11-12 03:37:32 +00:00
return {
with: context.popOptionalTerm(),
2025-11-12 03:37:32 +00:00
}
}
getDisplayName(): string {
return 'join'
}
isParseCandidate(token: LexInput): boolean {
return this.isKeyword(token, 'join')
}
execute(vm: StrVM, data: { with?: StrTerm }): Awaitable<StrVM> {
return vm.tapInPlace(ctx =>
ctx.replaceSubject(sub => {
if ( data.with ) {
return wrapString(
unwrapDestructured(sub)
.map(part => part.value)
.join(ctx.resolveString(data.with)))
}
return wrapString(joinDestructured(unwrapDestructured(sub)))
}))
}
2025-11-12 03:37:32 +00:00
}