30 lines
722 B
TypeScript
30 lines
722 B
TypeScript
import {Command, ParseContext, StrTerm} from "./command.js";
|
|
import {LexInput} from "../lexer.js";
|
|
import {StrVM} from "../vm.js";
|
|
|
|
export type PlusData = {
|
|
val: StrTerm,
|
|
}
|
|
|
|
export class Plus extends Command<PlusData> {
|
|
async attemptParse(context: ParseContext): Promise<PlusData> {
|
|
return {
|
|
val: await context.popTerm(),
|
|
}
|
|
}
|
|
|
|
getDisplayName(): string {
|
|
return 'plus'
|
|
}
|
|
|
|
isParseCandidate(token: LexInput): boolean {
|
|
return this.isKeyword(token, 'plus')
|
|
}
|
|
|
|
async execute(vm: StrVM, data: PlusData): Promise<StrVM> {
|
|
return vm.replaceContextMatchingTerm(ctx => ({
|
|
int: sub => sub + ctx.resolveInt(data.val),
|
|
}))
|
|
}
|
|
}
|