1
0
Fork 0
forked from fun/fun
fun/src/vm/core/swap.c

32 lines
740 B
C

/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*/
/**
* @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
*/
case OP_SWAP: {
vm_require_stack(vm, 2);
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;
}