Implement push command
This commit is contained in:
4
HELP.md
4
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`.
|
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]`
|
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
|
### Working with Variables
|
||||||
|
|
||||||
|
|||||||
@@ -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 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 keyword strKeyword line word each on drop take missing lines words split chunk join sort unique zip flatten group
|
||||||
syn match strKeyword "\<contains\>"
|
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
|
" Types
|
||||||
syn match strType "::\s*\zs\(string\|int\|destructured\)"
|
syn match strType "::\s*\zs\(string\|int\|destructured\)"
|
||||||
|
|||||||
@@ -59,6 +59,7 @@ import {Unless} from "./unless.js";
|
|||||||
import {Convert} from "./convert.js";
|
import {Convert} from "./convert.js";
|
||||||
import {While} from "./while.js";
|
import {While} from "./while.js";
|
||||||
import {Table} from "./table.js";
|
import {Table} from "./table.js";
|
||||||
|
import {Push} from "./push.js";
|
||||||
|
|
||||||
export type Commands = Command<CommandData>[]
|
export type Commands = Command<CommandData>[]
|
||||||
export const commands: Commands = [
|
export const commands: Commands = [
|
||||||
@@ -96,6 +97,7 @@ export const commands: Commands = [
|
|||||||
new Over,
|
new Over,
|
||||||
new Paste,
|
new Paste,
|
||||||
new Prefix,
|
new Prefix,
|
||||||
|
new Push,
|
||||||
new Quote,
|
new Quote,
|
||||||
new Redo,
|
new Redo,
|
||||||
new Replace,
|
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