~subsetpark/ec

ref: 0e89cde9278c1775aaf0eedd9d7fc08e6871828c ec/src/repl.janet -rw-r--r-- 2.9 KiB
0e89cde9 — Zach Smith Update deps 6 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
(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} 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 prompt get-completions)]
    (buffer/push-string buf ln "\n")
    buf))

(defn- 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))))))

(defn- 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) ")")))))

(defn- describe-all
  [s q]
  (defn- describe
    [elem]
    (let [evaled (eval/eval s elem)]
      (print (format evaled))))
  (each item (q :data)
    (describe item)))

(defn- handle-special
  [s special]
  (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
    (match token
      @[:special patt] (handle-special s patt)
      _ (eval/eval-and-push s token))))

(defn repl
  ```
  Interactive read-eval-print-loop.
  ```
  []
  (with-slots calc/Stack (env/new-env)
    (while true
      (def bak (array/slice (@ data)))
      (try (->> (getline (@) (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))))