Skip to content

Commit 163b4d8

Browse files
authored
Refactor proxpl_api.c for file handling and stubbing
Updated proxpl_api.c to include file reading capabilities and modified function implementations to handle source code strings instead of raw chunks. Stubbed out features for writing and reading chunks from files.
1 parent 6c366bc commit 163b4d8

File tree

1 file changed

+63
-18
lines changed

1 file changed

+63
-18
lines changed

src/proxpl_api.c

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,83 @@
1-
/* proxpl_api.c - public API wrappers for stable ABI
2-
* Exposes a minimal C-style API that callers can use to embed runtime.
3-
*/
1+
/* proxpl_api.c - public API wrappers for stable ABI */
42

5-
#include "proxpl_api.h"
6-
#include "vm.h"
7-
#include "chunk.h"
3+
#include <stdio.h>
84
#include <stdlib.h>
5+
#include <string.h>
6+
7+
#include "../include/proxpl_api.h"
8+
#include "../include/vm.h"
9+
#include "../include/chunk.h"
10+
#include "../include/common.h"
11+
12+
// Helper to read a file into a string
13+
static char* readFile(const char* path) {
14+
FILE* file = fopen(path, "rb");
15+
if (file == NULL) {
16+
fprintf(stderr, "Could not open file \"%s\".\n", path);
17+
return NULL;
18+
}
19+
20+
fseek(file, 0L, SEEK_END);
21+
size_t fileSize = ftell(file);
22+
rewind(file);
23+
24+
char* buffer = (char*)malloc(fileSize + 1);
25+
if (buffer == NULL) {
26+
fprintf(stderr, "Not enough memory to read \"%s\".\n", path);
27+
fclose(file);
28+
return NULL;
29+
}
30+
31+
size_t bytesRead = fread(buffer, sizeof(char), fileSize, file);
32+
if (bytesRead < fileSize) {
33+
fprintf(stderr, "Could not read file \"%s\".\n", path);
34+
free(buffer);
35+
fclose(file);
36+
return NULL;
37+
}
38+
39+
buffer[bytesRead] = '\0';
40+
fclose(file);
41+
return buffer;
42+
}
943

1044
void proxpl_vm_init(VM *vm) {
11-
initVM(vm);
45+
// Standard CLOX uses a global VM instance, so we ignore the pointer for now
46+
// or assume 'vm' points to the global one.
47+
initVM();
1248
}
1349

1450
void proxpl_vm_free(VM *vm) {
15-
freeVM(vm);
51+
freeVM();
1652
}
1753

1854
InterpretResult proxpl_interpret_chunk(VM *vm, const Chunk *chunk) {
19-
// interpret expects a mutable chunk; cast away const as the API currently
20-
return interpret(vm, (Chunk *)chunk);
55+
// ERROR: The standard VM interprets Source Code strings, not raw Chunks.
56+
// To support this, you would need to expose the internal run() function.
57+
fprintf(stderr, "API Error: interpreting raw chunks is not supported in this version.\n");
58+
return INTERPRET_RUNTIME_ERROR;
2159
}
2260

2361
InterpretResult proxpl_interpret_file(VM *vm, const char *path) {
24-
// Helper: read chunk and interpret
25-
Chunk c;
26-
if (read_chunk_from_file(path, &c) != 0) return INTERPRET_COMPILE_ERROR;
27-
InterpretResult r = interpret(vm, &c);
28-
chunk_free(&c);
29-
return r;
62+
char* source = readFile(path);
63+
if (source == NULL) return INTERPRET_COMPILE_ERROR;
64+
65+
// Pass the source string to interpret
66+
InterpretResult result = interpret(source);
67+
68+
free(source);
69+
return result;
3070
}
3171

72+
// These functions are STUBBED because bytecode.c was deleted.
73+
// You must re-implement binary serialization later if you need it.
3274
int proxpl_write_chunk_to_file(const char *path, const Chunk *chunk) {
33-
return write_chunk_to_file(path, chunk);
75+
fprintf(stderr, "Feature 'write_chunk_to_file' is temporarily disabled.\n");
76+
return -1;
3477
}
3578

3679
int proxpl_read_chunk_from_file(const char *path, Chunk *out) {
37-
return read_chunk_from_file(path, out);
80+
fprintf(stderr, "Feature 'read_chunk_from_file' is temporarily disabled.\n");
81+
return -1;
3882
}
83+

0 commit comments

Comments
 (0)