new str embedding fn

This commit is contained in:
zongor 2025-07-20 21:16:32 -04:00
parent c43761a8da
commit 02702315cb
6 changed files with 26 additions and 65 deletions

View File

@ -4,8 +4,8 @@ fn main(argc real, argv str[]) {
let screen_width = 800; ! implied uint32
let screen_height = 450; ! implied uint32
let username = argv[0]; ! implied str
let password = argv[1]; ! implied str
let username = argv[1]; ! implied str
let password = argv[2]; ! implied str
let me = Player(username, [0.0, 1.0, 2.0], PURPLE); ! implied Player struct
@ -13,7 +13,7 @@ fn main(argc real, argv str[]) {
while (running) {
window("zwl client", screen_width, screen_height) {
splitbox(parent.size 0.25) {
canvas("2D") {
canvas() {
if (button("logout")) {
me.logout();
running = false;
@ -21,7 +21,7 @@ fn main(argc real, argv str[]) {
}
}
splitbox(parent.size 0.75) {
canvas("3D") {
canvas() {
model(Floor([0, 0, 0], 30));
me.update();
model(Cube(me.pos, [0.5, 0.5, 0.5], me.appearance));

View File

@ -10,14 +10,14 @@ int main(int argc, char **argv) {
vm.stack_size = STACK_SIZE;
vm.memory_size = MEMORY_SIZE;
test_hello_world_compile(&vm);
/* test_add_compile(&vm); */
/* test_add_function_compile(&vm); */
/* test_loop_compile(&vm); */
/* test_hello_world_compile(&vm); */
test_recursive_function_compile(&vm);
/* test_recursive_function_compile(&vm); */
while(step_vm(&vm));
core_dump(&vm);
return 0;
uint32_t buffer_size = 640 * 480 * sizeof(uint32_t);

View File

@ -9,6 +9,7 @@ int core_dump(VM *vm) {
perror("Failed to open file");
return EXIT_FAILURE;
}
size_t written = fwrite(vm->memory, 1, vm->memory_size, file);
if (written != vm->memory_size) {
fprintf(stderr, "Incomplete write: %zu bytes written out of %u\n", written,

View File

@ -1,23 +1,12 @@
#include "test.h"
#include "vm.h"
bool test_hello_world_compile(VM *vm) {
str(vm, "nuqneH 'u'?", 0);
vm->code[vm->cp++].u = OP(OP_LOADU, 1, 0, 0);
vm->code[vm->cp++].u = 0;
vm->code[vm->cp++].u = OP(OP_PRINT_STRING, 0, 1, 0); /* print("nuqneH 'u'?"); */
vm->code[vm->cp++].u = OP(OP_HALT, 0, 0, 0); /* explicit halt */
vm->memory[vm->mp++].u = 12;
vm->memory[vm->mp].c[0] = 'n';
vm->memory[vm->mp].c[1] = 'u';
vm->memory[vm->mp].c[2] = 'q';
vm->memory[vm->mp++].c[3] = 'n';
vm->memory[vm->mp].c[0] = 'e';
vm->memory[vm->mp].c[1] = 'H';
vm->memory[vm->mp].c[2] = ' ';
vm->memory[vm->mp++].c[3] = '\'';
vm->memory[vm->mp].c[0] = 'u';
vm->memory[vm->mp].c[1] = '\'';
vm->memory[vm->mp].c[2] = '?';
vm->memory[vm->mp++].c[3] = '\0';
return true;
}
@ -34,23 +23,6 @@ bool test_add_compile(VM *vm) {
}
bool test_loop_compile(VM *vm) {
vm->memory[vm->mp++].u = 17;
vm->memory[vm->mp].c[0] = 'E';
vm->memory[vm->mp].c[1] = 'n';
vm->memory[vm->mp].c[2] = 't';
vm->memory[vm->mp++].c[3] = 'e';
vm->memory[vm->mp].c[0] = 'r';
vm->memory[vm->mp].c[1] = ' ';
vm->memory[vm->mp].c[2] = 'a';
vm->memory[vm->mp++].c[3] = ' ';
vm->memory[vm->mp].c[0] = 's';
vm->memory[vm->mp].c[1] = 't';
vm->memory[vm->mp].c[2] = 'r';
vm->memory[vm->mp++].c[3] = 'i';
vm->memory[vm->mp].c[0] = 'n';
vm->memory[vm->mp].c[1] = 'g';
vm->memory[vm->mp].c[2] = ':';
vm->memory[vm->mp++].c[3] = '\0';
vm->code[vm->cp++].u = OP(OP_LOADF, 0, 0, 0); /* let a = 5.0 */
vm->code[vm->cp++].f = 5.0f;
vm->code[vm->cp++].u = OP(OP_LOADI, 1, 0, 0); /* do (i = 50000, 0, -1) { */
@ -68,6 +40,7 @@ bool test_loop_compile(VM *vm) {
vm->code[vm->cp++].u = OP(OP_ADD_INT, 1, 1, 3); /* (implied by loop) i = i + (-1) */
vm->code[vm->cp++].u = OP(OP_JGE_INT, 4, 1, 2); /* } */
vm->code[vm->cp++].u = OP(OP_REAL_TO_UINT, 1, 0, 0); /* let b = a as nat; */
str(vm, "Enter a string:", 0);
vm->code[vm->cp++].u = OP(OP_LOADU, 5, 0, 0);
vm->code[vm->cp++].u = 0;
vm->code[vm->cp++].u = OP(OP_PRINT_STRING, 0, 5, 0); /* print("Enter a string: "); */

View File

@ -24,16 +24,20 @@
} while (0)
/**
* String copy in data memory.
* Embeds a string into the VM
*/
void mem_strcpy(Value *memory, const char *str, uint32_t length,
uint32_t dest) {
memory[dest].u = length;
uint32_t buffer = dest + 1;
uint32_t i;
void str(VM *vm, const char *str, uint32_t length) {
if (!length) length = strlen(str);
vm->memory[vm->mp++].u = length;
uint32_t i, j = 0;
for (i = 0; i < length; i++) {
memory[buffer + (i / 4)].c[i % 4] = str[i];
vm->memory[vm->mp].c[i % 4] = str[i];
if (++j == 4) {
j = 0;
vm->mp++;
}
}
vm->frames[vm->fp].allocated.end += length / 4;
}
/**
@ -191,41 +195,23 @@ bool step_vm(VM *vm) {
}
case OP_INT_TO_STRING: {
int32_t a = (int32_t)vm->frames[vm->fp].registers[src1].i; /* get value */
uint32_t str_dest = (uint32_t)vm->frames[vm->fp]
.allocated.end; /* get start of unallocated */
vm->frames[vm->fp].registers[dest].u =
str_dest; /* store ptr of string to dest register */
char buffer[32];
int len = sprintf(buffer, "%d", a);
mem_strcpy(vm->memory, buffer, len, str_dest); /* copy buffer to dest */
vm->frames[vm->fp].allocated.end +=
((len / 4) + (len % 4) + 1); /* increment to end of allocated */
str(vm, buffer, len); /* copy buffer to dest */
return true;
}
case OP_UINT_TO_STRING: {
uint32_t a = (uint32_t)vm->frames[vm->fp].registers[src1].u; /* get value */
uint32_t str_dest = (uint32_t)vm->frames[vm->fp]
.allocated.end; /* get start of unallocated */
vm->frames[vm->fp].registers[dest].u =
str_dest; /* store ptr of string to dest register */
char buffer[32];
int len = sprintf(buffer, "%d", a);
mem_strcpy(vm->memory, buffer, len, str_dest); /* copy buffer to dest */
vm->frames[vm->fp].allocated.end +=
((len / 4) + (len % 4) + 1); /* increment to end of allocated */
str(vm, buffer, len); /* copy buffer to dest */
return true;
}
case OP_REAL_TO_STRING: {
float a = (float)vm->frames[vm->fp].registers[src1].f; /* get value */
uint32_t str_dest = (uint32_t)vm->frames[vm->fp]
.allocated.end; /* get start of unallocated */
vm->frames[vm->fp].registers[dest].u =
str_dest; /* store ptr of string to dest register */
char buffer[32];
int len = sprintf(buffer, "%f", a);
mem_strcpy(vm->memory, buffer, len, str_dest); /* copy buffer to dest */
vm->frames[vm->fp].allocated.end +=
((len / 4) + (len % 4) + 1); /* increment to end of allocated */
str(vm, buffer, len); /* copy buffer to dest */
return true;
}
case OP_READ_STRING: {

View File

@ -5,5 +5,6 @@
VM* init_vm();
bool step_vm(VM *vm);
void str(VM *vm, const char *str, uint32_t length);
#endif