Refactored all OP_ functions to vm/CATEGORY/ and renamed the files.
This commit is contained in:
parent
108be8c41e
commit
50a91d0b6f
67 changed files with 91 additions and 92 deletions
9
src/vm/logic/and.c
Normal file
9
src/vm/logic/and.c
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
case OP_AND: {
|
||||
Value b = pop_value(vm);
|
||||
Value a = pop_value(vm);
|
||||
int res = value_is_truthy(&a) && value_is_truthy(&b);
|
||||
free_value(a);
|
||||
free_value(b);
|
||||
push_value(vm, make_int(res));
|
||||
break;
|
||||
}
|
||||
19
src/vm/logic/eq.c
Normal file
19
src/vm/logic/eq.c
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
case OP_EQ: {
|
||||
Value b = pop_value(vm);
|
||||
Value a = pop_value(vm);
|
||||
int eq = 0;
|
||||
if (a.type == b.type) {
|
||||
switch (a.type) {
|
||||
case VAL_INT: eq = (a.i == b.i); 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;
|
||||
}
|
||||
push_value(vm, make_int(eq ? 1 : 0));
|
||||
free_value(a); free_value(b);
|
||||
break;
|
||||
}
|
||||
11
src/vm/logic/gt.c
Normal file
11
src/vm/logic/gt.c
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
case OP_GT: {
|
||||
Value b = pop_value(vm);
|
||||
Value a = pop_value(vm);
|
||||
if (a.type != VAL_INT || b.type != VAL_INT) {
|
||||
fprintf(stderr, "Runtime type error: GT expects ints\n");
|
||||
exit(1);
|
||||
}
|
||||
push_value(vm, make_int(a.i > b.i ? 1 : 0));
|
||||
free_value(a); free_value(b);
|
||||
break;
|
||||
}
|
||||
11
src/vm/logic/gte.c
Normal file
11
src/vm/logic/gte.c
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
case OP_GTE: {
|
||||
Value b = pop_value(vm);
|
||||
Value a = pop_value(vm);
|
||||
if (a.type != VAL_INT || b.type != VAL_INT) {
|
||||
fprintf(stderr, "Runtime type error: GTE expects ints\n");
|
||||
exit(1);
|
||||
}
|
||||
push_value(vm, make_int(a.i >= b.i ? 1 : 0));
|
||||
free_value(a); free_value(b);
|
||||
break;
|
||||
}
|
||||
14
src/vm/logic/lt.c
Normal file
14
src/vm/logic/lt.c
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
case OP_LT: {
|
||||
Value b = pop_value(vm);
|
||||
Value a = pop_value(vm);
|
||||
if (a.type != VAL_INT || b.type != VAL_INT) {
|
||||
fprintf(stderr, "Runtime type error: LT expects ints, got %s and %s\n",
|
||||
value_type_name(a.type), value_type_name(b.type));
|
||||
exit(1);
|
||||
}
|
||||
Value res = make_int(a.i < b.i ? 1 : 0);
|
||||
free_value(a);
|
||||
free_value(b);
|
||||
push_value(vm, res);
|
||||
break;
|
||||
}
|
||||
13
src/vm/logic/lte.c
Normal file
13
src/vm/logic/lte.c
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
case OP_LTE: {
|
||||
Value b = pop_value(vm);
|
||||
Value a = pop_value(vm);
|
||||
if (a.type != VAL_INT || b.type != VAL_INT) {
|
||||
fprintf(stderr, "Runtime type error: LTE expects ints\n");
|
||||
exit(1);
|
||||
}
|
||||
Value res = make_int(a.i <= b.i ? 1 : 0);
|
||||
free_value(a);
|
||||
free_value(b);
|
||||
push_value(vm, res);
|
||||
break;
|
||||
}
|
||||
19
src/vm/logic/neq.c
Normal file
19
src/vm/logic/neq.c
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
case OP_NEQ: {
|
||||
Value b = pop_value(vm);
|
||||
Value a = pop_value(vm);
|
||||
int neq = 1;
|
||||
if (a.type == b.type) {
|
||||
switch (a.type) {
|
||||
case VAL_INT: neq = (a.i != b.i); 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;
|
||||
}
|
||||
push_value(vm, make_int(neq ? 1 : 0));
|
||||
free_value(a); free_value(b);
|
||||
break;
|
||||
}
|
||||
7
src/vm/logic/not.c
Normal file
7
src/vm/logic/not.c
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
case OP_NOT: {
|
||||
Value v = pop_value(vm);
|
||||
int res = !value_is_truthy(&v);
|
||||
free_value(v);
|
||||
push_value(vm, make_int(res));
|
||||
break;
|
||||
}
|
||||
9
src/vm/logic/or.c
Normal file
9
src/vm/logic/or.c
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
case OP_OR: {
|
||||
Value b = pop_value(vm);
|
||||
Value a = pop_value(vm);
|
||||
int res = value_is_truthy(&a) || value_is_truthy(&b);
|
||||
free_value(a);
|
||||
free_value(b);
|
||||
push_value(vm, make_int(res));
|
||||
break;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue