[WIP] Start implementing execution in the new TS version
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import {Command, ParseContext, StrTerm} from "./command.js";
|
||||
import {LexInput} from "../lexer.js";
|
||||
import {StrVM} from "../vm.js";
|
||||
import {Awaitable} from "../../util/types.js";
|
||||
|
||||
export type EncloseData = {
|
||||
left?: StrTerm,
|
||||
@@ -21,4 +23,33 @@ export class Enclose extends Command<EncloseData> {
|
||||
isParseCandidate(token: LexInput): boolean {
|
||||
return this.isKeyword(token, 'enclose')
|
||||
}
|
||||
|
||||
execute(vm: StrVM, data: EncloseData): Awaitable<StrVM> {
|
||||
return vm.inPlace(ctx => {
|
||||
const [left, right] = this.determineSurroundingStrings(
|
||||
data.left ? ctx.resolveString(data.left) : undefined,
|
||||
data.right ? ctx.resolveString(data.right) : undefined,
|
||||
)
|
||||
|
||||
return ctx.replaceSubject(sub =>
|
||||
sub.modify(s => `${left}${s}${right}`))
|
||||
})
|
||||
}
|
||||
|
||||
private determineSurroundingStrings(left?: string, right?: string): [string, string] {
|
||||
if ( !left ) {
|
||||
left = '('
|
||||
}
|
||||
|
||||
if ( !right ) {
|
||||
right = ({
|
||||
'(': ')',
|
||||
'[': ']',
|
||||
'{': '}',
|
||||
'<': '>',
|
||||
})[left] ?? left
|
||||
}
|
||||
|
||||
return [left, right]
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user