ref: merge stack and arg_stack into single stack

This commit is contained in:
samsonium
2026-10-11 06:00:26 +03:00
parent e01bf6e4ab
commit 028c8270f7
5 changed files with 38 additions and 48 deletions
-1
View File
@@ -5,7 +5,6 @@
typedef enum
{
FZL_OPCODE_LOADCONST, // Load const into T (temp) reg
FZL_OPCODE_PUSHARG, // Push argument from T into args stack
FZL_OPCODE_CALL_BUILTIN, // Call builtin function
FZL_OPCODE_PUSHNUM, // Push number into stack
+10
View File
@@ -0,0 +1,10 @@
#ifndef FZL_TYPES_H
#define FZL_TYPES_H
typedef enum
{
FZL_TYPE_STRING,
FZL_TYPE_NUMBER
} fzl_type_t;
#endif // FZL_TYPES_H
-3
View File
@@ -18,9 +18,6 @@ typedef struct
fzl_const_t stack[256];
size_t stack_top;
fzl_const_t arg_stack[256];
size_t arg_top;
} fzl_vm_t;
// Allocate FZL VM
-4
View File
@@ -113,7 +113,6 @@ bool walk(fzl_walker_t *walker, fzl_node_t *node)
{
if (!walk(walker, node->call.args[i]))
return false;
push_op(walker, FZL_OPCODE_PUSHARG);
}
}
// NOTE: you need to implement dynamic names call
@@ -157,11 +156,9 @@ bool walk(fzl_walker_t *walker, fzl_node_t *node)
case FZL_NODETYPE_MOD:
if (!walk(walker, node->binary_op.left))
return false;
push_op(walker, FZL_OPCODE_PUSHARG);
if (!walk(walker, node->binary_op.right))
return false;
push_op(walker, FZL_OPCODE_PUSHARG);
switch (node->type)
{
@@ -193,7 +190,6 @@ bool walk(fzl_walker_t *walker, fzl_node_t *node)
if (!walk(walker, node->unary_op.operand))
return false;
push_op(walker, FZL_OPCODE_PUSHARG);
push_op(walker, FZL_OPCODE_NEG);
break;
}
+28 -40
View File
@@ -27,8 +27,6 @@ fzl_vm_t *fzl_mkvm(const char *filepath)
vm->ip = 0;
memset(vm->stack, 0, sizeof(vm->stack));
vm->stack_top = 0;
memset(vm->arg_stack, 0, sizeof(vm->arg_stack));
vm->arg_top = 0;
FILE *fd = fopen(filepath, "rb");
if (!fd)
@@ -119,7 +117,7 @@ int fzl_vm_run(fzl_vm_t *vm)
uint64_t fn_id = vm->ops[++vm->ip];
if (fn_id == FZL_BUILTIN_PRINTLINE)
{
fzl_const_t arg = vm->arg_stack[--vm->arg_top];
fzl_const_t arg = vm->stack[--vm->stack_top];
if (arg.type == FZL_CONSTTYPE_STRING)
printf("%s\n", arg.as.string.chars);
else if (arg.type == FZL_CONSTTYPE_NUMBER)
@@ -145,81 +143,71 @@ int fzl_vm_run(fzl_vm_t *vm)
vm->stack[vm->stack_top++] = c;
break;
case FZL_OPCODE_PUSHARG:
if (vm->stack_top < 1)
{
snprintf(last_err, sizeof(last_err),
"runtime error - cannot push argument: stack is empty");
return -1;
}
vm->arg_stack[vm->arg_top++] = vm->stack[--vm->stack_top];
break;
case FZL_OPCODE_ADD:
if (vm->arg_top < 2)
if (vm->stack_top < 2)
{
snprintf(last_err, sizeof(last_err),
"runtime error - expected 2 values in argstack");
"runtime error - expected 2 values in stack");
return -1;
}
fzl_const_t add1 = vm->arg_stack[--vm->arg_top];
fzl_const_t add2 = vm->arg_stack[--vm->arg_top];
fzl_const_t add1 = vm->stack[--vm->stack_top];
fzl_const_t add2 = vm->stack[--vm->stack_top];
vm->stack[vm->stack_top++] =
(fzl_const_t){.type = FZL_CONSTTYPE_NUMBER,
.as.num.value = add1.as.num.value + add2.as.num.value};
break;
case FZL_OPCODE_SUB:
if (vm->arg_top < 2)
if (vm->stack_top < 2)
{
snprintf(last_err, sizeof(last_err),
"runtime error - expected 2 values in argstack");
"runtime error - expected 2 values in stack");
return -1;
}
fzl_const_t sub1 = vm->arg_stack[--vm->arg_top];
fzl_const_t sub2 = vm->arg_stack[--vm->arg_top];
fzl_const_t sub1 = vm->stack[--vm->stack_top];
fzl_const_t sub2 = vm->stack[--vm->stack_top];
vm->stack[vm->stack_top++] =
(fzl_const_t){.type = FZL_CONSTTYPE_NUMBER,
.as.num.value = sub1.as.num.value - sub2.as.num.value};
break;
case FZL_OPCODE_MUL:
if (vm->arg_top < 2)
if (vm->stack_top < 2)
{
snprintf(last_err, sizeof(last_err),
"runtime error - expected 2 values in argstack");
"runtime error - expected 2 values in stack");
return -1;
}
fzl_const_t mul1 = vm->arg_stack[--vm->arg_top];
fzl_const_t mul2 = vm->arg_stack[--vm->arg_top];
fzl_const_t mul1 = vm->stack[--vm->stack_top];
fzl_const_t mul2 = vm->stack[--vm->stack_top];
vm->stack[vm->stack_top++] =
(fzl_const_t){.type = FZL_CONSTTYPE_NUMBER,
.as.num.value = mul1.as.num.value * mul2.as.num.value};
break;
case FZL_OPCODE_DIV:
if (vm->arg_top < 2)
if (vm->stack_top < 2)
{
snprintf(last_err, sizeof(last_err),
"runtime error - expected 2 values in argstack");
"runtime error - expected 2 values in stack");
return -1;
}
fzl_const_t div1 = vm->arg_stack[--vm->arg_top];
fzl_const_t div2 = vm->arg_stack[--vm->arg_top];
fzl_const_t div1 = vm->stack[--vm->stack_top];
fzl_const_t div2 = vm->stack[--vm->stack_top];
vm->stack[vm->stack_top++] =
(fzl_const_t){.type = FZL_CONSTTYPE_NUMBER,
.as.num.value = div2.as.num.value / div1.as.num.value};
break;
case FZL_OPCODE_POW:
if (vm->arg_top < 2)
if (vm->stack_top < 2)
{
snprintf(last_err, sizeof(last_err),
"runtime error - expected 2 values in argstack");
"runtime error - expected 2 values in stack");
return -1;
}
fzl_const_t pow1 = vm->arg_stack[--vm->arg_top];
fzl_const_t pow2 = vm->arg_stack[--vm->arg_top];
fzl_const_t pow1 = vm->stack[--vm->stack_top];
fzl_const_t pow2 = vm->stack[--vm->stack_top];
uint64_t pow_n = pow1.as.num.value;
uint64_t pow_res = pow_n;
@@ -232,27 +220,27 @@ int fzl_vm_run(fzl_vm_t *vm)
break;
case FZL_OPCODE_MOD:
if (vm->arg_top < 2)
if (vm->stack_top < 2)
{
snprintf(last_err, sizeof(last_err),
"runtime error - expected 2 values in argstack");
"runtime error - expected 2 values in stack");
return -1;
}
fzl_const_t mod1 = vm->arg_stack[--vm->arg_top];
fzl_const_t mod2 = vm->arg_stack[--vm->arg_top];
fzl_const_t mod1 = vm->stack[--vm->stack_top];
fzl_const_t mod2 = vm->stack[--vm->stack_top];
vm->stack[vm->stack_top++] =
(fzl_const_t){.type = FZL_CONSTTYPE_NUMBER,
.as.num.value = mod2.as.num.value % mod1.as.num.value};
break;
case FZL_OPCODE_NEG:
if (vm->arg_top < 1)
if (vm->stack_top < 1)
{
snprintf(last_err, sizeof(last_err),
"runtime error - expected value in argstack");
"runtime error - expected value in stack");
return -1;
}
fzl_const_t neg_n = vm->arg_stack[--vm->arg_top];
fzl_const_t neg_n = vm->stack[--vm->stack_top];
vm->stack[vm->stack_top++] = (fzl_const_t){
.type = FZL_CONSTTYPE_NUMBER, .as.num.value = -neg_n.as.num.value};
break;