feat: finished variables declaration / assign
This commit is contained in:
+81
-2
@@ -1,3 +1,4 @@
|
||||
#include "vm/fzl_vm_env.h"
|
||||
#include <common/fzl_builtins.h>
|
||||
#include <common/fzl_opcodes.h>
|
||||
#include <common/fzl_types.h>
|
||||
@@ -122,11 +123,87 @@ int fzl_vm_run(fzl_vm_t *vm)
|
||||
|
||||
switch (opcode)
|
||||
{
|
||||
case FZL_OPCODE_MK_ENVTABLE:
|
||||
case FZL_OPCODE_STORE:
|
||||
case FZL_OPCODE_WRITE:
|
||||
if (vm->dtop < 1)
|
||||
{
|
||||
snprintf(last_err, sizeof(last_err),
|
||||
"runtime error - expected var name in data stack");
|
||||
return -1;
|
||||
}
|
||||
|
||||
fzl_value_t varname = vm->dstack[--vm->dtop];
|
||||
fzl_env_storevalue(vm->estack[vm->etop - 1], (char *)varname.as.s.value,
|
||||
(fzl_value_t){});
|
||||
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_WRITE:
|
||||
if (vm->dtop < 2)
|
||||
{
|
||||
snprintf(last_err, sizeof(last_err),
|
||||
"runtime error - expected 2 values in data stack");
|
||||
return -1;
|
||||
}
|
||||
|
||||
fzl_value_t writevar = vm->dstack[--vm->dtop];
|
||||
fzl_value_t value = vm->dstack[--vm->dtop];
|
||||
|
||||
fzl_vm_env_wresult_t wresult = fzl_env_writevalue(
|
||||
vm->estack[vm->etop - 1], (char *)writevar.as.s.value, value);
|
||||
if (wresult != FZL_VM_ENV_WRESULT_OK)
|
||||
{
|
||||
switch (wresult)
|
||||
{
|
||||
case FZL_VM_ENV_WRESULT_ERR:
|
||||
snprintf(last_err, sizeof(last_err),
|
||||
"runtime error - failed to write variable");
|
||||
return -1;
|
||||
|
||||
case FZL_VM_ENV_WRESULT_ERR_EXISTS:
|
||||
snprintf(last_err, sizeof(last_err),
|
||||
"runtime error - redefenition of '%s'",
|
||||
writevar.as.s.value);
|
||||
return -1;
|
||||
|
||||
case FZL_VM_ENV_WRESULT_ERR_NOT_FOUND:
|
||||
snprintf(last_err, sizeof(last_err),
|
||||
"runtime error - cannot find name '%s'",
|
||||
writevar.as.s.value);
|
||||
return -1;
|
||||
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
vm->dstack[vm->dtop++] = value;
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_MK_ENVTABLE:
|
||||
vm->estack[vm->etop++] = fzl_mkenv();
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_RM_ENVTABLE:
|
||||
fzl_freeenv(vm->estack[--vm->etop]);
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_LOOKUP_ENV:
|
||||
{
|
||||
if (vm->dtop < 1)
|
||||
{
|
||||
snprintf(last_err, sizeof(last_err),
|
||||
"runtime error - LOOKUP_ENV: empty data stack");
|
||||
return -1;
|
||||
}
|
||||
|
||||
fzl_value_t val = vm->dstack[--vm->dtop];
|
||||
fzl_value_t env_val =
|
||||
fzl_env_lookup(vm->estack[vm->etop - 1], (char *)val.as.s.value);
|
||||
vm->dstack[vm->dtop++] = env_val;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case FZL_OPCODE_CALL_BUILTIN:
|
||||
{
|
||||
uint64_t fn_id = vm->ops[++vm->ip];
|
||||
@@ -143,6 +220,8 @@ int fzl_vm_run(fzl_vm_t *vm)
|
||||
printf("%lu\n", arg.as.u);
|
||||
else if (arg.type == FZL_TYPE_BOOL)
|
||||
printf("%b\n", arg.as.b);
|
||||
else if (arg.type == FZL_TYPE_NULL)
|
||||
printf("<null>\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
#include "common/fzl_opcodes.h"
|
||||
#include <stdio.h>
|
||||
#include <vm/fzl_vm.h>
|
||||
|
||||
void fzl_vm_print_ops(fzl_vm_t *vm)
|
||||
{
|
||||
if (!vm)
|
||||
return;
|
||||
|
||||
if (vm->ops_count < 1)
|
||||
{
|
||||
printf("no opcodes\n");
|
||||
return;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < vm->ops_count; i++)
|
||||
{
|
||||
switch (vm->ops[i])
|
||||
{
|
||||
case FZL_OPCODE_LOADCONST:
|
||||
printf("LOADCONST #%lu\n", vm->ops[++i]);
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_CALL_BUILTIN:
|
||||
printf("CALL_BUILTIN #%lu\n", vm->ops[++i]);
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_PUSHNUM:
|
||||
printf("PUSHNUM %lu\n", vm->ops[++i]);
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_MK_ENVTABLE:
|
||||
printf("MK_ENVTABLE\n");
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_RM_ENVTABLE:
|
||||
printf("RM_ENVTABLE\n");
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_STORE:
|
||||
printf("STORE\n");
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_LOOKUP_ENV:
|
||||
printf("LOOKUP_ENV\n");
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_WRITE:
|
||||
printf("WRITE\n");
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_ADD:
|
||||
case FZL_OPCODE_SUB:
|
||||
case FZL_OPCODE_MUL:
|
||||
case FZL_OPCODE_DIV:
|
||||
case FZL_OPCODE_POW:
|
||||
case FZL_OPCODE_MOD:
|
||||
printf("Math opcode\n");
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_NEG:
|
||||
printf("NEG\n");
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("Unknown");
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -62,7 +62,7 @@ fzl_vm_env_wresult_t fzl_env_storevalue(fzl_vm_env_t *env, char *name,
|
||||
if (fzl_env_contains(env, name))
|
||||
return FZL_VM_ENV_WRESULT_ERR_EXISTS;
|
||||
|
||||
env->table[env->size] = (fzl_vm_env_entry_t){.name = name, .value = val};
|
||||
env->table[env->size++] = (fzl_vm_env_entry_t){.name = name, .value = val};
|
||||
|
||||
return FZL_VM_ENV_WRESULT_OK;
|
||||
}
|
||||
|
||||
+15
-6
@@ -9,6 +9,7 @@ typedef struct
|
||||
{
|
||||
char *filepath;
|
||||
bool help;
|
||||
bool debug;
|
||||
} run_args_t;
|
||||
|
||||
void print_hint()
|
||||
@@ -28,8 +29,9 @@ run_args_t *parse_args(int argc, char **argv)
|
||||
}
|
||||
|
||||
run_args_t *args = malloc(sizeof(run_args_t));
|
||||
args->filepath = NULL;
|
||||
args->help = false;
|
||||
args->filepath = NULL;
|
||||
args->help = false;
|
||||
args->debug = false;
|
||||
|
||||
for (size_t i = 0; i < argc; i++)
|
||||
{
|
||||
@@ -41,6 +43,8 @@ run_args_t *parse_args(int argc, char **argv)
|
||||
args->help = true;
|
||||
return args;
|
||||
}
|
||||
else if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--debug") == 0)
|
||||
args->debug = true;
|
||||
else
|
||||
{
|
||||
printf("Error: unknown flag '%s'.\n", argv[i]);
|
||||
@@ -79,11 +83,16 @@ int main(int argc, char **argv)
|
||||
return -1;
|
||||
}
|
||||
|
||||
int result = fzl_vm_run(vm);
|
||||
if (result != 0)
|
||||
if (args->debug)
|
||||
fzl_vm_print_ops(vm);
|
||||
else
|
||||
{
|
||||
printf("Error: %s\n", fzl_vm_geterror());
|
||||
return -1;
|
||||
int result = fzl_vm_run(vm);
|
||||
if (result != 0)
|
||||
{
|
||||
printf("Error: %s\n", fzl_vm_geterror());
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
free(args);
|
||||
|
||||
Reference in New Issue
Block a user