From 9e4eac6f128ae68ff195950e616b13fdc37764f1 Mon Sep 17 00:00:00 2001 From: chakr Date: Sun, 19 Feb 2023 11:07:46 -0500 Subject: [PATCH] add pick op, increment version --- SPEC.html | 44 ++++++------------------------------ SPEC.md | 15 +++++++++--- examples/aoc2022/day1/p2.vqe | 2 +- main.go | 3 ++- varaq/interpreter.go | 29 +++++++++++++++++++++++- varaq/scanner.go | 2 ++ varaq/token.go | 2 ++ 7 files changed, 54 insertions(+), 43 deletions(-) diff --git a/SPEC.html b/SPEC.html index 2631c84..10399e1 100755 --- a/SPEC.html +++ b/SPEC.html @@ -15,41 +15,6 @@ display: none; } -/* From extension ms-toolsai.jupyter */ -/* These classnames are inherited from bootstrap, but are present in most notebook renderers */ - -.alert { - width: auto; - padding: 1em; - margin-top: 1em; - margin-bottom: 1em; -} -.alert > *:last-child { - margin-bottom: 0; -} -#preview > .alert:last-child { - /* Prevent this being set to zero by the default notebook stylesheet */ - padding-bottom: 1em; -} - -.alert-success { - /* Note there is no suitable color available, so we just copy "info" */ - background-color: var(--theme-info-background); - color: var(--theme-info-foreground); -} -.alert-info { - background-color: var(--theme-info-background); - color: var(--theme-info-foreground); -} -.alert-warning { - background-color: var(--theme-warning-background); - color: var(--theme-warning-foreground); -} -.alert-danger { - background-color: var(--theme-error-background); - color: var(--theme-error-foreground); -} - @@ -408,11 +373,16 @@

Make a copy of the second item on top of the stack.

The direct Klingon translation for QI is bridge, as in a bridge over a river, which works close enough.

Errors: stackUnderflow

-

7.1.2 rot/jIr

+

7.1.2 pick/woH

+

N woH objN

+

Make a copy of the Nth item on top of the stack.

+

For example 1 woH is the same as latlh & 2 woH is the same as QI

+

Errors: stackUnderflow, stackOverflow

+

7.1.3 rot/jIr

obj1 obj2 obj3 jIr obj2 obj3 obj1

Rotate the third item to the top of the stack.

Errors: stackUnderflow

-

7.1.3 depth/juv

+

7.1.4 depth/juv

- juv num

Count the number of items on the stack and push that number to the top of the stack.

Errors: stackUnderflow

diff --git a/SPEC.md b/SPEC.md index 74d3805..b0146e3 100755 --- a/SPEC.md +++ b/SPEC.md @@ -1,5 +1,5 @@ _var'aq_ Specification -================================== +================================= For better flavor text please read the [Original `var'aq` spec](https://www.oocities.org/connorbd/varaq/varaqspec.html) the following is a trimmed down version of the spec with a couple of fixes and addendums to how this implementation of the language works. @@ -655,8 +655,17 @@ Make a copy of the second item on top of the stack. The direct Klingon translation for `QI` is bridge, as in a bridge over a river, which works close enough. Errors: stackUnderflow +#### 7.1.2 pick/woH -#### 7.1.2 rot/jIr +_N `woH` objN_ + +Make a copy of the Nth item on top of the stack. + +For example _1 `woH`_ is the same as `latlh` & _2 `woH`_ is the same as `QI` + +Errors: stackUnderflow, stackOverflow + +#### 7.1.3 rot/jIr _obj1 obj2 obj3 `jIr` obj2 obj3 obj1_ @@ -664,7 +673,7 @@ Rotate the third item to the top of the stack. Errors: stackUnderflow -#### 7.1.3 depth/juv +#### 7.1.4 depth/juv _- `juv` num_ diff --git a/examples/aoc2022/day1/p2.vqe b/examples/aoc2022/day1/p2.vqe index 4bedee9..a1cf2a0 100755 --- a/examples/aoc2022/day1/p2.vqe +++ b/examples/aoc2022/day1/p2.vqe @@ -9,7 +9,7 @@ } repeat elves shatter depth sub1 { - over over lt? + 2 pick 2 pick lt? choose { pop exch pop true } ifyes { pop } ifno diff --git a/main.go b/main.go index 52f6d11..d822dfd 100755 --- a/main.go +++ b/main.go @@ -20,8 +20,8 @@ func main() { func repl() { scanner := bufio.NewScanner(os.Stdin) + fmt.Print("> ") for scanner.Scan() { - fmt.Print("> ") line := scanner.Text() if len(line) == 0 { return @@ -30,6 +30,7 @@ func repl() { if err != nil { fmt.Printf("%v\n", err) } + fmt.Print("> ") } } diff --git a/varaq/interpreter.go b/varaq/interpreter.go index f3a4a69..b136546 100755 --- a/varaq/interpreter.go +++ b/varaq/interpreter.go @@ -170,6 +170,33 @@ func Interpret(code Expr, argv []string) error { push(stack[n-2]) stack = append(stack[:n-2], stack[n-3:]...) + case PICK: + n := len(stack) - 1 + a, e := pop() + if e != nil { + return e + } + if a.Tok == IDENTIFIER { + a = get(a.Value.(string)) + } + + if a.Tok == NUMBER { + idx := int(a.Value.(float64)) + + if idx <= 0 { + return fmt.Errorf("`pick`: index must be a positive nonzero value") + } + if n-idx < 0 { + return fmt.Errorf("`pick`: stack underflow") + } + if idx > n { + return fmt.Errorf("`pick`: stack overflow") + } + push(stack[n-idx]) + } else { + return fmt.Errorf("cannot `pick` index is not a number @%v", idx) + } + case DEPTH: push(Expr{NUMBER, nil, float64(len(stack)), 0}) case CLEAR: @@ -1232,7 +1259,7 @@ func Interpret(code Expr, argv []string) error { fmt.Println(localAddr.IP.String()) case VERSION: - fmt.Println("var'aq -- 0.9.0 Martoq") + fmt.Println("var'aq -- 0.9.1 Martoq") case ARGV: arg := Expr{LIST, make([]Expr, 0), nil, 0} for _, v := range argv { diff --git a/varaq/scanner.go b/varaq/scanner.go index 76692ab..da9fba6 100755 --- a/varaq/scanner.go +++ b/varaq/scanner.go @@ -181,6 +181,8 @@ var keywords = map[string]TokenType{ "QI": OVER, "depth": DEPTH, "juv": DEPTH, + "pick": PICK, + "woH": PICK, } func isDigit(s string) bool { diff --git a/varaq/token.go b/varaq/token.go index e361a07..49a8331 100755 --- a/varaq/token.go +++ b/varaq/token.go @@ -105,6 +105,7 @@ const ( OVER ROT DEPTH + PICK ) var tokens = [...]string{ @@ -209,6 +210,7 @@ var tokens = [...]string{ OVER: "OVER", ROT: "ROT", DEPTH: "DEPTH", + PICK: "PICK", } func (me TokenType) String() string {