Skip to content

Instantly share code, notes, and snippets.

@obriencj
Created August 4, 2017 19:34
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save obriencj/0dd5778416afb094e63ecf4c41450298 to your computer and use it in GitHub Desktop.
sibilant run and compile time special definition example
(defmacro pseudop (method . args)
(let ((as-name (symbol (+ "emit_" (str method))))
(method (symbol (+ "__compiler__.pseudop_" (str method)))))
(if args
`(defmacro ,as-name ,args
(cons ',method ,@args nil))
`(defmacro ,as-name ()
(cons ',method nil)))))
;; this is just extra laziness -- it creates an emit_BLA macro that
;; just calls __compiler__.pseudop_BLA
(pseudop position_of body)
(pseudop get_var varname)
(pseudop const value)
(pseudop call argc)
(defmacro defspecial (name args . &body)
`(define ,name (special (lambda ,args ,@&body) ,(str name))))
(defspecial push-symbol (env &body)
(emit_position_of &body)
(let ((called_as (first &body))
(sym (second &body)))
(emit_get_var "symbol")
(emit_const (str sym))
(emit_call 1))
;; needs to return None to let the compiler know that no further
;; transformations are needed. I do that explicitly, but the result
;; of the above should have also evaluated to None
None)
(let ((T (push-symbol tacos)))
(print (symbol? T)) ; True
(print T) ; tacos
(print (type T)) ; <class 'sibilant.Symbol'>
(print (repr T))) ; symbol('tacos')
;; The end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment