commit 9144fd2fb359c694cf8a1e810e2c3470c4045f84
parent 2f37d0706cc2ba869a104ad7bb3af7a17f3b656a
Author: Ed van Bruggen <edvb@uw.edu>
Date: Thu, 3 Oct 2019 23:30:36 -0700
Add macro to check for minimum number of arguments
Diffstat:
2 files changed, 13 insertions(+), 10 deletions(-)
diff --git a/tisp.c b/tisp.c
@@ -908,8 +908,7 @@ prim_type(Env env, Val args)
static Val
prim_lambda(Env env, Val args)
{
- if (list_len(args) < 2)
- tsp_warnf("lambda: expected 2 or more arguments, received %d", list_len(args));
+ tsp_arg_min(args, "lambda", 2);
if (car(args)->t != PAIR && !nilp(car(args)))
tsp_warn("lambda: incorrect format, no argument list found");
return mk_func(FUNCTION, car(args), cdr(args), env);
@@ -918,8 +917,7 @@ prim_lambda(Env env, Val args)
static Val
prim_macro(Env env, Val args)
{
- if (list_len(args) < 2)
- tsp_warnf("macro: expected 2 or more arguments, received %d", list_len(args));
+ tsp_arg_min(args, "macro", 2);
if (car(args)->t != PAIR && !nilp(car(args)))
tsp_warn("macro: incorrect format, no argument list found");
return mk_func(MACRO, car(args), cdr(args), env);
@@ -933,8 +931,7 @@ prim_define(Env env, Val args)
{
Val sym, val;
Hash h;
- if (list_len(args) < 2)
- tsp_warnf("define: expected 2 or more arguments, received %d", list_len(args));
+ tsp_arg_min(args, "define", 2);
if (car(args)->t == PAIR) {
sym = caar(args);
if (sym->t != SYMBOL)
diff --git a/tisp.h b/tisp.h
@@ -39,15 +39,21 @@
return NULL; \
} while(0)
#endif
+
+#define tsp_arg_min(ARGS, NAME, NARGS) do { \
+ if (list_len(ARGS) < NARGS) \
+ tsp_warnf("%s: expected at least %d argument%s, received %d", \
+ NAME, NARGS, NARGS > 1 ? "s" : "", list_len(ARGS)); \
+} while(0)
#define tsp_arg_num(ARGS, NAME, NARGS) do { \
if (list_len(ARGS) != NARGS && NARGS != -1) \
tsp_warnf("%s: expected %d argument%s, received %d", \
NAME, NARGS, NARGS > 1 ? "s" : "", list_len(ARGS)); \
} while(0)
-#define tsp_arg_type(ARG, NAME, TYPE) do { \
- if (!(ARG->t & (TYPE))) \
- tsp_warnf(NAME ": expected %s, received %s", \
- type_str(TYPE), type_str(ARG->t)); \
+#define tsp_arg_type(ARG, NAME, TYPE) do { \
+ if (!(ARG->t & (TYPE))) \
+ tsp_warnf(NAME ": expected %s, received %s", \
+ type_str(TYPE), type_str(ARG->t)); \
} while(0)
#define tsp_env_name_fn(NAME, FN) tisp_env_add(env, #NAME, mk_prim(prim_##FN))