summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriamcheeseman <[email protected]>2026-04-07 10:52:10 -0400
committeriamcheeseman <[email protected]>2026-04-07 10:52:10 -0400
commite735745b0a65a67331d1fd0dd963f2da3ff67f36 (patch)
tree03a7b25e3c7aeca0370a0a501ae906644dd79938
parente0f38c44e9888c9efd1487e8d1ba9d7e5d845e1d (diff)
microscript: keep bytecode listings in a consistent order
-rw-r--r--uscript/us_debug.c26
-rw-r--r--uscript/vm.c46
2 files changed, 36 insertions, 36 deletions
diff --git a/uscript/us_debug.c b/uscript/us_debug.c
index d4c6cdf..496b191 100644
--- a/uscript/us_debug.c
+++ b/uscript/us_debug.c
@@ -42,13 +42,13 @@ int print_instruction(struct us_proto *proto, int idx)
);
return idx + 2 + upvalc * 2;
}
+ case BC_SMALL_INT:
case BC_ARR:
- case BC_SET_UPVAL:
case BC_GET_UPVAL:
- case BC_SET_LOCAL:
+ case BC_SET_UPVAL:
case BC_GET_LOCAL:
- case BC_CALL:
- case BC_SMALL_INT: {
+ case BC_SET_LOCAL:
+ case BC_CALL: {
fprintf(
stderr,
"%d\n",
@@ -57,8 +57,8 @@ int print_instruction(struct us_proto *proto, int idx)
return idx + 2;
}
case BC_JMP:
- case BC_LOOP:
- case BC_FALSEY_JMP: {
+ case BC_FALSEY_JMP:
+ case BC_LOOP: {
u16 jmp = (u16)(proto->bytecode[idx + 1] << 8) | proto->bytecode[idx + 2];
int dst = idx + jmp + 3;
if (instruction == BC_LOOP) {
@@ -71,9 +71,8 @@ int print_instruction(struct us_proto *proto, int idx)
);
return idx + 3;
}
- case BC_PRINT:
- case BC_TRUE:
case BC_FALSE:
+ case BC_TRUE:
case BC_ZILCH:
case BC_GET_INDEX:
case BC_SET_INDEX:
@@ -84,15 +83,16 @@ int print_instruction(struct us_proto *proto, int idx)
case BC_MULT:
case BC_DIV:
case BC_MOD:
+ case BC_GT:
+ case BC_GTE:
+ case BC_LT:
+ case BC_LTE:
case BC_NEG:
case BC_NOT:
- case BC_CONCAT:
case BC_EQL:
case BC_NEQL:
- case BC_GT:
- case BC_LT:
- case BC_GTE:
- case BC_LTE:
+ case BC_CONCAT:
+ case BC_PRINT:
case BC_RET:
putc('\n', stderr);
return idx + 1;
diff --git a/uscript/vm.c b/uscript/vm.c
index 7a617dd..7c2d85e 100644
--- a/uscript/vm.c
+++ b/uscript/vm.c
@@ -148,20 +148,19 @@ void us_exec(struct us_func *func)
vm_push(wrap_arr(arr));
break;
}
- case BC_SET_LOCAL:
- vm.cf->stackbot[read_byte()] = vm_peek();
- break;
case BC_GET_LOCAL:
vm_push(vm.cf->stackbot[read_byte()]);
break;
+ case BC_SET_LOCAL:
+ vm.cf->stackbot[read_byte()] = vm_peek();
+ break;
case BC_GET_UPVAL:
vm_push(*func->upvals[read_byte()]->loc);
break;
case BC_SET_UPVAL:
*func->upvals[read_byte()]->loc = vm_peek();
break;
- case BC_SET_INDEX: {
- struct us_val set_val = vm_pop();
+ case BC_GET_INDEX: {
struct us_val idx_val = vm_pop();
struct us_val arr_val = vm_pop();
if (idx_val.type != VAL_NUM)
@@ -180,11 +179,11 @@ void us_exec(struct us_func *func)
da_len(arr->e) - 1
);
}
- arr->e[idx] = set_val;
- vm_push(set_val);
+ vm_push(arr->e[idx]);
break;
}
- case BC_GET_INDEX: {
+ case BC_SET_INDEX: {
+ struct us_val set_val = vm_pop();
struct us_val idx_val = vm_pop();
struct us_val arr_val = vm_pop();
if (idx_val.type != VAL_NUM)
@@ -203,14 +202,15 @@ void us_exec(struct us_func *func)
da_len(arr->e) - 1
);
}
- vm_push(arr->e[idx]);
+ arr->e[idx] = set_val;
+ vm_push(set_val);
break;
}
+ case BC_POP: vm_pop(); break;
case BC_POP_UPVAL:
close_upvals(vm.stacktop - 1);
vm_pop();
break;
- case BC_POP: vm_pop(); break;
case BC_ADD: {
struct us_val b = vm_pop();
struct us_val a = vm_pop();
@@ -259,20 +259,20 @@ void us_exec(struct us_func *func)
vm_push(create_bool(get_num(a) > get_num(b)));
break;
}
- case BC_LT: {
+ case BC_GTE: {
struct us_val b = vm_pop();
struct us_val a = vm_pop();
if (b.type != VAL_NUM || a.type != VAL_NUM)
log_fatal(1, "Invalid operands");
- vm_push(create_bool(get_num(a) < get_num(b)));
+ vm_push(create_bool(get_num(a) >= get_num(b)));
break;
}
- case BC_GTE: {
+ case BC_LT: {
struct us_val b = vm_pop();
struct us_val a = vm_pop();
if (b.type != VAL_NUM || a.type != VAL_NUM)
log_fatal(1, "Invalid operands");
- vm_push(create_bool(get_num(a) >= get_num(b)));
+ vm_push(create_bool(get_num(a) < get_num(b)));
break;
}
case BC_LTE: {
@@ -295,12 +295,6 @@ void us_exec(struct us_func *func)
vm_push(create_bool(negated));
break;
}
- case BC_CONCAT: {
- struct us_val b = vm_pop();
- struct us_val a = vm_pop();
- vm_push(wrap_str(concat(a, b)));
- break;
- }
case BC_EQL: {
struct us_val b = vm_pop();
struct us_val a = vm_pop();
@@ -313,6 +307,15 @@ void us_exec(struct us_func *func)
vm_push(create_bool(!vals_eql(a, b)));
break;
}
+ case BC_CONCAT: {
+ struct us_val b = vm_pop();
+ struct us_val a = vm_pop();
+ vm_push(wrap_str(concat(a, b)));
+ break;
+ }
+ case BC_JMP:
+ i += read_short(func->proto, &i);
+ break;
case BC_FALSEY_JMP: {
u16 jmp = read_short(func->proto, &i);
if (as_bool(vm_pop()))
@@ -320,9 +323,6 @@ void us_exec(struct us_func *func)
i += jmp;
break;
}
- case BC_JMP:
- i += read_short(func->proto, &i);
- break;
case BC_LOOP:
i -= read_short(func->proto, &i);
break;