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

Implements the OP_INDEX_OF opcode for finding the index of a value in an array 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)
 free_value (arr)
 free_value (needle)
 push_value (vm, make_int(idx))

Variables

case OP_INDEX_OF
Value arr = pop_value(vm)
int idx = array_index_of(&arr, &needle)
 break

Detailed Description

Implements the OP_INDEX_OF opcode for finding the index of a value in an array in the VM.

This file handles the OP_INDEX_OF instruction, which finds the index of a value in an array. The value and array are popped from the stack, and the index (or -1) is pushed back.

Behavior:

  • Pops the value and array from the stack.
  • Finds the index of the value in the array.
  • Pushes the index (or -1 if not found) onto the stack.

Error Handling:

  • Exits with an error if the array is of the wrong type.

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

Definition in file index_of.c.

Function Documentation

◆ free_value() [1/2]

free_value(arr)

◆ free_value() [2/2]

free_value(needle)

◆ if()

if(arr.type ! = VAL_ARRAY)

Definition at line 34 of file index_of.c.

◆ push_value()

push_value(vm,
make_int(idx) )

Variable Documentation

◆ arr

Value arr = pop_value(vm)

Definition at line 33 of file index_of.c.

◆ break

break

Definition at line 42 of file index_of.c.

◆ idx

int idx = array_index_of(&arr, &needle)

Definition at line 38 of file index_of.c.

◆ OP_INDEX_OF

case OP_INDEX_OF

Definition at line 31 of file index_of.c.