tisp

tiny lisp
git clone git://edryd.org/tisp
Log | Files | Refs | LICENSE

commit ff7d38834fdbe9dd5e2d09111c0c7e5dc191f1bb
parent 6dee97ce3fa8001cf6a4f47e73ced3382a300c9b
Author: Ed van Bruggen <edvb@uw.edu>
Date:   Sun,  5 May 2019 17:59:33 -0700

Add control flow macros when, unless, let

Map else to true for use in cond expression

Diffstat:
tibs/lib.tsp | 17+++++++++++++++--
tibs/repl.tsp | 16++++++----------
2 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/tibs/lib.tsp b/tibs/lib.tsp @@ -58,14 +58,27 @@ (cond ((= (type x) "pair") t) (t ()))) +;;; Control Flow +(define if + (macro (con a b) + (list 'cond (list con a) (list t b)))) +(define else t) +(define when + (macro (con . body) + (list 'cond (list con (cons 'do body)) (list t (void))))) +(define unless + (macro (con . body) + (list 'cond (list con (void)) (list t (cons 'do body))))) +(define let + (macro (vars body) + (cons (list 'lambda (map car vars) body) (map cadr vars)))) + (define (not x) (cond (x ()) (t t))) (define (list . rest) rest) (define (apply fn args) (eval (cons fn args))) -(define if (macro (condition true false) - (list 'cond (list condition true) (list else false)))) (define pi (* 4 (arctan 1.))) (define tau (* 2 pi)) diff --git a/tibs/repl.tsp b/tibs/repl.tsp @@ -1,14 +1,10 @@ (define (repl) (print "> ") - ((lambda (expr) - (if (= expr 'quit) - (void) - (do - ((lambda (ans) - (cond ((not (void? ans)) - (disp-string ans)))) - (eval expr)) - (repl)))) - (read))) + (let ((expr (read))) + (unless (= expr 'quit) + (let ((ans (eval expr))) + (unless (void? ans) + (disp-string ans))) + (repl)))) (repl)