31#define FUN_VERSION "0.0.0-dev"
46 printf(
" %s [--trace|-t] [--repl-on-error] [script.fun]\n", prog ? prog :
"fun");
47 printf(
" %s --help | -h\n", prog ? prog :
"fun");
48 printf(
" %s --version | -V\n", prog ? prog :
"fun");
51 printf(
" --trace, -t Print executed ops and stack tops during run\n");
52 printf(
" --repl-on-error Enter interactive REPL on runtime error with stack preserved\n\n");
53 printf(
"When no script is provided, a REPL starts. Submit an empty line to execute the buffer.\n");
55 printf(
" %s [--trace|-t] <script.fun>\n", prog ? prog :
"fun");
56 printf(
" %s --help | -h\n", prog ? prog :
"fun");
57 printf(
" %s --version | -V\n", prog ? prog :
"fun");
59 printf(
"Options:\n --trace, -t Print executed ops and stack tops during run\n\n");
60 printf(
"REPL is disabled in this build. Please provide a script file to run.\n");
75int main(
int argc,
char **argv) {
77 setenv(
"FUN_EXECUTABLE", argv[0], 1);
83 for (; argi < argc; ++argi) {
84 const char *arg = argv[argi];
85 if (strcmp(arg,
"--help") == 0 || strcmp(arg,
"-h") == 0) {
89 if (strcmp(arg,
"--version") == 0 || strcmp(arg,
"-V") == 0) {
93 if (strcmp(arg,
"--trace") == 0 || strcmp(arg,
"-t") == 0) {
98 if (strcmp(arg,
"--repl-on-error") == 0) {
110 fprintf(stderr,
"Error: REPL is disabled. Please provide a script to run.\n");
117 const char *path = argv[argi];
120 int sargi = argi + 1;
121 int sargc = (sargi < argc) ? (argc - sargi) : 0;
126 snprintf(buf,
sizeof(buf),
"%d", sargc);
127 setenv(
"FUN_ARGC", buf, 1);
131 for (
int i = 0; i < sargc; ++i) {
133 snprintf(key,
sizeof(key),
"FUN_ARGV_%d", i);
134 setenv(key, argv[sargi + i], 1);
140 for (
int i = 0; i < sargc; ++i) {
141 total += strlen(argv[sargi + i]) + 1;
143 char *joined = (
char *)malloc(total);
146 for (
int i = 0; i < sargc; ++i) {
147 strcat(joined, argv[sargi + i]);
148 if (i + 1 < sargc) strcat(joined,
" ");
150 setenv(
"FUN_ARGS", joined, 1);
155 setenv(
"FUN_ARGS",
"", 1);
160 fprintf(stderr,
"Failed to compile script: %s\n", path);
174 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.
static void print_usage(const char *prog)
Print command-line usage instructions to stdout.
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.
int fun_run_repl(VM *vm)
Run the interactive Fun REPL session.
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...