-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathreader.clj
More file actions
29 lines (24 loc) · 882 Bytes
/
reader.clj
File metadata and controls
29 lines (24 loc) · 882 Bytes
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
(ns reader
(:require [clojure.string :as str]))
(defn tokenize [exp]
(remove empty? (-> exp
(str/replace "(" " ( ")
(str/replace ")" " ) ")
(str/replace "'" " ' ")
(str/split #"\s+"))))
(declare micro-read read-list)
(defn micro-read [[t & ts]]
(case t
"(" (read-list '() ts)
"'" (let [[new-t new-ts] (micro-read ts)]
[(list "quote" new-t) new-ts])
[t ts]))
(defn read-list [list-so-far tokens]
(let [[t ts] (micro-read tokens)]
(case t
")" [(reverse list-so-far) ts]
"(" (let [[new-list new-tokens] (read-list '() ts)]
(read-list (cons new-list list-so-far) new-tokens))
(read-list (cons t list-so-far) ts))))
(defn read* [exp]
(first (micro-read (tokenize exp))))