feat: implement format strings
This commit is contained in:
+134
@@ -13,6 +13,7 @@
|
||||
#include <vm/fzl_vm_env.h>
|
||||
#include <vm/fzl_vm_object.h>
|
||||
|
||||
char build_result[512];
|
||||
char last_err[256];
|
||||
size_t last_jmp_ip = 0;
|
||||
size_t total_jmp_on_same = 0;
|
||||
@@ -297,6 +298,139 @@ int fzl_vm_run(fzl_vm_t *vm, bool allow_infinite, uint64_t max_iter)
|
||||
|
||||
switch (opcode)
|
||||
{
|
||||
case FZL_OPCODE_BUILD_STR:
|
||||
{
|
||||
uint64_t n = vm->ops[++vm->ip];
|
||||
if (vm->dtop < n)
|
||||
{
|
||||
snprintf(last_err, sizeof(last_err),
|
||||
"runtime error - expected %zu value(s) in dstack", n);
|
||||
print_err_data(vm);
|
||||
return -1;
|
||||
}
|
||||
|
||||
char temp[64];
|
||||
size_t gi = 0;
|
||||
memset(build_result, 0, sizeof(build_result));
|
||||
for (size_t i = 0; i < n; i++)
|
||||
{
|
||||
if (gi >= 512)
|
||||
{
|
||||
for (size_t i = 511; i > 508; i--)
|
||||
build_result[i] = '.';
|
||||
break;
|
||||
}
|
||||
|
||||
memset(temp, 0, sizeof(temp));
|
||||
fzl_value_t val = vm->dstack[i];
|
||||
|
||||
switch (val.type)
|
||||
{
|
||||
case FZL_TYPE_UINT:
|
||||
snprintf(temp, sizeof(temp), "%zu", val.as.u);
|
||||
strcpy(build_result + gi, temp);
|
||||
gi += strlen(temp);
|
||||
break;
|
||||
|
||||
case FZL_TYPE_UINT64:
|
||||
snprintf(temp, sizeof(temp), "%lu", val.as.u64);
|
||||
strcpy(build_result + gi, temp);
|
||||
gi += strlen(temp);
|
||||
break;
|
||||
|
||||
case FZL_TYPE_UINT32:
|
||||
snprintf(temp, sizeof(temp), "%u", val.as.u32);
|
||||
strcpy(build_result + gi, temp);
|
||||
gi += strlen(temp);
|
||||
break;
|
||||
|
||||
case FZL_TYPE_UINT16:
|
||||
snprintf(temp, sizeof(temp), "%u", val.as.u16);
|
||||
strcpy(build_result + gi, temp);
|
||||
gi += strlen(temp);
|
||||
break;
|
||||
|
||||
case FZL_TYPE_UINT8:
|
||||
snprintf(temp, sizeof(temp), "%u", val.as.u8);
|
||||
strcpy(build_result + gi, temp);
|
||||
gi += strlen(temp);
|
||||
break;
|
||||
|
||||
case FZL_TYPE_INT:
|
||||
snprintf(temp, sizeof(temp), "%zd", val.as.i);
|
||||
strcpy(build_result + gi, temp);
|
||||
gi += strlen(temp);
|
||||
break;
|
||||
|
||||
case FZL_TYPE_INT64:
|
||||
snprintf(temp, sizeof(temp), "%ld", val.as.i64);
|
||||
strcpy(build_result + gi, temp);
|
||||
gi += strlen(temp);
|
||||
break;
|
||||
|
||||
case FZL_TYPE_INT32:
|
||||
snprintf(temp, sizeof(temp), "%d", val.as.i32);
|
||||
strcpy(build_result + gi, temp);
|
||||
gi += strlen(temp);
|
||||
break;
|
||||
|
||||
case FZL_TYPE_INT16:
|
||||
snprintf(temp, sizeof(temp), "%d", val.as.i16);
|
||||
strcpy(build_result + gi, temp);
|
||||
gi += strlen(temp);
|
||||
break;
|
||||
|
||||
case FZL_TYPE_INT8:
|
||||
snprintf(temp, sizeof(temp), "%d", val.as.i8);
|
||||
strcpy(build_result + gi, temp);
|
||||
gi += strlen(temp);
|
||||
break;
|
||||
|
||||
case FZL_TYPE_BOOL:
|
||||
snprintf(build_result + gi, sizeof(build_result) - gi, "%s",
|
||||
val.as.b ? "true" : "false");
|
||||
gi += val.as.b ? 4 : 5;
|
||||
break;
|
||||
|
||||
case FZL_TYPE_CHAR:
|
||||
snprintf(build_result + gi, sizeof(build_result) - gi, "%d",
|
||||
val.as.c);
|
||||
gi += 1;
|
||||
break;
|
||||
|
||||
case FZL_TYPE_STRING:
|
||||
snprintf(build_result + gi, sizeof(build_result) - gi, "%s",
|
||||
val.as.s.value);
|
||||
gi += val.as.s.length;
|
||||
break;
|
||||
|
||||
case FZL_TYPE_EXTERN_FN:
|
||||
snprintf(temp, sizeof(temp), "<extern:%zu>", val.as.u);
|
||||
strcpy(build_result + gi, temp);
|
||||
gi += strlen(temp);
|
||||
break;
|
||||
|
||||
case FZL_TYPE_OBJECT:
|
||||
snprintf(temp, sizeof(temp), "<ref:%p>", val.as.obj);
|
||||
strcpy(build_result + gi, temp);
|
||||
gi += strlen(temp);
|
||||
break;
|
||||
|
||||
case FZL_TYPE_NULL:
|
||||
snprintf(build_result + gi, sizeof(build_result) - gi, "<null>");
|
||||
gi += 6;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
vm->dtop -= n;
|
||||
vm->dstack[vm->dtop++] =
|
||||
(fzl_value_t){.type = FZL_TYPE_STRING,
|
||||
.as.s.length = strlen(build_result),
|
||||
.as.s.value = build_result};
|
||||
break;
|
||||
}
|
||||
|
||||
case FZL_OPCODE_DEREFERENCE:
|
||||
{
|
||||
if (vm->dtop < 1)
|
||||
|
||||
@@ -20,6 +20,10 @@ void fzl_vm_print_ops(fzl_vm_t *vm)
|
||||
|
||||
switch (vm->ops[i])
|
||||
{
|
||||
case FZL_OPCODE_BUILD_STR:
|
||||
printf("BUILD_STR %zu\n", vm->ops[++i]);
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_DEREFERENCE:
|
||||
printf("DEREFERENCE\n");
|
||||
break;
|
||||
@@ -163,9 +167,6 @@ void fzl_vm_print_ops(fzl_vm_t *vm)
|
||||
case FZL_OPCODE_JMP:
|
||||
printf("JMP IP: %ld\n", vm->ops[++i]);
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("<UNKNOWN>\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user