Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
set.c File Reference

Implements the OP_SET opcode for setting elements in arrays in the VM. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

 if (arr.type !=VAL_ARRAY||idx.type !=VAL_INT)
 if (!array_set(&arr,(int) idx.i, v))
 free_value (arr)
 free_value (idx)
 push_value (vm, copy_value(&v))
 free_value (v)

Variables

case OP_SET
Value idx = pop_value(vm)
Value arr = pop_value(vm)
 break

Detailed Description

Implements the OP_SET opcode for setting elements in arrays in the VM.

Handles the OP_SET instruction, which sets a value at a specified index in an array. The value, index, and array are popped from the stack; the value is returned back on the stack (as a copy) to mirror expression semantics.

Behavior:

  • Pops the value, index, and array from the stack.
  • Sets the value at the specified index in the array.
  • Pushes a copy of the value back onto the stack.

Error Handling:

  • Exits with a runtime error if the array or index is of the wrong type.
  • Exits with a runtime error if the index is out of bounds.

Example: // Bytecode: OP_SET // Stack before: [42, 1, [10, 20, 30]] // Stack after: [42]

Definition in file set.c.

Function Documentation

◆ free_value() [1/3]

free_value(arr)

◆ free_value() [2/3]

free_value(idx)

◆ free_value() [3/3]

free_value(v)

◆ if() [1/2]

if(!array_set(&, intidx.i,
v )

Definition at line 41 of file set.c.

◆ if() [2/2]

if(arr.type ! = VAL_ARRAY || idx.type != VAL_INT)

Definition at line 37 of file set.c.

◆ push_value()

push_value(vm,
copy_value &v )

Variable Documentation

◆ arr

Value arr = pop_value(vm)

Definition at line 36 of file set.c.

◆ break

break

Definition at line 50 of file set.c.

◆ idx

Value idx = pop_value(vm)

Definition at line 35 of file set.c.

◆ OP_SET

case OP_SET

Definition at line 33 of file set.c.