Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
regex_match.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2025 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
34/* Regex full-match opcode using POSIX regex */
35#ifdef __unix__
36#include <regex.h>
37#endif
38
40 Value pattern = pop_value(vm);
41 Value str = pop_value(vm);
42 if (str.type != VAL_STRING || pattern.type != VAL_STRING) {
43 fprintf(stderr, "Runtime type error: REGEX_MATCH expects (string, string)\n");
44 exit(1);
45 }
46#ifndef __unix__
47 /* Not supported on non-UNIX: return 0 gracefully */
49 int truth = 0;
52 break;
53#else
54 regex_t rx;
55 int rc = regcomp(&rx, pattern.s ? pattern.s : "", REG_EXTENDED);
56 if (rc != 0) {
57 /* invalid regex -> false */
60 push_value(vm, make_int(0));
61 break;
62 }
63 regmatch_t m;
64 int ok = regexec(&rx, str.s ? str.s : "", 1, &m, 0) == 0;
65 int truth = 0;
66 if (ok) {
67 /* full match means the match spans whole string */
68 if (m.rm_so == 0 && str.s) {
69 size_t slen = strlen(str.s);
70 truth = (m.rm_eo == (regoff_t)slen) ? 1 : 0;
71 }
72 }
73 regfree(&rx);
77 break;
78#endif
79}
@ OP_REGEX_MATCH
Definition bytecode.h:107
int ok
Definition contains.c:38
int rc
Value m
Definition has_key.c:27
free_value(pattern)
Value str
Definition regex_match.c:41
push_value(vm, make_int(truth))
int truth
Definition regex_match.c:49
Value pattern
Tagged union representing a Fun value.
Definition value.h:68
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
@ VAL_STRING
Definition value.h:53
#define fprintf
Definition vm.c:200
#define exit(code)
Definition vm.c:230