#include "cvm_core.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int run_file(Runtime *rt, const char *path) {
if (rt_run_file(rt, path) != 0) {
fprintf(stderr, "%s\n", rt->errbuf);
return 1;
}
return 0;
}
static void repl(Runtime *rt) {
printf("cvmruntime REPL (输入 exit 退出)\n");
char buf[65536];
buf[0] = '\0';
char line[4096];
printf("cvmruntime> ");
fflush(stdout);
while (fgets(line, sizeof(line), stdin)) {
if (strcmp(line, "exit\n") == 0 || strcmp(line, "quit\n") == 0) break;
size_t bl = strlen(buf), ll = strlen(line);
if (bl + ll + 1 < sizeof(buf)) memcpy(buf + bl, line, ll + 1);
int r = rt_run(rt, buf, "repl");
if (r == 0) {
buf[0] = '\0';
printf("cvmruntime> ");
} else if (strstr(rt->errbuf, "incomplete") != NULL) {
printf("... ");
} else {
fprintf(stderr, "%s\n", rt->errbuf);
buf[0] = '\0';
printf("cvmruntime> ");
}
fflush(stdout);
}
printf("\n");
}
int main(int argc, char **argv) {
Runtime *rt = rt_new();
rt_load_ffi_allowfile(rt, "cvmruntime.ffi");
int rc = 0;
if (argc >= 2) {
for (int i = 1; i < argc; ++i) rc |= run_file(rt, argv[i]);
} else {
repl(rt);
}
rt_free(rt);
return rc;
}