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

VM opcode snippet for OP_REGEX_SEARCH (POSIX first-match search). More...

Go to the source code of this file.

Functions

 if (str.type !=VAL_STRING||pattern.type !=VAL_STRING)
void map_setm ("")
void map_setm (-1)
 free_value (pattern)
 free_value (str)
 push_value (vm, m)

Variables

case OP_REGEX_SEARCH
Value str = pop_value(vm)
Value m = make_map_empty()
Value emptyArr = make_array_from_values(NULL, 0)
 break

Detailed Description

VM opcode snippet for OP_REGEX_SEARCH (POSIX first-match search).

Finds the first match of a POSIX regular expression within an input string and returns a map with details about the match and captured groups.

Behavior (stack effects):

  • Pops: pattern (string), input (string)
  • Pushes: map with keys:
    • "match": string — the matched substring (empty if no match)
    • "start": int — start index of the match, or -1
    • "end": int — end index (exclusive) of the match, or -1
    • "groups": array<string> — captured groups (may be empty)

Platform notes:

  • On non-UNIX platforms (no POSIX regex available), returns a default map with empty match, start=-1, end=-1, and empty groups.

Errors:

  • If operands are not strings, the VM prints a runtime type error and exits.
  • If the pattern is an invalid regex, returns the default empty map.

Example:

  • pattern = "h(ell)o", input = "oh hello!" -> { match: "hello", start: 3, end: 8, groups: ["ell"] }

Definition in file regex_search.c.

Function Documentation

◆ free_value() [1/2]

free_value(pattern)

◆ free_value() [2/2]

free_value(str)

◆ if()

if(str.type ! = VAL_STRING || pattern.type != VAL_STRING)

Definition at line 47 of file regex_search.c.

◆ m() [1/2]

void map_set & m("")

◆ m() [2/2]

void map_set & m(-1)

◆ push_value()

push_value(vm,
m )

Variable Documentation

◆ break

break

Definition at line 62 of file regex_search.c.

◆ emptyArr

Value emptyArr = make_array_from_values(NULL, 0)

Definition at line 57 of file regex_search.c.

◆ m

Definition at line 53 of file regex_search.c.

◆ OP_REGEX_SEARCH

case OP_REGEX_SEARCH

Definition at line 44 of file regex_search.c.

◆ str

Value str = pop_value(vm)

Definition at line 46 of file regex_search.c.