1
0
Fork 0
forked from fun/fun

Fixed the code style in *.c files to two spaces indentation and add a linter named funstx to Fun. (0.39.0)

This commit is contained in:
Johannes Findeisen 2026-03-18 20:52:00 +01:00
commit 03b2532474
237 changed files with 17550 additions and 13911 deletions

View file

@ -8,7 +8,7 @@
*/
/**
* @file call.c
* @file call.c
* @brief Implements the OP_CALL opcode for function calls in the VM.
*
* This file handles the OP_CALL instruction, which calls a function with arguments.
@ -28,27 +28,27 @@
*/
case OP_CALL: {
int argc = inst.operand;
if (argc < 0) argc = 0;
/* collect args in reverse (preserve order) */
Value *args = NULL;
if (argc > 0) {
args = (Value*)malloc(sizeof(Value) * argc);
/* pop args into array in reverse */
for (int i = argc - 1; i >= 0; --i) {
args[i] = pop_value(vm);
}
int argc = inst.operand;
if (argc < 0) argc = 0;
/* collect args in reverse (preserve order) */
Value *args = NULL;
if (argc > 0) {
args = (Value *)malloc(sizeof(Value) * argc);
/* pop args into array in reverse */
for (int i = argc - 1; i >= 0; --i) {
args[i] = pop_value(vm);
}
/* pop function value */
Value fnv = pop_value(vm);
if (fnv.type != VAL_FUNCTION) {
fprintf(stderr, "Runtime type error: CALL expects function\n");
exit(1);
}
/* push new frame and transfer args */
vm_push_frame(vm, fnv.fn, argc, args);
/* free args array (locals moved), free fnv (no-op for function) */
free(args);
/* note: fnv contains a pointer to the Bytecode, don't free here */
break;
}
/* pop function value */
Value fnv = pop_value(vm);
if (fnv.type != VAL_FUNCTION) {
fprintf(stderr, "Runtime type error: CALL expects function\n");
exit(1);
}
/* push new frame and transfer args */
vm_push_frame(vm, fnv.fn, argc, args);
/* free args array (locals moved), free fnv (no-op for function) */
free(args);
/* note: fnv contains a pointer to the Bytecode, don't free here */
break;
}

View file

@ -8,7 +8,7 @@
*/
/**
* @file dup.c
* @file dup.c
* @brief Implements the OP_DUP opcode for duplicating the top stack value in the VM.
*
* This file handles the OP_DUP instruction, which duplicates the top value on the stack.
@ -25,17 +25,17 @@
* // Bytecode: OP_DUP
* // Stack before: [42]
* // Stack after: [42, 42]
*
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_DUP: {
if (vm->sp < 0) {
fprintf(stderr, "Runtime error: stack underflow for DUP\n");
exit(1);
}
Value top = vm->stack[vm->sp];
push_value(vm, copy_value(&top));
break;
if (vm->sp < 0) {
fprintf(stderr, "Runtime error: stack underflow for DUP\n");
exit(1);
}
Value top = vm->stack[vm->sp];
push_value(vm, copy_value(&top));
break;
}

View file

@ -10,7 +10,7 @@
*/
/**
* @file exit.c
* @file exit.c
* @brief Implements the OP_EXIT opcode to terminate the script with an exit code.
*
* Behavior:
@ -20,22 +20,22 @@
*/
case OP_EXIT: {
int code = 0;
if (vm->sp >= 0) {
Value v = pop_value(vm);
if (v.type == VAL_INT) {
code = (int)v.i;
} else if (v.type == VAL_STRING) {
/* best-effort parse number from string */
code = (int)strtoll(v.s, NULL, 10);
} else if (v.type == VAL_NIL) {
code = 0;
} else {
/* unsupported type for exit; default to 0 */
code = 0;
}
free_value(v);
int code = 0;
if (vm->sp >= 0) {
Value v = pop_value(vm);
if (v.type == VAL_INT) {
code = (int)v.i;
} else if (v.type == VAL_STRING) {
/* best-effort parse number from string */
code = (int)strtoll(v.s, NULL, 10);
} else if (v.type == VAL_NIL) {
code = 0;
} else {
/* unsupported type for exit; default to 0 */
code = 0;
}
vm->exit_code = code;
return;
free_value(v);
}
vm->exit_code = code;
return;
}

View file

@ -8,7 +8,7 @@
*/
/**
* @file halt.c
* @file halt.c
* @brief Implements the OP_HALT opcode for stopping VM execution.
*
* This file handles the OP_HALT instruction, which stops the execution of the VM.
@ -27,4 +27,4 @@
*/
case OP_HALT:
return;
return;

View file

@ -8,7 +8,7 @@
*/
/**
* @file jump.c
* @file jump.c
* @brief Implements the OP_JUMP opcode for unconditional jumps in the VM.
*
* This file handles the OP_JUMP instruction, which performs an unconditional
@ -28,6 +28,6 @@
*/
case OP_JUMP: {
f->ip = inst.operand;
break;
f->ip = inst.operand;
break;
}

View file

@ -8,7 +8,7 @@
*/
/**
* @file jump_if_false.c
* @file jump_if_false.c
* @brief Implements the OP_JUMP_IF_FALSE opcode for conditional jumps in the VM.
*
* This file handles the OP_JUMP_IF_FALSE instruction, which jumps if the top
@ -29,11 +29,11 @@
*/
case OP_JUMP_IF_FALSE: {
Value cond = pop_value(vm);
int truthy = value_is_truthy(&cond);
free_value(cond);
if (!truthy) {
f->ip = inst.operand;
}
break;
Value cond = pop_value(vm);
int truthy = value_is_truthy(&cond);
free_value(cond);
if (!truthy) {
f->ip = inst.operand;
}
break;
}

View file

@ -8,7 +8,7 @@
*/
/**
* @file load_const.c
* @file load_const.c
* @brief Implements the OP_LOAD_CONST opcode for loading constants in the VM.
*
* This file handles the OP_LOAD_CONST instruction, which loads a constant value
@ -26,12 +26,12 @@
*/
case OP_LOAD_CONST: {
int idx = inst.operand;
if (idx < 0 || idx >= f->fn->const_count) {
fprintf(stderr, "Runtime error: constant index out of range\n");
exit(1);
}
Value c = copy_value(&f->fn->constants[idx]);
push_value(vm, c);
break;
int idx = inst.operand;
if (idx < 0 || idx >= f->fn->const_count) {
fprintf(stderr, "Runtime error: constant index out of range\n");
exit(1);
}
Value c = copy_value(&f->fn->constants[idx]);
push_value(vm, c);
break;
}

View file

@ -8,7 +8,7 @@
*/
/**
* @file load_global.c
* @file load_global.c
* @brief Implements the OP_LOAD_GLOBAL opcode for loading global variables in the VM.
*
* This file handles the OP_LOAD_GLOBAL instruction, which loads a global variable
@ -31,14 +31,14 @@
*/
case OP_LOAD_GLOBAL: {
int idx = inst.operand;
if (idx < 0 || idx >= MAX_GLOBALS) {
fprintf(stderr, "Runtime error: global index out of range\n");
exit(1);
}
int idx = inst.operand;
if (idx < 0 || idx >= MAX_GLOBALS) {
fprintf(stderr, "Runtime error: global index out of range\n");
exit(1);
}
#ifdef FUN_DEBUG
fprintf(stderr, "DEBUG LOAD_GLOBAL[%d]: type=%d\n", idx, vm->globals[idx].type);
fprintf(stderr, "DEBUG LOAD_GLOBAL[%d]: type=%d\n", idx, vm->globals[idx].type);
#endif
push_value(vm, copy_value(&vm->globals[idx]));
break;
push_value(vm, copy_value(&vm->globals[idx]));
break;
}

View file

@ -8,7 +8,7 @@
*/
/**
* @file load_local.c
* @file load_local.c
* @brief Implements the OP_LOAD_LOCAL opcode for loading local variables in the VM.
*
* This file handles the OP_LOAD_LOCAL instruction, which loads a local variable
@ -31,12 +31,12 @@
*/
case OP_LOAD_LOCAL: {
int slot = inst.operand;
if (slot < 0 || slot >= MAX_FRAME_LOCALS) {
fprintf(stderr, "Runtime error: local slot out of range\n");
exit(1);
}
Value val = copy_value(&f->locals[slot]);
push_value(vm, val);
break;
int slot = inst.operand;
if (slot < 0 || slot >= MAX_FRAME_LOCALS) {
fprintf(stderr, "Runtime error: local slot out of range\n");
exit(1);
}
Value val = copy_value(&f->locals[slot]);
push_value(vm, val);
break;
}

View file

@ -8,7 +8,7 @@
*/
/**
* @file nop.c
* @file nop.c
* @brief Implements the OP_NOP opcode for no operation in the VM.
*
* This file handles the OP_NOP instruction, which performs no operation.
@ -27,4 +27,4 @@
*/
case OP_NOP:
break;
break;

View file

@ -8,7 +8,7 @@
*/
/**
* @file pop.c
* @file pop.c
* @brief Implements the OP_POP opcode for removing the top stack value in the VM.
*
* This file handles the OP_POP instruction, which removes the top value from the stack.
@ -29,11 +29,11 @@
*/
case OP_POP: {
if (vm->sp < 0) {
fprintf(stderr, "Runtime error: stack underflow for POP\n");
exit(1);
}
Value v = pop_value(vm);
free_value(v);
break;
if (vm->sp < 0) {
fprintf(stderr, "Runtime error: stack underflow for POP\n");
exit(1);
}
Value v = pop_value(vm);
free_value(v);
break;
}

View file

@ -8,7 +8,7 @@
*/
/**
* @file return.c
* @file return.c
* @brief Implements the OP_RETURN opcode for returning from a function in the VM.
*
* This file handles the OP_RETURN instruction, which returns from the current function
@ -32,10 +32,12 @@
*/
case OP_RETURN: {
Value retv;
if (vm->sp >= 0) retv = pop_value(vm);
else retv = make_nil();
vm_pop_frame(vm);
push_value(vm, retv);
break;
Value retv;
if (vm->sp >= 0)
retv = pop_value(vm);
else
retv = make_nil();
vm_pop_frame(vm);
push_value(vm, retv);
break;
}

View file

@ -8,7 +8,7 @@
*/
/**
* @file store_global.c
* @file store_global.c
* @brief Implements the OP_STORE_GLOBAL opcode for storing global variables in the VM.
*
* This file handles the OP_STORE_GLOBAL instruction, which stores a value into a global variable
@ -31,16 +31,16 @@
*/
case OP_STORE_GLOBAL: {
int idx = inst.operand;
if (idx < 0 || idx >= MAX_GLOBALS) {
fprintf(stderr, "Runtime error: global index out of range\n");
exit(1);
}
Value v = pop_value(vm);
int idx = inst.operand;
if (idx < 0 || idx >= MAX_GLOBALS) {
fprintf(stderr, "Runtime error: global index out of range\n");
exit(1);
}
Value v = pop_value(vm);
#ifdef FUN_DEBUG
fprintf(stderr, "DEBUG STORE_GLOBAL[%d]: new.type=%d\n", idx, v.type);
fprintf(stderr, "DEBUG STORE_GLOBAL[%d]: new.type=%d\n", idx, v.type);
#endif
free_value(vm->globals[idx]);
vm->globals[idx] = v;
break;
free_value(vm->globals[idx]);
vm->globals[idx] = v;
break;
}

View file

@ -8,7 +8,7 @@
*/
/**
* @file store_local.c
* @file store_local.c
* @brief Implements the OP_STORE_LOCAL opcode for storing local variables in the VM.
*
* This file handles the OP_STORE_LOCAL instruction, which stores a value into a local variable
@ -31,14 +31,14 @@
*/
case OP_STORE_LOCAL: {
int slot = inst.operand;
if (slot < 0 || slot >= MAX_FRAME_LOCALS) {
fprintf(stderr, "Runtime error: local slot out of range\n");
exit(1);
}
Value v = pop_value(vm);
/* free previous local then move v into it */
free_value(f->locals[slot]);
f->locals[slot] = v;
break;
int slot = inst.operand;
if (slot < 0 || slot >= MAX_FRAME_LOCALS) {
fprintf(stderr, "Runtime error: local slot out of range\n");
exit(1);
}
Value v = pop_value(vm);
/* free previous local then move v into it */
free_value(f->locals[slot]);
f->locals[slot] = v;
break;
}

View file

@ -8,7 +8,7 @@
*/
/**
* @file swap.c
* @file swap.c
* @brief Implements the OP_SWAP opcode for stack manipulation in the VM.
*
* This file handles the OP_SWAP instruction, which swaps the top two values
@ -26,13 +26,13 @@
*/
case OP_SWAP: {
if (vm->sp < 1) {
fprintf(stderr, "Runtime error: stack underflow for SWAP\n");
exit(1);
}
Value a = vm->stack[vm->sp];
Value b = vm->stack[vm->sp - 1];
vm->stack[vm->sp] = b;
vm->stack[vm->sp - 1] = a;
break;
if (vm->sp < 1) {
fprintf(stderr, "Runtime error: stack underflow for SWAP\n");
exit(1);
}
Value a = vm->stack[vm->sp];
Value b = vm->stack[vm->sp - 1];
vm->stack[vm->sp] = b;
vm->stack[vm->sp - 1] = a;
break;
}

View file

@ -8,26 +8,26 @@
*/
case OP_THROW: {
Value err = pop_value(vm);
/* if there is a handler in this frame, jump to it and push err for catch */
if (f->try_sp >= 0) {
int try_idx = f->try_stack[f->try_sp--];
int target = f->fn->instructions[try_idx].operand;
/* push error for catch block */
push_value(vm, err); /* transfer ownership to stack */
f->ip = target;
break;
}
/* Unhandled: print error and terminate */
char *s = value_to_string_alloc(&err);
if (s) {
fprintf(stdout, "%s\n", s);
free(s);
} else {
fprintf(stdout, "<error>\n");
}
free_value(err);
/* clear frames to stop execution */
vm->fp = -1;
Value err = pop_value(vm);
/* if there is a handler in this frame, jump to it and push err for catch */
if (f->try_sp >= 0) {
int try_idx = f->try_stack[f->try_sp--];
int target = f->fn->instructions[try_idx].operand;
/* push error for catch block */
push_value(vm, err); /* transfer ownership to stack */
f->ip = target;
break;
}
/* Unhandled: print error and terminate */
char *s = value_to_string_alloc(&err);
if (s) {
fprintf(stdout, "%s\n", s);
free(s);
} else {
fprintf(stdout, "<error>\n");
}
free_value(err);
/* clear frames to stop execution */
vm->fp = -1;
break;
}

View file

@ -8,6 +8,6 @@
*/
case OP_TRY_POP: {
if (f->try_sp >= 0) f->try_sp--;
break;
if (f->try_sp >= 0) f->try_sp--;
break;
}

View file

@ -8,11 +8,11 @@
*/
case OP_TRY_PUSH: {
/* push index of this TRY instruction; handler ip is in its operand (may be patched later) */
if (f->try_sp >= (int)(sizeof(f->try_stack)/sizeof(f->try_stack[0])) - 1) {
fprintf(stderr, "Runtime error: try depth exceeded\n");
exit(1);
}
f->try_stack[++f->try_sp] = f->ip - 1; /* index of TRY_PUSH instruction */
break;
/* push index of this TRY instruction; handler ip is in its operand (may be patched later) */
if (f->try_sp >= (int)(sizeof(f->try_stack) / sizeof(f->try_stack[0])) - 1) {
fprintf(stderr, "Runtime error: try depth exceeded\n");
exit(1);
}
f->try_stack[++f->try_sp] = f->ip - 1; /* index of TRY_PUSH instruction */
break;
}