Replace StringRange w/ an actual StrRVal + more command implementations
This commit is contained in:
33
src/vm/commands/words.ts
Normal file
33
src/vm/commands/words.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import {Command, ParseContext, unwrapString, wrapDestructured} from "./command.js";
|
||||
import {LexInput} from "../lexer.js";
|
||||
import {StrVM} from "../vm.js";
|
||||
import {Awaitable} from "../../util/types.js";
|
||||
|
||||
export class Words extends Command<{}> {
|
||||
attemptParse(context: ParseContext): {} {
|
||||
return {}
|
||||
}
|
||||
|
||||
getDisplayName(): string {
|
||||
return 'words'
|
||||
}
|
||||
|
||||
isParseCandidate(token: LexInput): boolean {
|
||||
return this.isKeyword(token, 'words')
|
||||
}
|
||||
|
||||
execute(vm: StrVM): Awaitable<StrVM> {
|
||||
return vm.tapInPlace(ctx =>
|
||||
ctx.replaceSubject(sub => {
|
||||
const val = unwrapString(sub)
|
||||
const separators = [...val.matchAll(/\s+/sg)]
|
||||
const parts = val.split(/\s+/sg)
|
||||
|
||||
return wrapDestructured(
|
||||
parts.map((part, idx) => ({
|
||||
prefix: idx ? separators[idx - 1][0] : undefined,
|
||||
value: part,
|
||||
})))
|
||||
}))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user