Files
str/src/vm/commands/enclose.ts

57 lines
1.5 KiB
TypeScript
Raw Normal View History

import {Command, ParseContext, StrTerm, unwrapString, wrapString} from "./command.js";
2025-11-11 21:37:32 -06:00
import {LexInput} from "../lexer.js";
import {StrVM} from "../vm.js";
import {Awaitable} from "../../util/types.js";
2025-11-11 21:37:32 -06:00
export type EncloseData = {
left?: StrTerm,
right?: StrTerm,
}
export class Enclose extends Command<EncloseData> {
attemptParse(context: ParseContext): EncloseData {
return {
left: context.popOptionalTerm(),
right: context.popOptionalTerm(),
}
}
getDisplayName(): string {
return 'enclose'
}
isParseCandidate(token: LexInput): boolean {
return this.isKeyword(token, 'enclose')
}
execute(vm: StrVM, data: EncloseData): Awaitable<StrVM> {
return vm.replaceContextMatchingTerm(ctx => ({
string: sub => {
const [left, right] = this.determineSurroundingStrings(
data.left ? ctx.resolveString(data.left) : undefined,
data.right ? ctx.resolveString(data.right) : undefined,
)
return `${left}${sub}${right}`
}
}))
}
private determineSurroundingStrings(left?: string, right?: string): [string, string] {
if ( !left ) {
left = '('
}
if ( !right ) {
right = ({
'(': ')',
'[': ']',
'{': '}',
'<': '>',
})[left] ?? left
}
return [left, right]
}
2025-11-11 21:37:32 -06:00
}