~subsetpark/ec

45225db3a5d3c2b59d2a2f28dc9639747726b623 — Zach Smith 28 days ago fbef686 v0.5.0
Handle file input
2 files changed, 23 insertions(+), 10 deletions(-)

M main.janet
M src/repl.janet
M main.janet => main.janet +17 -5
@@ 2,7 2,6 @@

(use argparse)


(def params
  [```
   A desk calculator with vectors and quotations.


@@ 10,6 9,7 @@
   USAGE:
      ec <expression>: evaluate <expression> as a sequence of
                       ec commands and print the resulting stack.
      ec -f <file>   : load and evaluate <file> as an ec program.
      ec             : enter interactive (repl) mode.
   
      ec operates as a RPN desk calculator with quotation


@@ 76,12 76,24 @@
   
      Will print a description of the `apply` adverb.
   ```
   :default {:kind :accumulate}])
   :default {:kind :accumulate}
   "file" {:kind :option
           :short "f"
           :help "The filename to evaluate"}])

(defn main
  [& _args]
  (let [res (argparse ;params)]
    (when res
      (match (res :default)
        nil (repl/repl)
        inputs (repl/handle-line (string/join inputs " "))))))
      (match res
        {"file" filename} (->>
                           filename
                           (slurp)
                           (string/split "\n")
                           (repl/handle-lines))
        {:default inputs} (->
                           inputs
                           (string/join " ")
                           (tuple)
                           (repl/handle-lines))
        _ (repl/repl)))))

M src/repl.janet => src/repl.janet +6 -5
@@ 124,13 124,14 @@
          (propagate err fib)
          (put s :data bak))))))

(defn handle-line
(defn handle-lines
  ```
  Handle a single input line from the command line.
  ```
  [line]
  [lines]
  (let [s (env/new-env)]
    (->> line
         (parser/parse)
         (handle-commands s))
    (each line lines
      (->> line
           (parser/parse)
           (handle-commands s)))
    (display-all (s :data))))