Created
July 26, 2012 22:13
-
-
Save Idorobots/3184915 to your computer and use it in GitHub Desktop.
Reader macro facility - proof of concept
This file contains hidden or 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
;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; Simple PEG Lisp grammar: | |
;;;;;;;;;; | |
(syntax (Expression <- (/ Atom List))) | |
(syntax (Atom <- (/ Number Symbol))) | |
(syntax (Number <- "[0-9]+") | |
`(,(car Number) | |
(,(string->number (caadr Number))))) | |
(syntax (Symbol <- "[a-zA-Z0-9_]+") | |
`(,(car Symbol) | |
(,(string->symbol (caadr Symbol))))) | |
(syntax (List <- (: "\\(") (* Expression) (: "\\)")) | |
`(,(car List) | |
,(cdr List))) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; Let's extend it with a simple reader macro: | |
;; args -> expression ===> (lambda args expression) | |
;;;;;;;;;; | |
(syntax (Lambda <- (/ Symbol List) (: "->") Expression) | |
`(,(car Lambda) | |
((lambda ,(caadr Lambda) ,(cadadr Lambda))))) | |
;; Interaction with other rules clearly visible: | |
(syntax (Expression <- (/ Lambda Atom List))) | |
(Expression "(let ((sq (x) -> (* x x))) (sq 23))") | |
;; Parses to: (let ((sq (lambda (x) (* x x)))) (sq 23)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment