diff --git a/HELP.md b/HELP.md index 0c9180b..06be9c8 100644 --- a/HELP.md +++ b/HELP.md @@ -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 ` +Append the given `value` to the current destructured. +Example: `[foo, bar]` -> `push foo` -> `[foo, bar, foo]` + ### Working with Variables diff --git a/editor-support/vim/syntax/str.vim b/editor-support/vim/syntax/str.vim index ca9135b..d0962e1 100644 --- a/editor-support/vim/syntax/str.vim +++ b/editor-support/vim/syntax/str.vim @@ -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 "\" -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\)" diff --git a/src/vm/commands/index.ts b/src/vm/commands/index.ts index f5f4209..3c04ed3 100644 --- a/src/vm/commands/index.ts +++ b/src/vm/commands/index.ts @@ -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[] 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, diff --git a/src/vm/commands/push.ts b/src/vm/commands/push.ts new file mode 100644 index 0000000..bef2b6c --- /dev/null +++ b/src/vm/commands/push.ts @@ -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 { + async attemptParse(context: ParseContext): Promise { + 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 { + return vm.replaceContextMatchingTerm(ctx => ({ + destructured: parts => [ + ...parts, + { value: ctx.resolveRequired(data.val) }, + ], + })) + } +}