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);
}
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;

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)
{
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;
}

18
lake.c
View file

@ -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) {

9
lake.h
View file

@ -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 <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 "bool.h"
#include "int.h"

17
list.c
View file

@ -9,6 +9,7 @@
#include <glib.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#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)

15
parse.c
View file

@ -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 */
}