diff options
author | Yaroslav de la Peña Smirnov <yps@yaroslavps.com> | 2022-01-21 01:19:31 +0300 |
---|---|---|
committer | Yaroslav de la Peña Smirnov <yps@yaroslavps.com> | 2022-01-21 01:19:31 +0300 |
commit | 29779fc525f1f69dc4e7390ee06b8c406c5bb03f (patch) | |
tree | 10c0275a46b092696c37b7c61e09d4a1815fbf5e | |
parent | c0cd4e5f199e8567ec3b5e216fbee27837d21bea (diff) | |
download | cmonkey-master.tar.gz cmonkey-master.zip |
-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 * |