Implement push command

This commit is contained in:
2026-07-07 09:22:22 -05:00
parent faf3a16767
commit e728c3fdfa
4 changed files with 39 additions and 1 deletions

View File

@@ -314,6 +314,10 @@ Example: Say `$a` is `[1, 2, 3]` and subject is `[a, b, c]` -> `zip $a` -> `[a,
Collapse a nested destructured string down a single level, optionally limiting to a specific `index`.
Example: `[[a,b], c, [d,e]]` -> `flatten 2` -> `[[a,b], c, d, e]`
#### `push <value>`
Append the given `value` to the current destructured.
Example: `[foo, bar]` -> `push foo` -> `[foo, bar, foo]`
### Working with Variables

View File

@@ -11,7 +11,7 @@ syn keyword strKeyword exit paste copy infile outfile assign clear show undo red
syn keyword strKeyword enclose lower upper lsub rsub prefix suffix quote unquote replace rev trim indent concat
syn keyword strKeyword line word each on drop take missing lines words split chunk join sort unique zip flatten group
syn match strKeyword "\<contains\>"
syn keyword strKeyword to from set over call lipsum convert if unless while table
syn keyword strKeyword to from set over call lipsum convert if unless while table push
" Types
syn match strType "::\s*\zs\(string\|int\|destructured\)"

View File

@@ -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
View 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) },
],
}))
}
}