From d0615d2c3e09b3b2f1a2bbe53aec61f38b3faa0a Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 10 Aug 2018 16:34:28 -0700 Subject: [PATCH] Implement the if statement. --- src/interpreter.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/interpreter.c b/src/interpreter.c index 08a0292..7405a3a 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -1212,6 +1212,28 @@ libab_result _interpreter_run(struct interpreter_state* state, libab_tree* tree, libab_get_true_value(state->ab, into); } else if(tree->variant == TREE_FALSE) { libab_get_false_value(state->ab, into); + } else if (tree->variant == TREE_IF) { + libab_ref condition; + libab_value* condition_value; + libab_parsetype* condition_type; + result = _interpreter_run(state, vec_index(&tree->children, 0), + &condition, scope, SCOPE_NORMAL); + + if(result == LIBAB_SUCCESS) { + condition_value = libab_ref_get(&condition); + condition_type = libab_ref_get(&condition_value->type); + if(condition_type->data_u.base != libab_get_basetype_bool(state->ab)) { + result = LIBAB_MISMATCHED_TYPE; + } + } + + if(result == LIBAB_SUCCESS) { + int* boolean = libab_ref_get(&condition_value->data); + result = _interpreter_run(state, vec_index(&tree->children, + *boolean ? 1 : 2), into, scope, SCOPE_NORMAL); + } + + libab_ref_free(&condition); } else { libab_get_unit_value(state->ab, into); }