~subsetpark/ec

7304ccde8737d14a9793f04b9f463ecd8fe7dd41 — Zach Smith 3 years ago d48e38e
make handle-token a generic
1 files changed, 21 insertions(+), 13 deletions(-)

M src/repl.janet
M src/repl.janet => src/repl.janet +21 -13
@@ 16,7 16,7 @@
      proto (array/concat these-keys (env-keys proto)))))

(defn- getline
  [{:env env} prompt buf]
  [{:env env} repl-prompt buf]

  (defn get-completions
    [line start end]


@@ 31,11 31,11 @@
        (debug/stacktrace f err)
        @[])))

  (when-let [ln (rl/readline prompt get-completions)]
  (when-let [ln (rl/readline repl-prompt get-completions)]
    (buffer/push-string buf ln "\n")
    buf))

(defn- prompt
(defn- repl-prompt
  [s]
  (->> s
       (calc/peek)


@@ 65,6 65,9 @@
      (printf "%s: %s" k (string (v :type))))))

(defgeneric format
  ```
  Generate a string for displaying any value.
  ```
  [obj]
  (string/format ```
                 --


@@ 75,14 78,22 @@
                 (print/p obj)
                 (string (obj :type))
                 (obj :doc)
                 (match (obj :composes)
                   nil ""
                   words (string "\n\n(" (print/p words) ")"))))
                 (match obj
                   {:composes words} (string "\n\n(" (print/p words) ")")
                   _ "")))

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

(defgeneric handle-token
  ```
  Process a single token; either execute hardcoded special form, or push a
  value on the stack.
  ```
  [token s]
  (eval/eval-and-push s token))

(defn- describe-all
  [s q]
  (defn- describe


@@ 92,8 103,8 @@
  (each item (q :data)
    (describe item)))

(defn- handle-special
  [s special]
(defmethod handle-token parser/Special
  [{:value special} s]
  (case (freeze (string/trim special))
    "." (display (calc/pop s))
    "p" (display (calc/peek s))


@@ 103,10 114,7 @@

(defn- handle-commands
  [s input]
  (each token input
    (match token
      (@ parser/Special {:value char}) (handle-special s char)
      _ (eval/eval-and-push s token))))
  (each token input (handle-token token s)))

(defn repl
  ```


@@ 116,7 124,7 @@
  (with-slots calc/Stack (env/new-env)
    (while true
      (def bak (array/slice (@ data)))
      (try (->> (getline @ (prompt @) @"")
      (try (->> (getline @ (repl-prompt @) @"")
                (handle-signal)
                (parser/parse)
                (handle-commands @))