-
Notifications
You must be signed in to change notification settings - Fork 0
/
unlcore.scm
executable file
·220 lines (180 loc) · 5.55 KB
/
unlcore.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/gosh
(add-load-path "." :relative)
(use parser)
(use unlc)
(use lib)
(use unlasm)
(use srfi-60)
;; little endian binary number ---------------------------------------
(defmacro add-table
(icons
(icons
(icons
(lambda (rest) ((icons K) (rest K)))
(lambda (rest) ((icons KI) (rest K))))
(icons
(lambda (rest) ((icons KI) (rest K)))
(lambda (rest) ((icons K) (rest KI)))))
(icons
(icons
(lambda (rest) ((icons KI) (rest K)))
(lambda (rest) ((icons K) (rest KI))))
(icons
(lambda (rest) ((icons K) (rest KI)))
(lambda (rest) ((icons KI) (rest KI)))))))
(defrecmacro (le-add-rec xs ys carry)
(add-table (car xs) (car ys) carry
(le-add-rec (cdr xs) (cdr ys))))
(defmacro (le-add xs ys) (le-add-rec xs ys KI))
(defmacro sub-table
(icons
(icons
(icons
(lambda (rest) ((icons K) (rest K)))
(lambda (rest) ((icons KI) (rest KI))))
(icons
(lambda (rest) ((icons KI) (rest KI)))
(lambda (rest) ((icons K) (rest KI)))))
(icons
(icons
(lambda (rest) ((icons KI) (rest K)))
(lambda (rest) ((icons K) (rest K))))
(icons
(lambda (rest) ((icons K) (rest K)))
(lambda (rest) ((icons KI) (rest KI)))))))
(defrecmacro (le-sub-rec xs ys borrow)
(sub-table (car xs) (car ys) borrow
(le-sub-rec (cdr xs) (cdr ys))))
(defmacro (le-sub xs ys) (le-sub-rec xs ys KI))
(defmacro eq-table
(icons
(icons I (K (K (K I))))
(icons (K (K (K I))) I)))
;; returns K(true) or KI(false)
(defrecmacro (le-eq-rec xs ys)
(eq-table (car xs) (car ys) le-eq-rec (cdr xs) (cdr ys)))
(defmacro (le-eq xs ys)
(if (le-eq-rec xs ys) KI K))
(defmacro borrow-table
(icons
(icons I (K KI))
(icons (K K) I)))
;; returns K(true) or KI(false)
(defmacro (le-lt xs ys)
(vm-bits (lambda (g xs ys borrow)
(g (cdr xs) (cdr ys) (borrow-table (car xs) (car ys) borrow)))
(lambda (xs ys b) b)
xs ys KI))
(defrecmacro (->le-inc)
(icons (lambda (tl) ((icons KI) (tl ->le-inc)))
(icons K)))
(defmacro (le-inc xs)
(xs ->le-inc))
(defrecmacro (->le-dec)
(icons (icons KI)
(lambda (tl) ((icons K) (tl ->le-dec)))))
(defmacro (le-dec xs)
(xs ->le-dec))
;; for debug
(defrecmacro (le->number xs)
(if (pair? xs)
(((car xs) succ I)
(dbl (le->number (cdr xs))))
c0))
(defmacro (print-le n)
(print-digit (le->number n) I #\newline I))
;; memory ------------------------------------------------------------
(defrecmacro (zero-memory bits)
(if (cons1? bits)
((lambda (a f) (f a a))
(zero-memory (1-of-1 bits)))
le-0))
(defrecmacro (initialize-memory-rec bits lst)
(if (pair? lst)
(if (cons1? bits)
((initialize-memory-rec (1-of-1 bits) lst)
(lambda (left lst2)
((initialize-memory-rec (1-of-1 bits) lst2)
(lambda (right lst3)
(cons (cons right left) lst3)))))
lst)
(cons (zero-memory bits) nil)))
(defmacro (initialize-memory data)
(car (initialize-memory-rec (to-cons1 vm-bits) data)))
(defmacro (load-le mem)
(vm-bits (lambda (f a) (f (cdr a) (car a)))
(K mem)))
(defmacro (store-step f g addr)
(f ((car addr)
(lambda (m) (cons (g (car m)) (cdr m)))
(lambda (m) (cons (car m) (g (cdr m)))))
(cdr addr)))
(defmacro (store-le mem addr val)
(vm-bits store-step
K ; (lambda (g addr) g)
(K val) ; (lambda (oldval) val)
addr
mem))
; Runner -------------------------------------------------------------
(defmacro run
(lambda (code initial-vm)
(let rec ((vm initial-vm))
(rec ((load-le code (vm-pc vm)) (inc-pc vm le-inc))))))
; Runtime library ----------------------------------------------------
(defmacro (mem-load vm)
(load-le (vm-memory vm)))
(defmacro (mem-store vm addr val)
(set-memory vm (store-le (vm-memory vm) addr val)))
;; TODO: make this 8-bit clean
(define (putc-func bits)
(let* ((d (length bits))
(hd (string->symbol (string-append "hd" (number->string d))))
(tl (string->symbol (string-append "tl" (number->string d)))))
(if (= (length bits) 6) ; Only supports characters up to 127
`(lambda (,hd _)
(,hd ,(integer->char (list->integer (cons #t bits)))
,(integer->char (list->integer (cons #f bits)))))
`(lambda (,hd ,tl)
(,tl (,hd ,(putc-func (cons #t bits))
,(putc-func (cons #f bits))))))))
(add-unl-macro! 'putc '(n) `(n ,(putc-func '()) I))
(add-unl-macro! 'getc '()
`(lambda (cont)
(@ I
,(fold-right
(lambda (i rest)
`(((? ,(integer->char i)) I cont ,(le-number i))
,rest))
'(cont le-0)
(filter
(lambda (i)
(let ((c (integer->char i)))
(or (char-whitespace? c) (not (eq? (char-general-category c) 'Cc)))))
(iota 128))))))
(defmacro all-libs (list le-add le-sub le-eq le-lt mem-load mem-store putc getc))
(defmacro core-libs (cons* le-add le-sub le-eq le-lt mem-load mem-store ""))
(defmacro test-code (list (lambda (vm) (mem-store vm be-1 le-1))
(lambda (vm) (K vm (print-le (lib-load vm vm be-1))))
(lambda (vm) (K vm (print-le (vm-pc vm))))
(lambda (vm) (K vm (print-le (vm-pc vm))))
(lambda (vm) (K vm (print-le (vm-pc vm))))
(lambda (vm) (exit I))))
(defmacro (initial-vm data lib)
(cons initial-regs
(cons (initialize-memory data)
lib)))
(defmacro (main lib)
(lambda (code data)
(run (initialize-memory code) (initial-vm data lib))))
(define (main args)
(if (equal? (cdr args) '("--generate-core"))
(begin
(print-as-unl '(main core-libs))
(exit)))
(let ((parsed (parse)))
(print "``")
(print "# VM core")
(print-as-unl '(main all-libs))
(newline)
(generate (car parsed) (cadr parsed))
0))