Created
June 15, 2011 05:30
-
-
Save swannodette/1026540 to your computer and use it in GitHub Desktop.
parser.clj
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
(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