1
0
Fork 0
forked from fun/fun

Some more opcode refactoring. (0.3.1)

This commit is contained in:
Johannes Findeisen 2025-09-16 04:51:49 +02:00
commit 228615db18
12 changed files with 42 additions and 59 deletions

49
src/vm/arrays/apop.c Normal file
View file

@ -0,0 +1,49 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file apop.c
* @brief Implements the OP_APOP opcode for removing elements from arrays in the VM.
*
* This file handles the OP_APOP instruction, which removes the last element from an array
* and pushes the removed element onto the stack. The array is popped from the stack.
*
* Behavior:
* - Pops the array from the stack.
* - Removes the last element from the array.
* - Pushes the removed element onto the stack.
*
* Error Handling:
* - Exits with an error if the array is of the wrong type.
* - Exits with an error if the array is empty.
*
* Example:
* // Bytecode: OP_APOP
* // Stack before: [[10, 20, 30]]
* // Stack after: [30]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_APOP: {
Value arr = pop_value(vm);
if (arr.type != VAL_ARRAY) {
fprintf(stderr, "Runtime type error: ARR_APOP expects array\n");
exit(1);
}
Value out;
if (!array_pop(&arr, &out)) {
fprintf(stderr, "Runtime error: pop from empty array\n");
exit(1);
}
free_value(arr);
push_value(vm, out);
break;
}