commit 0da98a3d83b5aa6ccae73658ef0692c4023bc70f
parent c6b1341fab071ab9d9419dace857b826ddf2acf1
Author: Charlie Stanton <charlie@shtanton.xyz>
Date: Wed, 26 Apr 2023 11:57:37 +0100
Add Y and Z registers
Diffstat:
4 files changed, 39 insertions(+), 3 deletions(-)
diff --git a/README.md b/README.md
@@ -18,9 +18,9 @@ I'm hoping it will be capable of nearly as much as jq, with comparable or better
## Usage
### Registers and Atoms
-Commands operate on data that is store in 'registers'. There are 3 registers: path, value and X.
+Commands operate on data that is store in 'registers'. There are 5 registers: path, value, X, Y and Z.
-The path register stores the global path from the root node to the current token, the value register stores the token itself and the X register is a general purpose register, good for keeping things for later.
+The path register stores the global path from the root node to the current token, the value register stores the token itself and the X, Y and Z registers are general purpose registers, good for keeping things for later.
With the following JSON being read:
```
@@ -162,5 +162,9 @@ With an understanding of subexes, we can look at the stred commands
| `o` | Do nothing |
| `x` | Swap the value register with the X register |
| `X` | Append the contents of the value register to the X register |
+| `y` | Swap the value register with the Y register |
+| `Y` | Append the contents of the value register to the Y register |
+| `z` | Swap the value register with the Z register |
+| `Z` | Append the contents of the value register to the Z register |
| `k` | Swap the value register with the path register |
| `K` | Append the contents of the value register to the path register |
diff --git a/main/command.go b/main/command.go
@@ -107,6 +107,30 @@ func (cmd AppendXRegCommand) exec(state *ProgramState) {
state.xreg = append(state.xreg, state.value...)
}
+type SwapYRegCommand struct {}
+func (cmd SwapYRegCommand) exec(state *ProgramState) {
+ v := state.value
+ state.value = state.yreg
+ state.yreg = v
+}
+
+type AppendYRegCommand struct {}
+func (cmd AppendYRegCommand) exec(state *ProgramState) {
+ state.yreg = append(state.yreg, state.value...)
+}
+
+type SwapZRegCommand struct {}
+func (cmd SwapZRegCommand) exec(state *ProgramState) {
+ v := state.value
+ state.value = state.zreg
+ state.zreg = v
+}
+
+type AppendZRegCommand struct {}
+func (cmd AppendZRegCommand) exec(state *ProgramState) {
+ state.zreg = append(state.zreg, state.value...)
+}
+
type SwapPathCommand struct {}
func (cmd SwapPathCommand) exec(state *ProgramState) {
v := state.value
diff --git a/main/main.go b/main/main.go
@@ -9,7 +9,7 @@ import (
type Program []Command
type ProgramState struct {
- path, value, xreg []walk.Atom
+ path, value, xreg, yreg, zreg []walk.Atom
in walk.JSONIn
out walk.JSONOut
program []Command
diff --git a/main/parse.go b/main/parse.go
@@ -142,6 +142,14 @@ func (p *parser) parseBasicCommand(commandChar rune) Command {
return SwapXRegCommand{}
case 'X':
return AppendXRegCommand{}
+ case 'y':
+ return SwapYRegCommand{}
+ case 'Y':
+ return AppendYRegCommand{}
+ case 'z':
+ return SwapZRegCommand{}
+ case 'Z':
+ return AppendZRegCommand{}
case 'k':
return SwapPathCommand{}
case 'K':