aboutsummaryrefslogtreecommitdiffstats
path: root/csc/vec.csc
diff options
context:
space:
mode:
authorRose Hogenson <rhogenson@posteo.net>2022-01-15 22:40:01 -0800
committerRose Hogenson <rhogenson@posteo.net>2022-01-15 22:40:01 -0800
commit0d7cb228076354f893527bce2d426e21697832a9 (patch)
treefa12d1afff4cdb077437b11544c9ef8f299cd88e /csc/vec.csc
parenta968e8595b999d5c0a43c59de69bcb4f34e205c1 (diff)
downloadchromatopelma-0d7cb228076354f893527bce2d426e21697832a9.tar.zst
Finish the macro expander.
It works!! At least it passes all the test cases. Next I will: 1. finish the builtins, 2. add a conversion to IR2 in continuation passing style, 3. add a compiler from IR2 to bytecode, 4. and add an option to the frontend compiler to generate a standalone executable.
Diffstat (limited to 'csc/vec.csc')
-rw-r--r--csc/vec.csc47
1 files changed, 29 insertions, 18 deletions
diff --git a/csc/vec.csc b/csc/vec.csc
index 03f2b0f..2730c39 100644
--- a/csc/vec.csc
+++ b/csc/vec.csc
@@ -31,24 +31,35 @@
(list->vec xs))
- (define (vec-append v . xs)
- (let ((append-one
- (lambda (v x)
- (let ((new-v (if (> (vector-length (vec-arr v)) (vec-length v))
- v
- (let ((new-arr (make-vector (max 1 (* 2 (vec-length v))))))
- (vector-copy! new-arr 0 (vec-arr v))
- (make-vec (vec-length v) new-arr)))))
- (vector-set! (vec-arr new-v) (vec-length new-v) x)
- (make-vec (+ 1 (vec-length new-v)) (vec-arr new-v))))))
- (let loop ((v v)
- (xs xs))
- (if (null? xs)
- v
- (loop (append-one v (car xs)) (cdr xs))))))
-
-
(define (vec-ref v k)
(if (>= k (vec-length v))
(error "index out of bounds" k)
- (vector-ref (vec-arr v) k)))))
+ (vector-ref (vec-arr v) k)))
+
+
+ (define (append-one v x)
+ (let ((new-v (if (> (vector-length (vec-arr v)) (vec-length v))
+ v
+ (let ((new-arr (make-vector (max 1 (* 2 (vec-length v))))))
+ (vector-copy! new-arr 0 (vec-arr v))
+ (make-vec (vec-length v) new-arr)))))
+ (vector-set! (vec-arr new-v) (vec-length new-v) x)
+ (make-vec (+ 1 (vec-length new-v)) (vec-arr new-v))))
+
+
+ (define (append2 v1 v2)
+ (if (vec? v2)
+ (let loop ((i 0)
+ (v v1))
+ (if (< i (vec-length v2))
+ (loop (+ 1 i) (append-one v (vec-ref v2 i)))
+ v))
+ (append-one v1 v2)))
+
+
+ (define (vec-append v . xs)
+ (let loop ((xs xs)
+ (v v))
+ (if (null? xs)
+ v
+ (loop (cdr xs) (append2 v (car xs))))))))