~subsetpark/ec

b968a7d84e158cda379d69c9bd894e13c44fca9e — Zach Smith 17 days ago c9e7fd0
add load
6 files changed, 43 insertions(+), 11 deletions(-)

M project.janet
M src/adverbs.janet
M src/calc.janet
M src/env.janet
M src/operations.janet
M src/parser.janet
M project.janet => project.janet +2 -1
@@ 4,7 4,8 @@
  :dependencies ["https://git.sr.ht/~subsetpark/fugue"
                 "https://github.com/janet-lang/argparse.git"
                 "https://github.com/pyrmont/testament"
                 "https://git.sr.ht/~subsetpark/janet-rl"])
                 "https://git.sr.ht/~subsetpark/janet-rl"
                 "https://github.com/zenlor/janet-csv.git"])

(def *static-build* (= (or (os/getenv "EC_STATIC_BUILD") "0") "1"))


M src/adverbs.janet => src/adverbs.janet +26 -0
@@ 1,4 1,6 @@
(use fugue)
(import csv)

(import /src/calc)
(import /src/eval)



@@ 123,6 125,8 @@
    (calc/push stack top)
    (calc/push stack second)))

(def- root-slurp slurp)

(defadv slurp 2
  ```
  v? a -- v


@@ 286,3 290,25 @@
        then (calc/pop stack)
        if-q (calc/pop stack)]
    (linear-recursion stack if-q then else else2)))

(defadv load 2
  ```
  filename format -- v
  Load the file at `filename` and parse according to `format`. Push
  the resulting vector to the stack.

  Currently supported formats:
  - csv
  ```
  (let [format (calc/pop stack)
        format-parser (case (format :value)
                        "csv" csv/parse)
        filename ((calc/pop stack) :value)
        parsed (->> filename
                    (root-slurp)
                    (format-parser)
                    (map |(map parse $)))
        wrapped (calc/wrap ;(map
                              |(calc/wrap ;(map calc/make-element $))
                              parsed))]
    (calc/push stack wrapped)))

M src/calc.janet => src/calc.janet +7 -6
@@ 85,12 85,13 @@
            (reverse y))))

# TODO: Do we want to do something more sophisticated here?
(defmulti make-element [:number] [n] (new-Float n
                                                :doc (string n)))
(defmulti make-element [:boolean] [b] (if b
                                        (make-element 1)
                                        (make-element 0)))
(defmulti make-element [_] [v] v)
(declare-open-multi make-element)
(extend-multi make-element [:number] [n] (new-Float n :doc (string n)))
(extend-multi make-element [:boolean]
              [b]
              (if b
                (make-element 1)
                (make-element 0)))

(defn quote-wrap
  [& data]

M src/env.janet => src/env.janet +1 -0
@@ 57,6 57,7 @@
                  "slurpr" adverbs/slurp
                  "shape" adverbs/shape
                  "concat" adverbs/concat
                  "load" adverbs/load

                  "def" define


M src/operations.janet => src/operations.janet +4 -4
@@ 40,10 40,10 @@
(defmacro defop
  [name arity f &opt doc]
  ~(def ,name (,new-Operation
                :doc ,doc
                :type ,(keyword name)
                :arity ,arity
                :fun-ref ,f)))
                 :doc ,doc
                 :type ,(keyword name)
                 :arity ,arity
                 :fun-ref ,f)))

(defop add 2 +)
(defop sub 2 -)

M src/parser.janet => src/parser.janet +3 -0
@@ 5,6 5,9 @@
  value {:init? true}
  quoted? {:default false})

(extend-multi calc/make-element [:symbol] [s] (new-Symbol s))
(extend-multi calc/make-element [_] [v] v)

(defn handle-parens
  [& patt]
  [:parens (string ;patt)])