2016-10-23 10:32:40 +00:00
|
|
|
|
|
|
|
(define-module (guile gc))
|
|
|
|
|
|
|
|
(define (R) (reload-module (current-module)))
|
|
|
|
|
2016-10-23 12:22:53 +00:00
|
|
|
(define gc-size 10)
|
|
|
|
(define the-cars (make-vector gc-size '(* . *)))
|
|
|
|
(define the-cdrs (make-vector gc-size '(* . *)))
|
2016-10-23 10:32:40 +00:00
|
|
|
(define gc-free 0)
|
|
|
|
(define (show-gc)
|
2016-10-23 12:22:53 +00:00
|
|
|
(display "\nfree:") (display gc-free) (newline)
|
|
|
|
(display "cars:") (display the-cars) (newline)
|
|
|
|
(display "cdrs:") (display the-cdrs) (newline))
|
2016-10-23 10:32:40 +00:00
|
|
|
(show-gc)
|
|
|
|
|
2016-10-23 15:21:56 +00:00
|
|
|
(define (cell-type c) (car (gc-cell c)))
|
2016-10-23 12:22:53 +00:00
|
|
|
(define cell-index cdr)
|
2016-10-23 15:21:56 +00:00
|
|
|
(define (cell-value c) (cdr (gc-cell c)))
|
2016-10-23 12:22:53 +00:00
|
|
|
|
2016-10-23 10:32:40 +00:00
|
|
|
(define (make-cell type . x)
|
2016-10-23 15:21:56 +00:00
|
|
|
(cons type (if (pair? x) (car x) '*)))
|
2016-10-23 10:32:40 +00:00
|
|
|
|
|
|
|
(define (gc-alloc)
|
|
|
|
((lambda (index)
|
|
|
|
(set! gc-free (+ gc-free 1))
|
2016-10-23 12:22:53 +00:00
|
|
|
(make-cell '* index))
|
2016-10-23 10:32:40 +00:00
|
|
|
gc-free))
|
|
|
|
|
2016-10-23 15:21:56 +00:00
|
|
|
(define (make-number x)
|
2016-10-23 10:32:40 +00:00
|
|
|
((lambda (cell)
|
2016-10-23 12:22:53 +00:00
|
|
|
(vector-set! the-cars (cell-index cell) (make-cell 'n x))
|
2016-10-23 10:32:40 +00:00
|
|
|
cell)
|
|
|
|
(gc-alloc)))
|
|
|
|
|
2016-10-23 15:21:56 +00:00
|
|
|
(define (make-symbol x)
|
|
|
|
((lambda (cell)
|
|
|
|
(vector-set! the-cars (cell-index cell) (make-cell 's x))
|
|
|
|
cell)
|
|
|
|
(gc-alloc)))
|
|
|
|
|
2016-10-23 12:22:53 +00:00
|
|
|
(define (gc-cons x y)
|
|
|
|
((lambda (cell)
|
2016-10-23 15:21:56 +00:00
|
|
|
(vector-set! the-cars (cell-index cell) (make-cell 'p (cell-index x)))
|
|
|
|
(vector-set! the-cdrs (cell-index cell) y)
|
|
|
|
cell)
|
2016-10-23 12:22:53 +00:00
|
|
|
(gc-alloc)))
|
|
|
|
|
|
|
|
(define (gc-car c)
|
2016-10-23 15:21:56 +00:00
|
|
|
(vector-ref the-cars (cell-index c)))
|
2016-10-23 12:22:53 +00:00
|
|
|
|
|
|
|
(define (gc-cdr c)
|
2016-10-23 15:21:56 +00:00
|
|
|
(vector-ref the-cdrs (cell-index c)))
|
|
|
|
|
|
|
|
(define gc-cell gc-car)
|
|
|
|
(define (gc-pair? c)
|
|
|
|
(eq? (cell-type c) 'p))
|
|
|
|
|
|
|
|
(define (gc-set-car! c x)
|
|
|
|
(if (gc-pair? c) (vector-set! the-cars (cell-index c) x)))
|
|
|
|
|
|
|
|
(define (gc-set-cdr! c x)
|
|
|
|
(if (gc-pair? c) (vector-set! the-cdrs (cell-index c) x)))
|
2016-10-23 12:22:53 +00:00
|
|
|
|
2016-10-23 15:21:56 +00:00
|
|
|
(display "number 7=") (display (make-number 7)) (newline)
|
|
|
|
(define first (make-number 8)) (newline)
|
2016-10-23 12:22:53 +00:00
|
|
|
(show-gc)
|
2016-10-23 15:21:56 +00:00
|
|
|
(define second (make-number 9)) (newline)
|
2016-10-23 10:32:40 +00:00
|
|
|
(show-gc)
|
2016-10-23 12:22:53 +00:00
|
|
|
(define pair (gc-cons first second))
|
|
|
|
(show-gc)
|
|
|
|
(display "pair:") (display pair) (newline)
|
|
|
|
|
|
|
|
(display "car:") (display (gc-car pair)) (newline)
|
|
|
|
(display "cdr:") (display (gc-cdr pair)) (newline)
|
2016-10-23 15:21:56 +00:00
|
|
|
|
|
|
|
(define (gc-null? x) (eq? (car x) 'e))
|
|
|
|
|
|
|
|
(define gc-nil (make-cell 'e 0))
|
|
|
|
(display "nil: ") (display gc-nil) (newline)
|
|
|
|
|
|
|
|
(define (gc-list . rest)
|
|
|
|
(if (null? rest) gc-nil
|
|
|
|
(gc-cons (car rest) (apply gc-list (cdr rest)))))
|
|
|
|
|
|
|
|
(define lst (gc-list (make-symbol 'a) (make-symbol 'b) (make-symbol 'c)))
|
|
|
|
(display "lst:") (display lst) (newline)
|
|
|
|
(show-gc)
|
|
|
|
|
|
|
|
(define (gc-display x . cont?)
|
|
|
|
(if (gc-pair? x) (begin (if (null? cont?) (display "("))
|
|
|
|
(gc-display (gc-car x))
|
|
|
|
(if (gc-pair? (gc-cdr x)) (display " "))
|
|
|
|
(gc-display (gc-cdr x) #t)
|
|
|
|
(if (null? cont?) (display ")")))
|
|
|
|
(if (gc-null? x) (if (not cont?) (display "()"))
|
|
|
|
(display (cell-value x)))))
|
|
|
|
(display "gc-display lst=") (gc-display lst) (newline)
|
|
|
|
(show-gc)
|