gensym and hygienic macro

Started by starseed, August 03, 2006, 11:29:27 AM

Previous topic - Next topic

starseed

OK, while I was thinking, and learning newLisp, and tried to implement a gensym a gensym. It does not technically give you an unlimited number of different different symbols, but it should be enough for most real world problems.



(context 'gensym)
(set 'no 0)
(define (gensym:gensym)
   "creates lots of unique symbols ... is that enough?"
   (sym (string "___" (inc 'no 0.1))))
   
(context MAIN)


And a macro to create hygienic macros, using gensym.



(define-macro (def-macro)
   "creates macro definitions with automatically created symbols"
   (let ((__def-macro_def (clean (fn (x) (= ', x)) (rest (args 0))))
         (__def-macro_sym '()))
      (dolist (__def-macro_ __def-macro_def)
         (push (list __def-macro_ (gensym)) __def-macro_sym))
      (set '__def-macro_def (expand (args) __def-macro_sym))
      (push 'define-macro __def-macro_def)
      (eval __def-macro_def)
   )
)


Disclaimer: I'm a newbie when it comes to newLisp (and the whole lisp family), so take my code with a grain of salt. ;-)



Comments are highly appreciated.



Ingo