blob: 5486344f312e6a273026841a7a2dd7708b9059b9 (
plain) (
blame)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
(define-library (csc compiler)
(import (scheme base)
(only (csc hash-map)
hash-bytevector
insert
key-not-found-error?
lookup
make-map
map-for-each
merge)
(only (csc ir1)
toplevel-define?)
(only (csc loop)
loop
return)
(only (csc macros)
builtins-environment
expand)
(only (csc match) match))
(begin
(define (hash-symbol s)
(hash-bytevector (string->utf8 (symbol->string s))))
(define (cmp-symbol s1 s2)
(string<? (symbol->string s1) (symbol->string s2)))
(define-record-type <syntax-error>
(make-syntax-error msg irritants)
syntax-error?)
(define (raise-syntax-error msg . irritants)
(raise (make-syntax-error msg irritants)))
(define (parse-import-set expr)
(match expr
(((! 'only) import-set . idents)
(loop with bindings = (parse-import-set import-set)
and new-bindings = (make-map hash-symbol cmp-symbol)
for ident in idents
do (set! new-bindings
(insert new-bindings
ident
(guard (e ((key-not-found-error? e) (raise-syntax-error "unknown symbol in `only' form" ident)))
(lookup bindings ident))))
finally (return new-bindings)))
((! '(csc builtins))
builtins-environment)
(_ (raise-syntax-error "unknown or unsupported import set form" expr))))
(define (parse-import expr)
(match expr
(((! 'import) . import-sets)
(loop with bindings = (make-map hash-symbol cmp-symbol)
for import-set in import-sets
do (set! bindings
(merge bindings (parse-import-set import-set)))
finally (return bindings)))
(_ (raise-syntax-error "expected import form" expr))))
(define-record-type <program>
(make-program imports body)
program?
(imports program-imports)
(body program-body))
; A Scheme program consists of one or more import declarations
; followed by a sequence of expressions and definitions.
; -- R7RS
(define (program->ir1 program)
(loop with bindings = (make-map hash-symbol cmp-symbol)
for body on program
for expr = (car body)
do (match expr
(((! 'import) . import-sets)
(set! bindings
(merge bindings (parse-import expr))))
(_ (loop for expr in body
for expanded-expr = (expand expr bindings)
if (toplevel-define? expanded-expr)
|