Add peephole optimizer, --help, examples, README update

- 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.
This commit is contained in:
russell@unturf.com 2026-04-13 16:56:21 -04:00
parent f89d3bce6f
commit 1326e8a106
6 changed files with 314 additions and 96 deletions

21
examples/fibonacci.lsp Normal file
View file

@ -0,0 +1,21 @@
;;; fibonacci.lsp — iterative and recursive Fibonacci
;;; Run: python3 uncommonlisp.py --fast examples/fibonacci.lsp
(define (fib-iter n)
(let loop ((a 0) (b 1) (i 0))
(if (= i n) a (loop b (+ a b) (+ i 1)))))
(define (fib-rec n)
(if (<= n 1) n (+ (fib-rec (- n 1)) (fib-rec (- n 2)))))
(display "fib(30) iterative: ")
(display (fib-iter 30))
(newline)
(display "fib(20) recursive: ")
(display (fib-rec 20))
(newline)
(display "First 15 Fibonacci numbers: ")
(display (map fib-iter (iota 15)))
(newline)

50
examples/generator.lsp Normal file
View file

@ -0,0 +1,50 @@
;;; generator.lsp — coroutine-style generators using full continuations
;;; Run: python3 uncommonlisp.py --fast examples/generator.lsp
(define (make-generator thunk)
(let ((k #f) (done #f))
(lambda ()
(if done 'done
(call/cc
(lambda (return)
(if k
(k return)
(begin
(thunk (lambda (val)
(call/cc (lambda (next)
(set! k next)
(return val)))))
(set! done #t)
(return 'done)))))))))
;; Range generator
(define (range-gen start end)
(make-generator
(lambda (yield)
(let loop ((i start))
(when (< i end)
(yield i)
(loop (+ i 1)))))))
(define gen (range-gen 0 5))
(display "Generator: ")
(let loop ()
(let ((v (gen)))
(unless (eq? v 'done)
(display v) (display " ")
(loop))))
(newline)
;; Fibonacci generator (infinite)
(define (fib-gen)
(make-generator
(lambda (yield)
(let loop ((a 0) (b 1))
(yield a)
(loop b (+ a b))))))
(define fibs (fib-gen))
(display "First 10 fibs: ")
(do ((i 0 (+ i 1))) ((= i 10))
(display (fibs)) (display " "))
(newline)

27
examples/mergesort.lsp Normal file
View file

@ -0,0 +1,27 @@
;;; mergesort.lsp — functional merge sort
;;; Run: python3 uncommonlisp.py --fast examples/mergesort.lsp
(define (merge a b)
(cond ((null? a) b)
((null? b) a)
((< (car a) (car b))
(cons (car a) (merge (cdr a) b)))
(else
(cons (car b) (merge a (cdr b))))))
(define (split lst)
(let loop ((l lst) (a '()) (b '()))
(if (null? l)
(list a b)
(loop (cdr l) b (cons (car l) a)))))
(define (msort lst)
(if (or (null? lst) (null? (cdr lst)))
lst
(let ((halves (split lst)))
(merge (msort (car halves))
(msort (cadr halves))))))
(define data (reverse (iota 20)))
(display "Input: ") (display data) (newline)
(display "Sorted: ") (display (msort data)) (newline)

49
examples/objects.lsp Normal file
View file

@ -0,0 +1,49 @@
;;; 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)