2025-09-16 02:11:22 +02:00
|
|
|
/**
|
|
|
|
|
* This file is part of the Fun programming language.
|
|
|
|
|
* https://hanez.org/project/fun/
|
|
|
|
|
*
|
|
|
|
|
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
2025-09-30 22:52:12 +02:00
|
|
|
* Licensed under the terms of the Apache-2.0 license.
|
|
|
|
|
* https://opensource.org/license/apache-2-0
|
2025-09-16 02:11:22 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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
|
|
|
|
|
* on the stack.
|
|
|
|
|
*
|
|
|
|
|
* Behavior:
|
|
|
|
|
* - Swaps stack[sp] and stack[sp-1]
|
|
|
|
|
* - No type checking
|
|
|
|
|
*
|
|
|
|
|
* Error Handling:
|
|
|
|
|
* - Exits if stack underflow
|
|
|
|
|
*
|
|
|
|
|
* @author Johannes Findeisen
|
|
|
|
|
* @date 2025-10-16
|
|
|
|
|
*/
|
|
|
|
|
|
2025-09-15 06:31:32 +02:00
|
|
|
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;
|
|
|
|
|
}
|