Implement push command
This commit is contained in:
@@ -59,6 +59,7 @@ import {Unless} from "./unless.js";
|
||||
import {Convert} from "./convert.js";
|
||||
import {While} from "./while.js";
|
||||
import {Table} from "./table.js";
|
||||
import {Push} from "./push.js";
|
||||
|
||||
export type Commands = Command<CommandData>[]
|
||||
export const commands: Commands = [
|
||||
@@ -96,6 +97,7 @@ export const commands: Commands = [
|
||||
new Over,
|
||||
new Paste,
|
||||
new Prefix,
|
||||
new Push,
|
||||
new Quote,
|
||||
new Redo,
|
||||
new Replace,
|
||||
|
||||
32
src/vm/commands/push.ts
Normal file
32
src/vm/commands/push.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import {Command, ParseContext, StrTerm} from "./command.js";
|
||||
import {LexInput} from "../lexer.js";
|
||||
import {StrVM} from "../vm.js";
|
||||
|
||||
export type PushData = {
|
||||
val: StrTerm,
|
||||
}
|
||||
|
||||
export class Push extends Command<PushData> {
|
||||
async attemptParse(context: ParseContext): Promise<PushData> {
|
||||
return {
|
||||
val: await context.popTerm(),
|
||||
}
|
||||
}
|
||||
|
||||
getDisplayName(): string {
|
||||
return 'push'
|
||||
}
|
||||
|
||||
isParseCandidate(token: LexInput): boolean {
|
||||
return this.isKeyword(token, 'push')
|
||||
}
|
||||
|
||||
async execute(vm: StrVM, data: PushData): Promise<StrVM> {
|
||||
return vm.replaceContextMatchingTerm(ctx => ({
|
||||
destructured: parts => [
|
||||
...parts,
|
||||
{ value: ctx.resolveRequired(data.val) },
|
||||
],
|
||||
}))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user