tisp

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

commit e54d2fd87a003d4cab9a1929c92db8ab9c87a3f8
parent 6dc20d5e12a292e1d7679838475a92231872b5bd
Author: Ed van Bruggen <edvb@uw.edu>
Date:   Fri,  1 Feb 2019 19:58:05 -0800

Add io tib

Diffstat:
main.c | 2++
tibs/io.c | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
tibs/io.h | 1+
tibs/math.c | 20++++++++++----------
tisp.h | 3+++
5 files changed, 71 insertions(+), 10 deletions(-)

diff --git a/main.c b/main.c @@ -13,6 +13,7 @@ #include "tisp.h" #if TIB_STATIC # include "tibs/math.h" +# include "tibs/io.h" #endif char *argv0; @@ -83,6 +84,7 @@ main(int argc, char *argv[]) Env env = tisp_env_init(64); #if TIB_STATIC tib_env_math(env); + tib_env_io(env); #endif if (argc > 0) { diff --git a/tibs/io.c b/tibs/io.c @@ -0,0 +1,55 @@ +/* zlib License + * + * Copyright (c) 2017-2019 Ed van Bruggen + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + */ +#include <stdio.h> +#include <stdlib.h> + +#include "../tisp.h" + +static Val +prim_print(Env env, Val args) +{ + Val v; + if (!(v = tisp_eval_list(env, args))) + return NULL; + for (; !nilp(v); v = cdr(v)) { + if (car(v)->t & STRING) /* don't print quotes arond string */ + fprintf(stdout, "%s", car(v)->v.s); + else + tisp_print(stdout, car(v)); + if (!nilp(cdr(v))) + putchar(' '); + } + fflush(stdout); + return env->none; +} + +static Val +prim_newline(Env env, Val args) +{ + return mk_str(env, "\n"); +} + +void +tib_env_io(Env env) +{ + tsp_env_fn(print); + tsp_env_fn(newline); +} diff --git a/tibs/io.h b/tibs/io.h @@ -0,0 +1 @@ +void tib_env_io(Env env); diff --git a/tibs/math.c b/tibs/math.c @@ -162,14 +162,14 @@ tib_env_math(Env env) tisp_env_add(env, "pi", mk_dub(3.141592653589793)); tisp_env_add(env, "e", mk_dub(2.718281828459045)); - tisp_env_add(env, "+", mk_prim(prim_add)); - tisp_env_add(env, "-", mk_prim(prim_sub)); - tisp_env_add(env, "*", mk_prim(prim_mul)); - tisp_env_add(env, "/", mk_prim(prim_div)); - tisp_env_add(env, "mod", mk_prim(prim_mod)); - - tisp_env_add(env, "<", mk_prim(prim_lt)); - tisp_env_add(env, ">", mk_prim(prim_gt)); - tisp_env_add(env, "<=", mk_prim(prim_lte)); - tisp_env_add(env, ">=", mk_prim(prim_gte)); + tsp_env_name_fn(+, add); + tsp_env_name_fn(-, sub); + tsp_env_name_fn(*, mul); + tsp_env_name_fn(/, div); + tsp_env_fn(mod); + + tsp_env_name_fn(<, lt); + tsp_env_name_fn(>, gt); + tsp_env_name_fn(<=, lte); + tsp_env_name_fn(>=, gte); } diff --git a/tisp.h b/tisp.h @@ -39,6 +39,9 @@ tsp_warnf(NAME ": expected %s, received %s", type_str(TYPE), type_str(ARG->t)); \ } while(0) +#define tsp_env_name_fn(NAME, FN) tisp_env_add(env, #NAME, mk_prim(prim_##FN)) +#define tsp_env_fn(NAME) tsp_env_name_fn(NAME, NAME) + #define car(P) ((P)->v.p.car) #define cdr(P) ((P)->v.p.cdr) #define nilp(P) ((P)->t == NIL)