29 lines
916 B
TypeScript
29 lines
916 B
TypeScript
import { LexInput } from "../lexer.js";
|
|
import {Command, ParseContext, StrTerm, unwrapString, wrapString} from "./command.js";
|
|
import {StrVM} from "../vm.js";
|
|
import {Awaitable} from "../../util/types.js";
|
|
|
|
export class Contains extends Command<{ find: StrTerm }> {
|
|
attemptParse(context: ParseContext): { find: StrTerm } {
|
|
return {
|
|
find: context.popTerm(),
|
|
}
|
|
}
|
|
|
|
getDisplayName(): string {
|
|
return 'contains'
|
|
}
|
|
|
|
isParseCandidate(token: LexInput): boolean {
|
|
return this.isKeyword(token, 'contains')
|
|
}
|
|
|
|
execute(vm: StrVM, data: { find: StrTerm }): Awaitable<StrVM> {
|
|
return vm.replaceContextMatchingTerm(ctx => ({
|
|
string: sub => sub.includes(ctx.resolveString(data.find)) ? sub : '',
|
|
destructured: parts => parts.filter(part =>
|
|
part.value.includes(ctx.resolveString(data.find))),
|
|
}))
|
|
}
|
|
}
|