54 lines
1.5 KiB
C
54 lines
1.5 KiB
C
#include "vm.h"
|
|
#include "native.h"
|
|
#ifdef _WIN32
|
|
#include <Windows.h>
|
|
#else
|
|
#include <unistd.h>
|
|
#endif
|
|
|
|
Value native_print_const(VM* vm, Value* argv, uint8_t argc) {
|
|
if (argc != 1) {
|
|
fprintf(stderr, "print_const requires exactly 1 argument, got %d\n", argc);
|
|
return (Value){.int64_val = -1};
|
|
}
|
|
uint64_t idx = argv[0].int64_val;
|
|
if (!vm->program || idx >= vm->program->constants.count) {
|
|
fprintf(stderr, "Invalid constant index or no program loaded\n");
|
|
return (Value){.int64_val = -1};
|
|
}
|
|
Constant c = vm->program->constants.entries[idx];
|
|
switch (c.type) {
|
|
case TYPE_BOOL:
|
|
print(c.value.bool_val ? "true" : "false");
|
|
break;
|
|
case TYPE_CHAR:
|
|
print(&c.value.char_val);
|
|
break;
|
|
case TYPE_FLOAT32:
|
|
printf("%f", c.value.float32_val);
|
|
break;
|
|
case TYPE_FLOAT64:
|
|
printf("%f", c.value.float64_val);
|
|
break;
|
|
case TYPE_INT16:
|
|
printf("%d", c.value.int16_val);
|
|
break;
|
|
case TYPE_STRING:
|
|
print(c.value.string_val.value);
|
|
break;
|
|
default:
|
|
printf("%d\n", c.value.int64_val);
|
|
break;
|
|
}
|
|
return (Value) {.int32_val = 0};
|
|
}
|
|
|
|
void print(const char *str) {
|
|
#ifdef _WIN32
|
|
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
DWORD written;
|
|
WriteFile(hConsole, str, strlen(str), &written, NULL);
|
|
#else
|
|
write(1, str, strlen(str));
|
|
#endif
|
|
} |