public
Created — forked from samth/gist:1652932

Macros

  • Download Gist
gistfile1.scm
Scheme
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
;; AST style:
 
((equal? term "cond")
(define (transform i)
(if (or (> i node.children.length)
(eq? i node.children.length))
null
(let ((n (vector-ref node.children i)))
(let ((condition (vector-ref n.children 0))
(res (ast.node ast.LIST
null
(vector-concat
(list (ast.node ast.TERM (make-symbol "begin")))
(n.children.slice 1)))))
(if (and (eq? condition.type ast.TERM)
(equal? condition.data.str "else"))
res
(ast.add_child
(ast.node ast.LIST
null
(list (ast.node ast.TERM (make-symbol "if"))
condition
res))
(transform (+ i 1))))))))
(let ((res (transform 1)))
;; maintain node links
(set! res.link node.link)
(parse res)))
 
;; define-macro style:
 
(define-macro (cond . forms)
(if (null? forms)
'#f
(let ((form (car forms)))
(if (eq? (car form) 'else)
`(begin ,@(cdr form))
`(if ,(car form)
(begin ,@(cdr form))
(cond ,@(cdr forms)))))))
 
;; syntax-parse style:
(define-syntax cond
(syntax-parser
[(cond) #'#f]
[(cond [(~literal else) rest ...] more ....) #'(begin rest ...)]
[(cond [tst rhs ...] more ...)
#'(if tst (begin rhs ...) (cond more ...))]))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.