Skip to content

Add support for syntax-case and vanilla macro #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions define-library.scm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

(##include "syntax.scm")
(##include "syntaxrulesxform.scm")
(##include "syntaxcasexform.scm")
(##include "syntaxxform.scm")

(define-runtime-syntax define-syntax
(lambda (src)
Expand All @@ -28,6 +30,20 @@
(define-runtime-syntax syntax-rules
syn#syntax-rules-form-transformer)

(define-runtime-syntax syntax-case
syn#syntax-case-form-transformer)

(define-runtime-syntax syntax
(lambda (src) (syn#syntax-form-transformer src '())))

(define-runtime-syntax macro
(lambda (src)
(let ((locat (##source-locat src)))
(##make-source
`(##lambda (##src)
(##apply (##lambda ,@(cdr (##source-strip src))) (##cdr (##source-strip ##src))))
locat))))

;;;============================================================================

(define (keep keep? lst)
Expand Down Expand Up @@ -582,29 +598,25 @@
(pair? (cdr expr))
(symbol? (##source-strip (cadr expr)))
(pair? (cddr expr))
(let ((x (##source-strip (caddr expr))))
(and (pair? x)
(eq? (##source-strip (car x)) 'syntax-rules)))
(null? (cdddr expr))))
(done)
(let ((id (##source-strip (cadr expr)))
(crules (syn#syntax-rules->crules (caddr expr))))
(form (caddr expr)))

(define (generate-local-macro-def id crules expr-src)
(define (generate-local-macro-def id form expr-src)
(let ((locat (##source-locat expr-src)))
(##make-source
`(##define-syntax ,id
(##lambda (##src)
(syn#apply-rules ',crules ##src)))
,form)
locat)))

;; replace original define-syntax by local macro def
;; to avoid having to load syntax-rules implementation
(set-car! expr-srcs
(generate-local-macro-def id crules expr-src))
(generate-local-macro-def id form expr-src))

(loop (cdr expr-srcs)
(cons (cons id crules)
(cons (cons id form)
rev-macros))))))))

(let ((form (##source-strip src)))
Expand Down Expand Up @@ -684,10 +696,7 @@
(string-append
(idmap-namespace idmap)
(symbol->string id)))
(##lambda (src)
(syn#apply-rules
(##quote ,(cdr m))
src))))
,(cdr m)))
'())))
(idmap-macros idmap))))))

Expand Down
6 changes: 5 additions & 1 deletion test.scm
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
(begin

(define skip-test #f)
(define z 10)

(unless skip-test

;; should give "da39a3ee5e6b4b0d3255bfef95601890afd80709"
(pp (digest-string "" 'sha-1 'hex))

;; should give "a9993e364706816aba3e25717850c26c9cd0d89d"
(pp (digest-string "abc" 'sha-1 'hex)))))
(pp (digest-string "abc" 'sha-1 'hex)))

(when z
(pp (addn 5 z)))))
12 changes: 8 additions & 4 deletions when-unless.scm
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
(import (only (gambit)
if not begin)) ;; required by expansions of when and unless

(export when unless)
(export when unless addn)

(begin
(define-syntax addn
(macro args
`(##+ ,(car args) ,(cadr args))))

(define-syntax when
(syntax-rules ()
((_ test expr expr* ...)
(if test (begin expr expr* ...)))))
(lambda (x)
(syntax-case x ()
((_ test e e* ...)
#'(if test (begin e e* ...))))))

(define-syntax unless
(syntax-rules ()
Expand Down