2008-06-30 21:54:22 -04:00
|
|
|
; femtoLisp standard library
|
2009-01-04 21:45:21 -05:00
|
|
|
; by Jeff Bezanson (C) 2009
|
2008-08-29 22:56:46 -04:00
|
|
|
; Distributed under the BSD License
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2010-01-06 13:27:28 -05:00
|
|
|
(define (void) #t) ; the unspecified value
|
|
|
|
|
2010-05-05 01:31:46 -04:00
|
|
|
(define *builtins*
|
|
|
|
(vector
|
|
|
|
0 0 0 0 0 0 0 0 0 0 0 0
|
|
|
|
(lambda (x y) (eq? x y)) (lambda (x y) (eqv? x y))
|
|
|
|
(lambda (x y) (equal? x y)) (lambda (x) (atom? x))
|
|
|
|
(lambda (x) (not x)) (lambda (x) (null? x))
|
|
|
|
(lambda (x) (boolean? x)) (lambda (x) (symbol? x))
|
|
|
|
(lambda (x) (number? x)) (lambda (x) (bound? x))
|
|
|
|
(lambda (x) (pair? x)) (lambda (x) (builtin? x))
|
|
|
|
(lambda (x) (vector? x)) (lambda (x) (fixnum? x))
|
|
|
|
(lambda (x) (function? x)) (lambda (x y) (cons x y))
|
2013-06-11 17:58:15 -04:00
|
|
|
(lambda rest rest) (lambda (x) (car x))
|
2010-05-05 01:31:46 -04:00
|
|
|
(lambda (x) (cdr x)) (lambda (x y) (set-car! x y))
|
|
|
|
(lambda (x y) (set-cdr! x y)) (lambda rest (apply apply rest))
|
|
|
|
(lambda rest (apply + rest)) (lambda rest (apply - rest))
|
|
|
|
(lambda rest (apply * rest)) (lambda rest (apply / rest))
|
|
|
|
(lambda rest (apply div0 rest)) (lambda (x y) (= x y))
|
|
|
|
(lambda (x y) (< x y)) (lambda (x y) (compare x y))
|
|
|
|
(lambda rest (apply vector rest)) (lambda (x y) (aref x y))
|
|
|
|
(lambda (x y z) (aset! x y z))))
|
|
|
|
|
2009-07-17 22:16:18 -04:00
|
|
|
(if (not (bound? '*syntax-environment*))
|
|
|
|
(define *syntax-environment* (table)))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2009-07-17 22:16:18 -04:00
|
|
|
(define (set-syntax! s v) (put! *syntax-environment* s v))
|
2009-04-20 20:56:05 -04:00
|
|
|
(define (symbol-syntax s) (get *syntax-environment* s #f))
|
|
|
|
|
2009-07-17 22:16:18 -04:00
|
|
|
(define-macro (define-macro form . body)
|
|
|
|
`(set-syntax! ',(car form)
|
2019-08-09 10:18:36 -04:00
|
|
|
(lambda ,(cdr form) ,@body)))
|
2009-07-17 22:16:18 -04:00
|
|
|
|
2010-12-23 01:49:37 -05:00
|
|
|
#;(define (map1 f lst acc)
|
2010-05-05 01:31:46 -04:00
|
|
|
(cdr
|
|
|
|
(prog1 acc
|
2019-08-09 10:18:36 -04:00
|
|
|
(while (pair? lst)
|
|
|
|
(begin (set! acc
|
|
|
|
(cdr (set-cdr! acc (cons (f (car lst)) ()))))
|
|
|
|
(set! lst (cdr lst)))))))
|
2010-05-05 01:31:46 -04:00
|
|
|
|
2010-12-23 01:49:37 -05:00
|
|
|
#;(define (mapn f lsts)
|
2010-05-05 01:31:46 -04:00
|
|
|
(if (null? (car lsts))
|
|
|
|
()
|
|
|
|
(cons (apply f (map1 car lsts (list ())))
|
2019-08-09 10:18:36 -04:00
|
|
|
(mapn f (map1 cdr lsts (list ()))))))
|
2010-05-05 01:31:46 -04:00
|
|
|
|
2010-12-23 01:49:37 -05:00
|
|
|
#;(define (map f lst . lsts)
|
2009-07-08 01:53:29 -04:00
|
|
|
(if (null? lsts)
|
2009-07-08 15:07:56 -04:00
|
|
|
(map1 f lst (list ()))
|
|
|
|
(mapn f (cons lst lsts))))
|
2009-07-08 01:53:29 -04:00
|
|
|
|
2010-12-23 01:49:37 -05:00
|
|
|
(define-macro (letrec binds . body)
|
|
|
|
`((lambda ,(map car binds)
|
|
|
|
,.(map (lambda (b) `(set! ,@b)) binds)
|
|
|
|
,@body)
|
|
|
|
,.(map (lambda (x) (void)) binds)))
|
|
|
|
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
(define-macro (let binds . body)
|
2010-01-06 13:27:28 -05:00
|
|
|
(let ((lname #f))
|
2009-07-17 22:16:18 -04:00
|
|
|
(if (symbol? binds)
|
2019-08-09 10:18:36 -04:00
|
|
|
(begin (set! lname binds)
|
|
|
|
(set! binds (car body))
|
|
|
|
(set! body (cdr body))))
|
2009-07-17 22:16:18 -04:00
|
|
|
(let ((thelambda
|
2019-08-09 10:18:36 -04:00
|
|
|
`(lambda ,(map (lambda (c) (if (pair? c) (car c) c))
|
|
|
|
binds)
|
|
|
|
,@body))
|
|
|
|
(theargs
|
|
|
|
(map (lambda (c) (if (pair? c) (cadr c) (void))) binds)))
|
2009-07-17 22:16:18 -04:00
|
|
|
(cons (if lname
|
2019-08-09 10:18:36 -04:00
|
|
|
`(letrec ((,lname ,thelambda)) ,lname)
|
|
|
|
thelambda)
|
|
|
|
theargs))))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2009-05-29 00:38:50 -04:00
|
|
|
(define-macro (cond . clauses)
|
|
|
|
(define (cond-clauses->if lst)
|
|
|
|
(if (atom? lst)
|
2019-08-09 10:18:36 -04:00
|
|
|
#f
|
|
|
|
(let ((clause (car lst)))
|
|
|
|
(if (or (eq? (car clause) 'else)
|
|
|
|
(eq? (car clause) #t))
|
|
|
|
(if (null? (cdr clause))
|
|
|
|
(car clause)
|
|
|
|
(cons 'begin (cdr clause)))
|
|
|
|
(if (null? (cdr clause))
|
|
|
|
; test by itself
|
|
|
|
(list 'or
|
|
|
|
(car clause)
|
|
|
|
(cond-clauses->if (cdr lst)))
|
|
|
|
; test => expression
|
|
|
|
(if (eq? (cadr clause) '=>)
|
|
|
|
(if (1arg-lambda? (caddr clause))
|
|
|
|
; test => (lambda (x) ...)
|
|
|
|
(let ((var (caadr (caddr clause))))
|
|
|
|
`(let ((,var ,(car clause)))
|
|
|
|
(if ,var ,(cons 'begin (cddr (caddr clause)))
|
|
|
|
,(cond-clauses->if (cdr lst)))))
|
|
|
|
; test => proc
|
|
|
|
(let ((b (gensym)))
|
|
|
|
`(let ((,b ,(car clause)))
|
|
|
|
(if ,b
|
|
|
|
(,(caddr clause) ,b)
|
|
|
|
,(cond-clauses->if (cdr lst))))))
|
|
|
|
(list 'if
|
|
|
|
(car clause)
|
|
|
|
(cons 'begin (cdr clause))
|
|
|
|
(cond-clauses->if (cdr lst)))))))))
|
2009-05-29 00:38:50 -04:00
|
|
|
(cond-clauses->if clauses))
|
2009-04-08 14:17:02 -04:00
|
|
|
|
2019-08-12 11:18:46 -04:00
|
|
|
(define-macro (import . rest)
|
|
|
|
`(import-procedure ,@(map (lambda (x) `(quote ,x)) rest)))
|
|
|
|
|
2019-08-09 10:18:36 -04:00
|
|
|
; standard procedures --------------------------------------------------------
|
2009-03-28 17:39:04 -04:00
|
|
|
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
(define (member item lst)
|
2009-01-31 20:53:58 -05:00
|
|
|
(cond ((atom? lst) #f)
|
2009-03-01 23:26:16 -05:00
|
|
|
((equal? (car lst) item) lst)
|
2009-01-31 20:53:58 -05:00
|
|
|
(#t (member item (cdr lst)))))
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
(define (memv item lst)
|
2009-01-31 20:53:58 -05:00
|
|
|
(cond ((atom? lst) #f)
|
2009-03-01 23:26:16 -05:00
|
|
|
((eqv? (car lst) item) lst)
|
2009-01-31 20:53:58 -05:00
|
|
|
(#t (memv item (cdr lst)))))
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
|
|
|
|
(define (assoc item lst)
|
2009-01-31 20:53:58 -05:00
|
|
|
(cond ((atom? lst) #f)
|
2019-08-09 10:18:36 -04:00
|
|
|
((equal? (caar lst) item) (car lst))
|
|
|
|
(#t (assoc item (cdr lst)))))
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
(define (assv item lst)
|
2009-01-31 20:53:58 -05:00
|
|
|
(cond ((atom? lst) #f)
|
2019-08-09 10:18:36 -04:00
|
|
|
((eqv? (caar lst) item) (car lst))
|
|
|
|
(#t (assv item (cdr lst)))))
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
|
2008-06-30 21:54:22 -04:00
|
|
|
(define (> a b) (< b a))
|
2016-09-17 14:30:05 -04:00
|
|
|
(define (nan? x) (or (equal? x +nan.0) (equal? x -nan.0)))
|
|
|
|
(define (<= a b) (not (or (< b a)
|
|
|
|
(and (nan? a) (nan? b)))))
|
|
|
|
(define (>= a b) (not (or (< a b)
|
|
|
|
(and (nan? a) (nan? b)))))
|
2009-03-11 22:47:34 -04:00
|
|
|
(define (negative? x) (< x 0))
|
|
|
|
(define (zero? x) (= x 0))
|
|
|
|
(define (positive? x) (> x 0))
|
|
|
|
(define (even? x) (= (logand x 1) 0))
|
|
|
|
(define (odd? x) (not (even? x)))
|
2009-07-08 15:07:56 -04:00
|
|
|
(define (identity x) x)
|
2008-06-30 21:54:22 -04:00
|
|
|
(define (1+ n) (+ n 1))
|
|
|
|
(define (1- n) (- n 1))
|
2009-05-14 13:54:59 -04:00
|
|
|
(define (mod0 x y) (- x (* (div0 x y) y)))
|
|
|
|
(define (div x y) (+ (div0 x y)
|
2019-08-09 10:18:36 -04:00
|
|
|
(or (and (< x 0)
|
|
|
|
(or (and (< y 0) 1)
|
|
|
|
-1))
|
|
|
|
0)))
|
2009-05-13 21:30:25 -04:00
|
|
|
(define (mod x y) (- x (* (div x y) y)))
|
2009-05-07 22:52:25 -04:00
|
|
|
(define (random n)
|
|
|
|
(if (integer? n)
|
2009-05-13 21:30:25 -04:00
|
|
|
(mod (rand) n)
|
2009-05-07 22:52:25 -04:00
|
|
|
(* (rand.double) n)))
|
2008-06-30 21:54:22 -04:00
|
|
|
(define (abs x) (if (< x 0) (- x) x))
|
2009-07-08 15:07:56 -04:00
|
|
|
(define (max x0 . xs)
|
|
|
|
(if (null? xs) x0
|
|
|
|
(foldl (lambda (a b) (if (< a b) b a)) x0 xs)))
|
|
|
|
(define (min x0 . xs)
|
|
|
|
(if (null? xs) x0
|
|
|
|
(foldl (lambda (a b) (if (< a b) a b)) x0 xs)))
|
2009-03-01 23:26:16 -05:00
|
|
|
(define (char? x) (eq? (typeof x) 'wchar))
|
2009-05-08 00:08:31 -04:00
|
|
|
(define (array? x) (or (vector? x)
|
2019-08-09 10:18:36 -04:00
|
|
|
(let ((t (typeof x)))
|
|
|
|
(and (pair? t) (eq? (car t) 'array)))))
|
2009-07-10 00:17:39 -04:00
|
|
|
(define (closure? x) (and (function? x) (not (builtin? x))))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
|
|
|
(define (caar x) (car (car x)))
|
2009-03-28 17:39:04 -04:00
|
|
|
(define (cadr x) (car (cdr x)))
|
2008-06-30 21:54:22 -04:00
|
|
|
(define (cdar x) (cdr (car x)))
|
2009-03-28 17:39:04 -04:00
|
|
|
(define (cddr x) (cdr (cdr x)))
|
2008-06-30 21:54:22 -04:00
|
|
|
(define (caaar x) (car (car (car x))))
|
|
|
|
(define (caadr x) (car (car (cdr x))))
|
|
|
|
(define (cadar x) (car (cdr (car x))))
|
2009-03-28 17:39:04 -04:00
|
|
|
(define (caddr x) (car (cdr (cdr x))))
|
2008-06-30 21:54:22 -04:00
|
|
|
(define (cdaar x) (cdr (car (car x))))
|
|
|
|
(define (cdadr x) (cdr (car (cdr x))))
|
|
|
|
(define (cddar x) (cdr (cdr (car x))))
|
|
|
|
(define (cdddr x) (cdr (cdr (cdr x))))
|
2009-08-08 17:44:14 -04:00
|
|
|
(define (caaaar x) (car (car (car (car x)))))
|
|
|
|
(define (caaadr x) (car (car (car (cdr x)))))
|
|
|
|
(define (caadar x) (car (car (cdr (car x)))))
|
|
|
|
(define (caaddr x) (car (car (cdr (cdr x)))))
|
|
|
|
(define (cadaar x) (car (cdr (car (car x)))))
|
|
|
|
(define (cadadr x) (car (cdr (car (cdr x)))))
|
|
|
|
(define (caddar x) (car (cdr (cdr (car x)))))
|
2009-03-28 17:39:04 -04:00
|
|
|
(define (cadddr x) (car (cdr (cdr (cdr x)))))
|
2009-08-08 17:44:14 -04:00
|
|
|
(define (cdaaar x) (cdr (car (car (car x)))))
|
|
|
|
(define (cdaadr x) (cdr (car (car (cdr x)))))
|
|
|
|
(define (cdadar x) (cdr (car (cdr (car x)))))
|
|
|
|
(define (cdaddr x) (cdr (car (cdr (cdr x)))))
|
|
|
|
(define (cddaar x) (cdr (cdr (car (car x)))))
|
|
|
|
(define (cddadr x) (cdr (cdr (car (cdr x)))))
|
|
|
|
(define (cdddar x) (cdr (cdr (cdr (car x)))))
|
|
|
|
(define (cddddr x) (cdr (cdr (cdr (cdr x)))))
|
2009-03-28 17:39:04 -04:00
|
|
|
|
2009-07-16 21:30:26 -04:00
|
|
|
(let ((*values* (list '*values*)))
|
|
|
|
(set! values
|
2019-08-09 10:18:36 -04:00
|
|
|
(lambda vs
|
|
|
|
(if (and (pair? vs) (null? (cdr vs)))
|
|
|
|
(car vs)
|
|
|
|
(cons *values* vs))))
|
2009-07-16 21:30:26 -04:00
|
|
|
(set! call-with-values
|
2019-08-09 10:18:36 -04:00
|
|
|
(lambda (producer consumer)
|
|
|
|
(let ((res (producer)))
|
|
|
|
(if (and (pair? res) (eq? *values* (car res)))
|
|
|
|
(apply consumer (cdr res))
|
|
|
|
(consumer res))))))
|
2009-07-16 21:30:26 -04:00
|
|
|
|
2019-08-09 10:18:36 -04:00
|
|
|
; list utilities -------------------------------------------------------------
|
2008-06-30 21:54:22 -04:00
|
|
|
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
(define (every pred lst)
|
2009-01-31 20:53:58 -05:00
|
|
|
(or (atom? lst)
|
2008-06-30 21:54:22 -04:00
|
|
|
(and (pred (car lst))
|
|
|
|
(every pred (cdr lst)))))
|
|
|
|
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
(define (any pred lst)
|
2009-01-31 20:53:58 -05:00
|
|
|
(and (pair? lst)
|
2008-06-30 21:54:22 -04:00
|
|
|
(or (pred (car lst))
|
|
|
|
(any pred (cdr lst)))))
|
|
|
|
|
2009-01-31 20:53:58 -05:00
|
|
|
(define (list? a) (or (null? a) (and (pair? a) (list? (cdr a)))))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2009-04-08 14:17:02 -04:00
|
|
|
(define (list-tail lst n)
|
2008-06-30 21:54:22 -04:00
|
|
|
(if (<= n 0) lst
|
2009-04-08 14:17:02 -04:00
|
|
|
(list-tail (cdr lst) (- n 1))))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2009-04-01 00:31:49 -04:00
|
|
|
(define (list-head lst n)
|
|
|
|
(if (<= n 0) ()
|
|
|
|
(cons (car lst)
|
2019-08-09 10:18:36 -04:00
|
|
|
(list-head (cdr lst) (- n 1)))))
|
2009-04-01 00:31:49 -04:00
|
|
|
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
(define (list-ref lst n)
|
2009-04-08 14:17:02 -04:00
|
|
|
(car (list-tail lst n)))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2009-03-04 22:48:17 -05:00
|
|
|
; bounded length test
|
|
|
|
; use this instead of (= (length lst) n), since it avoids unnecessary
|
|
|
|
; work and always terminates.
|
|
|
|
(define (length= lst n)
|
|
|
|
(cond ((< n 0) #f)
|
2019-08-09 10:18:36 -04:00
|
|
|
((= n 0) (atom? lst))
|
|
|
|
((atom? lst) (= n 0))
|
|
|
|
(else (length= (cdr lst) (- n 1)))))
|
2009-03-04 22:48:17 -05:00
|
|
|
|
2009-05-31 17:06:04 -04:00
|
|
|
(define (length> lst n)
|
|
|
|
(cond ((< n 0) lst)
|
2019-08-09 10:18:36 -04:00
|
|
|
((= n 0) (and (pair? lst) lst))
|
|
|
|
((atom? lst) (< n 0))
|
|
|
|
(else (length> (cdr lst) (- n 1)))))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2009-04-08 14:17:02 -04:00
|
|
|
(define (last-pair l)
|
2009-05-31 17:06:04 -04:00
|
|
|
(if (atom? (cdr l))
|
|
|
|
l
|
|
|
|
(last-pair (cdr l))))
|
|
|
|
|
|
|
|
(define (lastcdr l)
|
|
|
|
(if (atom? l)
|
|
|
|
l
|
|
|
|
(cdr (last-pair l))))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2009-03-28 17:39:04 -04:00
|
|
|
(define (to-proper l)
|
|
|
|
(cond ((null? l) l)
|
2019-08-09 10:18:36 -04:00
|
|
|
((atom? l) (list l))
|
|
|
|
(else (cons (car l) (to-proper (cdr l))))))
|
2009-03-28 17:39:04 -04:00
|
|
|
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
(define (map! f lst)
|
2008-06-30 21:54:22 -04:00
|
|
|
(prog1 lst
|
2019-08-09 10:18:36 -04:00
|
|
|
(while (pair? lst)
|
|
|
|
(set-car! lst (f (car lst)))
|
|
|
|
(set! lst (cdr lst)))))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2009-07-26 23:34:33 -04:00
|
|
|
(define (filter pred lst)
|
|
|
|
(define (filter- f lst acc)
|
|
|
|
(cdr
|
|
|
|
(prog1 acc
|
|
|
|
(while (pair? lst)
|
2019-08-09 10:18:36 -04:00
|
|
|
(begin (if (pred (car lst))
|
|
|
|
(set! acc
|
|
|
|
(cdr (set-cdr! acc (cons (car lst) ())))))
|
|
|
|
(set! lst (cdr lst)))))))
|
2009-07-26 23:34:33 -04:00
|
|
|
(filter- pred lst (list ())))
|
2009-05-29 00:38:50 -04:00
|
|
|
|
2010-05-03 01:07:22 -04:00
|
|
|
(define (separate pred lst)
|
|
|
|
(define (separate- pred lst yes no)
|
2010-05-05 01:31:46 -04:00
|
|
|
(let ((vals
|
2019-08-09 10:18:36 -04:00
|
|
|
(prog1
|
|
|
|
(cons yes no)
|
|
|
|
(while (pair? lst)
|
|
|
|
(begin (if (pred (car lst))
|
|
|
|
(set! yes
|
|
|
|
(cdr (set-cdr! yes (cons (car lst) ()))))
|
|
|
|
(set! no
|
|
|
|
(cdr (set-cdr! no (cons (car lst) ())))))
|
|
|
|
(set! lst (cdr lst)))))))
|
2010-05-05 01:31:46 -04:00
|
|
|
(values (cdr (car vals)) (cdr (cdr vals)))))
|
|
|
|
(separate- pred lst (list ()) (list ())))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2009-05-31 17:06:04 -04:00
|
|
|
(define (count f l)
|
|
|
|
(define (count- f l n)
|
|
|
|
(if (null? l)
|
2019-08-09 10:18:36 -04:00
|
|
|
n
|
|
|
|
(count- f (cdr l) (if (f (car l))
|
|
|
|
(+ n 1)
|
|
|
|
n))))
|
2009-05-31 17:06:04 -04:00
|
|
|
(count- f l 0))
|
|
|
|
|
2009-03-28 17:39:04 -04:00
|
|
|
(define (nestlist f zero n)
|
|
|
|
(if (<= n 0) ()
|
|
|
|
(cons zero (nestlist f (f zero) (- n 1)))))
|
|
|
|
|
2008-06-30 21:54:22 -04:00
|
|
|
(define (foldr f zero lst)
|
2009-01-31 20:53:58 -05:00
|
|
|
(if (null? lst) zero
|
2009-08-09 00:04:31 -04:00
|
|
|
(f (car lst) (foldr f zero (cdr lst)))))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
|
|
|
(define (foldl f zero lst)
|
2009-01-31 20:53:58 -05:00
|
|
|
(if (null? lst) zero
|
2009-08-09 00:04:31 -04:00
|
|
|
(foldl f (f (car lst) zero) (cdr lst))))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2010-12-23 01:49:37 -05:00
|
|
|
(define (reverse- zero lst)
|
|
|
|
(if (null? lst) zero
|
|
|
|
(reverse- (cons (car lst) zero) (cdr lst))))
|
|
|
|
|
|
|
|
(define (reverse lst) (reverse- () lst))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2011-04-10 23:24:30 -04:00
|
|
|
(define (reverse!- prev l)
|
|
|
|
(while (pair? l)
|
2019-08-09 10:18:36 -04:00
|
|
|
(set! l (prog1 (cdr l)
|
|
|
|
(set-cdr! l (prog1 prev
|
|
|
|
(set! prev l))))))
|
2011-04-10 23:24:30 -04:00
|
|
|
prev)
|
|
|
|
|
|
|
|
(define (reverse! l) (reverse!- () l))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2009-05-30 17:13:13 -04:00
|
|
|
(define (copy-tree l)
|
|
|
|
(if (atom? l) l
|
|
|
|
(cons (copy-tree (car l))
|
|
|
|
(copy-tree (cdr l)))))
|
|
|
|
|
2009-03-28 17:39:04 -04:00
|
|
|
(define (delete-duplicates lst)
|
2013-02-11 04:48:21 -05:00
|
|
|
(if (length> lst 20)
|
|
|
|
(let ((t (table)))
|
2019-08-09 10:18:36 -04:00
|
|
|
(let loop ((l lst) (acc '()))
|
|
|
|
(if (atom? l)
|
|
|
|
(reverse! acc)
|
|
|
|
(if (has? t (car l))
|
|
|
|
(loop (cdr l) acc)
|
|
|
|
(begin
|
|
|
|
(put! t (car l) #t)
|
|
|
|
(loop (cdr l) (cons (car l) acc)))))))
|
2013-02-11 04:48:21 -05:00
|
|
|
(if (atom? lst)
|
2019-08-09 10:18:36 -04:00
|
|
|
lst
|
|
|
|
(let ((elt (car lst))
|
|
|
|
(tail (cdr lst)))
|
|
|
|
(if (member elt tail)
|
|
|
|
(delete-duplicates tail)
|
|
|
|
(cons elt
|
|
|
|
(delete-duplicates tail)))))))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2019-08-09 10:18:36 -04:00
|
|
|
; backquote ------------------------------------------------------------------
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2011-04-10 23:24:30 -04:00
|
|
|
(define (revappend l1 l2) (reverse- l2 l1))
|
|
|
|
(define (nreconc l1 l2) (reverse!- l2 l1))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
(define (self-evaluating? x)
|
2009-01-31 20:53:58 -05:00
|
|
|
(or (and (atom? x)
|
|
|
|
(not (symbol? x)))
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
(and (constant? x)
|
2019-08-09 10:18:36 -04:00
|
|
|
(symbol? x)
|
2009-04-17 10:41:15 -04:00
|
|
|
(eq x (top-level-value x)))))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2011-04-10 23:24:30 -04:00
|
|
|
(define-macro (quasiquote x) (bq-process x 0))
|
|
|
|
|
|
|
|
(define (splice-form? x)
|
|
|
|
(or (and (pair? x) (or (eq? (car x) 'unquote-splicing)
|
2019-08-09 10:18:36 -04:00
|
|
|
(eq? (car x) 'unquote-nsplicing)
|
|
|
|
(and (eq? (car x) 'unquote)
|
|
|
|
(length> x 2))))
|
2011-04-10 23:24:30 -04:00
|
|
|
(eq? x 'unquote)))
|
|
|
|
|
|
|
|
;; bracket without splicing
|
|
|
|
(define (bq-bracket1 x d)
|
|
|
|
(if (and (pair? x) (eq? (car x) 'unquote))
|
|
|
|
(if (= d 0)
|
2019-08-09 10:18:36 -04:00
|
|
|
(cadr x)
|
|
|
|
(list cons ''unquote
|
|
|
|
(bq-process (cdr x) (- d 1))))
|
2011-04-10 23:24:30 -04:00
|
|
|
(bq-process x d)))
|
|
|
|
|
|
|
|
(define (bq-bracket x d)
|
|
|
|
(cond ((atom? x) (list list (bq-process x d)))
|
2019-08-09 10:18:36 -04:00
|
|
|
((eq? (car x) 'unquote)
|
|
|
|
(if (= d 0)
|
|
|
|
(cons list (cdr x))
|
|
|
|
(list list (list cons ''unquote
|
|
|
|
(bq-process (cdr x) (- d 1))))))
|
|
|
|
((eq? (car x) 'unquote-splicing)
|
|
|
|
(if (= d 0)
|
|
|
|
(list 'copy-list (cadr x))
|
|
|
|
(list list (list list ''unquote-splicing
|
|
|
|
(bq-process (cadr x) (- d 1))))))
|
|
|
|
((eq? (car x) 'unquote-nsplicing)
|
|
|
|
(if (= d 0)
|
|
|
|
(cadr x)
|
|
|
|
(list list (list list ''unquote-nsplicing
|
|
|
|
(bq-process (cadr x) (- d 1))))))
|
|
|
|
(else (list list (bq-process x d)))))
|
2011-04-10 23:24:30 -04:00
|
|
|
|
|
|
|
(define (bq-process x d)
|
|
|
|
(cond ((symbol? x) (list 'quote x))
|
2019-08-09 10:18:36 -04:00
|
|
|
((vector? x)
|
|
|
|
(let ((body (bq-process (vector->list x) d)))
|
|
|
|
(if (eq? (car body) list)
|
|
|
|
(cons vector (cdr body))
|
|
|
|
(list apply vector body))))
|
2011-04-10 23:24:30 -04:00
|
|
|
((atom? x) x)
|
|
|
|
((eq? (car x) 'quasiquote)
|
2019-08-09 10:18:36 -04:00
|
|
|
(list list ''quasiquote (bq-process (cadr x) (+ d 1))))
|
2011-04-10 23:24:30 -04:00
|
|
|
((eq? (car x) 'unquote)
|
2019-08-09 10:18:36 -04:00
|
|
|
(if (and (= d 0) (length= x 2))
|
|
|
|
(cadr x)
|
|
|
|
(list cons ''unquote (bq-process (cdr x) (- d 1)))))
|
|
|
|
((not (any splice-form? x))
|
2008-06-30 21:54:22 -04:00
|
|
|
(let ((lc (lastcdr x))
|
2011-04-10 23:24:30 -04:00
|
|
|
(forms (map (lambda (x) (bq-bracket1 x d)) x)))
|
2009-01-31 20:53:58 -05:00
|
|
|
(if (null? lc)
|
2011-04-10 23:24:30 -04:00
|
|
|
(cons list forms)
|
2019-08-09 10:18:36 -04:00
|
|
|
(if (null? (cdr forms))
|
|
|
|
(list cons (car forms) (bq-process lc d))
|
|
|
|
(nconc (cons list* forms) (list (bq-process lc d)))))))
|
|
|
|
(else
|
|
|
|
(let loop ((p x) (q ()))
|
|
|
|
(cond ((null? p) ;; proper list
|
|
|
|
(cons 'nconc (reverse! q)))
|
|
|
|
((pair? p)
|
|
|
|
(cond ((eq? (car p) 'unquote)
|
|
|
|
;; (... . ,x)
|
|
|
|
(cons 'nconc
|
|
|
|
(nreconc q
|
|
|
|
(if (= d 0)
|
|
|
|
(cdr p)
|
|
|
|
(list (list list ''unquote)
|
|
|
|
(bq-process (cdr p)
|
|
|
|
(- d 1)))))))
|
|
|
|
(else
|
|
|
|
(loop (cdr p) (cons (bq-bracket (car p) d) q)))))
|
|
|
|
(else
|
|
|
|
;; (... . x)
|
|
|
|
(cons 'nconc (reverse! (cons (bq-process p d) q)))))))))
|
|
|
|
|
|
|
|
; standard macros ------------------------------------------------------------
|
2009-03-28 17:39:04 -04:00
|
|
|
|
2009-02-20 00:11:05 -05:00
|
|
|
(define (quote-value v)
|
|
|
|
(if (self-evaluating? v)
|
|
|
|
v
|
|
|
|
(list 'quote v)))
|
|
|
|
|
2009-03-28 17:39:04 -04:00
|
|
|
(define-macro (let* binds . body)
|
2009-05-29 00:38:50 -04:00
|
|
|
(if (atom? binds) `((lambda () ,@body))
|
2009-04-01 18:23:19 -04:00
|
|
|
`((lambda (,(caar binds))
|
2019-08-09 10:18:36 -04:00
|
|
|
,@(if (pair? (cdr binds))
|
|
|
|
`((let* ,(cdr binds) ,@body))
|
|
|
|
body))
|
|
|
|
,(cadar binds))))
|
2009-04-01 18:23:19 -04:00
|
|
|
|
2009-05-20 20:56:25 -04:00
|
|
|
(define-macro (when c . body) (list 'if c (cons 'begin body) #f))
|
|
|
|
(define-macro (unless c . body) (list 'if c #f (cons 'begin body)))
|
2009-03-28 17:39:04 -04:00
|
|
|
|
2009-02-20 00:11:05 -05:00
|
|
|
(define-macro (case key . clauses)
|
2009-03-01 23:26:16 -05:00
|
|
|
(define (vals->cond key v)
|
2009-02-20 00:11:05 -05:00
|
|
|
(cond ((eq? v 'else) 'else)
|
2019-08-09 10:18:36 -04:00
|
|
|
((null? v) #f)
|
|
|
|
((symbol? v) `(eq? ,key ,(quote-value v)))
|
2009-03-13 10:54:48 -04:00
|
|
|
((atom? v) `(eqv? ,key ,(quote-value v)))
|
2019-08-09 10:18:36 -04:00
|
|
|
((null? (cdr v)) `(eqv? ,key ,(quote-value (car v))))
|
|
|
|
((every symbol? v)
|
|
|
|
`(memq ,key ',v))
|
|
|
|
(else `(memv ,key ',v))))
|
2009-02-20 00:11:05 -05:00
|
|
|
(let ((g (gensym)))
|
|
|
|
`(let ((,g ,key))
|
2009-07-17 22:16:18 -04:00
|
|
|
(cond ,.(map (lambda (clause)
|
2019-08-09 10:18:36 -04:00
|
|
|
(cons (vals->cond g (car clause))
|
|
|
|
(cdr clause)))
|
|
|
|
clauses)))))
|
2009-02-20 00:11:05 -05:00
|
|
|
|
2009-02-20 14:50:35 -05:00
|
|
|
(define-macro (do vars test-spec . commands)
|
|
|
|
(let ((loop (gensym))
|
2019-08-09 10:18:36 -04:00
|
|
|
(test-expr (car test-spec))
|
|
|
|
(vars (map car vars))
|
|
|
|
(inits (map cadr vars))
|
|
|
|
(steps (map (lambda (x)
|
|
|
|
(if (pair? (cddr x))
|
|
|
|
(caddr x)
|
|
|
|
(car x)))
|
|
|
|
vars)))
|
2009-02-20 14:50:35 -05:00
|
|
|
`(letrec ((,loop (lambda ,vars
|
2019-08-09 10:18:36 -04:00
|
|
|
(if ,test-expr
|
|
|
|
(begin
|
|
|
|
,@(cdr test-spec))
|
|
|
|
(begin
|
|
|
|
,@commands
|
|
|
|
(,loop ,.steps))))))
|
2009-07-17 22:16:18 -04:00
|
|
|
(,loop ,.inits))))
|
2009-02-20 14:50:35 -05:00
|
|
|
|
2009-07-16 21:30:26 -04:00
|
|
|
; SRFI 8
|
|
|
|
(define-macro (receive formals expr . body)
|
|
|
|
`(call-with-values (lambda () ,expr)
|
|
|
|
(lambda ,formals ,@body)))
|
|
|
|
|
2019-09-03 03:02:05 -04:00
|
|
|
(define-macro (let-values bindings . body)
|
|
|
|
(unless (pair? bindings) (error "let-values: syntax error"))
|
|
|
|
(let* ((binding (car bindings))
|
|
|
|
(names (car binding))
|
|
|
|
(form (cadr binding)))
|
|
|
|
(unless (null? (cddr binding)) (error "let-values: syntax error"))
|
|
|
|
(unless (null? (cdr bindings)) (error "let-values: syntax error"))
|
|
|
|
`(receive ,names ,form ,@body)))
|
|
|
|
|
2009-02-20 00:11:05 -05:00
|
|
|
(define-macro (dotimes var . body)
|
|
|
|
(let ((v (car var))
|
|
|
|
(cnt (cadr var)))
|
|
|
|
`(for 0 (- ,cnt 1)
|
2009-05-29 00:38:50 -04:00
|
|
|
(lambda (,v) ,@body))))
|
2009-02-20 00:11:05 -05:00
|
|
|
|
|
|
|
(define (map-int f n)
|
|
|
|
(if (<= n 0)
|
|
|
|
()
|
|
|
|
(let ((first (cons (f 0) ()))
|
|
|
|
(acc ()))
|
|
|
|
(set! acc first)
|
|
|
|
(for 1 (- n 1)
|
|
|
|
(lambda (i)
|
2009-03-01 23:26:16 -05:00
|
|
|
(begin (set-cdr! acc (cons (f i) ()))
|
2009-02-20 00:11:05 -05:00
|
|
|
(set! acc (cdr acc)))))
|
|
|
|
first)))
|
|
|
|
|
|
|
|
(define (iota n) (map-int identity n))
|
|
|
|
|
2009-08-17 23:46:09 -04:00
|
|
|
(define (for-each f l . lsts)
|
|
|
|
(define (for-each-n f lsts)
|
|
|
|
(if (pair? (car lsts))
|
2019-08-09 10:18:36 -04:00
|
|
|
(begin (apply f (map car lsts))
|
|
|
|
(for-each-n f (map cdr lsts)))))
|
2009-08-17 23:46:09 -04:00
|
|
|
(if (null? lsts)
|
|
|
|
(while (pair? l)
|
2019-08-09 10:18:36 -04:00
|
|
|
(begin (f (car l))
|
|
|
|
(set! l (cdr l))))
|
2009-08-17 23:46:09 -04:00
|
|
|
(for-each-n f (cons l lsts)))
|
|
|
|
#t)
|
2009-03-11 22:47:34 -04:00
|
|
|
|
2009-08-07 20:29:55 -04:00
|
|
|
(define-macro (with-bindings binds . body)
|
|
|
|
(let ((vars (map car binds))
|
2019-08-09 10:18:36 -04:00
|
|
|
(vals (map cadr binds))
|
|
|
|
(olds (map (lambda (x) (gensym)) binds)))
|
2009-08-07 20:29:55 -04:00
|
|
|
`(let ,(map list olds vars)
|
|
|
|
,@(map (lambda (v val) `(set! ,v ,val)) vars vals)
|
|
|
|
(unwind-protect
|
2019-08-09 10:18:36 -04:00
|
|
|
(begin ,@body)
|
|
|
|
(begin ,@(map (lambda (v old) `(set! ,v ,old)) vars olds))))))
|
2009-08-07 20:29:55 -04:00
|
|
|
|
2019-08-09 10:18:36 -04:00
|
|
|
; exceptions -----------------------------------------------------------------
|
2009-03-28 17:39:04 -04:00
|
|
|
|
2009-02-20 00:11:05 -05:00
|
|
|
(define (error . args) (raise (cons 'error args)))
|
|
|
|
|
|
|
|
(define-macro (throw tag value) `(raise (list 'thrown-value ,tag ,value)))
|
|
|
|
(define-macro (catch tag expr)
|
|
|
|
(let ((e (gensym)))
|
|
|
|
`(trycatch ,expr
|
|
|
|
(lambda (,e) (if (and (pair? ,e)
|
|
|
|
(eq (car ,e) 'thrown-value)
|
|
|
|
(eq (cadr ,e) ,tag))
|
|
|
|
(caddr ,e)
|
2019-08-09 10:18:36 -04:00
|
|
|
(raise ,e))))))
|
2009-02-20 00:11:05 -05:00
|
|
|
|
|
|
|
(define-macro (unwind-protect expr finally)
|
2009-05-05 00:01:06 -04:00
|
|
|
(let ((e (gensym))
|
2019-08-09 10:18:36 -04:00
|
|
|
(thk (gensym)))
|
2009-05-05 00:01:06 -04:00
|
|
|
`(let ((,thk (lambda () ,finally)))
|
|
|
|
(prog1 (trycatch ,expr
|
2019-08-09 10:18:36 -04:00
|
|
|
(lambda (,e) (begin (,thk) (raise ,e))))
|
|
|
|
(,thk)))))
|
2009-02-20 00:11:05 -05:00
|
|
|
|
2019-08-09 10:18:36 -04:00
|
|
|
; debugging utilities --------------------------------------------------------
|
2009-03-11 22:47:34 -04:00
|
|
|
|
2009-01-31 20:53:58 -05:00
|
|
|
(define-macro (assert expr) `(if ,expr #t (raise '(assert-failed ,expr))))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2009-05-29 00:38:50 -04:00
|
|
|
(define traced?
|
2019-08-25 15:57:31 -04:00
|
|
|
(letrec ((sample-traced-lambda (lambda args (begin (writeln (cons 'x args))
|
2019-08-09 10:18:36 -04:00
|
|
|
(apply #.apply args)))))
|
2009-05-29 00:38:50 -04:00
|
|
|
(lambda (f)
|
2009-08-09 16:34:07 -04:00
|
|
|
(and (closure? f)
|
2019-08-09 10:18:36 -04:00
|
|
|
(equal? (function:code f)
|
|
|
|
(function:code sample-traced-lambda))))))
|
2009-05-12 21:13:40 -04:00
|
|
|
|
2009-03-28 17:39:04 -04:00
|
|
|
(define (trace sym)
|
2009-05-12 21:13:40 -04:00
|
|
|
(let* ((func (top-level-value sym))
|
2019-08-09 10:18:36 -04:00
|
|
|
(args (gensym)))
|
2009-05-12 21:13:40 -04:00
|
|
|
(if (not (traced? func))
|
2019-08-09 10:18:36 -04:00
|
|
|
(set-top-level-value! sym
|
|
|
|
(eval
|
|
|
|
`(lambda ,args
|
2019-08-25 15:57:31 -04:00
|
|
|
(begin (writeln (cons ',sym ,args))
|
2019-08-09 10:18:36 -04:00
|
|
|
(apply ',func ,args)))))))
|
2009-03-28 17:39:04 -04:00
|
|
|
'ok)
|
|
|
|
|
|
|
|
(define (untrace sym)
|
2009-05-12 21:13:40 -04:00
|
|
|
(let ((func (top-level-value sym)))
|
|
|
|
(if (traced? func)
|
2019-08-09 10:18:36 -04:00
|
|
|
(set-top-level-value! sym
|
|
|
|
(aref (function:vals func) 2)))))
|
2009-03-28 17:39:04 -04:00
|
|
|
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
(define-macro (time expr)
|
2008-06-30 21:54:22 -04:00
|
|
|
(let ((t0 (gensym)))
|
|
|
|
`(let ((,t0 (time.now)))
|
|
|
|
(prog1
|
2019-08-09 10:18:36 -04:00
|
|
|
,expr
|
2019-08-25 15:57:31 -04:00
|
|
|
(display "Elapsed time: ")
|
|
|
|
(display (- (time.now) ,t0))
|
|
|
|
(display " seconds")
|
|
|
|
(newline)))))
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
|
2019-08-09 10:18:36 -04:00
|
|
|
; text I/O -------------------------------------------------------------------
|
2009-03-28 17:39:04 -04:00
|
|
|
|
2009-08-08 17:44:14 -04:00
|
|
|
(define (io.readline s) (io.readuntil s #\linefeed))
|
2009-03-28 17:39:04 -04:00
|
|
|
|
2009-07-21 23:50:57 -04:00
|
|
|
; call f on a stream until the stream runs out of data
|
|
|
|
(define (read-all-of f s)
|
|
|
|
(let loop ((lines ())
|
2019-08-09 10:18:36 -04:00
|
|
|
(curr (f s)))
|
2009-07-21 23:50:57 -04:00
|
|
|
(if (io.eof? s)
|
2019-08-09 10:18:36 -04:00
|
|
|
(reverse! lines)
|
|
|
|
(loop (cons curr lines) (f s)))))
|
2009-07-21 23:50:57 -04:00
|
|
|
|
|
|
|
(define (io.readlines s) (read-all-of io.readline s))
|
|
|
|
(define (read-all s) (read-all-of read s))
|
|
|
|
|
2009-08-08 19:43:12 -04:00
|
|
|
(define (io.readall s)
|
|
|
|
(let ((b (buffer)))
|
|
|
|
(io.copy b s)
|
2009-08-09 14:04:03 -04:00
|
|
|
(let ((str (io.tostring! b)))
|
|
|
|
(if (and (equal? str "") (io.eof? s))
|
2019-08-09 10:18:36 -04:00
|
|
|
(eof-object)
|
|
|
|
str))))
|
2009-08-08 19:43:12 -04:00
|
|
|
|
2009-08-07 20:29:55 -04:00
|
|
|
(define-macro (with-output-to stream . body)
|
|
|
|
`(with-bindings ((*output-stream* ,stream))
|
2019-08-09 10:18:36 -04:00
|
|
|
,@body))
|
2009-08-09 14:27:05 -04:00
|
|
|
(define-macro (with-input-from stream . body)
|
|
|
|
`(with-bindings ((*input-stream* ,stream))
|
2019-08-09 10:18:36 -04:00
|
|
|
,@body))
|
2009-08-07 20:29:55 -04:00
|
|
|
|
2019-08-09 10:18:36 -04:00
|
|
|
; vector functions -----------------------------------------------------------
|
2009-03-28 17:39:04 -04:00
|
|
|
|
|
|
|
(define (list->vector l) (apply vector l))
|
|
|
|
(define (vector->list v)
|
|
|
|
(let ((n (length v))
|
|
|
|
(l ()))
|
|
|
|
(for 1 n
|
|
|
|
(lambda (i)
|
|
|
|
(set! l (cons (aref v (- n i)) l))))
|
|
|
|
l))
|
|
|
|
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
(define (vector.map f v)
|
2008-12-21 00:55:00 -05:00
|
|
|
(let* ((n (length v))
|
|
|
|
(nv (vector.alloc n)))
|
|
|
|
(for 0 (- n 1)
|
|
|
|
(lambda (i)
|
2009-01-31 20:53:58 -05:00
|
|
|
(aset! nv i (f (aref v i)))))
|
2008-12-21 00:55:00 -05:00
|
|
|
nv))
|
|
|
|
|
2019-08-09 10:18:36 -04:00
|
|
|
; table functions ------------------------------------------------------------
|
2009-03-28 17:39:04 -04:00
|
|
|
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
(define (table.pairs t)
|
2008-12-21 00:55:00 -05:00
|
|
|
(table.foldl (lambda (k v z) (cons (cons k v) z))
|
|
|
|
() t))
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
(define (table.keys t)
|
2008-12-21 00:55:00 -05:00
|
|
|
(table.foldl (lambda (k v z) (cons k z))
|
|
|
|
() t))
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
(define (table.values t)
|
2008-12-21 00:55:00 -05:00
|
|
|
(table.foldl (lambda (k v z) (cons v z))
|
|
|
|
() t))
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
(define (table.clone t)
|
2008-12-22 01:36:50 -05:00
|
|
|
(let ((nt (table)))
|
2009-01-31 20:53:58 -05:00
|
|
|
(table.foldl (lambda (k v z) (put! nt k v))
|
2008-12-22 01:36:50 -05:00
|
|
|
() t)
|
|
|
|
nt))
|
2009-03-28 17:39:04 -04:00
|
|
|
(define (table.invert t)
|
|
|
|
(let ((nt (table)))
|
|
|
|
(table.foldl (lambda (k v z) (put! nt v k))
|
2019-08-09 10:18:36 -04:00
|
|
|
() t)
|
2009-03-28 17:39:04 -04:00
|
|
|
nt))
|
|
|
|
(define (table.foreach f t)
|
|
|
|
(table.foldl (lambda (k v z) (begin (f k v) #t)) () t))
|
2009-02-05 22:41:24 -05:00
|
|
|
|
2019-08-09 10:18:36 -04:00
|
|
|
; string functions -----------------------------------------------------------
|
2009-02-18 22:31:40 -05:00
|
|
|
|
2009-05-06 22:10:52 -04:00
|
|
|
(define (string.tail s n) (string.sub s (string.inc s 0 n)))
|
2009-02-19 17:29:47 -05:00
|
|
|
|
2009-02-27 20:59:01 -05:00
|
|
|
(define *whitespace*
|
|
|
|
(string.encode #array(wchar 9 10 11 12 13 32 133 160 5760 6158 8192
|
2019-08-09 10:18:36 -04:00
|
|
|
8193 8194 8195 8196 8197 8198 8199 8200
|
|
|
|
8201 8202 8232 8233 8239 8287 12288)))
|
2009-02-27 20:59:01 -05:00
|
|
|
|
|
|
|
(define (string.trim s at-start at-end)
|
|
|
|
(define (trim-start s chars i L)
|
2009-04-08 14:17:02 -04:00
|
|
|
(if (and (< i L)
|
2019-08-09 10:18:36 -04:00
|
|
|
(string.find chars (string.char s i)))
|
|
|
|
(trim-start s chars (string.inc s i) L)
|
|
|
|
i))
|
2009-02-27 20:59:01 -05:00
|
|
|
(define (trim-end s chars i)
|
|
|
|
(if (and (> i 0)
|
2019-08-09 10:18:36 -04:00
|
|
|
(string.find chars (string.char s (string.dec s i))))
|
|
|
|
(trim-end s chars (string.dec s i))
|
|
|
|
i))
|
2009-04-08 14:17:02 -04:00
|
|
|
(let ((L (length s)))
|
2009-02-27 20:59:01 -05:00
|
|
|
(string.sub s
|
2019-08-09 10:18:36 -04:00
|
|
|
(trim-start s at-start 0 L)
|
|
|
|
(trim-end s at-end L))))
|
2009-02-27 20:59:01 -05:00
|
|
|
|
2009-03-04 22:48:17 -05:00
|
|
|
(define (string.map f s)
|
|
|
|
(let ((b (buffer))
|
2019-08-09 10:18:36 -04:00
|
|
|
(n (length s)))
|
2009-03-12 23:30:10 -04:00
|
|
|
(let ((i 0))
|
2009-04-08 14:17:02 -04:00
|
|
|
(while (< i n)
|
2019-08-09 10:18:36 -04:00
|
|
|
(begin (io.putc b (f (string.char s i)))
|
|
|
|
(set! i (string.inc s i)))))
|
2009-03-12 23:30:10 -04:00
|
|
|
(io.tostring! b)))
|
2009-03-04 22:48:17 -05:00
|
|
|
|
2009-03-28 17:39:04 -04:00
|
|
|
(define (string.rep s k)
|
|
|
|
(cond ((< k 4)
|
2019-08-09 10:18:36 -04:00
|
|
|
(cond ((<= k 0) "")
|
|
|
|
((= k 1) (string s))
|
|
|
|
((= k 2) (string s s))
|
|
|
|
(else (string s s s))))
|
|
|
|
((odd? k) (string s (string.rep s (- k 1))))
|
|
|
|
(else (string.rep (string s s) (/ k 2)))))
|
2009-03-28 17:39:04 -04:00
|
|
|
|
2009-05-29 22:52:22 -04:00
|
|
|
(define (string.lpad s n c) (string (string.rep c (- n (string.count s))) s))
|
|
|
|
(define (string.rpad s n c) (string s (string.rep c (- n (string.count s)))))
|
2009-03-28 17:39:04 -04:00
|
|
|
|
2019-08-25 15:57:31 -04:00
|
|
|
(define (write-to-string v)
|
2009-03-04 22:48:17 -05:00
|
|
|
(let ((b (buffer)))
|
2019-08-25 15:57:31 -04:00
|
|
|
(write v b)
|
2009-03-04 22:48:17 -05:00
|
|
|
(io.tostring! b)))
|
|
|
|
|
2019-08-25 15:57:31 -04:00
|
|
|
(define print-to-string write-to-string) ;; TODO: remove
|
|
|
|
|
2009-04-19 12:48:09 -04:00
|
|
|
(define (string.join strlist sep)
|
|
|
|
(if (null? strlist) ""
|
|
|
|
(let ((b (buffer)))
|
2019-08-09 10:18:36 -04:00
|
|
|
(io.write b (car strlist))
|
|
|
|
(for-each (lambda (s) (begin (io.write b sep)
|
|
|
|
(io.write b s)))
|
|
|
|
(cdr strlist))
|
|
|
|
(io.tostring! b))))
|
2009-04-19 12:48:09 -04:00
|
|
|
|
2019-08-09 10:18:36 -04:00
|
|
|
; toplevel -------------------------------------------------------------------
|
2009-03-28 17:39:04 -04:00
|
|
|
|
|
|
|
(define (macrocall? e) (and (symbol? (car e))
|
2019-08-09 10:18:36 -04:00
|
|
|
(symbol-syntax (car e))))
|
2009-03-28 17:39:04 -04:00
|
|
|
|
|
|
|
(define (macroexpand-1 e)
|
|
|
|
(if (atom? e) e
|
|
|
|
(let ((f (macrocall? e)))
|
2019-08-09 10:18:36 -04:00
|
|
|
(if f (apply f (cdr e))
|
|
|
|
e))))
|
2009-03-28 17:39:04 -04:00
|
|
|
|
2009-08-12 00:56:32 -04:00
|
|
|
(define (expand e)
|
|
|
|
; symbol resolves to toplevel; i.e. has no shadowing definition
|
|
|
|
(define (top? s env) (not (or (bound? s) (assq s env))))
|
2019-08-09 10:18:36 -04:00
|
|
|
|
2009-08-12 00:56:32 -04:00
|
|
|
(define (splice-begin body)
|
|
|
|
(cond ((atom? body) body)
|
2019-08-09 10:18:36 -04:00
|
|
|
((equal? body '((begin)))
|
|
|
|
body)
|
|
|
|
((and (pair? (car body))
|
|
|
|
(eq? (caar body) 'begin))
|
|
|
|
(append (splice-begin (cdar body)) (splice-begin (cdr body))))
|
|
|
|
(else
|
|
|
|
(cons (car body) (splice-begin (cdr body))))))
|
|
|
|
|
2009-08-12 00:56:32 -04:00
|
|
|
(define *expanded* (list '*expanded*))
|
2019-08-09 10:18:36 -04:00
|
|
|
|
2009-08-12 00:56:32 -04:00
|
|
|
(define (expand-body body env)
|
|
|
|
(if (atom? body) body
|
2019-08-09 10:18:36 -04:00
|
|
|
(let* ((body (if (top? 'begin env)
|
|
|
|
(splice-begin body)
|
|
|
|
body))
|
|
|
|
(def? (top? 'define env))
|
|
|
|
(dvars (if def? (get-defined-vars body) ()))
|
|
|
|
(env (nconc (map list dvars) env)))
|
|
|
|
(if (not def?)
|
|
|
|
(map (lambda (x) (expand-in x env)) body)
|
|
|
|
(let* ((ex-nondefs ; expand non-definitions
|
|
|
|
(let loop ((body body))
|
|
|
|
(cond ((atom? body) body)
|
|
|
|
((and (pair? (car body))
|
|
|
|
(eq? 'define (caar body)))
|
|
|
|
(cons (car body) (loop (cdr body))))
|
|
|
|
(else
|
|
|
|
(let ((form (expand-in (car body) env)))
|
|
|
|
(set! env (nconc
|
|
|
|
(map list (get-defined-vars form))
|
|
|
|
env))
|
|
|
|
(cons
|
|
|
|
(cons *expanded* form)
|
|
|
|
(loop (cdr body))))))))
|
|
|
|
(body ex-nondefs))
|
|
|
|
(while (pair? body) ; now expand deferred definitions
|
|
|
|
(if (not (eq? *expanded* (caar body)))
|
|
|
|
(set-car! body (expand-in (car body) env))
|
|
|
|
(set-car! body (cdar body)))
|
|
|
|
(set! body (cdr body)))
|
|
|
|
ex-nondefs)))))
|
|
|
|
|
2009-08-12 00:56:32 -04:00
|
|
|
(define (expand-lambda-list l env)
|
2009-08-13 00:09:35 -04:00
|
|
|
(if (atom? l) l
|
2019-08-09 10:18:36 -04:00
|
|
|
(cons (if (and (pair? (car l)) (pair? (cdr (car l))))
|
|
|
|
(list (caar l) (expand-in (cadar l) env))
|
|
|
|
(car l))
|
|
|
|
(expand-lambda-list (cdr l) env))))
|
|
|
|
|
2009-08-12 00:56:32 -04:00
|
|
|
(define (l-vars l)
|
2009-08-13 00:09:35 -04:00
|
|
|
(cond ((atom? l) (list l))
|
2019-08-09 10:18:36 -04:00
|
|
|
((pair? (car l)) (cons (caar l) (l-vars (cdr l))))
|
|
|
|
(else (cons (car l) (l-vars (cdr l))))))
|
|
|
|
|
2009-08-12 00:56:32 -04:00
|
|
|
(define (expand-lambda e env)
|
|
|
|
(let ((formals (cadr e))
|
2019-08-09 10:18:36 -04:00
|
|
|
(name (lastcdr e))
|
|
|
|
(body (cddr e))
|
|
|
|
(vars (l-vars (cadr e))))
|
2009-08-28 20:54:51 -04:00
|
|
|
(let ((env (nconc (map list vars) env)))
|
2019-08-09 10:18:36 -04:00
|
|
|
`(lambda ,(expand-lambda-list formals env)
|
|
|
|
,.(expand-body body env)
|
|
|
|
. ,name))))
|
|
|
|
|
2009-08-12 00:56:32 -04:00
|
|
|
(define (expand-define e env)
|
|
|
|
(if (or (null? (cdr e)) (atom? (cadr e)))
|
2019-08-09 10:18:36 -04:00
|
|
|
(if (null? (cddr e))
|
|
|
|
e
|
|
|
|
`(define ,(cadr e) ,(expand-in (caddr e) env)))
|
|
|
|
(let ((formals (cdadr e))
|
|
|
|
(name (caadr e))
|
|
|
|
(body (cddr e))
|
|
|
|
(vars (l-vars (cdadr e))))
|
|
|
|
(let ((env (nconc (map list vars) env)))
|
|
|
|
`(define ,(cons name (expand-lambda-list formals env))
|
|
|
|
,.(expand-body body env))))))
|
|
|
|
|
2009-08-12 00:56:32 -04:00
|
|
|
(define (expand-let-syntax e env)
|
|
|
|
(let ((binds (cadr e)))
|
|
|
|
(cons 'begin
|
2019-08-09 10:18:36 -04:00
|
|
|
(expand-body (cddr e)
|
|
|
|
(nconc
|
|
|
|
(map (lambda (bind)
|
|
|
|
(list (car bind)
|
|
|
|
((compile-thunk
|
|
|
|
(expand-in (cadr bind) env)))
|
|
|
|
env))
|
|
|
|
binds)
|
|
|
|
env)))))
|
|
|
|
|
2009-08-12 00:56:32 -04:00
|
|
|
; given let-syntax definition environment (menv) and environment
|
|
|
|
; at the point of the macro use (lenv), return the environment to
|
|
|
|
; expand the macro use in. TODO
|
|
|
|
(define (local-expansion-env menv lenv) menv)
|
2019-08-09 10:18:36 -04:00
|
|
|
|
2009-08-12 00:56:32 -04:00
|
|
|
(define (expand-in e env)
|
|
|
|
(if (atom? e) e
|
2019-08-09 10:18:36 -04:00
|
|
|
(let* ((head (car e))
|
|
|
|
(bnd (assq head env))
|
|
|
|
(default (lambda ()
|
|
|
|
(let loop ((e e))
|
|
|
|
(if (atom? e) e
|
|
|
|
(cons (if (atom? (car e))
|
|
|
|
(car e)
|
|
|
|
(expand-in (car e) env))
|
|
|
|
(loop (cdr e))))))))
|
|
|
|
(cond ((and bnd (pair? (cdr bnd))) ; local macro
|
|
|
|
(expand-in (apply (cadr bnd) (cdr e))
|
|
|
|
(local-expansion-env (caddr bnd) env)))
|
|
|
|
((or bnd ; bound lexical or toplevel var
|
|
|
|
(not (symbol? head))
|
|
|
|
(bound? head))
|
|
|
|
(default))
|
|
|
|
((macrocall? e) => (lambda (f)
|
|
|
|
(expand-in (apply f (cdr e)) env)))
|
|
|
|
((eq? head 'quote) e)
|
|
|
|
((eq? head 'lambda) (expand-lambda e env))
|
|
|
|
((eq? head 'define) (expand-define e env))
|
|
|
|
((eq? head 'let-syntax) (expand-let-syntax e env))
|
|
|
|
(else (default))))))
|
2009-08-12 00:56:32 -04:00
|
|
|
(expand-in e ()))
|
2009-03-28 17:39:04 -04:00
|
|
|
|
2009-04-20 20:56:05 -04:00
|
|
|
(define (eval x) ((compile-thunk (expand x))))
|
|
|
|
|
|
|
|
(define (load-process x) (eval x))
|
2009-04-14 20:12:01 -04:00
|
|
|
|
2009-03-28 17:39:04 -04:00
|
|
|
(define (load filename)
|
|
|
|
(let ((F (file filename :read)))
|
|
|
|
(trycatch
|
|
|
|
(let next (prev E v)
|
|
|
|
(if (not (io.eof? F))
|
2019-08-09 10:18:36 -04:00
|
|
|
(next (read F)
|
2009-03-28 17:39:04 -04:00
|
|
|
prev
|
2019-08-09 10:18:36 -04:00
|
|
|
(load-process E))
|
|
|
|
(begin (io.close F)
|
|
|
|
; evaluate last form in almost-tail position
|
|
|
|
(load-process E))))
|
2009-03-28 17:39:04 -04:00
|
|
|
(lambda (e)
|
|
|
|
(begin
|
2019-08-09 10:18:36 -04:00
|
|
|
(io.close F)
|
|
|
|
(raise `(load-error ,filename ,e)))))))
|
2009-03-28 17:39:04 -04:00
|
|
|
|
2019-08-28 06:10:37 -04:00
|
|
|
(define (sgr . ns)
|
|
|
|
(let ((out (open-output-string)))
|
|
|
|
(display "\x1b[" out)
|
|
|
|
(unless (null? ns)
|
|
|
|
(let loop ((ns ns))
|
|
|
|
(display (car ns) out)
|
|
|
|
(let ((ns (cdr ns)))
|
|
|
|
(unless (null? ns)
|
|
|
|
(display #\; out)
|
|
|
|
(loop ns)))))
|
|
|
|
(display "m" out)
|
|
|
|
(get-output-string out)))
|
|
|
|
|
|
|
|
(define bold 1)
|
|
|
|
|
|
|
|
(define (fg n) (+ 30 n))
|
|
|
|
(define (bg n) (+ 40 n))
|
|
|
|
|
|
|
|
(define black 0)
|
|
|
|
(define red 1)
|
|
|
|
(define green 2)
|
|
|
|
(define yellow 3)
|
|
|
|
(define blue 4)
|
|
|
|
(define magenta 5)
|
|
|
|
(define cyan 6)
|
|
|
|
(define white 7)
|
|
|
|
(define default-color 8)
|
|
|
|
|
2019-08-28 07:22:08 -04:00
|
|
|
(define history '())
|
|
|
|
|
|
|
|
(define (history-push form values)
|
|
|
|
(set! history (cons (cons form values) history)))
|
|
|
|
|
|
|
|
(define (history-get x proc)
|
|
|
|
(cond ((pair? x) (proc x))
|
|
|
|
((eqv? #f x) (history-get 0 proc))
|
|
|
|
((and (integer? x) (>= x 0) (< x (length history)))
|
|
|
|
(history-get (list-ref history x) proc))
|
|
|
|
(else #f)))
|
|
|
|
|
|
|
|
(define (history-form (x #f))
|
|
|
|
(history-get x car))
|
|
|
|
|
|
|
|
(define (history-value (x #f))
|
|
|
|
(history-get x cadr))
|
|
|
|
|
|
|
|
(define (history-values (x #f))
|
|
|
|
(history-get x cdr))
|
|
|
|
|
|
|
|
(define (history-exception (x #f))
|
|
|
|
#f)
|
|
|
|
|
2009-02-18 22:31:40 -05:00
|
|
|
(define (repl)
|
|
|
|
(define (prompt)
|
2019-08-28 06:12:43 -04:00
|
|
|
(display (string-append (sgr bold (fg cyan)) "up>" (sgr) " "))
|
2019-08-25 15:07:38 -04:00
|
|
|
(io.flush *output-stream*)
|
2019-08-28 07:22:08 -04:00
|
|
|
(let ((form (trycatch (read)
|
|
|
|
(lambda (e)
|
|
|
|
(io.discardbuffer *input-stream*)
|
|
|
|
(raise e)))))
|
2009-02-18 22:31:40 -05:00
|
|
|
(and (not (io.eof? *input-stream*))
|
2019-08-28 07:22:08 -04:00
|
|
|
(let ((value #f) (exception #f))
|
|
|
|
(trycatch (begin (set! value (load-process form))
|
|
|
|
(writeln value)
|
|
|
|
(set! that value))
|
|
|
|
(lambda (e)
|
|
|
|
(set! exception e)
|
|
|
|
(top-level-exception-handler e)))
|
|
|
|
(history-push form (list value))
|
|
|
|
(when (or (eqv? 'help form) (eqv? 'exit form))
|
|
|
|
(newline)
|
|
|
|
(displayln "Type (help) for help or (exit) to exit."))
|
|
|
|
#t))))
|
2009-02-18 22:31:40 -05:00
|
|
|
(define (reploop)
|
2019-08-28 07:22:08 -04:00
|
|
|
(when (trycatch (prompt) (lambda (e) (top-level-exception-handler e) #t))
|
2019-08-28 07:07:50 -04:00
|
|
|
(reploop)))
|
2009-02-18 22:31:40 -05:00
|
|
|
(reploop)
|
2019-08-25 15:57:31 -04:00
|
|
|
(newline))
|
2009-02-18 22:31:40 -05:00
|
|
|
|
2009-08-07 20:29:55 -04:00
|
|
|
(define (top-level-exception-handler e)
|
|
|
|
(with-output-to *stderr*
|
2019-08-09 10:18:36 -04:00
|
|
|
(print-exception e)
|
|
|
|
(print-stack-trace (stacktrace))))
|
2009-08-07 20:29:55 -04:00
|
|
|
|
2009-07-05 23:56:48 -04:00
|
|
|
(define (print-stack-trace st)
|
2009-07-10 00:17:39 -04:00
|
|
|
(define (find-in-f f tgt path)
|
|
|
|
(let ((path (cons (function:name f) path)))
|
|
|
|
(if (eq? (function:code f) (function:code tgt))
|
2019-08-09 10:18:36 -04:00
|
|
|
(throw 'ffound path)
|
|
|
|
(let ((v (function:vals f)))
|
|
|
|
(for 0 (1- (length v))
|
|
|
|
(lambda (i) (if (closure? (aref v i))
|
|
|
|
(find-in-f (aref v i) tgt path))))))))
|
2009-07-05 23:56:48 -04:00
|
|
|
(define (fn-name f e)
|
2009-07-10 00:17:39 -04:00
|
|
|
(let ((p (catch 'ffound
|
2019-08-09 10:18:36 -04:00
|
|
|
(begin
|
|
|
|
(for-each (lambda (topfun)
|
|
|
|
(find-in-f topfun f ()))
|
|
|
|
e)
|
|
|
|
#f))))
|
2009-07-10 00:17:39 -04:00
|
|
|
(if p
|
2019-08-09 10:18:36 -04:00
|
|
|
(symbol (string.join (map string (reverse! p)) "/"))
|
|
|
|
'lambda)))
|
2009-08-09 14:04:03 -04:00
|
|
|
(let ((st (reverse! (list-tail st (if *interactive* 5 4))))
|
2019-08-09 10:18:36 -04:00
|
|
|
(e (filter closure? (map (lambda (s) (and (bound? s)
|
|
|
|
(top-level-value s)))
|
|
|
|
(environment))))
|
|
|
|
(n 0))
|
2009-07-05 23:56:48 -04:00
|
|
|
(for-each
|
|
|
|
(lambda (f)
|
2019-08-25 15:57:31 -04:00
|
|
|
(display "#")
|
|
|
|
(display n)
|
|
|
|
(display " ")
|
|
|
|
(writeln (cons (fn-name (aref f 0) e)
|
|
|
|
(cdr (vector->list f))))
|
2009-07-05 23:56:48 -04:00
|
|
|
(set! n (+ n 1)))
|
|
|
|
st)))
|
|
|
|
|
2009-02-18 22:31:40 -05:00
|
|
|
(define (print-exception e)
|
|
|
|
(cond ((and (pair? e)
|
2019-08-09 10:18:36 -04:00
|
|
|
(eq? (car e) 'type-error)
|
|
|
|
(length= e 4))
|
2019-08-25 15:57:31 -04:00
|
|
|
(display "type error: ")
|
|
|
|
(display (cadr e))
|
|
|
|
(display ": expected ")
|
|
|
|
(display (caddr e))
|
|
|
|
(display ", got ")
|
|
|
|
(write (cadddr e)))
|
2019-08-28 11:12:41 -04:00
|
|
|
|
2019-08-09 10:18:36 -04:00
|
|
|
((and (pair? e)
|
|
|
|
(eq? (car e) 'bounds-error)
|
|
|
|
(length= e 4))
|
2019-08-25 15:57:31 -04:00
|
|
|
(display (cadr e))
|
|
|
|
(display ": index ")
|
|
|
|
(display (cadddr e))
|
|
|
|
(display " out of bounds for ")
|
|
|
|
(write (caddr e)))
|
2019-08-09 10:18:36 -04:00
|
|
|
|
|
|
|
((and (pair? e)
|
|
|
|
(eq? (car e) 'unbound-error)
|
|
|
|
(pair? (cdr e)))
|
2019-08-25 15:57:31 -04:00
|
|
|
(display "eval: variable ")
|
|
|
|
(display (cadr e))
|
|
|
|
(display " has no value"))
|
2019-08-09 10:18:36 -04:00
|
|
|
|
|
|
|
((and (pair? e)
|
|
|
|
(eq? (car e) 'error))
|
2019-08-25 15:57:31 -04:00
|
|
|
(display "error: ")
|
|
|
|
(for-each display (cdr e)))
|
2019-08-09 10:18:36 -04:00
|
|
|
|
|
|
|
((and (pair? e)
|
|
|
|
(eq? (car e) 'load-error))
|
|
|
|
(print-exception (caddr e))
|
2019-08-25 15:57:31 -04:00
|
|
|
(display "in file ")
|
|
|
|
(display (cadr e)))
|
2019-08-09 10:18:36 -04:00
|
|
|
|
|
|
|
((and (list? e)
|
|
|
|
(length= e 2))
|
2019-08-25 15:57:31 -04:00
|
|
|
(write (car e))
|
|
|
|
(display ": ")
|
2019-08-09 10:18:36 -04:00
|
|
|
(let ((msg (cadr e)))
|
|
|
|
((if (or (string? msg) (symbol? msg))
|
2019-08-25 15:57:31 -04:00
|
|
|
display write)
|
2019-08-09 10:18:36 -04:00
|
|
|
msg)))
|
|
|
|
|
2019-08-25 15:57:31 -04:00
|
|
|
(else (display "*** Unhandled exception: ")
|
|
|
|
(write e)))
|
2009-02-18 22:31:40 -05:00
|
|
|
|
2019-08-25 15:57:31 -04:00
|
|
|
(newline))
|
2009-02-18 22:31:40 -05:00
|
|
|
|
2009-05-07 22:52:25 -04:00
|
|
|
(define (simple-sort l)
|
|
|
|
(if (or (null? l) (null? (cdr l))) l
|
2010-05-03 01:07:22 -04:00
|
|
|
(let ((piv (car l)))
|
2019-08-09 10:18:36 -04:00
|
|
|
(receive (less grtr)
|
|
|
|
(separate (lambda (x) (< x piv)) (cdr l))
|
|
|
|
(nconc (simple-sort less)
|
|
|
|
(list piv)
|
|
|
|
(simple-sort grtr))))))
|
2009-05-07 22:52:25 -04:00
|
|
|
|
2019-08-28 06:01:02 -04:00
|
|
|
(define (sort l (less? <) (key identity))
|
2019-08-28 14:22:06 -04:00
|
|
|
(let sort ((l l))
|
|
|
|
(if (or (null? l) (null? (cdr l)))
|
|
|
|
l
|
|
|
|
(let ((piv (car l)))
|
|
|
|
(receive (less grtr)
|
|
|
|
(separate (lambda (x) (less? (key x) (key piv)))
|
|
|
|
(cdr l))
|
|
|
|
(nconc (sort less)
|
|
|
|
(list piv)
|
|
|
|
(sort grtr)))))))
|
2019-08-28 06:01:02 -04:00
|
|
|
|
|
|
|
(define (apropos-list key)
|
|
|
|
(let ((key (string-downcase
|
|
|
|
(cond ((string? key) key)
|
|
|
|
((symbol? key) (symbol->string key))
|
|
|
|
(else (error "Please give a string or symbol key"))))))
|
2019-08-28 11:13:14 -04:00
|
|
|
(if (= 0 (string-length key))
|
|
|
|
'()
|
|
|
|
(sort (filter (λ (sym)
|
|
|
|
(string.find
|
|
|
|
(string-downcase (symbol->string sym)) key))
|
|
|
|
(environment))
|
|
|
|
string<? symbol->string))))
|
2019-08-28 06:01:02 -04:00
|
|
|
|
|
|
|
(define (apropos . args)
|
2019-10-13 14:41:02 -04:00
|
|
|
(let ((syms (apply apropos-list args)))
|
|
|
|
(and (not (null? syms))
|
|
|
|
(for-each (λ (sym)
|
|
|
|
(displayln
|
|
|
|
(string-append
|
|
|
|
(let ((val (symbol-value sym)))
|
|
|
|
(if (procedure? val) "procedure" "variable "))
|
|
|
|
" "
|
|
|
|
(symbol->string sym))))
|
|
|
|
syms)
|
|
|
|
#t)))
|
2019-08-28 06:01:02 -04:00
|
|
|
|
2019-08-28 07:07:50 -04:00
|
|
|
(define-macro (help . args)
|
|
|
|
`(apply help* ',args))
|
|
|
|
|
|
|
|
(define (display-banner)
|
|
|
|
(displayln (string-append (sgr (fg cyan)) "Up Scheme" (sgr))))
|
|
|
|
|
2019-08-13 16:07:44 -04:00
|
|
|
(define (system-image->buffer)
|
|
|
|
(let ((out (buffer))
|
2019-08-09 10:18:36 -04:00
|
|
|
(excludes '(*linefeed* *directory-separator* *argv* that
|
|
|
|
*print-pretty* *print-width* *print-readably*
|
|
|
|
*print-level* *print-length* *os-name*)))
|
2009-08-13 00:09:35 -04:00
|
|
|
(with-bindings ((*print-pretty* #t)
|
2019-08-09 10:18:36 -04:00
|
|
|
(*print-readably* #t))
|
2009-08-07 20:29:55 -04:00
|
|
|
(let ((syms
|
2019-08-09 10:18:36 -04:00
|
|
|
(filter (lambda (s)
|
|
|
|
(and (bound? s)
|
|
|
|
(not (constant? s))
|
|
|
|
(or (not (builtin? (top-level-value s)))
|
|
|
|
(not (equal? (string s) ; alias of builtin
|
|
|
|
(string (top-level-value s)))))
|
|
|
|
(not (memq s excludes))
|
|
|
|
(not (iostream? (top-level-value s)))))
|
|
|
|
(simple-sort (environment)))))
|
2019-08-13 16:07:44 -04:00
|
|
|
(write (apply nconc (map list syms (map top-level-value syms)))
|
|
|
|
out)
|
|
|
|
(io.seek out 0)
|
|
|
|
out))))
|
2009-04-20 20:56:05 -04:00
|
|
|
|
|
|
|
; initialize globals that need to be set at load time
|
|
|
|
(define (__init_globals)
|
2019-08-09 17:35:16 -04:00
|
|
|
(if (eq? *os-name* 'windows)
|
2009-04-20 20:56:05 -04:00
|
|
|
(begin (set! *directory-separator* "\\")
|
2019-08-09 10:18:36 -04:00
|
|
|
(set! *linefeed* "\r\n"))
|
2009-04-20 20:56:05 -04:00
|
|
|
(begin (set! *directory-separator* "/")
|
2019-08-09 10:18:36 -04:00
|
|
|
(set! *linefeed* "\n")))
|
2009-04-20 20:56:05 -04:00
|
|
|
(set! *output-stream* *stdout*)
|
2009-05-05 19:51:13 -04:00
|
|
|
(set! *input-stream* *stdin*)
|
|
|
|
(set! *error-stream* *stderr*))
|
2009-04-20 20:56:05 -04:00
|
|
|
|
2009-02-18 22:31:40 -05:00
|
|
|
(define (__script fname)
|
|
|
|
(trycatch (load fname)
|
2019-08-09 10:18:36 -04:00
|
|
|
(lambda (e) (begin (top-level-exception-handler e)
|
|
|
|
(exit 1)))))
|
2009-02-18 22:31:40 -05:00
|
|
|
|
2009-04-16 23:40:52 -04:00
|
|
|
(define (__start argv)
|
2009-04-20 20:56:05 -04:00
|
|
|
(__init_globals)
|
2009-02-18 22:31:40 -05:00
|
|
|
(if (pair? (cdr argv))
|
|
|
|
(begin (set! *argv* (cdr argv))
|
2019-08-09 10:18:36 -04:00
|
|
|
(set! *interactive* #f)
|
|
|
|
(__script (cadr argv)))
|
2009-02-18 22:31:40 -05:00
|
|
|
(begin (set! *argv* argv)
|
2019-08-09 10:18:36 -04:00
|
|
|
(set! *interactive* #t)
|
2019-08-28 07:07:50 -04:00
|
|
|
(display-banner)
|
2019-08-09 10:18:36 -04:00
|
|
|
(repl)))
|
2009-02-18 22:31:40 -05:00
|
|
|
(exit 0))
|