~subsetpark/ec

ref: 7304ccde8737d14a9793f04b9f463ecd8fe7dd41 ec/src/repl.janet -rw-r--r-- 3.2 KiB
7304ccde — Zach Smith make handle-token a generic 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
(use fugue)

(import rl)
(import /src/env)
(import /src/print)
(import /src/eval)
(import /src/calc)
(import /src/parser)

(defn- env-keys
  [env]
  (let [these-keys (keys env)
        proto (table/getproto env)]
    (match proto
      nil these-keys
      proto (array/concat these-keys (env-keys proto)))))

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

  (defn get-completions
    [line start end]
    (try
      (let [to-expand (string (string/slice line start end))]
        (->>
          (env-keys env)
          (filter (fn [s] (string/has-prefix? to-expand s)))))

      ([err f]
        (eprint "error in tab completion function:")
        (debug/stacktrace f err)
        @[])))

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

(defn- repl-prompt
  [s]
  (->> s
       (calc/peek)
       (print/p)
       (print/truncate)
       (string/format "<%s> $ ")))

(defn- handle-signal
  [input]
  (let [str (string input)]
    (when (= str "") (os/exit 0))
    str))

(defn- display
  [data]
  (print (print/p data)))

(defn- display-all
  [stack]
  (let [inner (map print/p stack)]
    (print (string/join inner " "))))

(defn- display-help
  [{:env env}]
  (each k (env-keys env)
    (when-let [v (env k)]
      (printf "%s: %s" k (string (v :type))))))

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

                 %s%s
                 ```
                 (print/p obj)
                 (string (obj :type))
                 (obj :doc)
                 (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
    [elem]
    (let [evaled (eval/eval s elem)]
      (print (format evaled))))
  (each item (q :data)
    (describe item)))

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

(defn- handle-commands
  [s input]
  (each token input (handle-token token s)))

(defn repl
  ```
  Interactive read-eval-print-loop.
  ```
  []
  (with-slots calc/Stack (env/new-env)
    (while true
      (def bak (array/slice (@ data)))
      (try (->> (getline @ (repl-prompt @) @"")
                (handle-signal)
                (parser/parse)
                (handle-commands @))

        ([err fib]
          (eprint err)
          (if (os/getenv "EC_TRACEBACK")
            (propagate err fib)
            (set (@ data) bak)))))))

(defn handle-lines
  ```
  Handle a single input line from the command line.
  ```
  [lines]
  (with-slots calc/Stack (env/new-env)
    (each line lines
      (->> line
           (parser/parse)
           (handle-commands @)))
    (display-all (@ data))))