diff options
Diffstat (limited to 'src/eval.c')
-rw-r--r-- | src/eval.c | 63 |
1 files changed, 27 insertions, 36 deletions
@@ -86,7 +86,6 @@ static struct object * eval_prefix_expression(struct environment *env, struct token op, struct object *right) { - struct object *res; switch (op.type) { case TOKEN_BANG: return eval_bang_prefix_expression(env, right); @@ -105,49 +104,42 @@ static inline struct object * eval_integer_infix_expression(struct environment *env, struct token op, struct object *left, struct object *right) { - bool bres; - struct object *ires; + struct object *res; switch (op.type) { case TOKEN_PLUS: - ires = object_new(left->integer + right->integer); + res = object_new(left->integer + right->integer); break; case TOKEN_MINUS: - ires = object_new(left->integer - right->integer); + res = object_new(left->integer - right->integer); break; case TOKEN_ASTERISK: - ires = object_new(left->integer * right->integer); + res = object_new(left->integer * right->integer); break; case TOKEN_SLASH: - ires = object_new(left->integer / right->integer); + res = object_new(left->integer / right->integer); break; case TOKEN_LT: - bres = left->integer < right->integer; - goto boolres; + res = get_bool_object(left->integer < right->integer); + break; case TOKEN_GT: - bres = left->integer > right->integer; - goto boolres; + res = get_bool_object(left->integer > right->integer); + break; case TOKEN_EQ: - bres = left->integer == right->integer; - goto boolres; + res = get_bool_object(left->integer == right->integer); + break; case TOKEN_NOTEQ: - bres = left->integer != right->integer; - goto boolres; + res = get_bool_object(left->integer != right->integer); + break; default:; char buf[256]; - struct object *error = new_error("unknown operator: %s %s %s", + res = new_error("unknown operator: %s %s %s", object_type_print(left->type), slice_sprint(&op.literal, buf), object_type_print(right->type)); - object_unref(left); - object_unref(right); - return error; + break; } object_unref(left); object_unref(right); - return ires; -boolres: - object_unref(left); - object_unref(right); - return get_bool_object(bres); + return res; } static inline struct object * @@ -157,33 +149,32 @@ eval_infix_expression(struct environment *env, struct token op, if (left->type == OBJECT_INT && right->type == OBJECT_INT) { return eval_integer_infix_expression(env, op, left, right); } - bool res; + struct object *res; switch (op.type) { case TOKEN_EQ: - res = left->integer == right->integer; + res = get_bool_object(left->integer == right->integer); break; case TOKEN_NOTEQ: - res = left->integer != right->integer; + res = get_bool_object(left->integer != right->integer); break; default:; char buf[256]; - struct object *error; if (left->type != right->type) { - error = new_error("type mismatch: %s %s %s", - object_type_print(left->type), slice_sprint(&op.literal, buf), + res = new_error("type mismatch: %s %s %s", + object_type_print(left->type), + slice_sprint(&op.literal, buf), object_type_print(right->type)); } else { - error = new_error("unknown operator: %s %s %s", - object_type_print(left->type), slice_sprint(&op.literal, buf), + res = new_error("unknown operator: %s %s %s", + object_type_print(left->type), + slice_sprint(&op.literal, buf), object_type_print(right->type)); } - object_unref(left); - object_unref(right); - return error; + break; } object_unref(left); object_unref(right); - return get_bool_object(res); + return res; } static inline struct object * |