Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
substr.c File Reference

Implements the OP_SUBSTR opcode for extracting substrings 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 (str.type !=VAL_STRING||startv.type !=VAL_INT||lenv.type !=VAL_INT)
 free_value (str)
 free_value (startv)
 free_value (lenv)
 push_value (vm, out)

Variables

case OP_SUBSTR
Value startv = pop_value(vm)
Value str = pop_value(vm)
Value out = bi_substr(&str, (int)startv.i, (int)lenv.i)
 break

Detailed Description

Implements the OP_SUBSTR opcode for extracting substrings in the VM.

This file handles the OP_SUBSTR instruction, which extracts a substring from a string using a start index and length. The length, start index, and string are popped from the stack, and the resulting substring is pushed back.

Behavior:

  • Pops the length, start index, and string from the stack.
  • Extracts the substring from the string.
  • Pushes the resulting substring onto the stack.

Error Handling:

  • Exits with an error if the operands are not integers or strings.
  • Exits with an error if the start index or length is out of bounds.

Example:

  • Bytecode: OP_SUBSTR
  • Stack before: [5, 6, "hello world"]
  • Stack after: ["world"]

Definition in file substr.c.

Function Documentation

◆ free_value() [1/3]

free_value ( lenv )

◆ free_value() [2/3]

free_value ( startv )

◆ free_value() [3/3]

free_value ( str )

◆ if()

if ( str.type ! = VAL_STRING || startv.type != VAL_INT || lenv.type != VAL_INT)

Definition at line 37 of file substr.c.

◆ push_value()

push_value ( vm ,
out  )

Variable Documentation

◆ break

break

Definition at line 46 of file substr.c.

◆ OP_SUBSTR

case OP_SUBSTR

Definition at line 33 of file substr.c.

◆ out

Value out = bi_substr(&str, (int)startv.i, (int)lenv.i)

Definition at line 41 of file substr.c.

◆ startv

Value startv = pop_value(vm)

Definition at line 35 of file substr.c.

◆ str

Value str = pop_value(vm)

Definition at line 36 of file substr.c.