tisp

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

commit 0f6b058217edda69fa80755511f40a8c453dd7a5
parent 0b6ecc1987f1664e29fd2f48a5d99ad8c08f4d0a
Author: Ed van Bruggen <edvb@uw.edu>
Date:   Mon, 14 Jan 2019 12:47:38 -0800

Remove util.c

Diffstat:
Makefile | 4++--
main.c | 27++++++++++++++++++++++++---
test.c | 1-
tisp.c | 42+++++++++++++++++++++---------------------
util.c | 75---------------------------------------------------------------------------
util.h | 14--------------
6 files changed, 47 insertions(+), 116 deletions(-)

diff --git a/Makefile b/Makefile @@ -4,7 +4,7 @@ include config.mk EXE = tisp -SRC = tisp.c main.c util.c extern/linenoise.c +SRC = tisp.c main.c extern/linenoise.c TIB = tib/math.c OBJ = $(SRC:.c=.o) $(TIB:.c=.o) LIB = tib/libtibmath.so @@ -65,7 +65,7 @@ uninstall: test: $(OBJ) $(LIB) test.o @echo running tests @echo $(CC) -o test - @$(CC) -o test tisp.o tib/math.o util.o test.o $(LDFLAGS) + @$(CC) -o test tisp.o tib/math.o test.o $(LDFLAGS) @./test man: diff --git a/main.c b/main.c @@ -1,12 +1,13 @@ /* See LICENSE file for copyright and license details. */ #include <libgen.h> +#include <stdarg.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> #include "extern/arg.h" #include "extern/linenoise.h" -#include "util.h" #include "config.h" #include "tisp.h" @@ -16,6 +17,26 @@ char *argv0; +void +die(int eval, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + if (fmt[0] && fmt[strlen(fmt)-1] == ':') { + fputc(' ', stderr); + perror(NULL); + } else { + fputc('\n', stderr); + } + + if (eval > -1) + exit(eval); +} + static Val read_val(Env env, Str cmd) { @@ -32,7 +53,7 @@ read_val(Env env, Str cmd) strf = str.d; ret = tisp_read(env, &str); - efree(strf); + free(strf); return ret; } @@ -68,7 +89,7 @@ main(int argc, char *argv[]) if (!(fp = fopen(*argv, "r"))) die(1, "%s: %s:", argv[0], *argv); while ((nread = fread(buf, 1, sizeof(buf), fp)) > 0) ; - str.d = estrdup(buf); + str.d = strdup(buf); } while ((v = read_val(env, &str))) { diff --git a/test.c b/test.c @@ -5,7 +5,6 @@ #include <string.h> #include <unistd.h> -#include "util.h" #include "tisp.h" #include "tib/math.h" diff --git a/tisp.c b/tisp.c @@ -36,27 +36,6 @@ static void hash_add(Hash ht, char *key, Val val); static Hash hash_extend(Hash ht, Val args, Val vals); static void hash_merge(Hash ht, Hash ht2); -char * -type_str(Type t) -{ - switch (t) { - case NONE: return "none"; - case NIL: return "nil"; - case INTEGER: return "integer"; - case DOUBLE: return "double"; - case RATIO: return "ratio"; - case STRING: return "string"; - case SYMBOL: return "symbol"; - case PRIMITIVE: return "primitive"; - case FUNCTION: return "function"; - case PAIR: return "pair"; - default: - if (t & NUMBER) - return "number"; - return "invalid"; - } -} - static void die(const char *fmt, ...) { @@ -102,6 +81,27 @@ erealloc(void *p, size_t size) return p; } +char * +type_str(Type t) +{ + switch (t) { + case NONE: return "void"; + case NIL: return "nil"; + case INTEGER: return "integer"; + case DOUBLE: return "double"; + case RATIO: return "ratio"; + case STRING: return "string"; + case SYMBOL: return "symbol"; + case PRIMITIVE: return "primitive"; + case FUNCTION: return "function"; + case PAIR: return "pair"; + default: + if (t & NUMBER) + return "number"; + return "invalid"; + } +} + static int issym(char c) { diff --git a/util.c b/util.c @@ -1,75 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include <assert.h> -#include <stdarg.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include "util.h" - -void * -ecalloc(size_t nmemb, size_t size) -{ - void *p; - - if (!(p = calloc(nmemb, size))) - die(1, "calloc:"); - - return p; -} - -void * -emalloc(size_t size) -{ - void *p; - - if (!(p = malloc(size))) - die(1, "malloc:"); - - return p; -} - -void * -erealloc(void *p, size_t size) -{ - if (!(p = realloc(p, size))) - die(1, "realloc:"); - - return p; -} - -char * -estrdup(char *s) -{ - if (!(s = strdup(s))) - die(1, "strdup:"); - - return s; -} - -void -efree(void *p) -{ - if (p) - free(p); -} - -void -die(int eval, const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - vfprintf(stderr, fmt, ap); - va_end(ap); - - if (fmt[0] && fmt[strlen(fmt)-1] == ':') { - fputc(' ', stderr); - perror(NULL); - } else { - fputc('\n', stderr); - } - - if (eval > -1) - exit(eval); -} diff --git a/util.h b/util.h @@ -1,14 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#define MAX(A, B) ((A) > (B) ? (A) : (B)) -#define MIN(A, B) ((A) < (B) ? (A) : (B)) -#define BETWEEN(X, A, B) ((A) <= (X) && (X) <= (B)) -#define LEN(X) (sizeof(X) / sizeof((X)[0])) - -void *ecalloc(size_t nmemb, size_t size); -void *emalloc(size_t size); -void *erealloc(void *p, size_t size); -char *estrdup(char *s); -void efree(void *p); - -void die(int eval, const char *fmt, ...);