-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscheme.scm
168 lines (138 loc) · 4.42 KB
/
scheme.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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
;;
;; LeeScheme/scheme.scm. Copyright (C) Lee Richard Boynton, 1992-1994.
;;
;
;The first expression bootstraps the world by reading the rest of the file.
;
(let loop ((obj (read)))
(if (eof-object? obj)
#t
(begin
((system:compile (system:macroexpand obj)))
(loop (read)))))
;(system:declare! '(bind-global-procs #t) '(disable-type-checks #t))
(system:declare! '(bind-global-procs #t))
;
;; this normally gets redefined in the read-eval-print-loop to catch errors
;
(define (system:*restart* . whatever)
(display system:*error-info*) (newline)
(system:exit -1))
(define (system:raise-error the-error)
(system:*restart* the-error))
(define system:*display-line* #f) ;gets redefined below
(define (system:object->string obj)
(let ((buf (make-string 256)))
(let ((port (system:open-output-string buf)))
(write obj port)
(let ((buf (system:string-port-buffer port))
(pos (system:string-port-position port)))
(substring buf 0 pos)))))
(define (system:void? obj) (eq? obj system:*void*))
(define (system:error-string)
(let ((type (vector-ref system:*error-info* 0))
(desc (vector-ref system:*error-info* 1))
(obj (vector-ref system:*error-info* 2))
(state (vector-ref system:*error-info* 3))
(fun (vector-ref system:*error-info* 4)))
(case type
((interrupt) "Interrupt")
((error)
(string-append desc
(if (system:void? obj)
""
(string-append " : "
(system:object->string obj))))))))
(define (system:error-context-string)
(let ((fun (vector-ref system:*error-info* 4))
(type (vector-ref system:*error-info* 3)))
(if (symbol? type)
(string-append "While "
(symbol->string type)
" '" (symbol->string fun) "'")
#f)))
(define (system:print-error . rest)
(let ((port (if (pair? rest) (car rest) (current-output-port)))
(context (system:error-context-string)))
(if (eq? (vector-ref system:*error-info* 0) 'interrupt)
(begin
(display "*** Interrupt ***" port)
(newline port))
(begin
(display "*** Error: " port)
(display (system:error-string) port)
(newline port)
(if context
(begin
(display "*** " port)
(display context port)
(newline port)))))))
(define (system:error . args)
(apply system:*display-line* "*** Error: " args)
(system:raise-error 'generic-error))
(define (system:interrupt)
(system:*display-line* "*** Interrupt ***")
(system:raise-error 'interrupt))
(define system:*load-verbose* #f)
(define system:*macroexpand-hook* (lambda (expr) (system:macroexpand expr)))
(define (load file)
(let ((path (system:find-file file)))
(if path
(let ((inport (open-input-file path)))
(if system:*load-verbose*
(system:*display-line* "[loading " path "]"))
(let loop ((obj (read inport)))
(if (eof-object? obj)
#t
(begin
((system:compile (system:*macroexpand-hook* obj)))
(loop (read inport))))
(close-input-port inport)
#t))
(system:error "File not found: " file))))
(define (features) system:*features*)
(define (provided? sym) (and (memq sym system:*features*) #t))
(define (provide sym)
(if (not (memq sym system:*features*))
(set! system:*features* (cons sym system:*features*)))
sym)
(define (require sym . rest)
(if (not (memq sym system:*features*))
(load (if (null? rest) sym (car rest)))
#t))
(define (open filename)
(let ((path (system:find-file filename)))
(and path (system:command (string-append "open " path)))))
(define (print-object obj)
(display obj))
(define (print . objects)
(define (print:internal args)
(if (null? args)
(newline)
(begin
(print-object (car args))
(print:internal (cdr args)))))
(print:internal objects)
(system:flush-output-port))
(set! system:*display-line* print)
(define error system:error)
(define unbound? system:unbound?)
(define (macroexpand expr)
(system:*macroexpand-hook* expr))
(define (compile expr)
(system:compile (macroexpand expr)))
(define (eval expr)
((compile expr)))
(define (standard-io)
(set! system:*current-input-port* (system:stdin))
(set! system:*current-output-port* (system:stdout))
#t)
(define (force-output)
(system:flush-output-port))
(begin
(load "r4rs.scm")
(close-input-port (current-input-port))
(standard-io)
(system:enable-keyboard-interrupts #t)
(load system:*repl-filename*)
(system:exit))