stefano / nyac

Not-Yet-Arc Compiler

This URL has Read+Write access

nyac / lib.arc
100644 329 lines (274 sloc) 9.086 kb
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
; Copyright (c) 2008 Dissegna Stefano
; Released under the terms of the GNU LGPL
 
(def + (x y)
  (if (fxp x) (if (fxp y) (fx+ x y)
                  (flp y) (fl+ (fx->fl x) y)
                  (err "+: Wrong types"))
      (flp x) (if (flp y) (fl+ x y)
                  (fxp y) (fl+ x (fx->fl y))
                  (err "+: Wrong types"))
      (err "+: Wrong types")))
 
; Warning: the current implementation of '-' is _very_ slow, because
; it needs to cons its arguments
(def - args
  (let l (len args)
    (if (is l 0) 0
        (is l 1) (-/1 (car args))
        (is l 2) (-/2 (car args) (cadr args))
        (apply - (-/2 (car args) (cadr args)) (cddr args)))))
 
(def -/1 (x)
  (if (fxp x) (fx- 0 x)
      (flp x) (fl- 0.0 x)
      (err "-: Wrong types")))
 
(def -/2 (x y)
  (if (fxp x) (if (fxp y) (fx- x y)
                  (flp y) (fl- (fx->fl x) y)
                  (err "-: Wrong types"))
      (flp x) (if (flp y) (fl- x y)
                  (fxp y) (fl- x (fx->fl y))
                  (err "-: Wrong types"))
      (err "-: Wrong types")))
 
(def / (x y)
  (if (fxp x) (if (fxp y) (fx/ x y)
                  (flp y) (fl/ (fx->fl x) y)
                  (err "/: Wrong types"))
      (flp x) (if (flp y) (fl/ x y)
                  (fxp y) (fl/ x (fx->fl y))
                  (err "/: Wrong types"))
      (err "/: Wrong types")))
 
(def * (x y)
  (if (fxp x) (if (fxp y) (fx* x y)
                  (flp y) (fl* (fx->fl x) y)
                  (err "*: Wrong types"))
      (flp x) (if (flp y) (fl* x y)
                  (fxp y) (fl* x (fx->fl y))
                  (err "*: Wrong types"))
      (err "*: Wrong types")))
 
(def < (x y)
  (if (fxp x) (if (fxp y) (fx< x y)
                  (flp y) (fl< (fx->fl x) y)
                  (err "<: Wrong types"))
      (flp x) (if (flp y) (fl< x y)
                  (fxp y) (fl< x (fx->fl y))
                  (err "<: Wrong types"))
      (err "<: Wrong types")))
 
(def rem (x y) (fxrem x y))
 
(def <= (x y)
  (if (fxp x) (if (fxp y) (fx<= x y)
                  (flp y) (fl<= (fx->fl x) y)
                  (err "<=: Wrong types"))
      (flp x) (if (flp y) (fl<= x y)
                  (fxp y) (fl<= x (fx->fl y))
                  (err "<=: Wrong types"))
      (err "<=: Wrong types")))
 
(def >= (x y)
  (if (fxp x) (if (fxp y) (fx>= x y)
                  (flp y) (fl>= (fx->fl x) y)
                  (err ">=: Wrong types"))
      (flp x) (if (flp y) (fl>= x y)
                  (fxp y) (fl>= x (fx->fl y))
                  (err ">=: Wrong types"))
      (err ">=: Wrong types")))
 
(def > (x y)
  (if (fxp x) (if (fxp y) (fx< y x)
                  (flp y) (fl< (fx->fl y) x)
                  (err ">: Wrong types"))
      (flp x) (if (flp y) (fl< y x)
                  (fxp y) (fl< y (fx->fl x))
                  (err ">: Wrong types"))
      (err ">: Wrong types")))
 
(def str-equal-a (a b i n)
  (if (is i n) t
      (is (str-ref a i) (str-ref b i)) (str-equal-a a b (+ i 1) n)))
 
(def str-equal (a b)
  (if (is (str-len a) (str-len b))
    (str-equal-a a b 0 (str-len a))))
 
(def iso (a b)
  (if
    (consp a)
      (if (consp b)
        (and (iso (car a) (car b)) (iso (cdr a) (cdr b)))
        nil)
    (strp a)
      (if (strp b)
        (str-equal a b)
        nil)
    (flp a)
      (if (flp b)
        (fl= a b))
    (atom a)
      (if (atom b) (is a b) nil)))
 
(def acons (x) (consp x))
 
(def car (x) (car x))
 
(def not (x) (not x))
 
(def symbolp (x) (symbolp x))
 
(def map1 (f l)
  (if l (cons (f (car l)) (map1 f (cdr l)))))
 
(def map (f l . args)
  (if l
    (let res (apply f (car l) (map1 (fn (x) (car x)) args))
      (cons res (apply map f (cdr l) (map1 (fn (x) (cdr x)) args))))))
 
(def mapl1 (f l)
  (if l (do (f l) (mapl1 f (cdr l)))))
 
(def reduce (f l val)
  (if l
      (reduce f (cdr l) (f val (car l)))
      val))
 
(def append (l1 l2)
  (if l1
      (cons (car l1) (append (cdr l1) l2))
      l2))
 
(def mem (el l)
  (if l
      (if (is (car l) el)
el
(mem el (cdr l)))
      nil))
 
(def assoc-f (l key . args)
  ; same as assoc, but keys in the a-list can be functions to apply to key
  ; and returns element, not cons cell
  (if l
    (withs (k (car (car l))
            test (if (fnp k) (fn (x) (apply k x args)) (fn (x) (is k x))))
(if (test key)
          (cdr (car l))
          (apply assoc-f (cdr l) key args)))))
 
(def vector args
  (let res (mkvec (len args) nil)
    (list-into-vector res args 0)
    res))
    
(def list-into-vector (v l i)
  (if (and l (< i (vec-len v)))
    (do
      (vec-set v i (car l))
      (list-into-vector v (cdr l) (+ i 1)))))
 
(def copy-vec-aux (from to start end i)
  (if (< start end)
      (do
       (vec-set to i (vec-ref from start))
       (copy-vec-aux from to (+ start 1) end (+ i 1)))
      to))
 
(def copy-vec (v)
  (let cp (mkvec (vec-len v) nil)
    (copy-vec-aux v cp 0 (vec-len v) 0)))
 
(def strcopy (from to start end i)
  (if (< start end)
      (do
       (str-set to i (str-ref from start))
       (strcopy from to (+ start 1) end (+ i 1)))
      to))
 
(def substr (s from to)
  (let res (mkstr (- to from))
    (strcopy s res from to 0)))
 
(def str-append (s1 s2)
  (let res (mkstr (+ (str-len s1) (str-len s2)))
    (strcopy s1 res 0 (str-len s1) 0)
    (strcopy s2 res (str-len s1) (str-len res) 0)
    res))
 
; stream: (list fd ungetted-chars)
 
(def open-file (path type)
  (if (not (strp path)) (err "Invalid path"))
  (withs (flags (if (is type 'in) 0
                    (is type 'out) 577
                    (err "Invalid type passed to open-file"))
          fd (ffi-call "a_open" path flags))
    (if (is fd -1)
      (do (print path) (err (make-string "Cannot open file " path)))
      (list fd nil))))
 
(def write-string (s stream)
  (ffi-call "a_write" (stream-fd stream) s (str-len s)))
 
(def stdin-stream ()
  (list 0 nil))
 
(def stdout-stream ()
  (list 1 nil))
 
(def stream-fd (s)
  (car s))
 
(def strstream/in (s)
  (list s 0))
 
(def readc (s)
  (if (strp (car s)) ; string stream?
    (do (setcar (cdr s) (+ (cadr s) 1))
        (str-ref (car s) (- (cadr s) 1)))
    (if (cadr s) ; there are ungetted chars
      (let c (car (cadr s))
        (setcar (cdr s) (cdr (cadr s))) ; set to next element in list
        c)
      (let val (mkstr 1)
        (let res (ffi-call "a_read" (stream-fd s) val 1)
          (if (is res 0)
            nil
            (str-ref val 0)))))))
 
(def ungetc (s c)
  (if (strp (car s)) ; string stream?
    (setcar (cdr s) (- (cadr s) 1)) ; c is ignored...
    (setcar (cdr s) (cons c (cadr s))))) ; append to head of ungetted chars
 
(def fwhile (test f)
  (if (test)
    (do (f) (fwhile test f))))
 
(def feach1 (f l)
  (if l
    (do (f (car l)) (feach f (cdr l)))))
 
(def feach (f l . args)
  (if l
    (let res (apply f (car l) (map1 (fn (x) (car x)) args))
      (apply feach f (cdr l) (map1 (fn (x) (cdr x)) args)))))
 
(def plist-set (sym k v)
  (let c (assoc k (get-symbol-plist sym))
    (if c
      (do
        (setcdr c v)
        v)
      (do
        (set-symbol-plist sym (cons (cons k v) (get-symbol-plist sym)))
        v))))
 
(def plist-get (sym k)
  (cdr (assoc k (get-symbol-plist sym))))
 
(def butlast (l)
  (if (and l (cdr l) (consp (cdr l)))
    (cons (car l) (butlast (cdr l)))
    nil))
; (if l (cons (car l) nil) nil)))
 
(def last (l)
  (if l (if (or (not (cdr l)) (not (consp (cdr l)))) l (last (cdr l)))))
 
(def trace (name)
  (let f (get-symbol-value name)
    (set-symbol-value name (fn args
                             (print (make-string "calling "
                                                 (to-string name) " with:"))
                             (print args)
                             (let res (apply f args)
                               (print (make-string "res: " (to-string res)))
                               res)))))
(def system (cmd)
  (if (not (strp cmd)) (err "system accepts only strings"))
  (ffi-call "a_system" cmd))
 
(let next-sym 0
  (def uniq ()
    (set next-sym (+ next-sym 1))
    (intern (make-string "gs" next-sym))))
 
(def read-from-string (s)
  (let in (strstream/in s)
    (read/tbl in read-table)))
 
(def eval (expr)
  (let out-name (make-string "tmp/" (uniq) ".s")
    (with (out (open-file out-name 'out)
           old stdout*)
      (set stdout* out)
      (emit-unit expr (mk-empty-env) transform-expr)
      (set stdout* old)
      (if (is (system (make-string "gcc --shared -o " out-name ".so "
                                   out-name " 2>err")) 0)
        (__load (make-string out-name ".so"))
        (err "Compilation error: see file err")))))
 
(def ccc (f)
  (__ccc (fn (cc) (f [__restore-continuation cc _]))))
 
; compile and load a file
(def load (file-name)
  (withs (out-name (make-string "tmp/to-load-" (uniq) ".s")
          so-name (make-string out-name ".so")
          in-file (open-file file-name 'in)
          out-file (open-file out-name 'out))
    (compile in-file out-file nil transform-expr)
    (if (is (system (make-string "gcc --shared -o " so-name " " out-name " 2>err")) 0)
      (__load so-name)
      (err "Compilation error: see file err"))))