Created
April 14, 2021 13:48
-
-
Save rcullito/86d1080952759acd86c3e8a9627ffd60 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns recurse.core) | |
;; TODO most likely add first and list here so that our sample code is fully accounted for | |
(def lisp->clj {'+ +}) | |
(defn tokenize | |
"walks through lisp input string, if current element is not a collection, add it to the vector, | |
otherwise create a nested vector by calling tokenize on it" | |
[string] | |
(mapv (fn [token] | |
(if (coll? token) | |
(tokenize token) | |
token)) | |
string)) | |
;; test input will now be in our vector format | |
;; (def test-input (tokenize (read-string "(first (list 1 (+ 2 3) 9))"))) | |
;; test-input | |
;; (vector? test-input) | |
;; [first [list 1 [+ 2 3] 9]] | |
(defn READ [] | |
(-> (read-line) | |
read-string | |
tokenize)) | |
(defn EVAL [ast] | |
(cond | |
(symbol? ast) (get lisp->clj ast ast) ;; if lookup doesn't find anything, return ast | |
(vector? ast) (map EVAL ast) | |
:else ast)) | |
;; (EVAL test-input) | |
(defn PRINT [input] | |
(println input)) | |
(defn -main | |
"lisp parser" | |
[] | |
(-> (READ) | |
EVAL | |
PRINT)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment