32#define FUN_VERSION "0.0.0-dev" 43static void print_usage(
const char *prog) {
47 printf(
" %s [--trace|-t] [--repl-on-error] [script.fun]\n", prog ? prog :
"fun");
48 printf(
" %s --help | -h\n", prog ? prog :
"fun");
49 printf(
" %s --version | -V\n", prog ? prog :
"fun");
52 printf(
" --trace, -t Print executed ops and stack tops during run\n");
53 printf(
" --repl-on-error Enter interactive REPL on runtime error with stack preserved\n\n");
54 printf(
"When no script is provided, a REPL starts. Submit an empty line to execute the buffer.\n");
56 printf(
" %s [--trace|-t] <script.fun>\n", prog ? prog :
"fun");
57 printf(
" %s --help | -h\n", prog ? prog :
"fun");
58 printf(
" %s --version | -V\n", prog ? prog :
"fun");
60 printf(
"Options:\n --trace, -t Print executed ops and stack tops during run\n\n");
61 printf(
"REPL is disabled in this build. Please provide a script file to run.\n");
76int main(
int argc,
char **argv) {
78 setenv(
"FUN_EXECUTABLE", argv[0], 1);
84 for (; argi < argc; ++argi) {
85 const char *arg = argv[argi];
86 if (strcmp(arg,
"--help") == 0 || strcmp(arg,
"-h") == 0) {
90 if (strcmp(arg,
"--version") == 0 || strcmp(arg,
"-V") == 0) {
94 if (strcmp(arg,
"--trace") == 0 || strcmp(arg,
"-t") == 0) {
99 if (strcmp(arg,
"--repl-on-error") == 0) {
111 fprintf(stderr,
"Error: REPL is disabled. Please provide a script to run.\n");
112 print_usage(argv[0]);
118 const char *
path = argv[argi];
121 int sargi = argi + 1;
122 int sargc = (sargi < argc) ? (argc - sargi) : 0;
127 snprintf(
buf,
sizeof(
buf),
"%d", sargc);
128 setenv(
"FUN_ARGC",
buf, 1);
132 for (
int i = 0; i < sargc; ++i) {
134 snprintf(key,
sizeof(key),
"FUN_ARGV_%d", i);
135 setenv(key, argv[sargi + i], 1);
141 for (
int i = 0; i < sargc; ++i) {
142 total += strlen(argv[sargi + i]) + 1;
144 char *joined = (
char *)malloc(total);
147 for (
int i = 0; i < sargc; ++i) {
148 strcat(joined, argv[sargi + i]);
149 if (i + 1 < sargc) strcat(joined,
" ");
151 setenv(
"FUN_ARGS", joined, 1);
156 setenv(
"FUN_ARGS",
"", 1);
161 fprintf(stderr,
"Failed to compile script: %s\n",
path);
173 return fun_run_repl(&vm);
175 fprintf(stderr,
"Internal error: REPL not available in this build.\n");
void bytecode_free(Bytecode *bc)
Free a Bytecode and all memory it owns.
Definitions for the Fun VM bytecode: opcodes, instruction format, and bytecode container API.
int main(void)
Build and execute a demo bytecode program and print results.
Bytecode * parse_file_to_bytecode(const char *path)
Parse a .fun source file and return compiled bytecode.
Public API for parsing Fun source into bytecode.
Interactive Read-Eval-Print Loop (REPL) entry point.
The Fun virtual machine state.
int(* on_error_repl)(struct VM *vm)
void vm_init(VM *vm)
Initialize a VM instance to its default state.
void vm_clear_output(VM *vm)
Clear the VM's buffered output values and partial flags.
void vm_print_output(VM *vm)
Print the VM's buffered output values to stdout.
Core virtual machine data structures and public VM API.
void vm_run(VM *vm, Bytecode *entry)
Execute the provided entry bytecode in the VM. Pushes an initial frame and runs until HALT or an unre...