1
0
Fork 0
forked from fun/fun

Made Booleans a first-class type. (0.23.0)

This commit is contained in:
Johannes Findeisen 2025-10-04 17:34:23 +02:00
commit 95eba1eb12
12 changed files with 131 additions and 18 deletions

View file

@ -37,6 +37,6 @@ case OP_AND: {
int res = value_is_truthy(&a) && value_is_truthy(&b);
free_value(a);
free_value(b);
push_value(vm, make_int(res));
push_value(vm, make_bool(res));
break;
}

View file

@ -38,15 +38,23 @@ case OP_EQ: {
if (a.type == b.type) {
switch (a.type) {
case VAL_INT: eq = (a.i == b.i); break;
case VAL_BOOL: eq = ((a.i != 0) == (b.i != 0)); break;
case VAL_STRING: eq = (a.s && b.s) ? (strcmp(a.s, b.s) == 0) : (a.s == b.s); break;
case VAL_FUNCTION: eq = (a.fn == b.fn); break;
case VAL_NIL: eq = 1; break;
default: eq = 0; break;
}
} else {
eq = 0;
/* interop: bool vs int (0/1) */
if ((a.type == VAL_BOOL && b.type == VAL_INT) || (a.type == VAL_INT && b.type == VAL_BOOL)) {
int ai = (a.type == VAL_BOOL) ? (a.i != 0) : (a.i != 0);
int bi = (b.type == VAL_BOOL) ? (b.i != 0) : (b.i != 0);
eq = (ai == bi);
} else {
eq = 0;
}
}
push_value(vm, make_int(eq ? 1 : 0));
push_value(vm, make_bool(eq));
free_value(a); free_value(b);
break;
}

View file

@ -38,15 +38,23 @@ case OP_NEQ: {
if (a.type == b.type) {
switch (a.type) {
case VAL_INT: neq = (a.i != b.i); break;
case VAL_BOOL: neq = ((a.i != 0) != (b.i != 0)); break;
case VAL_STRING: neq = (a.s && b.s) ? (strcmp(a.s, b.s) != 0) : (a.s != b.s); break;
case VAL_FUNCTION: neq = (a.fn != b.fn); break;
case VAL_NIL: neq = 0; break;
default: neq = 1; break;
}
} else {
neq = 1;
/* interop: bool vs int (0/1) */
if ((a.type == VAL_BOOL && b.type == VAL_INT) || (a.type == VAL_INT && b.type == VAL_BOOL)) {
int ai = (a.type == VAL_BOOL) ? (a.i != 0) : (a.i != 0);
int bi = (b.type == VAL_BOOL) ? (b.i != 0) : (b.i != 0);
neq = (ai != bi);
} else {
neq = 1;
}
}
push_value(vm, make_int(neq ? 1 : 0));
push_value(vm, make_bool(neq));
free_value(a); free_value(b);
break;
}

View file

@ -35,6 +35,6 @@ case OP_NOT: {
Value v = pop_value(vm);
int res = !value_is_truthy(&v);
free_value(v);
push_value(vm, make_int(res));
push_value(vm, make_bool(res));
break;
}

View file

@ -37,6 +37,6 @@ case OP_OR: {
int res = value_is_truthy(&a) || value_is_truthy(&b);
free_value(a);
free_value(b);
push_value(vm, make_int(res));
push_value(vm, make_bool(res));
break;
}

View file

@ -12,6 +12,7 @@ case OP_TYPEOF: {
const char *tname = "Unknown";
switch (v.type) {
case VAL_INT: tname = "Number"; break;
case VAL_BOOL: tname = "Boolean"; break;
case VAL_STRING: tname = "String"; break;
case VAL_FUNCTION: tname = "Function"; break;
case VAL_ARRAY: tname = "Array"; break;