Skip to content

Commit 2793c6d

Browse files
Added regular expressions back
1 parent 3f4ae73 commit 2793c6d

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

src/builtins.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
{.name="join", .calltype=call, .argc=2, .args={string, string}, .returns=true, .rettype=string},
5656
{.name="string-length", .calltype=call, .argc=1, .args={string}, .returns=true, .rettype=number},
5757
{.name="substring", .calltype=call, .argc=3, .args={number, number, string}, .returns=true, .rettype=string},
58-
//{.name="regex", .calltype=call, .argc=1, .args={string}, .returns=true, .rettype=block},
58+
{.name="regex", .calltype=call, .argc=1, .args={string}, .returns=true, .rettype=block},
5959
{.name="ordinal", .calltype=call, .argc=1, .args={string}, .returns=true, .rettype=number},
6060
{.name="character", .calltype=call, .argc=1, .args={number}, .returns=true, .rettype=string},
6161
{.name="split", .calltype=call, .argc=2, .args={string, string}, .returns=true, .rettype=list},

src/runtime.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <stdint.h>
2222
#include <sys/stat.h>
2323
#include <setjmp.h>
24+
#include <regex.h>
2425

2526
#define WORDSZ (sizeof(void*))
2627

@@ -288,7 +289,7 @@ static LIST ___stack(void);
288289
static LIST ___parameters(void);
289290
static void ___stop(void);
290291
static STRING ___show(ANY);
291-
//static BLOCK ___regex(STRING);
292+
static BLOCK ___regex(STRING);
292293
static NUMBER ___ordinal(STRING);
293294
static STRING ___character(NUMBER);
294295
static NUMBER ___floor(NUMBER);
@@ -1412,12 +1413,20 @@ static void ___stop(void)
14121413
exit(EXIT_SUCCESS);
14131414
}
14141415

1415-
/*
1416+
void apply_regex(void* env)
1417+
{
1418+
regex_t reg = *(regex_t*)env;
1419+
STRING str = unbox_STRING(pop());
1420+
const int found = regexec(&reg, str, 0, NULL, 0);
1421+
if unlikely(found != 0 && found != REG_NOMATCH)
1422+
throw_error_fmt("Regex failed matching string '%.32s'", str);
1423+
push(box_BOOLEAN(!found));
1424+
1425+
}
1426+
14161427
static BLOCK ___regex(STRING reg_str)
14171428
{
14181429
regex_t reg;
1419-
if unlikely(!*reg_str) // Empty string always matches.
1420-
return Block_copy(^{ unbox_STRING(pop()); push(box_BOOLEAN(1)); });
14211430
const int status = regcomp(&reg, reg_str, REG_EXTENDED | REG_NEWLINE | REG_NOSUB);
14221431
errno = 0; // Hmmm
14231432
if unlikely(status)
@@ -1426,15 +1435,10 @@ static BLOCK ___regex(STRING reg_str)
14261435
regerror(status, &reg, reg_err, 256);
14271436
throw_error_fmt("Compile error (%s) in regex '%.32s'", reg_err, reg_str);
14281437
}
1429-
return Block_copy(^{
1430-
STRING str = unbox_STRING(pop());
1431-
const int found = regexec(&reg, str, 0, NULL, 0);
1432-
if unlikely(found != 0 && found != REG_NOMATCH)
1433-
throw_error_fmt("Match error with regex '%.32s' on string '%.32s'", str, reg_str);
1434-
push(box_BOOLEAN(!found));
1435-
});
1438+
1439+
return (cognate_block){ .env = &reg, .fn = apply_regex };
14361440
}
1437-
*/
1441+
14381442

14391443
static NUMBER ___ordinal(STRING str)
14401444
{

tests/regex.cog

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
~~ This simple regex matches hexadecimal integers.
22
~~ ^ and $ are needed else it will only match part of the string.
3-
~
3+
44
Let R1 be "^[0-9A-Fa-f]*$";
5-
Print If Both Do Regex R1 "12EFab" and Not Do Regex R1 "foo123"
5+
Print If And Do Regex R1 "12EFab" and Not Do Regex R1 "foo123"
66
"PASS: Simple regex to identify hex integers"
77
else
88
"FAIL: Simple regex to identify hex integers";
99

1010
~~ Same regex as before; but with POSIX character classes
1111
Let R2 be "^[[:xdigit:]]*$";
12-
Print If Both Do Regex R2 "12EFab" and Not Do Regex R2 "foo123"
12+
Print If And Do Regex R2 "12EFab" and Not Do Regex R2 "foo123"
1313
"PASS: Regex to identify hex integers (character class)"
1414
else
1515
"FAIL: Regex to identify hex integers (character class)";
1616

1717
~~ This regex matches decimal numbers (d.p can be dot or comma)
1818
Let R3 be "^(-)?([0-9]+)((;|.)([0-9]+))?$";
19-
Print If Both Do Regex R3 "1.234" and Not Do Regex R3 "Hello world"
19+
Print If And Do Regex R3 "1.234" and Not Do Regex R3 "Hello world"
2020
"PASS: Extended regex to identify decimal numbers"
2121
else
2222
"FAIL: Extended regex to identify decimal numbers";
23-
~

0 commit comments

Comments
 (0)