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

Implements the OP_FIND opcode for finding 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 (hay.type !=VAL_STRING||needle.type !=VAL_STRING)
 free_value (hay)
 free_value (needle)
 push_value (vm, make_int(idx))

Variables

case OP_FIND
Value hay = pop_value(vm)
int idx = bi_find(&hay, &needle)
 break

Detailed Description

Implements the OP_FIND opcode for finding substrings in the VM.

This file handles the OP_FIND instruction, which finds the index of a substring within a string. The substring and string are popped from the stack, and the index (or -1) is pushed back.

Behavior:

  • Pops the substring and string from the stack.
  • Finds the index of the substring within the string.
  • Pushes the index (or -1 if not found) onto the stack.

Error Handling:

  • Exits with an error if the operands are not strings.

Example:

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

Definition in file find.c.

Function Documentation

◆ free_value() [1/2]

free_value ( hay )

◆ free_value() [2/2]

free_value ( needle )

◆ if()

if ( hay.type ! = VAL_STRING || needle.type != VAL_STRING)

Definition at line 35 of file find.c.

◆ push_value()

push_value ( vm ,
make_int(idx)  )

Variable Documentation

◆ break

break

Definition at line 43 of file find.c.

◆ hay

Value hay = pop_value(vm)

Definition at line 34 of file find.c.

◆ idx

int idx = bi_find(&hay, &needle)

Definition at line 39 of file find.c.

◆ OP_FIND

case OP_FIND

Definition at line 32 of file find.c.