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

Implements the OP_INSERT opcode for inserting elements into 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 (n< 0)
 free_value (arr)
 free_value (idx)
 push_value (vm, make_int(n))

Variables

case OP_INSERT
Value idx = pop_value(vm)
Value arr = pop_value(vm)
int n = array_insert(&arr, (int)idx.i, v)
 break

Detailed Description

Implements the OP_INSERT opcode for inserting elements into arrays in the VM.

This file handles the OP_INSERT instruction, which inserts a value into an array at a specified index. The array, index, and value are popped from the stack, and the new length of the array is pushed back onto the stack.

Behavior:

  • Pops the value, index, and array from the stack.
  • Inserts the value into the array at the specified index.
  • Pushes the new length of the array onto the stack.

Error Handling:

  • Exits with an error if the array or index is of the wrong type.
  • Exits with an error if memory allocation fails during the insertion.

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

Definition in file insert.c.

Function Documentation

◆ free_value() [1/2]

free_value(arr)

◆ free_value() [2/2]

free_value(idx)

◆ if() [1/2]

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

Definition at line 37 of file insert.c.

◆ if() [2/2]

if()

Definition at line 42 of file insert.c.

◆ push_value()

push_value(vm,
make_int(n) )

Variable Documentation

◆ arr

Value arr = pop_value(vm)

Definition at line 36 of file insert.c.

◆ break

break

Definition at line 49 of file insert.c.

◆ idx

Value idx = pop_value(vm)

Definition at line 35 of file insert.c.

◆ n

int n = array_insert(&arr, (int)idx.i, v)

Definition at line 41 of file insert.c.

◆ OP_INSERT

case OP_INSERT

Definition at line 33 of file insert.c.