(load "compat-r5rs.scm")
(load "runtime.scm")
(load "expander.scm")
(ex:expand-file "standard-libraries.scm" "standard-libraries.exp")
(load "standard-libraries.exp")
(ex:expand-file "sample-libs-and-program.scm" "sample-libs-and-program.exp")
(load "sample-libs-and-program.exp")
(ex:expand-file "sample-stack.scm" "sample-stack.exp")
(ex:expand-file "sample-balloons.scm" "sample-balloons.exp")
(ex:expand-file "sample-party.scm" "sample-party.exp")
(ex:expand-file "sample-program.scm" "sample-program.exp")
(load "sample-stack.exp")
(load "sample-balloons.exp")
(load "sample-party.exp")
(load "sample-program.exp")
(ex:repl
'(
(import (rnrs))
(import (only (rnrs eval) environment)
(only (rnrs eval reflection) environment-bindings))
(library (stack)
(export make push! pop! empty!)
(import (rnrs)
(rnrs mutable-pairs))
(define (make)
(list '()))
(define (push! s v)
(set-car! s (cons v (car s))))
(define (pop! s)
(let ((v (caar s))) (set-car! s (cdar s)) v))
(define (empty! s)
(set-car! s '()))
)
(library (balloons)
(export make push pop)
(import (rnrs))
(define (make w h)
(cons w h))
(define (push b amt)
(cons (- (car b) amt) (+ (cdr b) amt)))
(define (pop b)
(display "Boom! ")
(display (* (car b) (cdr b)))
(newline))
)
(library (party)
(export (rename (balloon:make make) (balloon:push push))
push! make-party
(rename (party-pop! pop!)))
(import (rnrs)
(only (stack) make push! pop!) (prefix (balloons) balloon:))
(define (make-party)
(let ((s (make))) (push! s (balloon:make 10 10))
(push! s (balloon:make 12 9)) s))
(define (party-pop! p)
(balloon:pop (pop! p)))
)
(library (main)
(export)
(import (rnrs) (party))
(define p (make-party))
(pop! p) (push! p (push (make 5 5) 1))
(pop! p))
(program
(import (main)))
(import (main))
(environment-bindings (environment '(party)))
(library (my-helpers id-stuff)
(export find-dup)
(import (rnrs))
(define (find-dup l)
(and (pair? l)
(let loop ((rest (cdr l)))
(cond ((null? rest)
(find-dup (cdr l)))
((bound-identifier=? (car l) (car rest))
(car rest))
(else (loop (cdr rest)))))))
)
(library (my-helpers value-stuff)
(export mvlet)
(import (rnrs)
(for (my-helpers id-stuff) expand))
(define-syntax mvlet
(lambda (stx)
(syntax-case stx ()
((_ ((id ...) expr) body0 body ...)
(not (find-dup (syntax (id ...))))
(syntax
(call-with-values
(lambda () expr)
(lambda (id ...) body0 body ...))))))))
(environment-bindings (environment '(for (my-helpers id-stuff) expand)))
(library (let-div)
(export let-div)
(import (rnrs) (my-helpers value-stuff))
(define (quotient+remainder n d)
(let ((q (floor (/ n d))))
(values q (- n (* q d)))))
(define-syntax let-div
(syntax-rules ()
((_ n d (q r) body0 body ...)
(mvlet ((q r) (quotient+remainder n d))
body0 body ...))))
)
(program
(import (let-div) (rnrs))
(let-div 5 2 (q r) (+ q r)) )
(library (foo (2 3 5))
(export)
(import))
(import (foo ()))
(import (foo (2)))
(import (foo (2 3)))
(import (foo (or (1 (>= 1)) (2))))
(import (foo ((or 1 2 3))))
(program
(import (rnrs))
(define x 1)
(set! x 2)
(values)
(display 4) (values 2 3)
(define y 3)
(+ x y))
(library (foo)
(export u)
(import (rnrs))
(define u 1))
(library (bar)
(export u v)
(import (rnrs) (foo))
(define-syntax v (lambda (e) (syntax u))))
(library (baz)
(export)
(import (for (rnrs) (meta 2) expand run)
(for (bar) (meta 2)))
(display
(let-syntax ((m (lambda (e)
(let-syntax ((n (lambda (e) (+ u (v)))))
(n)))))
(m))))
(import (baz))
(library (foo)
(export x y)
(import (rnrs))
(define x 2)
(define y 4))
(library (baz)
(export y) (import (rnrs) (for (foo) expand)))
(environment-bindings (environment '(baz)))
(library (bab)
(export f)
(import (for (rnrs) expand run) (for (foo) expand) (for (baz) expand)) (define (f)
(let-syntax ((foo (lambda (_)
(+ x y (let-syntax ((bar (lambda (_) y))) (bar))))))
(foo))))
(environment-bindings (environment '(for (foo) expand)
'(for (baz) expand)))
(import (bab))
(f)
(library (foo)
(export f)
(import (rnrs))
(define (f) 1))
(library (bar)
(export g)
(import (rnrs)
(for (foo) expand)) (define-syntax g
(syntax-rules ()
((_) (f)))))
(library (A)
(export x)
(import (rnrs))
(define x 37))
(library (B)
(export)
(import (A)))
(library (C)
(export foo)
(import (rnrs) (for (A) expand))
(define-syntax foo
(syntax-rules ()
((_) x))))
(library (D)
(export foo)
(import (rnrs) (C)))
(library (E)
(export)
(import (rnrs) (B) (D))
)
(library (foo)
(export x)
(import (rnrs))
(define x 42))
(library (bar)
(export get-x)
(import (rnrs)
(for (foo) (meta -1)))
(define (get-x) (syntax x)))
(library (baz)
(export)
(import (for (rnrs) (meta 3) (meta 2) expand run)
(for (bar) (meta 3) expand))
(display
(let-syntax ((m (lambda (ignore)
(get-x))))
(m)))
(display
(let-syntax ((m (lambda (ignore)
(let-syntax ((n (lambda (ignore)
(let-syntax ((o (lambda (ignore)
(get-x))))
(o)))))
(n)))))
(m)))
)
(import (baz))
(library (print)
(export print-args)
(import (rnrs))
(define print-args
(lambda (fml* act*)
(display "Lambda ")
(display fml*)
(display " : ")
(display act*)
(newline))))
(library (tracers-helpers)
(export trace-transformer untrace-transformer)
(import (for (rnrs) (meta -1) run)
(for (print) (meta -1)))
(define trace-transformer
(lambda (stx)
(syntax-case stx ()
((_ fml* b b* ...)
(syntax
(lambda act*
(print-args 'fml* act*)
(apply (lambda fml* b b* ...) act*)))))))
(define untrace-transformer
(lambda (stx)
(syntax-case stx ()
((_ fml* b b* ...)
(syntax
(lambda fml* b b* ...)))))))
(library (tracers)
(export trace-region untrace-region)
(import (rnrs)
(for (tracers-helpers) expand))
(define-syntax trace-region
(lambda (x)
(syntax-case x ()
((kwd b b* ...)
(with-syntax ((L (datum->syntax (syntax kwd) 'lambda)))
(syntax
(let-syntax ((L trace-transformer))
b b* ...)))))))
(define-syntax untrace-region
(lambda (x)
(syntax-case x ()
((kwd b b* ...)
(with-syntax ((L (datum->syntax (syntax kwd) 'lambda)))
(syntax
(let-syntax ((L untrace-transformer))
b b* ...))))))))
(library (FOO)
(export)
(import (rnrs) (tracers))
(define a (lambda (q) (display "A not traced\n")))
(trace-region
(define b (lambda (r) (display "did it work in B?\n")))
(untrace-region
(define c (lambda (s) (display "C not traced\n"))))
(define d (lambda (t) (display "did it work in D?\n"))))
(a 'a)
(b 'b)
(c 'c)
(d 'd))
(import (FOO))
(library (foo)
(export counter)
(import (rnrs))
(define counter
(let ((x 0))
(lambda ()
(set! x (+ x 1))
x))))
(library (bar)
(export)
(import (rnrs)
(for (foo) run expand))
(let-syntax ((m (lambda (e) (counter))))
(display (list (m) (counter)))))
(import (bar))
(library (baz)
(export)
(import (for (rnrs) run expand)
(for (foo) expand (meta 2)))
(let-syntax ((_ (let-syntax ((m (lambda (e) (counter))))
(display (list (m) (counter)))
(lambda (_) _))))))
(library (foo)
(export f)
(import (rnrs))
(define f
(let ((x 0))
(lambda ()
(set! x (+ x 1))
x))))
(library (bar)
(export m)
(import (rnrs) (for (foo) expand))
(define-syntax m
(lambda (e)
(f))))
(library (baz)
(export)
(import (for (rnrs) run expand)
(for (bar) expand (meta 2)))
(let-syntax ((n (lambda (e)
(let-syntax ((o (lambda (e) (m))))
(+ (m) (o))))))
(display (n))))
(import (baz))
(library (baz)
(export x)
(import (rnrs))
(define x 1))
(library (foo)
(export template)
(import (for (baz) expand)
(for (rnrs) run (meta -1)))
(define (template)
(syntax (let-syntax ((m (lambda (_)
(let-syntax ((n (lambda (_) x))) (n)))))
(m))))
(let-syntax ((n (lambda (_) x))) (n)))
(library (bar)
(export)
(import (rnrs)
(for (foo) expand))
(let-syntax ((n (lambda (_) (template))))
(display (n)))
)
(import (bar))
(library (D)
(export put! get)
(import (rnrs))
(define v #f)
(define (put! x) (set! v x))
(define (get) v))
(library (B)
(export b)
(import (rnrs))
(define b 7))
(library (A)
(export do-a)
(import (rnrs)
(for (D) expand)
(for (B) run))
(define-syntax do-a
(begin (put! (syntax b))
(lambda (stx)
#f))))
(library (C)
(export)
(import (for (rnrs) run expand)
(for (D) expand)
(for (A) run))
(define-syntax make-ref
(lambda (stx)
(get)))
(display (make-ref)))
(import (C))
(library (foo)
(export test)
(import (rnrs base))
(define-syntax test
(syntax-rules (car)
((_ car) #t)
((_ k) #f))))
(library (bar)
(export)
(import (for (rnrs base) (meta 21))
(foo))
(test cdr))
(library (foo)
(export test)
(import (except (rnrs base) car)
(for (only (rnrs base) car) (meta 21)))
(define-syntax test
(syntax-rules (car)
((_ car) #t) ((_ k) #f))))
(library (bar)
(export)
(import (rnrs base)
(foo))
(test cdr))
(library (foo)
(export test1 test2)
(import (except (rnrs) car)
(for (only (rnrs) car) (meta 21)))
(define-syntax test1
(lambda (form)
(free-identifier=? (syntax car) (syntax cdr))))
(define-syntax test2
(lambda (form)
(free-identifier=? (syntax car) (syntax car)))))
(import (foo))
(test1)
(library (foo)
(export bar)
(import (rnrs))
(define-syntax bar (syntax-rules () ((_) (baz)))))
(import (foo))
(define (baz) 1)
(library (foo)
(export bar)
(import (rnrs))
(define-syntax bar
(syntax-rules (unbound-literal)
((_ unbound-literal) #t)
((_ _) #f))))
(import (foo))
(bar unbound-literal) (bar x) (define unbound-literal 1)
(bar unbound-literal)
(library (records-helper)
(export register! registered?)
(import (rnrs))
(define table '())
(define (register! name)
(set! table (cons name table)))
(define (registered? name)
(memp (lambda (entry) (free-identifier=? name entry))
table)))
(library (records)
(export define-record record-switch)
(import (rnrs) (for (records-helper) expand))
(define-syntax define-record
(lambda (form)
(syntax-case form ()
((_ name)
(syntax
(begin
(define name 'record-type-descriptor)
(define-syntax dummy
(begin
(register! (syntax name))
(lambda (form) 'never-used)))))))))
(define-syntax record-switch
(lambda (form)
(syntax-case form ()
((_ exp (name consequence))
(if (registered? (syntax name))
(syntax (if (eq? exp 'name) consequence "no match"))
(syntax-violation #f "Invalid record type" (syntax name))))))))
(library (zoo)
(export zebra)
(import (records))
(define-record zebra))
(library (metrics)
(export)
(import (rnrs) (zoo) (records))
(display
(record-switch 'zebra (zebra 'zebra))))
(import (metrics))
(library (foo)
(export x bar baz bax
)
(import (rnrs))
(define x 1)
(define y 1)
(define z 1)
(define-syntax bar
(syntax-rules ()
((_) (set! x 2))))
(define-syntax baz
(syntax-rules ()
((_) (set! y 2))))
(define-syntax bax
(syntax-rules ()
((_) z)))
(set! z 2))
(library (boo)
(export)
(import (rnrs)
(foo))
)
(let ()
(define-syntax foo (lambda (e) (let ((+ -)) (+ 1 2))))
(define + 2)
(foo))
(let ((x #f))
(let-syntax ((foo (syntax-rules (x)
((_ x y) (define y 'outer))
((_ _ y) (define y 'inner)))))
(let ()
(define x #f)
(foo x p)
p)))
(let ()
(define-syntax foo
(syntax-rules ()
((_ def0) (def0 define 17))))
(foo define)
0)
(let ()
(define-syntax list-macro
(syntax-rules ()
((_ x ...) (list x ...))))
(define list cons)
(list-macro 1 2))
(let ()
(define-syntax macro
(let ((x `(+ ,2)))
(lambda (form) (cadr x))))
(define + 2)
(macro))
(import (rnrs eval))
(eval '(+ 1 2)
(environment '(rnrs)))
(library (foo)
(export foo-x foo-y)
(import (rnrs))
(define foo-x 4)
(define-syntax foo-y (syntax-rules () ((_) 22))))
(eval '(+ 1 (let-syntax ((foo (lambda (_) (+ foo-x (foo-y)))))
(foo)))
(environment '(rnrs) '(for (foo) expand)))
(library (bar)
(export)
(import (rnrs)
(rnrs eval))
(display
(eval '(+ 1 (let-syntax ((foo (lambda (_) foo-x)))
(foo)))
(environment '(rnrs) '(for (foo) expand)))))
(import (bar))
(import (for (rnrs) run expand (meta 2)))
(let-syntax ((m (lambda (e)
(let-syntax ((n (lambda (e) 3)))
(n)))))
(m))
(syntax-case '((1 2) (3 4)) ()
(((x ...) ...) (syntax (x ... ...))))
(syntax-case '(1 2 3 4) ()
((x ... y z) (syntax ((x ...) y z))))
(syntax-case '(1 2 3 . 4) ()
((x ... y . z) (syntax ((x ...) y z))))
(syntax-case '#(1 2 3 4) ()
(#(x ... y z) (syntax (#(x ...) y z))))
(syntax-case '((1 2) (3 4)) ()
(((a b) ...) (syntax ((a ...) (b ...)))))
(syntax-case '((1 2) 3) ()
(((a b) ...) (syntax ((a ...) (b ...))))
(_ #f))
(syntax-case '((1 2) (3 4) . 3) ()
(((a b) ... . c) (syntax ((a ...) (b ...)))))
(let-syntax ((foo (syntax-rules ()
((_ _ _) 'yes))))
(foo 3 4))
(define-syntax foo
(lambda (e)
(or (identifier? e)
(syntax-violation 'foo "Invalid expression" e))
40))
foo
(import (rnrs mutable-pairs))
(define p (cons 4 5))
(define-syntax p.car
(make-variable-transformer
(lambda (x)
(syntax-case x (set!)
((set! _ e) (syntax (set-car! p e)))
((_ . rest) (syntax ((car p) . rest)))
(_ (syntax (car p)))))))
(set! p.car 15)
p.car p
(define p (cons 4 5))
(define-syntax p.car (identifier-syntax (car p)))
p.car
(define p (cons 4 5))
(define-syntax p.car
(identifier-syntax
(_ (car p))
((set! _ e) (set-car! p e))))
(set! p.car 15)
p.car p
(define (f) (g))
(define (g) 15)
(f)
(define-syntax foo (lambda (_) (syntax (bar))))
(define-syntax bar (lambda (_) 1))
(foo)
(let ((x 'outer))
(define-syntax foo
(syntax-rules ()
((_ lhs) (define lhs x))))
(foo (f))
(define x 'inner)
(f))
(let ()
(define-syntax odd
(syntax-rules ()
((odd) #t)
((odd x . y) (not (even . y)))))
(define-syntax even
(syntax-rules ()
((even) #f)
((even x . y) (not (odd . y)))))
(odd x x x))
(let ()
(define-syntax foo
(syntax-rules ()
((_) bar)))
(define bar 1)
(foo))
(define x 1)
(let-syntax ((foo (lambda (e)
(syntax (begin
(define x 2)
x)))))
(foo)) x
(let ()
(letrec-syntax ((foo (syntax-rules ()
((_) (begin (define (x) 1)
(begin
(define-syntax y
(syntax-rules ()
((_) (x))))
(bar y))))))
(bar (syntax-rules ()
((_ y) (begin (define (z) (baz (y)))
(z)))))
(baz (syntax-rules ()
((baz z) z))))
(foo)))
(let ((foo /))
(letrec-syntax ((foo (syntax-rules ()
((_ z) (begin (define (x) 4)
(define-syntax y
(syntax-rules ()
((_) (x))))
(bar z y)))))
(bar (syntax-rules ()
((_ z y) (define (z) (baz (y))))))
(baz (syntax-rules ()
((baz z) z))))
(let-syntax ((foobar (syntax-rules () ((_ u z)
(define-syntax u
(syntax-rules ()
((_ x y) (z x y))))))))
(foo a)
(foobar gaga goo))) (define-syntax goo (syntax-rules ()
((_ x y) (define-syntax x
(syntax-rules ()
((_) y))))))
(gaga b (a))
(foo (b)))
(library (test)
(export)
(import (rnrs))
(let-syntax ((foo (syntax-rules ()
((_ bar)
(begin
(define x 7)
(define-syntax bar
(syntax-rules ()
((_) (display x)))))))))
(foo baz)
(baz)))
(import (test))
(let ((a 1)
(b 2))
(+ a b))
(define-syntax swap!
(lambda (exp)
(syntax-case exp ()
((_ a b)
(syntax
(let ((temp a))
(set! a b)
(set! b temp)))))))
(let ((temp 1)
(set! 2))
(swap! set! temp)
(values temp set!))
(let ((x 'outer))
(let-syntax ((foo (lambda (exp) (syntax x))))
(let ((x 'inner))
(foo))))
(let ()
(define-syntax foo
(syntax-rules ()
((foo x) (define x 37))))
(foo a)
a)
(case 'a
((b c) 'no)
((d a) 'yes))
(let ((x 1))
(let-syntax ((foo (lambda (exp) (syntax x))))
(let ((x 2))
(foo))))
(let ((x 1))
(let-syntax ((foo (lambda (exp) (datum->syntax (syntax y) 'x))))
(let ((x 2))
(foo))))
(let-syntax ((foo (lambda (exp)
(let ((id (cadr exp)))
(bound-identifier=? (syntax x)
(syntax id))))))
(foo x))
(cond (#f 1) (else 2)) (let ((else #f)) (cond (else 2)))
(let-syntax ((m (lambda (form)
(syntax-case form ()
((_ x) (syntax
(let-syntax ((n (lambda (_)
(syntax (let ((x 4)) x)))))
(n))))))))
(m z))
(+ (let-syntax ((foo (lambda (e) 1)))
(display 'foo)
(foo))
2)
(+ (begin (display 'foo)
1)
2)
(define-syntax if-it
(lambda (x)
(syntax-case x ()
((k e1 e2 e3)
(with-syntax ((it (datum->syntax (syntax k) 'it)))
(syntax (let ((it e1))
(if it e2 e3))))))))
(define-syntax when-it
(lambda (x)
(syntax-case x ()
((k e1 e2)
(with-syntax ((it* (datum->syntax (syntax k) 'it)))
(syntax (if-it e1
(let ((it* it)) e2)
(if #f #f))))))))
(define-syntax my-or
(lambda (x)
(syntax-case x ()
((k e1 e2)
(syntax (if-it e1 it e2))))))
(if-it 2 it 3) (when-it 42 it) (my-or 2 3)
(let ((it 1)) (if-it 42 it #f)) (let ((it 1)) (when-it 42 it)) (let ((it 1)) (my-or #f it)) (let ((if-it 1)) (when-it 42 it))
(let-syntax ((m (lambda (form)
(syntax-case form ()
((_ x ...)
(with-syntax ((::: (datum->syntax (syntax here) '...)))
(syntax
(let-syntax ((n (lambda (form)
(syntax-case form ()
((_ x ... :::)
(syntax `(x ... :::)))))))
(n a b c d)))))))))
(m u v))
(let-syntax ((m (lambda (form)
(syntax-case form ()
((_ x ...)
(syntax
(let-syntax ((n (lambda (form)
(syntax-case form ()
((_ x ... (... ...))
(syntax `(x ... (... ...))))))))
(n a b c d))))))))
(m u v))
(define-syntax or
(syntax-rules ()
((or) #f)
((or e) e)
((or e1 e ...) (let ((temp e1))
(if temp temp (or e ...))))))
(or #f #f 1)
(define-syntax or
(lambda (form)
(syntax-case form ()
((or) (syntax #f))
((or e) (syntax e))
((or e1 e ...) (syntax (let ((temp e1))
(if temp temp (or e ...))))))))
(or #f #f 1)
(let-syntax ((when (syntax-rules ()
((when test stmt1 stmt2 ...)
(if test
(begin stmt1
stmt2 ...))))))
(let ((if #t))
(when if (set! if 'now))
if))
(let ((x 'outer))
(let-syntax ((m (syntax-rules () ((m) x))))
(let ((x 'inner))
(m))))
(letrec-syntax
((my-or (syntax-rules ()
((my-or) #f)
((my-or e) e)
((my-or e1 e2 ...)
(let ((temp e1))
(if temp
temp
(my-or e2 ...)))))))
(let ((x #f)
(y 7)
(temp 8)
(let odd?)
(if even?))
(my-or x
(let temp)
(if y)
y)))
(define-syntax cond
(syntax-rules (else =>)
((cond (else result1 result2 ...))
(begin result1 result2 ...))
((cond (test => result))
(let ((temp test))
(if temp (result temp))))
((cond (test => result) clause1 clause2 ...)
(let ((temp test))
(if temp
(result temp)
(cond clause1 clause2 ...))))
((cond (test)) test)
((cond (test) clause1 clause2 ...)
(let ((temp test))
(if temp
temp
(cond clause1 clause2 ...))))
((cond (test result1 result2 ...))
(if test (begin result1 result2 ...)))
((cond (test result1 result2 ...)
clause1 clause2 ...)
(if test
(begin result1 result2 ...)
(cond clause1 clause2 ...)))))
(let ((=> #f))
(cond (#t => 'ok)))
(cond ('(1 2) => cdr))
(cond ((> 3 2) 'greater)
((< 3 2) 'less)) (cond ((> 3 3) 'greater)
((< 3 3) 'less)
(else 'equal))
(let-syntax ((foo
(syntax-rules ()
((_ expr) (+ expr 1)))))
(let ((+ *))
(foo 3)))
(let-syntax ((foo (syntax-rules ()
((_ var) (define var 1)))))
(let ((x 2))
(begin (define foo +))
(cond (else (foo x)))
x))
(let ((x 1))
(let-syntax
((foo (syntax-rules ()
((_ y) (let-syntax
((bar (syntax-rules ()
((_) (let ((x 2)) y)))))
(bar))))))
(foo x)))
(let ((x 1))
(let-syntax
((foo (syntax-rules ()
((_ y) (let-syntax
((bar (syntax-rules ()
((_ x) y))))
(bar 2))))))
(foo x)))
(let ((a 1))
(letrec-syntax
((foo (syntax-rules ()
((_ b)
(bar a b))))
(bar (syntax-rules ()
((_ c d)
(cons c (let ((c 3))
(list d c 'c)))))))
(let ((a 2))
(foo a))))
(let ((=> #f))
(cond (#t => 'ok)))
(cond ('(1 2) => cdr))
(cond ((< 3 2) 'less)
((> 3 2) 'greater))
(cond ((> 3 3) 'greater)
((< 3 3) 'less)
(else 'equal))
(define-syntax loop (lambda (x)
(syntax-case x ()
((k e ...)
(with-syntax ((break (datum->syntax (syntax k) 'break)))
(syntax (call-with-current-continuation
(lambda (break)
(let f () e ... (f))))))))))
(let ((n 3) (ls '()))
(loop
(if (= n 0) (break ls))
(set! ls (cons 'a ls))
(set! n (- n 1))))
(let ((x '(1 3 5 7 9)))
(do ((x x (cdr x))
(sum 0 (+ sum (car x))))
((null? x) sum)))
(define-syntax define-structure
(lambda (x)
(define gen-id
(lambda (template-id . args)
(datum->syntax template-id
(string->symbol
(apply string-append
(map (lambda (x)
(if (string? x)
x
(symbol->string
(syntax->datum x))))
args))))))
(syntax-case x ()
((_ name field ...)
(with-syntax
((constructor (gen-id (syntax name) "make-" (syntax name)))
(predicate (gen-id (syntax name) (syntax name) "?"))
((access ...)
(map (lambda (x) (gen-id x (syntax name) "-" x))
(syntax (field ...))))
((assign ...)
(map (lambda (x) (gen-id x "set-" (syntax name) "-" x "!"))
(syntax (field ...))))
(structure-length (+ (length (syntax (field ...))) 1))
((index ...) (let f ((i 1) (ids (syntax (field ...))))
(if (null? ids)
'()
(cons i (f (+ i 1) (cdr ids)))))))
(syntax (begin
(define constructor
(lambda (field ...)
(vector 'name field ...)))
(define predicate
(lambda (x)
(and (vector? x)
(= (vector-length x) structure-length)
(eq? (vector-ref x 0) 'name))))
(define access (lambda (x) (vector-ref x index))) ...
(define assign
(lambda (x update)
(vector-set! x index update)))
...)))))))
(define-structure tree left right)
(define t
(make-tree
(make-tree 0 1)
(make-tree 2 3)))
t (tree? t) (tree-left t) (tree-right t) (set-tree-left! t 0)
t
(define-syntax swap!
(lambda (e)
(syntax-case e ()
((_ a b)
(let ((a (syntax a))
(b (syntax b)))
(quasisyntax
(let ((temp (unsyntax a)))
(set! (unsyntax a) (unsyntax b))
(set! (unsyntax b) temp))))))))
(let ((temp 1)
(set! 2))
(swap! set! temp)
(values temp set!))
(define-syntax case
(lambda (x)
(syntax-case x ()
((_ e c1 c2 ...)
(quasisyntax
(let ((t e))
(unsyntax
(let f ((c1 (syntax c1))
(cmore (syntax (c2 ...))))
(if (null? cmore)
(syntax-case c1 (else)
((else e1 e2 ...) (syntax (begin e1 e2 ...)))
(((k ...) e1 e2 ...) (syntax (if (memv t '(k ...))
(begin e1 e2 ...)))))
(syntax-case c1 ()
(((k ...) e1 e2 ...)
(quasisyntax
(if (memv t '(k ...))
(begin e1 e2 ...)
(unsyntax
(f (car cmore) (cdr cmore))))))))))))))))
(case 'a
((b c) 'no)
((d a) 'yes))
(define-syntax let-in-order
(lambda (form)
(syntax-case form ()
((_ ((i e) ...) e0 e1 ...)
(let f ((ies (syntax ((i e) ...)))
(its (syntax ())))
(syntax-case ies ()
(() (quasisyntax (let (unsyntax its) e0 e1 ...)))
(((i e) . ies) (with-syntax (((t) (generate-temporaries '(t))))
(quasisyntax
(let ((t e))
(unsyntax
(f (syntax ies)
(quasisyntax
((i t) (unsyntax-splicing its)))))))))))))))
(let-in-order ((x 1)
(y 2))
(+ x y))
(let-syntax ((test-ellipses-over-unsyntax
(lambda (e)
(let ((a (syntax a)))
(with-syntax (((b ...) '(1 2 3)))
(quasisyntax
(quote ((b (unsyntax a)) ...))))))))
(test-ellipses-over-unsyntax))
(let-syntax ((test
(lambda (_)
(quasisyntax
'(list (unsyntax (+ 1 2)) 4)))))
(test))
(let-syntax ((test
(lambda (_)
(let ((name (syntax a)))
(quasisyntax '(list (unsyntax name) '(unsyntax name)))))))
(test))
(let-syntax ((test
(lambda (_)
(quasisyntax '(a (unsyntax (+ 1 2)) (unsyntax-splicing (map abs '(4 -5 6))) b)))))
(test))
(let-syntax ((test
(lambda (_)
(quasisyntax '((foo (unsyntax (- 10 3))) (unsyntax-splicing (cdr '(5))) . (unsyntax (car '(7))))))))
(test))
(let-syntax ((test
(lambda (_)
(quasisyntax (unsyntax (+ 2 3))))))
(test))
(let-syntax ((test
(lambda (_)
(quasisyntax
'(a (quasisyntax (b (unsyntax (+ 1 2)) (unsyntax (foo (unsyntax (+ 1 3)) d)) e)) f)))))
(test))
(let-syntax ((test
(lambda (_)
(let ((name1 (syntax x)) (name2 (syntax y)))
(quasisyntax
'(a (quasisyntax (b (unsyntax (unsyntax name1)) (unsyntax (syntax (unsyntax name2))) d)) e))))))
(test))
(let-syntax ((test
(lambda (_)
(quasisyntax '(a (unsyntax 1 2) b)))))
(test))
(let-syntax ((test
(lambda (_)
(quasisyntax '(a (unsyntax-splicing '(1 2) '(3 4)) b)))))
(test))
(let-syntax ((test
(lambda (_)
(let ((x (syntax (a b c))))
(quasisyntax '(quasisyntax ((unsyntax (unsyntax x))
(unsyntax-splicing (unsyntax x))
(unsyntax (unsyntax-splicing x))
(unsyntax-splicing (unsyntax-splicing x)))))))))
(test))
`(list ,(+ 1 2) 4)
(let ((name 'a)) `(list ,name ',name))
`(a ,(+ 1 2) ,@(map abs '(4 -5 6)) b)
`(( foo ,(- 10 3)) ,@(cdr '(c)) . ,(car '(cons)))
`#(10 5 ,(sqrt 4) ,@(map sqrt '(16 9)) 8)
(let ((name 'foo))
`((unquote name name name)))
(let ((name '(foo)))
`((unquote-splicing name name name)))
(let ((q '((append x y) (sqrt 9))))
``(foo ,,@q))
(let ((x '(2 3))
(y '(4 5)))
`(foo (unquote (append x y) (sqrt 9))))
`(a `(b ,(+ 1 2) ,(foo ,(+ 1 3) d) e) f)
(let ((name1 'x)
(name2 'y))
`(a `(b ,,name1 ,',name2 d) e))
(import (rnrs control))
(when (> 3 2) 'greater) (when (< 3 2) 'greater) (unless (> 3 2) 'less) (unless (< 3 2) 'less)
(do ((vec (make-vector 5))
(i 0 (+ i 1)))
((= i 5) vec)
(vector-set! vec i i))
(let ((x '(1 3 5 7 9)))
(do ((x x (cdr x))
(sum 0 (+ sum (car x))))
((null? x) sum)))
(define foo
(case-lambda
(() 'zero)
((x) (list 'one x))
((x y) (list 'two x y))
((a b c d . e) (list 'four a b c d e))
(rest (list 'rest rest))))
(foo) (foo 1) (foo 1 2) (foo 1 2 3) (foo 1 2 3 4)
(program
(import (rnrs base)
(rnrs io simple)
(rnrs eval)
(rnrs r5rs))
(display
(eval '(let ((x 1)) x)
(null-environment 5)))
(display
(eval '(let ((x (+ 1 2 3 4)))
(list x x))
(scheme-report-environment 5)))
(let ((x (delay (begin (display 'boo)))))
(force x)
(force x)) )
(program
(import (for (rnrs base) expand run)
(for (explicit-renaming) expand)
(rnrs io simple))
(define-syntax swap!
(er-transformer
(lambda (exp rename compare)
(let ((a (cadr exp))
(b (caddr exp)))
`(,(rename 'let) ((,(rename 'temp) ,a))
(,(rename 'set!) ,a ,b)
(,(rename 'set!) ,b ,(rename 'temp)))))))
(let ((temp 1)
(set! 2))
(swap! set! temp)
(values temp set!))
(define-syntax loop
(er-transformer
(lambda (x r c)
(let ((k (car x))
(body (cdr x)))
`(,(r 'call-with-current-continuation)
(,(r 'lambda) (,(datum->syntax k 'exit))
(,(r 'let) ,(r 'f) () ,@body (,(r 'f)))))))))
(let ((x 5))
(loop (if (zero? x)
(exit #f))
(display x)
(set! x (- x 1))))
(define-syntax while
(syntax-rules ()
((while test body ...)
(loop (if (not test) (exit #f))
body ...))))
(let ((x 5))
(while (> x 0)
(display x)
(set! x (- x 1))))
(define-syntax simple-cond
(er-transformer
(lambda (exp rename compare)
(let ((clauses (cdr exp)))
(if (null? clauses)
`(,(rename 'quote) unspecified)
(let* ((first (car clauses))
(rest (cdr clauses))
(test (car first)))
(cond ((and (identifier? test)
(compare test (rename 'else)))
`(,(rename 'begin) ,@(cdr first)))
(else `(,(rename 'if)
,test
(,(rename 'begin) ,@(cdr first))
(,(rename 'simple-cond) ,@rest))))))))))
(simple-cond (#f 1)
(else 2))
)
))