~subsetpark/ec

ref: 32fb47a265790f03d6024e74593aad9cb6bd07b9 ec/main.janet -rw-r--r-- 737 bytes
32fb47a2 — Zach Smith Working REPL 11 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
(import /src/calc)
(import /src/print)
(import /src/parser)

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

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

(defn handle-commands
  [in s]
  (case (freeze (string/trim in))
    "." (display (calc/peek s))
    "p" (display (calc/pop s))
    "s" (display-all (s :data))
    in))

(defn main
  [args]
  (let [s (:new calc/Stack)]
    (while true
      (let [buf @""
            prompt (string/format "[%s]> " (print/p (calc/peek s)))
            in (getline prompt buf parser/dictionary)
            after-commands (handle-commands in s)
            parsed (parser/parse after-commands)]
        (calc/push-all s parsed)))))