-
Notifications
You must be signed in to change notification settings - Fork 3
/
syntaxrulesxform.scm
52 lines (38 loc) · 1.82 KB
/
syntaxrulesxform.scm
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
49
50
51
52
;;;============================================================================
;;; File: "syntaxrulesxform.scm"
;;; Copyright (c) 2000-2014 by Marc Feeley, All Rights Reserved.
;;;============================================================================
;; This file implements an unhygienic version of the (syntax-rules ...)
;; form.
;;;----------------------------------------------------------------------------
(define (syn#syntax-rules->crules src)
(include "syntaxboot.scm") ;; get bootstrap versions of syntax-case and syntax forms
(syntax-case src ()
((_ (literal ...) (pattern template) ...)
(let* ((literals
(syntax->datum #'(literal ...)))
(patterns
(syntax->vector #'#(pattern ...)))
(templates
(syntax->vector #'#(template ...))))
(let loop ((i 0)
(crules-rev '()))
(if (< i (vector-length patterns))
(let ((pattern (vector-ref patterns i))
(template (vector-ref templates i)))
(syn#compile-pattern
pattern
literals
(lambda (cpattern pvars)
(let ((ctemplate (syn#compile-template template pvars)))
(loop (+ i 1)
(cons (vector cpattern ctemplate) crules-rev))))))
(reverse crules-rev)))))))
(define (syn#syntax-rules-form-transformer src)
(include "syntaxboot.scm") ;; get bootstrap versions of syntax-case and syntax forms
(include "withsyntaxboot.scm") ;; get bootstrap versions of with-syntax
(let ((crules (syn#syntax-rules->crules src)))
(with-syntax ((crules (datum->syntax src crules)))
#'(##lambda (##src)
(syn#apply-rules 'crules ##src)))))
;;;============================================================================