- Peephole optimizer: eliminates VOID+POP pairs, JUMP-to-next-instruction. Adjusts jump targets after dead code removal. - --help/-h and --version/-v flags. - 4 example programs: fibonacci, generator, mergesort, objects. - README rewritten: documents bytecode compiler, --fast flag, all features. - 529 tests green.
49 lines
1.5 KiB
Text
49 lines
1.5 KiB
Text
;;; objects.lsp — record types, closures as objects, hash tables
|
|
;;; Run: python3 uncommonlisp.py examples/objects.lsp
|
|
|
|
;; Record types with inheritance
|
|
(define-record-type <point>
|
|
(make-point x y)
|
|
point?
|
|
(x point-x)
|
|
(y point-y))
|
|
|
|
(define-record-type <point3d>
|
|
(inherit <point>)
|
|
(make-point3d x y z)
|
|
point3d?
|
|
(x point3d-x)
|
|
(y point3d-y)
|
|
(z point3d-z))
|
|
|
|
(define p (make-point 3 4))
|
|
(define p3 (make-point3d 1 2 3))
|
|
|
|
(display "point: ") (display (point-x p)) (display ",") (display (point-y p)) (newline)
|
|
(display "point3d: ") (display (point3d-x p3)) (display ",")
|
|
(display (point3d-y p3)) (display ",") (display (point3d-z p3)) (newline)
|
|
(display "point3d is point? ") (display (point? p3)) (newline)
|
|
|
|
;; Closure-based counter
|
|
(define (make-counter)
|
|
(let ((n 0))
|
|
(lambda (msg . args)
|
|
(cond ((eq? msg 'inc) (set! n (+ n 1)) n)
|
|
((eq? msg 'dec) (set! n (- n 1)) n)
|
|
((eq? msg 'get) n)
|
|
((eq? msg 'reset) (set! n 0) n)
|
|
(else (error "unknown message" msg))))))
|
|
|
|
(define c (make-counter))
|
|
(c 'inc) (c 'inc) (c 'inc)
|
|
(display "Counter after 3 increments: ") (display (c 'get)) (newline)
|
|
(c 'dec)
|
|
(display "Counter after decrement: ") (display (c 'get)) (newline)
|
|
|
|
;; Hash table
|
|
(define ht (make-hash-table))
|
|
(hash-table-set! ht 'name "uncommonlisp")
|
|
(hash-table-set! ht 'version "1.0.0")
|
|
(hash-table-set! ht 'features '(bytecode continuations macros))
|
|
(display "Name: ") (display (hash-table-ref ht 'name)) (newline)
|
|
(display "Features: ") (display (hash-table-ref ht 'features)) (newline)
|