Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
clear.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2025 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
10/**
11 * @file clear.c
12 * @brief Implements the OP_CLEAR opcode for clearing arrays in the VM.
13
14 * Handles the OP_CLEAR instruction, which removes all elements from an array.
15 * The array is popped from the stack; the opcode pushes an integer result
16 * (currently 0) as an acknowledgement.
17
18 * Behavior:
19 * - Pops the array from the stack.
20 * - Clears all elements from the array (array becomes empty in place).
21 * - Pushes 0 (integer) to acknowledge success.
22
23 * Error Handling:
24 * - Exits with a runtime error if the operand is not an array.
25
26 * Example:
27 * - Bytecode: OP_CLEAR
28 * - Stack before: [[10, 20, 30]]
29 * - Stack after: [0]
30 */
31
32case OP_CLEAR: {
33 Value arr = pop_value(vm);
34 if (arr.type != VAL_ARRAY) {
35 fprintf(stderr, "Runtime type error: CLEAR expects array\n");
36 exit(1);
37 }
41 break;
42}
void array_clear(Value *v)
Remove all elements from an array Value, freeing their contents.
Definition array_utils.c:68
@ OP_CLEAR
Definition bytecode.h:114
free_value(arr)
array_clear & arr
Definition clear.c:38
push_value(vm, make_int(0))
Tagged union representing a Fun value.
Definition value.h:68
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
@ VAL_ARRAY
Definition value.h:55
static Value pop_value(VM *vm)
Pop a Value from the VM operand stack.
Definition vm.c:580
#define fprintf
Definition vm.c:200
#define exit(code)
Definition vm.c:230