~subsetpark/ec

3fcb736eaa84bebba6dd5dbb56d7fe8d030d5cf2 — Zach Smith 9 months ago a37c903
Add swap
4 files changed, 20 insertions(+), 9 deletions(-)

M README.md
M main.janet
M src/adverbs.janet
M src/parser.janet
M README.md => README.md +11 -7
@@ 1,9 1,13 @@
An RPN calculator with array operations and quotations.

```
repl:69:> (def s (:new Stack))
repl:70:> (push s 0)
repl:71:> (push s (make-vector 1 2 3))
repl:72:> (push s (make-vector add))
repl:73:> (push s distribute-dyad)
repl:74:> (peek s)
6
(nil)> 4 5 +
(9)> p
9
(nil)> [3 4 5] 6 x
([18 24 30])> 0 swap
([18 24 30])> [+] /
(72)> p
72
(nil)> 
```

M main.janet => main.janet +1 -1
@@ 26,7 26,7 @@
  (let [s (:new calc/Stack)]
    (while true
      (let [buf @""
            prompt (string/format "[%s]> " (print/p (calc/peek s)))
            prompt (string/format "(%s)> " (print/p (calc/peek s)))
            in (getline prompt buf parser/dictionary)
            after-commands (handle-commands in s)
            parsed (parser/parse after-commands)]

M src/adverbs.janet => src/adverbs.janet +6 -0
@@ 25,3 25,9 @@
  (let [v (calc/wrap ;(stack :data))]
    (array/clear (stack :data))
    (calc/push stack v)))

(defadv swap 2
  (let [top (calc/pop stack)
        second (calc/pop stack)]
    (calc/push stack top)
    (calc/push stack second)))

M src/parser.janet => src/parser.janet +2 -1
@@ 16,7 16,8 @@
                 "cmp" operations/cmp
                 "/" adverbs/distribute-dyad
                 "!" adverbs/apply-vector
                 "@" adverbs/wrap-stack})
                 "@" adverbs/wrap-stack
                 "swap" adverbs/swap})

(defn handle-parens
  [& patt]