str/src/vm/commands/join.ts

35 lines
1.0 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.replaceContextMatchingTerm(ctx => ({
restructure: parts => {
if ( data.with ) {
return parts
.map(part => part.value)
.join(ctx.resolveString(data.with))
}
return joinDestructured(parts)
}
}))
}
2025-11-12 03:37:32 +00:00
}