Files
str/src/vm/commands/line.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 {Lines} from "./lines.js";
import {Each} from "./each.js";
import {Join} from "./join.js";
export type LineData = {
exec: Executable<CommandData>,
}
export class Line extends Command<LineData> {
async attemptParse(context: ParseContext): Promise<LineData> {
return {
exec: await context.popExecutable(),
}
}
getDisplayName(): string {
return 'line'
}
isParseCandidate(token: LexInput): boolean {
return this.isKeyword(token, 'line')
}
execute(vm: StrVM, data: LineData): Awaitable<StrVM> {
// `line <cmd>` is equivalent to `lines` -> `each <cmd>` -> `join`, so just do that
return vm.replaceContextFromChild(async child => {
await (new Lines).execute(child)
await (new Each).execute(child, { exec: data.exec })
await (new Join).execute(child, {})
})
}
}