tisp

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

commit af8587a599838cfcb0d6a3b69def48b72dc24d81
parent 5e44da88ee936974315c45d80bca75d68ea7535b
Author: Ed van Bruggen <edvb@uw.edu>
Date:   Mon, 23 Dec 2019 17:58:11 -0800

Allow write to append file

Diffstat:
tibs/io.c | 9+++++++--
tibs/lib.tsp | 3++-
tibs/repl.tsp | 4++--
3 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/tibs/io.c b/tibs/io.c @@ -26,15 +26,20 @@ #include "../tisp.h" /* write all arguemnts to given file, or stdout/stderr, without newline */ +/* first argument is file name, second is option to append file */ static Val prim_write(Tsp st, Hash env, Val args) { Val v; FILE *f; + const char *mode = "w"; tsp_arg_min(args, "write", 2); if (!(v = tisp_eval_list(st, env, args))) return NULL; + /* if second argument is true, append file don't write over */ + if (!nilp(cadr(v))) + mode = "a"; /* first argument can either be the symbol stdout or stderr, * or the file as a string */ if (car(v)->t == SYMBOL) @@ -42,13 +47,13 @@ prim_write(Tsp st, Hash env, Val args) else if (car(v)->t != STRING) tsp_warnf("write: expected file name as string, received %s", type_str(car(v)->t)); - else if (!(f = fopen(car(v)->v.s, "w"))) + else if (!(f = fopen(car(v)->v.s, mode))) tsp_warnf("write: could not load file '%s'", car(v)->v.s); if (f == stderr && strncmp(car(v)->v.s, "stderr", 7)) tsp_warn("write: expected file name as string, " "or symbol stdout/stderr"); - for (v = cdr(v); !nilp(v); v = cdr(v)) + for (v = cddr(v); !nilp(v); v = cdr(v)) if (car(v)->t & STRING) /* don't print quotes around string */ fprintf(f, "%s", car(v)->v.s); else diff --git a/tibs/lib.tsp b/tibs/lib.tsp @@ -253,9 +253,10 @@ (* n (! (- n 1))))) ;;; IO -(define (print . str) (apply write (cons 'stdout str))) +(define (print . str) (apply write (list* 'stdout nil str))) (define (newline) (print "\n")) (define (disp . str) (apply print str) (newline)) +; TODO rename displayln, also quote syms and lists (define (disp-string . str) (map (lambda (s) (if (string? s) diff --git a/tibs/repl.tsp b/tibs/repl.tsp @@ -10,6 +10,6 @@ ;; simple repl, only requires io c library (define (repl-simple) - (write 'stdout "> ") - (write 'stdout (eval (parse (read))) "\n") + (write 'stdout nil "> ") + (write 'stdout nil (eval (parse (read))) "\n") (repl-simple))