Browse Source

utility function to check if something is truthy

master
Martin Dørum 3 years ago
parent
commit
a81643f312
3 changed files with 9 additions and 9 deletions
  1. 2
    0
      include/lang2/vm/vm.h
  2. 2
    9
      lib/vm/builtins.c
  3. 5
    0
      lib/vm/vm.c

+ 2
- 0
include/lang2/vm/vm.h View File

void l2_vm_run(struct l2_vm *vm); void l2_vm_run(struct l2_vm *vm);
size_t l2_vm_gc(struct l2_vm *vm); size_t l2_vm_gc(struct l2_vm *vm);


int l2_vm_val_is_true(struct l2_vm *vm, l2_word id);

#endif #endif

+ 2
- 9
lib/vm/builtins.c View File

return l2_vm_error(vm, "Expected 2 or 3 arguments"); return l2_vm_error(vm, "Expected 2 or 3 arguments");
} }


struct l2_vm_value *cond = &vm->values[argv[0]];

if (
l2_value_get_type(cond) == L2_VAL_TYPE_ATOM &&
cond->atom == vm->values[vm->ktrue].atom) {
if (l2_vm_val_is_true(vm, argv[0])) {
l2_word ret_id = l2_vm_alloc(vm, L2_VAL_TYPE_CONTINUATION, 0); l2_word ret_id = l2_vm_alloc(vm, L2_VAL_TYPE_CONTINUATION, 0);
struct l2_vm_value *ret = &vm->values[ret_id]; struct l2_vm_value *ret = &vm->values[ret_id];
ret->extra.cont_call = argv[1]; ret->extra.cont_call = argv[1];
}; };


static l2_word loop_callback(struct l2_vm *vm, l2_word retval, l2_word cont) { static l2_word loop_callback(struct l2_vm *vm, l2_word retval, l2_word cont) {
struct l2_vm_value *ret = &vm->values[retval];
if (
l2_value_get_type(ret) == L2_VAL_TYPE_ATOM &&
ret->atom == vm->values[vm->ktrue].atom) {
if (l2_vm_val_is_true(vm, retval)) {
return cont; return cont;
} }



+ 5
- 0
lib/vm/vm.c View File

vm->gc_scheduled = 0; vm->gc_scheduled = 0;
} }
} }

int l2_vm_val_is_true(struct l2_vm *vm, l2_word id) {
struct l2_vm_value *val = &vm->values[id];
return l2_value_get_type(val) == L2_VAL_TYPE_ATOM && val->atom == vm->ktrue;
}

Loading…
Cancel
Save