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

38 lines
1.1 KiB
TypeScript

import {Command, CommandData, ParseContext} from "./command.js";
import {Executable} from "../parse.js";
import {LexInput} from "../lexer.js";
import {StrVM} from "../vm.js";
import {Awaitable} from "../../util/types.js";
import {Words} from "./words.js";
import {Each} from "./each.js";
import {Join} from "./join.js";
export type WordData = {
exec: Executable<CommandData>,
}
export class Word extends Command<WordData> {
async attemptParse(context: ParseContext): Promise<WordData> {
return {
exec: await context.popExecutable(),
}
}
getDisplayName(): string {
return 'word'
}
isParseCandidate(token: LexInput): boolean {
return this.isKeyword(token, 'word')
}
execute(vm: StrVM, data: WordData): Awaitable<StrVM> {
// `word <cmd>` is equivalent to `words` -> `each <cmd>` -> `join`, so just do that
return vm.replaceContextFromChild(async child => {
await (new Words).execute(child)
await (new Each).execute(child, { exec: data.exec })
await (new Join).execute(child, {})
})
}
}