Skip to content

Instantly share code, notes, and snippets.

@swannodette
Created June 15, 2011 05:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swannodette/1026540 to your computer and use it in GitHub Desktop.
Save swannodette/1026540 to your computer and use it in GitHub Desktop.
parser.clj
(def digits (into #{} "1234567890"))
(defn cr [c1 c2]
(map char (range (int c1) (int c2))))
(def alpha (into #{} (concat (cr \a \z) (cr \A \Z))))
(def alnum (into digits (concat (cr \a \z) (cr \A \Z))))
(def nonalnum (into #{} "+/-*><="))
(-->e wso
([\space] wso)
([]))
(def-->e digito [x]
([_] [x]
(!dcg
(nonrel/project [x]
(== (contains? digits x) true)))))
(def-->e numo [x]
([[?d . ?ds]] (digito ?d) (numo ?ds))
([[?d]] (digito ?d)))
(declare symro)
(def-->e symo [x]
([[?a . ?as]] [?a]
(!dcg
(nonrel/project [?a]
(conde
((== (contains? alpha ?a) true))
((== (contains? nonalnum ?a) true)))))
(symro ?as)))
(def-->e symro [x]
([[?a . ?as]] [?a]
(!dcg
(nonrel/project [?a]
(conde
((== (contains? alnum ?a) true))
((== (contains? nonalnum ?a) true)))))
(symro ?as))
([[]] []))
(declare exprso)
(def-->e expro [e]
([[:sym ?a]] (symo ?a))
([[:num ?n]] (numo ?n))
([[:list ?list]] [\(] (exprso ?list) [\)])
([[:sym :quote ?q]] [\'] (expro ?q)))
(defne exprso [exs l1 o]
([[?e . ?es] _ _]
(exist [l2 l3 l4]
(condu
((exist []
(wso l1 l2)
(expro ?e l2 l3)
(wso l3 l4))
(exprso ?es l4 o)))))
([[] _ _] (== l1 o)))
;; (([:list ([:sym (\+)] [:sym (\a \b \c)] [:sym (\b)] [:sym :quote [:list ([:num [\1]] [:num (\2 \3)])]])]))
(run 1 [q]
(exprso q (vec "(+ abc b '(1 23))") []))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment