diff --git a/lang/number.c b/lang/number.c index b09c941..bd229e7 100644 --- a/lang/number.c +++ b/lang/number.c @@ -27,30 +27,30 @@ return num->value; } -static void number_add(VmState state, Object obj, void *priv) { - (void)priv; +#define OP_ADD (void *)1 +#define OP_MINUS (void *)2 + +static void number_math(VmState state, Object obj, void *priv) { int arg_count = vm_stack_depth(state); - abort_if(arg_count != 2, "number_add called without 2 arguments"); + abort_if(arg_count != 2, "number_math called without 2 arguments"); Object arg1 = vm_stack_get(state, 1); - int added = number_value(obj) + number_value(arg1); - vm_stack_set(state, 0, number_create(added)); + int result = 0; + if (priv == OP_ADD) { + result = number_value(obj) + number_value(arg1); + } else if (priv == OP_MINUS) { + result = number_value(obj) - number_value(arg1); + } else { + abort_msg("number_math called with invalid priv"); + } + vm_stack_set(state, 0, number_create(result)); vm_stack_drop(state, 1); object_drop(&arg1); } -static void number_minus(VmState state, Object obj, void *priv) { - (void)priv; - int arg_count = vm_stack_depth(state); - abort_if(arg_count != 2, "number_minus called without 2 arguments"); - Object arg1 = vm_stack_get(state, 1); - int subbed = number_value(obj) - number_value(arg1); - vm_stack_set(state, 0, number_create(subbed)); - vm_stack_drop(state, 1); - object_drop(&arg1); -} - -static struct object_call calls[] = {{.name = "Add", .handler = number_add}, - {.name = "Minus", .handler = number_minus}, {.name = NULL, /* end */}}; +static struct object_call calls[] = { + {.name = "Add", .handler = number_math, .priv = OP_ADD}, + {.name = "Minus", .handler = number_math, .priv = OP_MINUS}, + {.name = NULL, /* end */}}; static struct object_class num_class = { .cleanup = number_cleanup,