1
0
Fork 0
forked from fun/fun

Refactored all OP_ functions to vm/CATEGORY/ and renamed the files.

This commit is contained in:
Johannes Findeisen 2025-09-15 23:26:42 +02:00
commit 50a91d0b6f
67 changed files with 91 additions and 92 deletions

21
src/vm/io/read_file.c Normal file
View file

@ -0,0 +1,21 @@
case OP_READ_FILE: {
Value path = pop_value(vm);
if (path.type != VAL_STRING) { fprintf(stderr, "READ_FILE expects string\n"); exit(1); }
const char *p = path.s ? path.s : "";
FILE *f = fopen(p, "rb");
if (!f) { free_value(path); push_value(vm, make_string("")); break; }
if (fseek(f, 0, SEEK_END) != 0) { fclose(f); free_value(path); push_value(vm, make_string("")); break; }
long sz = ftell(f);
if (sz < 0) { fclose(f); free_value(path); push_value(vm, make_string("")); break; }
rewind(f);
char *buf = (char*)malloc((size_t)sz + 1);
size_t n = buf ? fread(buf, 1, (size_t)sz, f) : 0;
fclose(f);
if (!buf) { free_value(path); push_value(vm, make_string("")); break; }
buf[n] = '\0';
Value out = make_string(buf);
free(buf);
free_value(path);
push_value(vm, out);
break;
}

17
src/vm/io/write_file.c Normal file
View file

@ -0,0 +1,17 @@
case OP_WRITE_FILE: {
Value data = pop_value(vm);
Value path = pop_value(vm);
if (path.type != VAL_STRING || data.type != VAL_STRING) { fprintf(stderr, "WRITE_FILE expects (string, string)\n"); exit(1); }
const char *p = path.s ? path.s : "";
FILE *f = fopen(p, "wb");
int ok = 0;
if (f) {
size_t len = data.s ? strlen(data.s) : 0;
ok = (fwrite(data.s ? data.s : "", 1, len, f) == len);
fclose(f);
}
free_value(path);
free_value(data);
push_value(vm, make_int(ok ? 1 : 0));
break;
}