replace terrible err/die/oom functions with nice ERR/DIE/OOM macros

This commit is contained in:
Sami Samhuri 2011-04-20 13:39:35 -07:00
parent 9604f23136
commit d0c17ee2cc
6 changed files with 31 additions and 62 deletions

4
env.c
View file

@ -74,9 +74,7 @@ LakeVal *env_get(Env *env, LakeSym *key)
val = env_get(env->parent, key); val = env_get(env->parent, key);
} }
if (!val) { if (!val) {
char *msg = g_strdup_printf("undefined variable: %s", SYM_S(key)); ERR("undefined variable: %s", SYM_S(key));
err(msg);
g_free(msg);
val = NULL; val = NULL;
} }
return val; return val;

30
eval.c
View file

@ -22,9 +22,7 @@ static void init_special_form_handlers(void);
static void invalid_special_form(LakeList *expr, char *detail) static void invalid_special_form(LakeList *expr, char *detail)
{ {
char *msg = g_strdup_printf("unrecognized special form, %s: %s", detail, repr(VAL(expr))); ERR("unrecognized special form, %s: %s", detail, repr(VAL(expr)));
err(msg);
g_free(msg);
} }
/* expr begins with the symbol "quote" so the quoted value is the 2nd value */ /* 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)); LakeSym *var = SYM(list_shift(expr));
LakeVal *form = list_shift(expr); LakeVal *form = list_shift(expr);
if (!env_set(env, var, form)) { if (!env_set(env, var, form)) {
char *msg = g_strdup_printf("%s is not defined", SYM_S(var)); ERR("%s is not defined", SYM_S(var));
err(msg);
g_free(msg);
} }
} }
else { else {
@ -198,9 +194,7 @@ static LakeVal *eval_special_form(Env *env, LakeList *expr)
if (handler) { if (handler) {
return handler(env, list_copy(expr)); return handler(env, list_copy(expr));
} }
char *msg = g_strdup_printf("unrecognized special form: %s", SYM_S(name)); ERR("unrecognized special form: %s", SYM_S(name));
err(msg);
g_free(msg);
return NULL; return NULL;
} }
@ -223,7 +217,7 @@ LakeVal *eval(Env *env, LakeVal *expr)
break; break;
case TYPE_DLIST: case TYPE_DLIST:
err("malformed function call"); ERR("malformed function call");
result = NULL; result = NULL;
break; break;
@ -262,8 +256,8 @@ LakeVal *eval(Env *env, LakeVal *expr)
break; break;
default: default:
printf("error: unrecognized value, type %d, size %zu bytes", expr->type, expr->size); ERR("unrecognized value, type %d, size %zu bytes", expr->type, expr->size);
die("we don't eval that around here!"); DIE("we don't eval that around here!");
} }
done: return result; done: return result;
@ -289,9 +283,7 @@ LakeVal *apply(LakeVal *fnVal, LakeList *args)
result = prim->fn(args); result = prim->fn(args);
} }
else { else {
char *msg = g_strdup_printf("%s expects %d params but got %zu", prim->name, arity, LIST_N(args)); ERR("%s expects %d params but got %zu", prim->name, arity, LIST_N(args));
err(msg);
g_free(msg);
result = NULL; result = NULL;
} }
} }
@ -301,9 +293,7 @@ LakeVal *apply(LakeVal *fnVal, LakeList *args)
/* Check # of params */ /* Check # of params */
size_t nparams = LIST_N(fn->params); size_t nparams = LIST_N(fn->params);
if (nparams != LIST_N(args) && !fn->varargs) { if (nparams != LIST_N(args) && !fn->varargs) {
char *msg = g_strdup_printf("expected %zu params but got %zu", nparams, LIST_N(args)); ERR("expected %zu params but got %zu", nparams, LIST_N(args));
err(msg);
g_free(msg);
return NULL; return NULL;
} }
@ -330,9 +320,7 @@ LakeVal *apply(LakeVal *fnVal, LakeList *args)
list_free(results); list_free(results);
} }
else { else {
char *msg = g_strdup_printf("not a function: %s", repr(fnVal)); ERR("not a function: %s", repr(fnVal));
err(msg);
g_free(msg);
} }
return result; return result;
} }

18
lake.c
View file

@ -27,22 +27,6 @@ static LakeBool _F = { { TYPE_BOOL, sizeof(LakeBool) }, FALSE };
LakeBool *T = &_T; LakeBool *T = &_T;
LakeBool *F = &_F; 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) static LakeVal *prim_nullP(LakeList *args)
{ {
LakeVal *val = list_shift(args); LakeVal *val = list_shift(args);
@ -108,7 +92,7 @@ static void run_repl_with_env(Env *env)
expr = prompt_read("> "); expr = prompt_read("> ");
if (expr == VAL(EOF)) break; if (expr == VAL(EOF)) break;
if (expr == VAL(PARSE_ERR)) { if (expr == VAL(PARSE_ERR)) {
err("parse error"); ERR("parse error");
continue; continue;
} }
if (expr) { if (expr) {

9
lake.h
View file

@ -133,11 +133,14 @@ struct lake_fn {
}; };
typedef struct lake_fn LakeFn; typedef struct lake_fn LakeFn;
void err(char *msg);
void die(char *msg);
void oom();
char *repr(LakeVal *val); char *repr(LakeVal *val);
#include <stdio.h>
#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 "sym.h"
#include "bool.h" #include "bool.h"
#include "int.h" #include "int.h"

17
list.c
View file

@ -9,6 +9,7 @@
#include <glib.h> #include <glib.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <string.h> #include <string.h>
#include "int.h" #include "int.h"
#include "lake.h" #include "lake.h"
@ -77,21 +78,21 @@ static void list_grow(LakeList *list)
LakeVal *list_set(LakeList *list, size_t i, LakeVal *val) LakeVal *list_set(LakeList *list, size_t i, LakeVal *val)
{ {
if (i < 0 || i >= list->n) { if (i >= 0 && i < list->n) {
err("list_set: index is out of bounds"); list->vals[i] = val;
return NULL; } else {
ERR("list_set: index %zu is out of bounds (%zu)", i, list->n);
} }
list->vals[i] = val;
return NULL; return NULL;
} }
LakeVal *list_get(LakeList *list, LakeInt *li) LakeVal *list_get(LakeList *list, LakeInt *li)
{ {
int i = int_val(li); int i = INT_VAL(li);
if (i < 0 || i >= list->n) { if (i >= 0 && i < list->n) {
return NULL; return list->vals[i];
} }
return list->vals[i]; return NULL;
} }
LakeVal *list_append(LakeList *list, LakeVal *val) LakeVal *list_append(LakeList *list, LakeVal *val)

15
parse.c
View file

@ -48,7 +48,7 @@ static char peek(Ctx *ctx)
static void consume(Ctx *ctx, size_t n) static void consume(Ctx *ctx, size_t n)
{ {
if (ctx->i + n > ctx->n) { if (ctx->i + n > ctx->n) {
die("ERROR: cannot consume, no more input"); DIE("cannot consume, no more input");
} }
ctx->i += n; ctx->i += n;
} }
@ -67,10 +67,7 @@ static char ch(Ctx *ctx, char expected)
consume1(ctx); consume1(ctx);
return c; return c;
} }
char *msg = g_malloc(64); DIE("parse error, expected '%c' got '%c'", expected, c);
sprintf(msg, "parse error, expected '%c' got '%c'", expected, c);
die(msg);
return 0; /* placate the compiler */
} }
static int maybe_spaces(Ctx *ctx) static int maybe_spaces(Ctx *ctx)
@ -121,7 +118,7 @@ static char *parse_while(Ctx *ctx, gboolean (*is_valid)(char))
if (i >= n) { if (i >= n) {
n *= 2; n *= 2;
char *t = g_realloc(s, n); char *t = g_realloc(s, n);
if (!t) oom(); if (!t) OOM();
s = t; s = t;
} }
} }
@ -216,7 +213,7 @@ static LakeVal *parse_str(Ctx *ctx)
if (i >= n) { if (i >= n) {
n *= 2; n *= 2;
char *t = g_realloc(s, n); char *t = g_realloc(s, n);
if (!t) oom(); if (!t) OOM();
s = t; s = t;
} }
} }
@ -304,9 +301,7 @@ static LakeVal *_parse_expr(Ctx *ctx)
/* noop */ /* noop */
} }
else { else {
char *msg = g_strdup_printf("unexpected char '%c'", c); ERR("unexpected char '%c'", c);
err(msg);
g_free(msg);
result = VAL(PARSE_ERR); result = VAL(PARSE_ERR);
ctx->i = ctx->n; /* consume the rest */ ctx->i = ctx->n; /* consume the rest */
} }