str/src/vm/commands/join.ts

35 lines
1.0 KiB
TypeScript

import {Command, joinDestructured, ParseContext, StrTerm, unwrapDestructured, wrapString} from "./command.js";
import {LexInput} from "../lexer.js";
import {StrVM} from "../vm.js";
import {Awaitable} from "../../util/types.js";
export class Join extends Command<{ with?: StrTerm }> {
attemptParse(context: ParseContext): { with?: StrTerm } {
return {
with: context.popOptionalTerm(),
}
}
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)
}
}))
}
}