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

Implements the OP_SLICE opcode for array slicing 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||start.type !=VAL_INT||end.type !=VAL_INT)
 free_value (arr)
 free_value (start)
 free_value (end)
 push_value (vm, out)

Variables

case OP_SLICE
Value start = pop_value(vm)
Value arr = pop_value(vm)
Value out = array_slice(&arr, (int)start.i, (int)end.i)
 break

Detailed Description

Implements the OP_SLICE opcode for array slicing in the VM.

This file handles the OP_SLICE instruction, which creates a new array containing elements from the original array between specified start and end indices.

Behavior:

  • Pops end index, start index, and array from the stack
  • Creates a new array containing elements from start to end-1
  • Pushes the new array onto the stack

Error Handling:

  • Exits with error if arguments are wrong types
  • Handles negative indices and out-of-bounds cases gracefully

Example: // Bytecode: OP_SLICE // Stack before: [3, 1, [10,20,30,40]] // Stack after: [[20,30]]

Definition in file slice.c.

Function Documentation

◆ free_value() [1/3]

free_value(arr)

◆ free_value() [2/3]

free_value(end)

◆ free_value() [3/3]

free_value(start)

◆ if()

if(arr.type ! = VAL_ARRAY || start.type != VAL_INT || end.type != VAL_INT)

Definition at line 36 of file slice.c.

◆ push_value()

push_value(vm,
out )

Variable Documentation

◆ arr

Value arr = pop_value(vm)

Definition at line 35 of file slice.c.

◆ break

break

Definition at line 45 of file slice.c.

◆ OP_SLICE

case OP_SLICE

Definition at line 32 of file slice.c.

◆ out

Value out = array_slice(&arr, (int)start.i, (int)end.i)

Definition at line 40 of file slice.c.

◆ start

Value start = pop_value(vm)

Definition at line 34 of file slice.c.