aboutsummaryrefslogtreecommitdiff
path: root/clojure/src/step1_read_print.clj
blob: 21b297d33d3c2efccec32323db011e006b27c48f (plain)
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
(ns step1-read-print
    (:require [clojure.repl]
              [readline]
              [reader]
              [printer]))

;; read
(defn READ [& [strng]]
  (let [line (if strng strng (read-line))]
    (reader/read-string strng)))

;; eval
(defn EVAL [ast env]
  ast)

;; print
(defn PRINT [exp] (pr-str exp))

;; repl
(defn rep
  [strng]
  (PRINT (EVAL (READ strng) {})))

;; repl loop
(defn repl-loop []
  (let [line (readline/readline "user> ")]
    (when line
      (when-not (re-seq #"^\s*$|^\s*;.*$" line) ; blank/comment
        (try
          (println (rep line))
          (catch Throwable e
            (clojure.repl/pst e))))
      (recur))))

(defn -main [& args]
  (repl-loop))