static void list_select_reject_common(lily_vm_state *vm, uint16_t argc, uint16_t *code, int expect) { lily_value **vm_regs = vm->vm_regs; lily_value *result_reg = vm->vm_regs[code[0]]; lily_list_val *list_val = vm_regs[code[1]]->value.list; lily_value *function_reg = vm_regs[code[2]]; lily_vm_list *vm_list = vm->vm_list; int vm_list_start = vm_list->pos; int cached = 0; lily_vm_list_ensure(vm, list_val->num_values); int i; for (i = 0;i < list_val->num_values;i++) { lily_value *result = lily_foreign_call(vm, &cached, 1, function_reg, 1, list_val->elems[i]); if (result->value.integer == expect) { vm_list->values[vm_list->pos] = lily_copy_value(list_val->elems[i]); vm_list->pos++; } } slice_vm_list(vm, vm_list_start, result_reg); }
void lily_list_each(lily_vm_state *vm, uint16_t argc, uint16_t *code) { lily_value **vm_regs = vm->vm_regs; lily_value *list_reg = vm_regs[code[1]]; lily_value *function_reg = vm_regs[code[2]]; lily_list_val *list_val = list_reg->value.list; lily_value *result_reg = vm_regs[code[0]]; int cached = 0; int i; for (i = 0;i < list_val->num_values;i++) lily_foreign_call(vm, &cached, 1, function_reg, 1, list_val->elems[i]); lily_assign_value(result_reg, list_reg); }
void lily_list_fold(lily_vm_state *vm, uint16_t argc, uint16_t *code) { lily_value **vm_regs = vm->vm_regs; lily_value *result_reg = vm->vm_regs[code[0]]; lily_list_val *list_val = vm_regs[code[1]]->value.list; lily_value *starting_reg = vm_regs[code[2]]; lily_value *function_reg = vm_regs[code[3]]; lily_value *current = starting_reg; int cached = 0; int i; for (i = 0;i < list_val->num_values;i++) { current = lily_foreign_call(vm, &cached, 1, function_reg, 2, current, list_val->elems[i]); } lily_assign_value(result_reg, current); }
void lily_list_each_index(lily_vm_state *vm, uint16_t argc, uint16_t *code) { lily_value **vm_regs = vm->vm_regs; lily_value *list_reg = vm_regs[code[1]]; lily_value *function_reg = vm_regs[code[2]]; lily_list_val *list_val = list_reg->value.list; lily_value *result_reg = vm_regs[code[0]]; lily_value fake_reg; fake_reg.value.integer = 0; fake_reg.flags = VAL_IS_INTEGER; int cached = 0; int i; for (i = 0;i < list_val->num_values;i++, fake_reg.value.integer++) lily_foreign_call(vm, &cached, 0, function_reg, 1, &fake_reg); lily_assign_value(result_reg, list_reg); }
void lily_pg_result_each_row(lily_vm_state *vm, uint16_t argc, uint16_t *code) { lily_value **vm_regs = vm->vm_regs; lily_pg_result *boxed_result = (lily_pg_result *) vm_regs[code[1]]->value.generic; PGresult *raw_result = boxed_result->pg_result; if (raw_result == NULL || boxed_result->row_count == 0) return; lily_value *function_reg = vm_regs[code[2]]; int cached = 0; int row; for (row = 0;row < boxed_result->row_count;row++) { lily_list_val *lv = lily_new_list_val_0(); lily_value fake_reg; lv->elems = lily_malloc(boxed_result->column_count * sizeof(lily_value *)); int col; for (col = 0;col < boxed_result->column_count;col++) { char *field_text; if (PQgetisnull(raw_result, row, col)) field_text = "(null)"; else field_text = PQgetvalue(raw_result, row, col); lily_string_val *sv = lily_new_raw_string(field_text); lv->elems[col] = lily_new_value(VAL_IS_STRING, (lily_raw_value){.string = sv}); } lv->num_values = col; fake_reg.value.list = lv; fake_reg.flags = VAL_IS_LIST | VAL_IS_DEREFABLE; lily_foreign_call(vm, &cached, 0, function_reg, 1, &fake_reg); }
void lily_list_count(lily_vm_state *vm, uint16_t argc, uint16_t *code) { lily_value **vm_regs = vm->vm_regs; lily_value *result_reg = vm->vm_regs[code[0]]; lily_list_val *list_val = vm_regs[code[1]]->value.list; lily_value *function_reg = vm_regs[code[2]]; int count = 0; int cached = 0; int i; for (i = 0;i < list_val->num_values;i++) { lily_value *result = lily_foreign_call(vm, &cached, 1, function_reg, 1, list_val->elems[i]); if (result->value.integer == 1) count++; } lily_move_integer(result_reg, count); }