Skip to content

Commit

Permalink
Prefix exported symbols with sch.
Browse files Browse the repository at this point in the history
  • Loading branch information
tadd committed Feb 22, 2025
1 parent 461b108 commit 09b9515
Show file tree
Hide file tree
Showing 4 changed files with 292 additions and 281 deletions.
112 changes: 56 additions & 56 deletions basic-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,109 +7,109 @@
#include "schaf.h"

#define expect_stringify(exp, v) do { \
char *s = stringify(v); \
char *s = sch_stringify(v); \
cr_expect_str_eq(s, exp); \
free(s); \
} while (0)

#define value_idfunc list
#define V(x) \
_Generic(x, int: value_of_int, char *: value_of_string, Value: value_idfunc)(x)
_Generic(x, int: sch_value_of_int, char *: sch_value_of_string, SchValue: value_idfunc)(x)
#define expect_v_eq(expected, actual) do { \
Value vexp = expected, vact = actual; \
if (value_is_int(vexp)) \
SchValue vexp = expected, vact = actual; \
if (sch_value_is_int(vexp)) \
expect_int_eq(vexp, vact); \
else if (value_is_string(vexp)) \
expect_vstr_eq(value_to_string(vexp), vact); \
else if (value_is_symbol(vexp)) \
expect_vsym_eq(value_to_string(vexp), vact); \
else if (sch_value_is_string(vexp)) \
expect_vstr_eq(sch_value_to_string(vexp), vact); \
else if (sch_value_is_symbol(vexp)) \
expect_vsym_eq(sch_value_to_string(vexp), vact); \
} while (0)
#define expect_list_eq(expected, actual) do { \
Value exp = expected, act = actual; \
cr_expect(value_is_pair(act)); \
expect_int_eq(length(exp), length(act)); \
for (; exp != Qnil; exp = cdr(exp), act = cdr(act)) \
expect_v_eq(car(exp), car(act)); \
SchValue exp = expected, act = actual; \
cr_expect(sch_value_is_pair(act)); \
expect_int_eq(sch_length(exp), sch_length(act)); \
for (; exp != SCH_Qnil; exp = sch_cdr(exp), act = sch_cdr(act)) \
expect_v_eq(sch_car(exp), sch_car(act)); \
} while (0)
#define expect_pair_eq(ecar, ecdr, act) do { \
Value a = act; \
cr_expect(value_is_pair(a)); \
expect_v_eq(V(ecar), car(a)); \
expect_v_eq(V(ecdr), cdr(a)); \
SchValue a = act; \
cr_expect(sch_value_is_pair(a)); \
expect_v_eq(V(ecar), sch_car(a)); \
expect_v_eq(V(ecdr), sch_cdr(a)); \
} while (0)

#define expect_int_eq(exp, act) cr_expect(eq(int, exp, act))
#define expect_string_eq(exp, act) cr_expect_str_eq(act, exp)
#define expect_error(pattern, v) do { \
expect_int_eq(Qundef, v); \
char *m = strstr(error_message(), pattern); \
expect_int_eq(SCH_Qundef, v); \
char *m = strstr(sch_error_message(), pattern); \
cr_expect_not_null(m, "expected \"%s\" includes \"%s\"", \
error_message(), pattern); \
sch_error_message(), pattern); \
} while (0)

#define expect_no_error(v) \
cr_expect_neq(v, Qundef, "got error with a message: '%s'", error_message())
cr_expect_neq(v, SCH_Qundef, "got error with a message: '%s'", sch_error_message())
#define expect_vx_eq(t, n, exp, act) do { \
Value a = act; \
SchValue a = act; \
expect_no_error(a); \
expect_int_eq(t, value_type_of(a)); \
expect_##n##_eq(exp, value_to_##n(a)); \
expect_int_eq(t, sch_value_type_of(a)); \
expect_##n##_eq(exp, sch_value_to_##n(a)); \
} while (0)
#define expect_x_eq_parsed(x, exp, act) expect_##x##_eq(exp, parse_expr_string(act))

#define expect_vint_eq(exp, act) expect_vx_eq(TYPE_INT, int, exp, act)
#define expect_vstr_eq(exp, act) expect_vx_eq(TYPE_STR, string, exp, act)
#define expect_vsym_eq(exp, act) expect_vx_eq(TYPE_SYMBOL, string, exp, act)
#define expect_vint_eq(exp, act) expect_vx_eq(SCH_TYPE_INT, int, exp, act)
#define expect_vstr_eq(exp, act) expect_vx_eq(SCH_TYPE_STR, string, exp, act)
#define expect_vsym_eq(exp, act) expect_vx_eq(SCH_TYPE_SYMBOL, string, exp, act)

#define expect_vint_eq_parsed(exp, act) expect_x_eq_parsed(vint, exp, act)
#define expect_vstr_eq_parsed(exp, act) expect_x_eq_parsed(vstr, exp, act)
#define expect_vsym_eq_parsed(exp, act) expect_x_eq_parsed(vsym, exp, act)
#define expect_list_eq_parsed(exp, act) expect_x_eq_parsed(list, exp, act)
#define expect_pair_eq_parsed(ecar, ecdr, act) expect_pair_eq(ecar, ecdr, parse_expr_string(act))
#define expect_parse_error(exp, act) expect_error(exp, parse_expr_string(act))
#define expect_runtime_error(exp, act) expect_error(exp, eval_string(act))
#define expect_runtime_error(exp, act) expect_error(exp, sch_eval_string(act))

static Value parse_expr_string(const char *in)
static SchValue parse_expr_string(const char *in)
{
Value v = parse_string(in);
if (v == Qundef || v == Qnil)
SchValue v = sch_parse_string(in);
if (v == SCH_Qundef || v == SCH_Qnil)
return v;
return car(v);
return sch_car(v);
}

// terminate with Qundef
static Value list(Value arg, ...)
static SchValue list(SchValue arg, ...)
{
if (arg == Qundef)
return Qnil;
Value vec[0x100] = { arg, }, o;
if (arg == SCH_Qundef)
return SCH_Qnil;
SchValue vec[0x100] = { arg, }, o;
va_list ap;
va_start(ap, arg);
long i = 1;
while ((o = va_arg(ap, Value)) != Qundef)
while ((o = va_arg(ap, SchValue)) != SCH_Qundef)
vec[i++] = o;
va_end(ap);
Value l = Qnil;
SchValue l = SCH_Qnil;
while (--i >= 0)
l = cons(vec[i], l);
l = sch_cons(vec[i], l);
return l;
}

Test(schaf, printing) {
expect_stringify("#t", Qtrue);
expect_stringify("#f", Qfalse);
expect_stringify("<undef>", Qundef);
expect_stringify("()", Qnil);
expect_stringify("#t", SCH_Qtrue);
expect_stringify("#f", SCH_Qfalse);
expect_stringify("<undef>", SCH_Qundef);
expect_stringify("()", SCH_Qnil);

expect_stringify("0", value_of_int(0));
expect_stringify("42", value_of_int(42));
expect_stringify("-42", value_of_int(-42));
expect_stringify("0", sch_value_of_int(0));
expect_stringify("42", sch_value_of_int(42));
expect_stringify("-42", sch_value_of_int(-42));

expect_stringify("foo", value_of_symbol("foo"));
expect_stringify("foo", sch_value_of_symbol("foo"));

expect_stringify("(1)", cons(V(1), Qnil));
expect_stringify("(1 . 2)", cons(V(1), V(2)));
expect_stringify("(1 2)", list(V(1), V(2), Qundef));
expect_stringify("(1)", sch_cons(V(1), SCH_Qnil));
expect_stringify("(1 . 2)", sch_cons(V(1), V(2)));
expect_stringify("(1 2)", list(V(1), V(2), SCH_Qundef));
}

Test(schaf, parse_int) {
Expand All @@ -118,11 +118,11 @@ Test(schaf, parse_int) {
}

Test(schaf, parse_nil) {
cr_expect(parse_expr_string("()") == Qnil);
cr_expect(parse_expr_string("()") == SCH_Qnil);
}

Test(schaf, parse_list) {
expect_list_eq_parsed(list(V(1), V(2), Qundef), "(1 2)");
expect_list_eq_parsed(list(V(1), V(2), SCH_Qundef), "(1 2)");
}

Test(schaf, parse_string) {
Expand All @@ -132,11 +132,11 @@ Test(schaf, parse_string) {
}

Test(schaf, parse_string_list) {
expect_list_eq_parsed(list(V("abc"), V("def"), Qundef),
expect_list_eq_parsed(list(V("abc"), V("def"), SCH_Qundef),
"(\"abc\" \"def\")");
}

#define caaaar(x) (car(car(car(car(x)))))
#define caaaar(x) (sch_car(sch_car(sch_car(sch_car(x)))))
Test(schaf, cxr) {
expect_vint_eq(42, caaaar(parse_expr_string("((((42))))")));
}
Expand All @@ -151,11 +151,11 @@ Test(schaf, parse_dot) {

Test(schaf, parse_peculiar) {
expect_vint_eq_parsed(42, "+42");
cr_expect(value_is_symbol(parse_expr_string("+")));
cr_expect(sch_value_is_symbol(parse_expr_string("+")));
}

Test(schaf, parse_lambda) {
expect_list_eq_parsed(list(value_of_symbol("lambda"), Qnil, V(42), Qundef),
expect_list_eq_parsed(list(sch_value_of_symbol("lambda"), SCH_Qnil, V(42), SCH_Qundef),
"(lambda () 42)");
}

Expand Down
12 changes: 6 additions & 6 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ static void print_vmhwm(void)
int main(int argc, char **argv)
{
Option o = parse_opt(argc, argv);
Value v;
SchValue v;
if (o.parse_only)
v = o.script ? parse_string(o.script) : parse(o.path);
v = o.script ? sch_parse_string(o.script) : sch_parse(o.path);
else
v = o.script ? eval_string(o.script) : load(o.path);
if (v == Qundef)
error("%s", error_message());
v = o.script ? sch_eval_string(o.script) : sch_load(o.path);
if (v == SCH_Qundef)
error("%s", sch_error_message());
if (o.print) {
display(v);
sch_display(v);
printf("\n");
}
if (o.cputime)
Expand Down
Loading

0 comments on commit 09b9515

Please sign in to comment.