commit b55dbd871e67038fad93fdc1252044d8a5e031a8
parent f07afd43ddde10cb2194feba67dc5fe8eb967baf
Author: Ed van Bruggen <edvb@uw.edu>
Date: Tue, 12 Feb 2019 23:24:32 -0800
Add tisp_parse_file to read all lines in a file
Diffstat:
2 files changed, 17 insertions(+), 1 deletion(-)
diff --git a/tisp.c b/tisp.c
@@ -151,7 +151,7 @@ list_len(Val v)
static Val
list_last(Val v)
{
- while (!(cdr(v)->t & NIL))
+ while (!nilp(cdr(v)))
v = cdr(v);
return car(v);
}
@@ -573,6 +573,21 @@ tisp_read_file(char *fname)
}
Val
+tisp_parse_file(Env env, char *fname)
+{
+ struct Str str = { NULL };
+ Val ret = mk_pair(mk_sym(env, "begin"), env->nil);
+ Val v, last = ret;
+ char *file;
+ if (!(file = tisp_read_file(fname)))
+ return ret;
+ for (str.d = file; *str.d && (v = tisp_read(env, &str)); last = cdr(last))
+ cdr(last) = mk_pair(v, env->nil);
+ free(file);
+ return ret;
+}
+
+Val
tisp_eval_list(Env env, Val v)
{
int cap = 1, size = 0;
diff --git a/tisp.h b/tisp.h
@@ -142,6 +142,7 @@ Val tisp_eval(Env env, Val v);
void tisp_print(FILE *f, Val v);
char *tisp_read_file(char *fname);
+Val tisp_parse_file(Env env, char *fname);
void tisp_env_add(Env e, char *key, Val v);
Env tisp_env_init(size_t cap);