~subsetpark/ec

d48e38e3de4186d9f151e3c349917750349ad837 — Zach Smith 2 months ago c036dd3
Use a proto instead of a tagged value
7 files changed, 50 insertions(+), 61 deletions(-)

M lockfile.jdn
M main.janet
M src/adverbs.janet
M src/operations.janet
M src/parser.janet
M src/print.janet
M src/repl.janet
M lockfile.jdn => lockfile.jdn +4 -4
@@ 1,5 1,5 @@
@[{:sha "bed4177e20050ab65f35664b6e8dfd3cc0cbdfc7" :repo "https://github.com/zenlor/janet-csv.git"}
@[{:sha "e564dba2d7541a5211be399ceaf4755a070cdb13" :repo "https://git.sr.ht/~subsetpark/janet-rl"}
  {:sha "5d1bea51664cf2f076ce6ac4786c2fb744f2c02c" :repo "https://github.com/janet-lang/argparse.git"}
  {:sha "ce2c6308fdfbd344d2c17b2712f02bbc1fb69bf3" :repo "https://github.com/pyrmont/testament"}
  {:sha "e564dba2d7541a5211be399ceaf4755a070cdb13" :repo "https://git.sr.ht/~subsetpark/janet-rl"}
  {:sha "508d55fee21ff1dee9043130d69bcb4ea8170f14" :repo "https://git.sr.ht/~subsetpark/fugue"}
  {:sha "5d1bea51664cf2f076ce6ac4786c2fb744f2c02c" :repo "https://github.com/janet-lang/argparse.git"}]
  {:sha "bed4177e20050ab65f35664b6e8dfd3cc0cbdfc7" :repo "https://github.com/zenlor/janet-csv.git"}
  {:sha "854d1d5cc876f889f1fed7e1c1ad107dac1c9647" :repo "https://git.sr.ht/~subsetpark/fugue"}]

M main.janet => main.janet +8 -8
@@ 86,13 86,13 @@
  (when-let [res (argparse ;params)]
    (match res
      {"file" filename} (->>
                         filename
                         (slurp)
                         (string/split "\n")
                         (repl/handle-lines))
                          filename
                          (slurp)
                          (string/split "\n")
                          (repl/handle-lines))
      {:default inputs} (->
                         inputs
                         (string/join " ")
                         (tuple)
                         (repl/handle-lines))
                          inputs
                          (string/join " ")
                          (tuple)
                          (repl/handle-lines))
      _ (repl/repl))))

M src/adverbs.janet => src/adverbs.janet +4 -4
@@ 17,10 17,10 @@
(defmacro defadv
  [name arity doc & body]
  ~(def ,name (,new-Adverb
                 :doc ,doc
                 :arity ,arity
                 :type ,(keyword name)
                 :fun-ref (fn ,name [stack] ,;body))))
                :doc ,doc
                :arity ,arity
                :type ,(keyword name)
                :fun-ref (fn ,name [stack] ,;body))))

(defadv apply-quotation 1
  ```

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 +4 -1
@@ 30,10 30,13 @@
  (calc/new-Int (scan-number patt)
                :doc patt))

(defproto Special calc/Element
  value {:init? true})

(defn handle-word
  [patt]
  (cond
    (index-of patt ["." "p" "s" "?" "??"]) [:special patt]
    (index-of patt ["." "p" "s" "?" "??"]) (new-Special patt)
    (string/has-prefix? "'" patt) (new-Symbol
                                    (string/slice patt 1)
                                    :quoted? true)

M src/print.janet => src/print.janet +8 -23
@@ 5,39 5,24 @@
(import /src/operations)
(import /src/adverbs)

(defmulti p [calc/Int]
  [n] (string (n :value)))

(defmulti p [calc/Float]
  [n] (string (n :value)))

(defmulti p [operations/Operation]
  [o] (string (o :type)))

(defmulti p [adverbs/Adverb]
  [a] (string (a :type)))

(defmulti p [parser/Symbol]
  [{:value value}] value)

(varfn join [q] nil)

(defmulti p [calc/Int] [n] (string (n :value)))
(defmulti p [calc/Float] [n] (string (n :value)))
(defmulti p [operations/Operation] [o] (string (o :type)))
(defmulti p [adverbs/Adverb] [a] (string (a :type)))
(defmulti p [parser/Symbol] [{:value value}] value)
(defmulti p [calc/Quotation] [q] (join q "(" ")"))

(defmulti p [calc/Vector] [v] (join v "[" "]"))
(defmulti p [:nil] [_] "")
(defmulti p [:array] [t] (string/join (map p t) " "))
(defmulti p [_] [val] (describe val))

(varfn join
  [q l r]
  (let [inner (map p (q :data))]
    (string l (string/join inner " ") r)))

(defmulti p [:nil] [_] "")

(defmulti p [:array] [t] (string/join (map p t) " "))

(defmulti p [_]
  [val] (describe val))

(def- max-width 40)
(def- delimiters-peg (peg/compile
                       ~{:main (+ :parens :brackets)

M src/repl.janet => src/repl.janet +18 -17
@@ 64,23 64,24 @@
    (when-let [v (env k)]
      (printf "%s: %s" k (string (v :type))))))

(defn- format
(defgeneric format
  [obj]
  (match obj
    @[:special char] (string char " (Special)")
    _ (string/format
        ```
       --
       %s : %s
       
       %s%s
       ```
        (print/p obj)
        (string (obj :type))
        (obj :doc)
        (match (obj :composes)
          nil ""
          words (string "\n\n(" (print/p words) ")")))))
  (string/format ```
                 --
                 %s : %s

                 %s%s
                 ```
                 (print/p obj)
                 (string (obj :type))
                 (obj :doc)
                 (match (obj :composes)
                   nil ""
                   words (string "\n\n(" (print/p words) ")"))))

(defmethod format parser/Special
  [{:value char}]
  (string char " (Special)"))

(defn- describe-all
  [s q]


@@ 104,7 105,7 @@
  [s input]
  (each token input
    (match token
      @[:special patt] (handle-special s patt)
      (@ parser/Special {:value char}) (handle-special s char)
      _ (eval/eval-and-push s token))))

(defn repl