Skip to content

Instantly share code, notes, and snippets.

@Idorobots
Created July 26, 2012 22:13
Show Gist options
  • Save Idorobots/3184915 to your computer and use it in GitHub Desktop.
Save Idorobots/3184915 to your computer and use it in GitHub Desktop.
Reader macro facility - proof of concept
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 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