tisp

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

commit a9f86c70e9ff1f85d298af89886b16335e964e28
parent 606233e93e498cd6a0ca72c5684eb09ff3c2bdf2
Author: Ed van Bruggen <edvb@uw.edu>
Date:   Mon, 30 Sep 2019 00:24:15 -0700

Add time tib

Diffstat:
Makefile | 2+-
main.c | 2++
tibs/time.c | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 57 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile @@ -10,7 +10,7 @@ endif EXE = tisp SRC = tisp.c main.c -TIB = tibs/math.c tibs/io.c +TIB = tibs/math.c tibs/io.c tibs/time.c OBJ = $(SRC:.c=.o) $(TIB:.c=.o) LIB = tibs/libtibmath.so tibs/libtibio.so TSP = tibs/lib.tsp tibs/repl.tsp diff --git a/main.c b/main.c @@ -9,6 +9,7 @@ #ifndef TIB_DYNAMIC tsp_include_tib(math); tsp_include_tib(io); + tsp_include_tib(time); #endif int @@ -21,6 +22,7 @@ main(int argc, char *argv[]) #ifndef TIB_DYNAMIC tib_env_math(env); tib_env_io(env); + tib_env_time(env); #endif if (argc == 1) diff --git a/tibs/time.c b/tibs/time.c @@ -0,0 +1,54 @@ +/* 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 <time.h> + +#include "../tisp.h" + +/* return number of seconds since 1970 (unix time stamp) */ +static Val +prim_time(Env env, Val args) +{ + tsp_arg_num(args, "time", 0); + return mk_int(time(NULL)); +} + +/* return time taken to run command given */ +static Val +prim_timeit(Env env, Val args) +{ + Val v; + clock_t t; + tsp_arg_num(args, "timeit", 1); + t = clock(); + if (!(v = tisp_eval(env, car(args)))) + return NULL; + t = clock() - t; + return mk_dec(((double)t)/CLOCKS_PER_SEC); +} + +void +tib_env_time(Env env) +{ + tsp_env_fn(time); + tsp_env_fn(timeit); +}