From d0c17ee2cc95348f3f24cca4610f27e7c1086fe9 Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Wed, 20 Apr 2011 13:39:35 -0700 Subject: [PATCH] replace terrible err/die/oom functions with nice ERR/DIE/OOM macros --- env.c | 4 +--- eval.c | 30 +++++++++--------------------- lake.c | 18 +----------------- lake.h | 9 ++++++--- list.c | 17 +++++++++-------- parse.c | 15 +++++---------- 6 files changed, 31 insertions(+), 62 deletions(-) diff --git a/env.c b/env.c index 215476c..ffcb2c9 100644 --- a/env.c +++ b/env.c @@ -74,9 +74,7 @@ LakeVal *env_get(Env *env, LakeSym *key) val = env_get(env->parent, key); } if (!val) { - char *msg = g_strdup_printf("undefined variable: %s", SYM_S(key)); - err(msg); - g_free(msg); + ERR("undefined variable: %s", SYM_S(key)); val = NULL; } return val; diff --git a/eval.c b/eval.c index 27dd77a..d21558a 100644 --- a/eval.c +++ b/eval.c @@ -22,9 +22,7 @@ static void init_special_form_handlers(void); static void invalid_special_form(LakeList *expr, char *detail) { - char *msg = g_strdup_printf("unrecognized special form, %s: %s", detail, repr(VAL(expr))); - err(msg); - g_free(msg); + ERR("unrecognized special form, %s: %s", detail, repr(VAL(expr))); } /* expr begins with the symbol "quote" so the quoted value is the 2nd value */ @@ -71,9 +69,7 @@ static LakeVal *setB_special_form(Env *env, LakeList *expr) LakeSym *var = SYM(list_shift(expr)); LakeVal *form = list_shift(expr); if (!env_set(env, var, form)) { - char *msg = g_strdup_printf("%s is not defined", SYM_S(var)); - err(msg); - g_free(msg); + ERR("%s is not defined", SYM_S(var)); } } else { @@ -198,9 +194,7 @@ static LakeVal *eval_special_form(Env *env, LakeList *expr) if (handler) { return handler(env, list_copy(expr)); } - char *msg = g_strdup_printf("unrecognized special form: %s", SYM_S(name)); - err(msg); - g_free(msg); + ERR("unrecognized special form: %s", SYM_S(name)); return NULL; } @@ -223,7 +217,7 @@ LakeVal *eval(Env *env, LakeVal *expr) break; case TYPE_DLIST: - err("malformed function call"); + ERR("malformed function call"); result = NULL; break; @@ -262,8 +256,8 @@ LakeVal *eval(Env *env, LakeVal *expr) break; default: - printf("error: unrecognized value, type %d, size %zu bytes", expr->type, expr->size); - die("we don't eval that around here!"); + ERR("unrecognized value, type %d, size %zu bytes", expr->type, expr->size); + DIE("we don't eval that around here!"); } done: return result; @@ -289,9 +283,7 @@ LakeVal *apply(LakeVal *fnVal, LakeList *args) result = prim->fn(args); } else { - char *msg = g_strdup_printf("%s expects %d params but got %zu", prim->name, arity, LIST_N(args)); - err(msg); - g_free(msg); + ERR("%s expects %d params but got %zu", prim->name, arity, LIST_N(args)); result = NULL; } } @@ -301,9 +293,7 @@ LakeVal *apply(LakeVal *fnVal, LakeList *args) /* Check # of params */ size_t nparams = LIST_N(fn->params); if (nparams != LIST_N(args) && !fn->varargs) { - char *msg = g_strdup_printf("expected %zu params but got %zu", nparams, LIST_N(args)); - err(msg); - g_free(msg); + ERR("expected %zu params but got %zu", nparams, LIST_N(args)); return NULL; } @@ -330,9 +320,7 @@ LakeVal *apply(LakeVal *fnVal, LakeList *args) list_free(results); } else { - char *msg = g_strdup_printf("not a function: %s", repr(fnVal)); - err(msg); - g_free(msg); + ERR("not a function: %s", repr(fnVal)); } return result; } diff --git a/lake.c b/lake.c index 087c9e6..2f0746f 100644 --- a/lake.c +++ b/lake.c @@ -27,22 +27,6 @@ static LakeBool _F = { { TYPE_BOOL, sizeof(LakeBool) }, FALSE }; LakeBool *T = &_T; LakeBool *F = &_F; -void die(char *msg) -{ - err(msg); - exit(1); -} - -void err(char *msg) -{ - printf("error: %s\n", msg); -} - -void oom(void) -{ - die("out of memory"); -} - static LakeVal *prim_nullP(LakeList *args) { LakeVal *val = list_shift(args); @@ -108,7 +92,7 @@ static void run_repl_with_env(Env *env) expr = prompt_read("> "); if (expr == VAL(EOF)) break; if (expr == VAL(PARSE_ERR)) { - err("parse error"); + ERR("parse error"); continue; } if (expr) { diff --git a/lake.h b/lake.h index 0247de5..6622dcd 100644 --- a/lake.h +++ b/lake.h @@ -133,11 +133,14 @@ struct lake_fn { }; typedef struct lake_fn LakeFn; -void err(char *msg); -void die(char *msg); -void oom(); char *repr(LakeVal *val); +#include + +#define ERR(...) printf("error: "); printf(__VA_ARGS__); putchar('\n') +#define DIE(...) ERR(__VA_ARGS__); exit(1) +#define OOM() DIE("out of memory") + #include "sym.h" #include "bool.h" #include "int.h" diff --git a/list.c b/list.c index 53dfbdd..e061cf5 100644 --- a/list.c +++ b/list.c @@ -9,6 +9,7 @@ #include #include +#include #include #include "int.h" #include "lake.h" @@ -77,21 +78,21 @@ static void list_grow(LakeList *list) LakeVal *list_set(LakeList *list, size_t i, LakeVal *val) { - if (i < 0 || i >= list->n) { - err("list_set: index is out of bounds"); - return NULL; + if (i >= 0 && i < list->n) { + list->vals[i] = val; + } else { + ERR("list_set: index %zu is out of bounds (%zu)", i, list->n); } - list->vals[i] = val; return NULL; } LakeVal *list_get(LakeList *list, LakeInt *li) { - int i = int_val(li); - if (i < 0 || i >= list->n) { - return NULL; + int i = INT_VAL(li); + if (i >= 0 && i < list->n) { + return list->vals[i]; } - return list->vals[i]; + return NULL; } LakeVal *list_append(LakeList *list, LakeVal *val) diff --git a/parse.c b/parse.c index 3167f45..b9e80ff 100644 --- a/parse.c +++ b/parse.c @@ -48,7 +48,7 @@ static char peek(Ctx *ctx) static void consume(Ctx *ctx, size_t n) { if (ctx->i + n > ctx->n) { - die("ERROR: cannot consume, no more input"); + DIE("cannot consume, no more input"); } ctx->i += n; } @@ -67,10 +67,7 @@ static char ch(Ctx *ctx, char expected) consume1(ctx); return c; } - char *msg = g_malloc(64); - sprintf(msg, "parse error, expected '%c' got '%c'", expected, c); - die(msg); - return 0; /* placate the compiler */ + DIE("parse error, expected '%c' got '%c'", expected, c); } static int maybe_spaces(Ctx *ctx) @@ -121,7 +118,7 @@ static char *parse_while(Ctx *ctx, gboolean (*is_valid)(char)) if (i >= n) { n *= 2; char *t = g_realloc(s, n); - if (!t) oom(); + if (!t) OOM(); s = t; } } @@ -216,7 +213,7 @@ static LakeVal *parse_str(Ctx *ctx) if (i >= n) { n *= 2; char *t = g_realloc(s, n); - if (!t) oom(); + if (!t) OOM(); s = t; } } @@ -304,9 +301,7 @@ static LakeVal *_parse_expr(Ctx *ctx) /* noop */ } else { - char *msg = g_strdup_printf("unexpected char '%c'", c); - err(msg); - g_free(msg); + ERR("unexpected char '%c'", c); result = VAL(PARSE_ERR); ctx->i = ctx->n; /* consume the rest */ }