Implement basic integer math operations
This commit is contained in:
29
src/vm/commands/plus.ts
Normal file
29
src/vm/commands/plus.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
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),
|
||||
}))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user