Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
44 lines (39 sloc) 1.31 KB
(ns elisp-evaluator.parser
(:require [clojure.string :as str]))
(defn- parse-atom
"Numbers become numbers, everything else is a symbol"
(Integer/parseInt token)
(catch java.lang.NumberFormatException ex
(Double/parseDouble token)
(catch java.lang.NumberFormatException ex
(defn- parse-sexp
(loop [[token & remaining-tokens] tokens
parsed-tokens []]
(nil? token) [parsed-tokens, remaining-tokens]
(= token ")") [parsed-tokens, remaining-tokens]
(= token "(")
(let [[inner-parsed-tokens, remaining-tokens-after-parsing]
(parse-sexp remaining-tokens)]
(recur remaining-tokens-after-parsing (conj parsed-tokens inner-parsed-tokens)))
(recur remaining-tokens (conj parsed-tokens (parse-atom token))))))
(defn parse
"Takes in stream of tokens, returns AST"
(let [[parsed-tokens, remaining-tokens] (parse-sexp tokens)]
(get parsed-tokens 0)))
(defn tokenize
"Takes in string of characters, returns list of tokens"
([] [])
(let [tokens (-> input-string
(str/replace #"\(" " ( ")
(str/replace #"\)" " ) ")
(str/split #" "))]
(filter #(not (empty? %)) tokens))))
You can’t perform that action at this time.