mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-11 05:25:28 +03:00
core: rename string and pattern types
This commit is contained in:
parent
fc693d9ba8
commit
a55eeb98a5
@ -54,7 +54,7 @@ SDL_Keycode Keycode_copy(SDL_Keycode* a) {
|
||||
return *a;
|
||||
}
|
||||
|
||||
string Keycode_str(SDL_Keycode a) {
|
||||
String Keycode_str(SDL_Keycode a) {
|
||||
char *buffer = CARP_MALLOC(32);
|
||||
snprintf(buffer, 32, "%d", a);
|
||||
return buffer;
|
||||
|
@ -3,20 +3,20 @@
|
||||
|
||||
#include <carp_string.h>
|
||||
|
||||
void IO_println(string *s) { puts(*s); }
|
||||
void IO_print(string *s) { printf("%s", *s); }
|
||||
void IO_println(String *s) { puts(*s); }
|
||||
void IO_print(String *s) { printf("%s", *s); }
|
||||
|
||||
#ifndef _WIN32
|
||||
string IO_get_MINUS_line() {
|
||||
String IO_get_MINUS_line() {
|
||||
size_t size = 1024;
|
||||
string buffer = CARP_MALLOC(size);
|
||||
String buffer = CARP_MALLOC(size);
|
||||
getline(&buffer, &size, stdin);
|
||||
return buffer;
|
||||
}
|
||||
#endif
|
||||
|
||||
string IO_read_MINUS_file(string *filename) {
|
||||
string buffer = 0;
|
||||
String IO_read_MINUS_file(String *filename) {
|
||||
String buffer = 0;
|
||||
long length;
|
||||
FILE *f = fopen(*filename, "rb");
|
||||
|
||||
|
@ -32,19 +32,19 @@
|
||||
#define CAP_NONE (-2)
|
||||
|
||||
typedef struct PatternMatchState {
|
||||
string src_init; /* init of source string */
|
||||
string src_end; /* end ('\0') of source string */
|
||||
string p_end; /* end ('\0') of pattern */
|
||||
String src_init; /* init of source String */
|
||||
String src_end; /* end ('\0') of source String */
|
||||
String p_end; /* end ('\0') of Pattern */
|
||||
int matchdepth; /* control for recursive depth (to avoid C stack overflow) */
|
||||
unsigned char level; /* total number of captures (finished or unfinished) */
|
||||
struct {
|
||||
string init;
|
||||
String init;
|
||||
ptrdiff_t len;
|
||||
} capture[CARP_MAXCAPTURES];
|
||||
} PatternMatchState;
|
||||
|
||||
/* recursive function */
|
||||
string Pattern_internal_match(PatternMatchState *ms, string s, string p);
|
||||
String Pattern_internal_match(PatternMatchState *ms, String s, String p);
|
||||
|
||||
/* maximum recursion depth for 'match' */
|
||||
#if !defined(MAXCCALLS)
|
||||
@ -75,19 +75,19 @@ int Pattern_internal_capture_to_close(PatternMatchState *ms) {
|
||||
for (level--; level>=0; level--) {
|
||||
if (ms->capture[level].len == CAP_UNFINISHED) return level;
|
||||
}
|
||||
return carp_regerror("invalid pattern capture");
|
||||
return carp_regerror("invalid Pattern capture");
|
||||
}
|
||||
|
||||
string Pattern_internal_classend(PatternMatchState *ms, string p) {
|
||||
String Pattern_internal_classend(PatternMatchState *ms, String p) {
|
||||
switch (*p++) {
|
||||
case C_ESC: {
|
||||
if (p == ms->p_end) carp_regerror("malformed pattern (ends with '%c')", C_ESC);
|
||||
if (p == ms->p_end) carp_regerror("malformed Pattern (ends with '%c')", C_ESC);
|
||||
return p+1;
|
||||
}
|
||||
case '[': {
|
||||
if (*p == '^') p++;
|
||||
do { /* look for a ']' */
|
||||
if (p == ms->p_end) carp_regerror("malformed pattern (missing ']')");
|
||||
if (p == ms->p_end) carp_regerror("malformed Pattern (missing ']')");
|
||||
if (*(p++) == C_ESC && p < ms->p_end) p++; /* skip escapes (e.g. '%]') */
|
||||
} while (*p != ']');
|
||||
return p+1;
|
||||
@ -116,7 +116,7 @@ int Pattern_internal_match_class(int c, int cl) {
|
||||
return (islower(cl) ? res : !res);
|
||||
}
|
||||
|
||||
int Pattern_internal_matchbracketclass(int c, string p, string ec) {
|
||||
int Pattern_internal_matchbracketclass(int c, String p, String ec) {
|
||||
int sig = 1;
|
||||
if (*(p+1) == '^') {
|
||||
sig = 0;
|
||||
@ -136,8 +136,8 @@ int Pattern_internal_matchbracketclass(int c, string p, string ec) {
|
||||
return !sig;
|
||||
}
|
||||
|
||||
int Pattern_internal_singlematch(PatternMatchState *ms, string s, string p,
|
||||
string ep) {
|
||||
int Pattern_internal_singlematch(PatternMatchState *ms, String s, String p,
|
||||
String ep) {
|
||||
if (s >= ms->src_end) {
|
||||
return 0;
|
||||
} else {
|
||||
@ -151,8 +151,8 @@ int Pattern_internal_singlematch(PatternMatchState *ms, string s, string p,
|
||||
}
|
||||
}
|
||||
|
||||
string Pattern_internal_matchbalance(PatternMatchState *ms, string s, string p) {
|
||||
if (p >= ms->p_end - 1) carp_regerror("malformed pattern (missing arguments to '%cb')", C_ESC);
|
||||
String Pattern_internal_matchbalance(PatternMatchState *ms, String s, String p) {
|
||||
if (p >= ms->p_end - 1) carp_regerror("malformed Pattern (missing arguments to '%cb')", C_ESC);
|
||||
if (*s != *p) {
|
||||
return NULL;
|
||||
} else {
|
||||
@ -168,35 +168,35 @@ string Pattern_internal_matchbalance(PatternMatchState *ms, string s, string p)
|
||||
}
|
||||
}
|
||||
}
|
||||
return NULL; /* string ends out of balance */
|
||||
return NULL; /* String ends out of balance */
|
||||
}
|
||||
|
||||
string Pattern_internal_max_expand(PatternMatchState *ms, string s, string p,
|
||||
string ep) {
|
||||
String Pattern_internal_max_expand(PatternMatchState *ms, String s, String p,
|
||||
String ep) {
|
||||
ptrdiff_t i = 0; /* counts maximum expand for item */
|
||||
while (Pattern_internal_singlematch(ms, s + i, p, ep)) i++;
|
||||
/* keeps trying to match with the maximum repetitions */
|
||||
while (i>=0) {
|
||||
string res = Pattern_internal_match(ms, (s+i), ep+1);
|
||||
String res = Pattern_internal_match(ms, (s+i), ep+1);
|
||||
if (res) return res;
|
||||
i--; /* else didn't match; reduce 1 repetition to try again */
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
string Pattern_internal_min_expand(PatternMatchState *ms, string s, string p,
|
||||
string ep) {
|
||||
String Pattern_internal_min_expand(PatternMatchState *ms, String s, String p,
|
||||
String ep) {
|
||||
for (;;) {
|
||||
string res = Pattern_internal_match(ms, s, ep+1);
|
||||
String res = Pattern_internal_match(ms, s, ep+1);
|
||||
if (res) return res;
|
||||
else if (Pattern_internal_singlematch(ms, s, p, ep)) s++; /* try with one more repetition */
|
||||
else return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
string Pattern_internal_start_capture(PatternMatchState *ms, string s, string p,
|
||||
String Pattern_internal_start_capture(PatternMatchState *ms, String s, String p,
|
||||
int what) {
|
||||
string res;
|
||||
String res;
|
||||
int level = ms->level;
|
||||
if (level >= CARP_MAXCAPTURES) carp_regerror("too many captures");
|
||||
ms->capture[level].init = s;
|
||||
@ -206,9 +206,9 @@ string Pattern_internal_start_capture(PatternMatchState *ms, string s, string p,
|
||||
return res;
|
||||
}
|
||||
|
||||
string Pattern_internal_end_capture(PatternMatchState *ms, string s, string p) {
|
||||
String Pattern_internal_end_capture(PatternMatchState *ms, String s, String p) {
|
||||
int l = Pattern_internal_capture_to_close(ms);
|
||||
string res;
|
||||
String res;
|
||||
ms->capture[l].len = s - ms->capture[l].init; /* close capture */
|
||||
if (!(res=Pattern_internal_match(ms, s, p))) {
|
||||
ms->capture[l].len = CAP_UNFINISHED; /* undo capture */
|
||||
@ -216,7 +216,7 @@ string Pattern_internal_end_capture(PatternMatchState *ms, string s, string p) {
|
||||
return res;
|
||||
}
|
||||
|
||||
string Pattern_internal_match_capture(PatternMatchState *ms, string s, int l) {
|
||||
String Pattern_internal_match_capture(PatternMatchState *ms, String s, int l) {
|
||||
size_t len;
|
||||
l = Pattern_internal_check_capture(ms, l);
|
||||
len = ms->capture[l].len;
|
||||
@ -226,10 +226,10 @@ string Pattern_internal_match_capture(PatternMatchState *ms, string s, int l) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
string Pattern_internal_match(PatternMatchState *ms, string s, string p) {
|
||||
if (ms->matchdepth-- == 0) carp_regerror("pattern too complex");
|
||||
String Pattern_internal_match(PatternMatchState *ms, String s, String p) {
|
||||
if (ms->matchdepth-- == 0) carp_regerror("Pattern too complex");
|
||||
init: /* using goto's to optimize tail recursion */
|
||||
if (p != ms->p_end) { /* end of pattern? */
|
||||
if (p != ms->p_end) { /* end of Pattern? */
|
||||
switch (*p) {
|
||||
case '(': { /* start capture */
|
||||
if (*(p + 1) == ')') s = Pattern_internal_start_capture(ms, s, p + 2,
|
||||
@ -243,12 +243,12 @@ string Pattern_internal_match(PatternMatchState *ms, string s, string p) {
|
||||
}
|
||||
case '$': {
|
||||
if ((p + 1) != ms->p_end) goto dflt; /* are we done as we should */
|
||||
s = (s == ms->src_end) ? s : NULL; /* check end of string */
|
||||
s = (s == ms->src_end) ? s : NULL; /* check end of String */
|
||||
break;
|
||||
}
|
||||
case C_ESC: { /* escaped sequences not in the format class[*+?-]? */
|
||||
switch (*(p + 1)) {
|
||||
case 'b': { /* balanced string? */
|
||||
case 'b': { /* balanced String? */
|
||||
s = Pattern_internal_matchbalance(ms, s, p + 2);
|
||||
if (s) {
|
||||
p += 4; goto init; /* return match(ms, s, p + 4); */
|
||||
@ -256,9 +256,9 @@ string Pattern_internal_match(PatternMatchState *ms, string s, string p) {
|
||||
break;
|
||||
}
|
||||
case 'f': { /* frontier? */
|
||||
string ep; char previous;
|
||||
String ep; char previous;
|
||||
p += 2;
|
||||
if (*p != '[') carp_regerror("missing '[' after '%cf' in pattern", C_ESC);
|
||||
if (*p != '[') carp_regerror("missing '[' after '%cf' in Pattern", C_ESC);
|
||||
ep = Pattern_internal_classend(ms, p); /* points to what is next */
|
||||
previous = (s == ms->src_init) ? '\0' : *(s - 1);
|
||||
if (!Pattern_internal_matchbracketclass(uchar(previous), p, ep - 1) &&
|
||||
@ -292,8 +292,8 @@ string Pattern_internal_match(PatternMatchState *ms, string s, string p) {
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: dflt: { /* pattern class plus optional suffix */
|
||||
string ep = Pattern_internal_classend(ms, p); /* points to optional suffix */
|
||||
default: dflt: { /* Pattern class plus optional suffix */
|
||||
String ep = Pattern_internal_classend(ms, p); /* points to optional suffix */
|
||||
/* does not match at least once? */
|
||||
if (!Pattern_internal_singlematch(ms, s, p, ep)) {
|
||||
if (*ep == '*' || *ep == '?' || *ep == '-') { /* accept empty? */
|
||||
@ -306,7 +306,7 @@ string Pattern_internal_match(PatternMatchState *ms, string s, string p) {
|
||||
else { /* matched once */
|
||||
switch (*ep) { /* handle optional suffix */
|
||||
case '?': { /* optional */
|
||||
string res;
|
||||
String res;
|
||||
if ((res = Pattern_internal_match(ms, s + 1, ep + 1))) {
|
||||
s = res;
|
||||
} else {
|
||||
@ -335,13 +335,13 @@ string Pattern_internal_match(PatternMatchState *ms, string s, string p) {
|
||||
return s;
|
||||
}
|
||||
|
||||
string Pattern_internal_lmemfind(string s1, size_t l1, string s2, size_t l2) {
|
||||
if (l2 == 0) return s1; /* empty strings are everywhere */
|
||||
String Pattern_internal_lmemfind(String s1, size_t l1, String s2, size_t l2) {
|
||||
if (l2 == 0) return s1; /* empty Strings are everywhere */
|
||||
if (l2 > l1) return NULL; /* avoids a negative 'l1' */
|
||||
string init; /* to search for a '*s2' inside 's1' */
|
||||
String init; /* to search for a '*s2' inside 's1' */
|
||||
l2--; /* 1st char will be checked by 'memchr' */
|
||||
l1 = l1-l2; /* 's2' cannot be found after that */
|
||||
while (l1 > 0 && (init = (string )memchr(s1, *s2, l1))) {
|
||||
while (l1 > 0 && (init = (String )memchr(s1, *s2, l1))) {
|
||||
init++; /* 1st char is already checked */
|
||||
if (!memcmp(init, s2+1, l2)) {
|
||||
return init-1;
|
||||
@ -353,8 +353,8 @@ string Pattern_internal_lmemfind(string s1, size_t l1, string s2, size_t l2) {
|
||||
return NULL; /* not found */
|
||||
}
|
||||
|
||||
string String_copy_len(string s, int len) {
|
||||
string ptr = CARP_MALLOC(len+1);
|
||||
String String_copy_len(String s, int len) {
|
||||
String ptr = CARP_MALLOC(len+1);
|
||||
|
||||
if (!ptr) return NULL;
|
||||
|
||||
@ -363,50 +363,50 @@ string String_copy_len(string s, int len) {
|
||||
return ptr;
|
||||
}
|
||||
|
||||
Array Array_push_string(Array a, string s, int i, int len) {
|
||||
((string*)a.data)[i] = String_copy_len(s, len);
|
||||
Array Array_push_String(Array a, String s, int i, int len) {
|
||||
((String*)a.data)[i] = String_copy_len(s, len);
|
||||
return a;
|
||||
}
|
||||
|
||||
Array Pattern_internal_push_onecapture(PatternMatchState *ms, int i, string s,
|
||||
string e, Array captures) {
|
||||
Array Pattern_internal_push_onecapture(PatternMatchState *ms, int i, String s,
|
||||
String e, Array captures) {
|
||||
if (i >= ms->level) {
|
||||
if (!i) return Array_push_string(captures, s, i, ms->capture[i].len); /* add whole match */
|
||||
if (!i) return Array_push_String(captures, s, i, ms->capture[i].len); /* add whole match */
|
||||
else carp_regerror("invalid capture index %cd", C_ESC, i + 1);
|
||||
}
|
||||
else {
|
||||
ptrdiff_t l = ms->capture[i].len;
|
||||
if (l == CAP_UNFINISHED) carp_regerror("unfinished capture");
|
||||
else if (l != CAP_NONE) return Array_push_string(captures, ms->capture[i].init,
|
||||
else if (l != CAP_NONE) return Array_push_String(captures, ms->capture[i].init,
|
||||
i, ms->capture[i].len);
|
||||
}
|
||||
return captures;
|
||||
}
|
||||
|
||||
Array Pattern_internal_push_captures(PatternMatchState *ms, string s, string e) {
|
||||
Array Pattern_internal_push_captures(PatternMatchState *ms, String s, String e) {
|
||||
int i;
|
||||
int nlevels = (ms->level == 0 && s) ? 1 : ms->level;
|
||||
Array res;
|
||||
res.len = nlevels;
|
||||
res.capacity = nlevels;
|
||||
res.data = CARP_MALLOC(nlevels*sizeof(string));
|
||||
res.data = CARP_MALLOC(nlevels*sizeof(String));
|
||||
for (i = 0; i < nlevels; i++) Pattern_internal_push_onecapture(ms, i, s, e,
|
||||
res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* check whether pattern has no special characters */
|
||||
int Pattern_internal_nospecials(string p, size_t l) {
|
||||
/* check whether Pattern has no special characters */
|
||||
int Pattern_internal_nospecials(String p, size_t l) {
|
||||
size_t upto = 0;
|
||||
do {
|
||||
if (strpbrk(p + upto, SPECIALS)) return 0; /* pattern has a special character */
|
||||
if (strpbrk(p + upto, SPECIALS)) return 0; /* Pattern has a special character */
|
||||
upto += strlen(p + upto) + 1; /* may have more after \0 */
|
||||
} while (upto <= l);
|
||||
return 1; /* no special chars found */
|
||||
}
|
||||
|
||||
void Pattern_internal_prepstate(PatternMatchState *ms, string s, size_t ls,
|
||||
string p, size_t lp) {
|
||||
void Pattern_internal_prepstate(PatternMatchState *ms, String s, size_t ls,
|
||||
String p, size_t lp) {
|
||||
ms->matchdepth = MAXCCALLS;
|
||||
ms->src_init = s;
|
||||
ms->src_end = s + ls;
|
||||
@ -418,47 +418,47 @@ void Pattern_internal_reprepstate(PatternMatchState *ms) {
|
||||
assert(ms->matchdepth == MAXCCALLS);
|
||||
}
|
||||
|
||||
int Pattern_find(pattern* p, string* s) {
|
||||
string str = *s;
|
||||
pattern pat = *p;
|
||||
int Pattern_find(Pattern* p, String* s) {
|
||||
String str = *s;
|
||||
Pattern pat = *p;
|
||||
int lstr = strlen(str);
|
||||
int lpat = strlen(pat);
|
||||
/* explicit request or no special characters? */
|
||||
if (Pattern_internal_nospecials(pat, lpat)) {
|
||||
/* do a plain search */
|
||||
string s2 = Pattern_internal_lmemfind(str, lstr, pat, lpat);
|
||||
String s2 = Pattern_internal_lmemfind(str, lstr, pat, lpat);
|
||||
if (!s2) return -1;
|
||||
return s2-str;
|
||||
}
|
||||
PatternMatchState ms;
|
||||
string s1 = str;
|
||||
String s1 = str;
|
||||
int anchor = (*pat == '^');
|
||||
if (anchor) {
|
||||
pat++; lpat--; /* skip anchor character */
|
||||
}
|
||||
Pattern_internal_prepstate(&ms, str, lstr, pat, lpat);
|
||||
do {
|
||||
string res;
|
||||
String res;
|
||||
Pattern_internal_reprepstate(&ms);
|
||||
if ((res=Pattern_internal_match(&ms, s1, pat))) return s1 - str;
|
||||
} while (s1++ < ms.src_end && !anchor);
|
||||
return -1;
|
||||
}
|
||||
|
||||
Array Pattern_match(pattern* p, string* s) {
|
||||
string str = *s;
|
||||
pattern pat = *p;
|
||||
Array Pattern_match(Pattern* p, String* s) {
|
||||
String str = *s;
|
||||
Pattern pat = *p;
|
||||
int lstr = strlen(str);
|
||||
int lpat = strlen(pat);
|
||||
PatternMatchState ms;
|
||||
string s1 = str;
|
||||
String s1 = str;
|
||||
int anchor = (*pat == '^');
|
||||
if (anchor) {
|
||||
pat++; lpat--; /* skip anchor character */
|
||||
}
|
||||
Pattern_internal_prepstate(&ms, str, lstr, pat, lpat);
|
||||
do {
|
||||
string res;
|
||||
String res;
|
||||
Pattern_internal_reprepstate(&ms);
|
||||
if ((res=Pattern_internal_match(&ms, s1, pat))) {
|
||||
return Pattern_internal_push_captures(&ms, s1, res);
|
||||
@ -471,20 +471,20 @@ Array Pattern_match(pattern* p, string* s) {
|
||||
return a;
|
||||
}
|
||||
|
||||
string Pattern_match_MINUS_str(pattern* p, string* s) {
|
||||
string str = *s;
|
||||
pattern pat = *p;
|
||||
String Pattern_match_MINUS_str(Pattern* p, String* s) {
|
||||
String str = *s;
|
||||
Pattern pat = *p;
|
||||
int lstr = strlen(str);
|
||||
int lpat = strlen(pat);
|
||||
PatternMatchState ms;
|
||||
string s1 = str;
|
||||
String s1 = str;
|
||||
int anchor = (*pat == '^');
|
||||
if (anchor) {
|
||||
pat++; lpat--; /* skip anchor character */
|
||||
}
|
||||
Pattern_internal_prepstate(&ms, str, lstr, pat, lpat);
|
||||
do {
|
||||
string res;
|
||||
String res;
|
||||
Pattern_internal_reprepstate(&ms);
|
||||
if ((res=Pattern_internal_match(&ms, s1, pat))) {
|
||||
int start = (s1 - str) + 1;
|
||||
@ -501,9 +501,9 @@ string Pattern_match_MINUS_str(pattern* p, string* s) {
|
||||
|
||||
/* state for 'gmatch' */
|
||||
typedef struct PatternGMatchState {
|
||||
string src; /* current position */
|
||||
string pat; /* pattern */
|
||||
string lastmatch; /* end of last match */
|
||||
String src; /* current position */
|
||||
String pat; /* Pattern */
|
||||
String lastmatch; /* end of last match */
|
||||
PatternMatchState ms; /* match state */
|
||||
} PatternGMatchState;
|
||||
|
||||
@ -514,10 +514,10 @@ typedef struct PatternGMatchRes {
|
||||
|
||||
|
||||
PatternGMatchRes Pattern_internal_gmatch_aux(PatternGMatchState* gm) {
|
||||
string src;
|
||||
String src;
|
||||
Array a;
|
||||
for (src = gm->src; src <= gm->ms.src_end; src++) {
|
||||
string e;
|
||||
String e;
|
||||
Pattern_internal_reprepstate(&gm->ms);
|
||||
if ((e = Pattern_internal_match(&gm->ms, src, gm->pat)) &&
|
||||
e != gm->lastmatch) {
|
||||
@ -536,9 +536,9 @@ Array Array_push_back(Array res, Array tmp) {
|
||||
return res;
|
||||
}
|
||||
|
||||
Array Pattern_global_MINUS_match(pattern* p, string* s) {
|
||||
string str = *s;
|
||||
pattern pat = *p;
|
||||
Array Pattern_global_MINUS_match(Pattern* p, String* s) {
|
||||
String str = *s;
|
||||
Pattern pat = *p;
|
||||
int lstr = strlen(str);
|
||||
int lpat = strlen(pat);
|
||||
PatternGMatchState gm;
|
||||
@ -556,22 +556,22 @@ Array Pattern_global_MINUS_match(pattern* p, string* s) {
|
||||
return res;
|
||||
}
|
||||
|
||||
string Pattern_internal_add_char(string a, char b) {
|
||||
String Pattern_internal_add_char(String a, char b) {
|
||||
if (!a) {
|
||||
string buffer = CARP_MALLOC(2);
|
||||
String buffer = CARP_MALLOC(2);
|
||||
snprintf(buffer, 2, "%c", b);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
int len = strlen(a) + 2;
|
||||
string buffer = CARP_MALLOC(len);
|
||||
String buffer = CARP_MALLOC(len);
|
||||
snprintf(buffer, len, "%s%c", a, b);
|
||||
CARP_FREE(a);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
string Pattern_internal_add_value(PatternMatchState *ms, string res, string src,
|
||||
string e, string tr) {
|
||||
String Pattern_internal_add_value(PatternMatchState *ms, String res, String src,
|
||||
String e, String tr) {
|
||||
size_t l, i;
|
||||
l = strlen(tr);
|
||||
for (i = 0; i < l; i++) {
|
||||
@ -580,7 +580,7 @@ string Pattern_internal_add_value(PatternMatchState *ms, string res, string src,
|
||||
i++; /* skip ESC */
|
||||
if (!isdigit(uchar(tr[i]))) {
|
||||
if (tr[i] != C_ESC) {
|
||||
carp_regerror( "invalid use of '%c' in replacement string", C_ESC);
|
||||
carp_regerror( "invalid use of '%c' in replacement String", C_ESC);
|
||||
}
|
||||
res = Pattern_internal_add_char(res, tr[i]);
|
||||
}
|
||||
@ -588,22 +588,22 @@ string Pattern_internal_add_value(PatternMatchState *ms, string res, string src,
|
||||
else {
|
||||
Array a = {.len = 0, .capacity = 0, .data = NULL};
|
||||
Pattern_internal_push_onecapture(ms, tr[i] - '1', src, e, a);
|
||||
res = String_append(res, ((string*)a.data)[0]); /* add capture to accumulated result */
|
||||
res = String_append(res, ((String*)a.data)[0]); /* add capture to accumulated result */
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
string Pattern_substitute(pattern* p, string *s, string *t, int ns) {
|
||||
string str = *s;
|
||||
pattern pat = *p;
|
||||
string tr = *t;
|
||||
String Pattern_substitute(Pattern* p, String *s, String *t, int ns) {
|
||||
String str = *s;
|
||||
Pattern pat = *p;
|
||||
String tr = *t;
|
||||
int lstr = strlen(str);
|
||||
int lpat = strlen(pat);
|
||||
string lastmatch = NULL; /* end of last match */
|
||||
String lastmatch = NULL; /* end of last match */
|
||||
int anchor = (*pat == '^');
|
||||
string res = NULL;
|
||||
String res = NULL;
|
||||
PatternMatchState ms;
|
||||
int n = 0;
|
||||
if (anchor) {
|
||||
@ -611,7 +611,7 @@ string Pattern_substitute(pattern* p, string *s, string *t, int ns) {
|
||||
}
|
||||
Pattern_internal_prepstate(&ms, str, lstr, pat, lpat);
|
||||
while (n < ns || ns == -1) {
|
||||
string e;
|
||||
String e;
|
||||
Pattern_internal_reprepstate(&ms); /* (re)prepare state for new match */
|
||||
if ((e = Pattern_internal_match(&ms, str, pat)) && e != lastmatch) { /* match? */
|
||||
n++;
|
||||
@ -631,34 +631,34 @@ string Pattern_substitute(pattern* p, string *s, string *t, int ns) {
|
||||
return res;
|
||||
}
|
||||
|
||||
pattern Pattern_copy(pattern *p) {
|
||||
Pattern Pattern_copy(Pattern *p) {
|
||||
size_t len = strlen(*p) + 1;
|
||||
pattern ptr = CARP_MALLOC(len);
|
||||
Pattern ptr = CARP_MALLOC(len);
|
||||
|
||||
if (!ptr) return NULL;
|
||||
|
||||
return (pattern) memcpy(ptr, *p, len);
|
||||
return (Pattern) memcpy(ptr, *p, len);
|
||||
}
|
||||
|
||||
void Pattern_delete(pattern p) {
|
||||
void Pattern_delete(Pattern p) {
|
||||
CARP_FREE(p);
|
||||
}
|
||||
|
||||
pattern Pattern_init(string* p) {
|
||||
Pattern Pattern_init(String* p) {
|
||||
return Pattern_copy(p);
|
||||
}
|
||||
|
||||
string Pattern_str(pattern *p) {
|
||||
String Pattern_str(Pattern *p) {
|
||||
return Pattern_copy(p);
|
||||
}
|
||||
|
||||
string Pattern_prn(pattern *p) {
|
||||
String Pattern_prn(Pattern *p) {
|
||||
int n = strlen(*p) + 4;
|
||||
string buffer = CARP_MALLOC(n);
|
||||
String buffer = CARP_MALLOC(n);
|
||||
snprintf(buffer, n, "#\"%s\"", *p);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
bool Pattern__EQ_(pattern *a, pattern *b) {
|
||||
bool Pattern__EQ_(Pattern *a, Pattern *b) {
|
||||
return strcmp(*a, *b) == 0;
|
||||
}
|
||||
|
@ -4,76 +4,76 @@
|
||||
#include <carp_memory.h>
|
||||
#include <core.h>
|
||||
|
||||
void String_delete(string s) {
|
||||
void String_delete(String s) {
|
||||
CARP_FREE(s);
|
||||
}
|
||||
|
||||
string String_copy(string *s) {
|
||||
String String_copy(String *s) {
|
||||
size_t len = strlen(*s) + 1;
|
||||
string ptr = CARP_MALLOC(len);
|
||||
String ptr = CARP_MALLOC(len);
|
||||
|
||||
if (ptr == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return (string) memcpy(ptr, *s, len);
|
||||
return (String) memcpy(ptr, *s, len);
|
||||
}
|
||||
|
||||
bool String__EQ_(string *a, string *b) {
|
||||
bool String__EQ_(String *a, String *b) {
|
||||
return strcmp(*a, *b) == 0;
|
||||
}
|
||||
|
||||
string String_append(string a, string b) {
|
||||
String String_append(String a, String b) {
|
||||
int la = strlen(a);
|
||||
int lb = strlen(b);
|
||||
int total = la + lb + 1;
|
||||
string buffer = CARP_MALLOC(total);
|
||||
String buffer = CARP_MALLOC(total);
|
||||
snprintf(buffer, total, "%s%s", a, b);
|
||||
CARP_FREE(a);
|
||||
CARP_FREE(b);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
int String_count(string *s) {
|
||||
int String_count(String *s) {
|
||||
return strlen(*s);
|
||||
}
|
||||
|
||||
// Replace with 'copy' later:
|
||||
string String_duplicate(string *s) {
|
||||
String String_duplicate(String *s) {
|
||||
return String_copy(s);
|
||||
}
|
||||
|
||||
char* String_cstr(string *s) {
|
||||
char* String_cstr(String *s) {
|
||||
return *s;
|
||||
}
|
||||
|
||||
string String_str(string *s) {
|
||||
String String_str(String *s) {
|
||||
int n = strlen(*s) + 1;
|
||||
string buffer = CARP_MALLOC(n);
|
||||
String buffer = CARP_MALLOC(n);
|
||||
snprintf(buffer, n, "%s", *s);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
string String_prn(string *s) {
|
||||
String String_prn(String *s) {
|
||||
int n = strlen(*s) + 4;
|
||||
string buffer = CARP_MALLOC(n);
|
||||
String buffer = CARP_MALLOC(n);
|
||||
snprintf(buffer, n, "@\"%s\"", *s);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
char String_char_MINUS_at(string* s, int i) {
|
||||
char String_char_MINUS_at(String* s, int i) {
|
||||
return (*s)[i];
|
||||
}
|
||||
|
||||
string String_format(string *str, string *s) {
|
||||
String String_format(String *str, String *s) {
|
||||
int size = snprintf(NULL, 0, *str, *s)+1;
|
||||
string buffer = CARP_MALLOC(size);
|
||||
String buffer = CARP_MALLOC(size);
|
||||
snprintf(buffer, size, *str, *s);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
Array String_chars(string *s) {
|
||||
Array String_chars(String *s) {
|
||||
Array chars;
|
||||
chars.len = strlen(*s);
|
||||
chars.capacity = chars.len;
|
||||
@ -81,30 +81,30 @@ Array String_chars(string *s) {
|
||||
return chars;
|
||||
}
|
||||
|
||||
string String_from_MINUS_chars(Array *a) {
|
||||
string s = CARP_MALLOC(a->len+1);
|
||||
String String_from_MINUS_chars(Array *a) {
|
||||
String s = CARP_MALLOC(a->len+1);
|
||||
memmove(s, a->data, a->len);
|
||||
s[a->len] = '\0';
|
||||
return s;
|
||||
}
|
||||
|
||||
string String_tail(string* s) {
|
||||
String String_tail(String* s) {
|
||||
int len = strlen(*s);
|
||||
string news = CARP_MALLOC(len);
|
||||
String news = CARP_MALLOC(len);
|
||||
memcpy(news, (*s)+1, len-1);
|
||||
news[len-1] = '\0';
|
||||
return news;
|
||||
}
|
||||
|
||||
string String_empty() {
|
||||
string s = CARP_MALLOC(1);
|
||||
String String_empty() {
|
||||
String s = CARP_MALLOC(1);
|
||||
s[0] = '\0';
|
||||
return s;
|
||||
}
|
||||
|
||||
string Bool_str(bool b) {
|
||||
string true_str = "true";
|
||||
string false_str = "false";
|
||||
String Bool_str(bool b) {
|
||||
String true_str = "true";
|
||||
String false_str = "false";
|
||||
if(b) {
|
||||
return String_copy(&true_str);
|
||||
} else {
|
||||
@ -112,86 +112,86 @@ string Bool_str(bool b) {
|
||||
}
|
||||
}
|
||||
|
||||
string Bool_format(string* str, bool b) {
|
||||
String Bool_format(String* str, bool b) {
|
||||
int size = snprintf(NULL, 0, *str, b)+1;
|
||||
string buffer = CARP_MALLOC(size);
|
||||
String buffer = CARP_MALLOC(size);
|
||||
snprintf(buffer, size, *str, b);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
string Char_str(char c) {
|
||||
string buffer = CARP_MALLOC(3);
|
||||
String Char_str(char c) {
|
||||
String buffer = CARP_MALLOC(3);
|
||||
snprintf(buffer, 3, "\\%c", c);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
string Char_format(string* str, char b) {
|
||||
String Char_format(String* str, char b) {
|
||||
int size = snprintf(NULL, 0, *str, b)+1;
|
||||
string buffer = CARP_MALLOC(size);
|
||||
String buffer = CARP_MALLOC(size);
|
||||
snprintf(buffer, size, *str, b);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
string Double_str(double x) {
|
||||
String Double_str(double x) {
|
||||
int size = snprintf(NULL, 0, "%g", x)+1;
|
||||
string buffer = CARP_MALLOC(size);
|
||||
String buffer = CARP_MALLOC(size);
|
||||
snprintf(buffer, size, "%g", x);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
string Double_format(string* s, double x) {
|
||||
String Double_format(String* s, double x) {
|
||||
int size = snprintf(NULL, 0, *s, x)+1;
|
||||
string buffer = CARP_MALLOC(size);
|
||||
String buffer = CARP_MALLOC(size);
|
||||
snprintf(buffer, size, *s, x);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
string Float_str(float x) {
|
||||
String Float_str(float x) {
|
||||
int size = snprintf(NULL, 0, "%gf", x)+1;
|
||||
string buffer = CARP_MALLOC(size);
|
||||
String buffer = CARP_MALLOC(size);
|
||||
snprintf(buffer, size, "%gf", x);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
string Float_format(string* str, float x) {
|
||||
String Float_format(String* str, float x) {
|
||||
int size = snprintf(NULL, 0, *str, x)+1;
|
||||
string buffer = CARP_MALLOC(size);
|
||||
String buffer = CARP_MALLOC(size);
|
||||
snprintf(buffer, size, *str, x);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
string Int_str(int x) {
|
||||
String Int_str(int x) {
|
||||
int size = snprintf(NULL, 0, "%d", x)+1;
|
||||
string buffer = CARP_MALLOC(size);
|
||||
String buffer = CARP_MALLOC(size);
|
||||
snprintf(buffer, size, "%d", x);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
string Int_format(string* str, int x) {
|
||||
String Int_format(String* str, int x) {
|
||||
int size = snprintf(NULL, 0, *str, x)+1;
|
||||
string buffer = CARP_MALLOC(size);
|
||||
String buffer = CARP_MALLOC(size);
|
||||
snprintf(buffer, size, *str, x);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
int Int_from_MINUS_string(string *s) {
|
||||
int Int_from_MINUS_string(String *s) {
|
||||
return atoi(*s);
|
||||
}
|
||||
|
||||
string Long_str(long x) {
|
||||
String Long_str(long x) {
|
||||
int size = snprintf(NULL, 0, "%ldl", x)+1;
|
||||
string buffer = CARP_MALLOC(size);
|
||||
String buffer = CARP_MALLOC(size);
|
||||
snprintf(buffer, size, "%ldl", x);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
string Long_format(string* str, long x) {
|
||||
String Long_format(String* str, long x) {
|
||||
int size = snprintf(NULL, 0, *str, x)+1;
|
||||
string buffer = CARP_MALLOC(size);
|
||||
String buffer = CARP_MALLOC(size);
|
||||
snprintf(buffer, size, *str, x);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
long Long_from_MINUS_string(string *s) {
|
||||
long Long_from_MINUS_string(String *s) {
|
||||
return atol(*s);
|
||||
}
|
||||
|
@ -37,6 +37,6 @@ int Int_random_MINUS_between(int lower, int upper) {
|
||||
return lower + (rand() % diff);
|
||||
}
|
||||
|
||||
void System_system(string *command) {
|
||||
void System_system(String *command) {
|
||||
system(*command);
|
||||
}
|
||||
|
@ -5,8 +5,8 @@
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
typedef char* string;
|
||||
typedef char* pattern;
|
||||
typedef char* String;
|
||||
typedef char* Pattern;
|
||||
|
||||
// Array
|
||||
typedef struct {
|
||||
|
@ -353,7 +353,7 @@ templateStrArray = defineTypeParameterizedTemplate templateCreator path t
|
||||
\typeEnv env ->
|
||||
Template
|
||||
t
|
||||
(const (toTemplate "string $NAME (Array* a)"))
|
||||
(const (toTemplate "String $NAME (Array* a)"))
|
||||
(\(FuncTy [RefTy arrayType] StringTy) ->
|
||||
[TokDecl, TokC " {\n"] ++
|
||||
strTy typeEnv env arrayType ++
|
||||
@ -367,10 +367,10 @@ templateStrArray = defineTypeParameterizedTemplate templateCreator path t
|
||||
strTy :: TypeEnv -> Env -> Ty -> [Token]
|
||||
strTy typeEnv env (StructTy "Array" [innerType]) =
|
||||
[ TokC ""
|
||||
, TokC " string temp = NULL;\n"
|
||||
, TokC " String temp = NULL;\n"
|
||||
, TokC $ calculateStrSize typeEnv env innerType
|
||||
, TokC " string buffer = CARP_MALLOC(size);\n"
|
||||
, TokC " string bufferPtr = buffer;\n"
|
||||
, TokC " String buffer = CARP_MALLOC(size);\n"
|
||||
, TokC " String bufferPtr = buffer;\n"
|
||||
, TokC "\n"
|
||||
, TokC " snprintf(buffer, size, \"[\");\n"
|
||||
, TokC " bufferPtr += 1;\n"
|
||||
|
@ -257,7 +257,7 @@ concreteStr :: TypeEnv -> Env -> Ty -> [(String, Ty)] -> String -> Template
|
||||
concreteStr typeEnv env concreteStructTy@(StructTy typeName _) memberPairs strOrPrn =
|
||||
Template
|
||||
(FuncTy [RefTy concreteStructTy] StringTy)
|
||||
(\(FuncTy [RefTy structTy] StringTy) -> (toTemplate $ "string $NAME(" ++ tyToC structTy ++ " *p)"))
|
||||
(\(FuncTy [RefTy structTy] StringTy) -> (toTemplate $ "String $NAME(" ++ tyToC structTy ++ " *p)"))
|
||||
(\(FuncTy [RefTy structTy@(StructTy _ concreteMemberTys)] StringTy) ->
|
||||
(tokensForStr typeEnv env typeName memberPairs concreteStructTy))
|
||||
(\(ft@(FuncTy [RefTy structTy@(StructTy _ concreteMemberTys)] StringTy)) ->
|
||||
@ -277,7 +277,7 @@ genericStr pathStrings originalStructTy@(StructTy typeName varTys) membersXObjs
|
||||
Template
|
||||
t
|
||||
(\(FuncTy [RefTy concreteStructTy] StringTy) ->
|
||||
(toTemplate $ "string $NAME(" ++ tyToC concreteStructTy ++ " *p)"))
|
||||
(toTemplate $ "String $NAME(" ++ tyToC concreteStructTy ++ " *p)"))
|
||||
(\(FuncTy [RefTy concreteStructTy@(StructTy _ concreteMemberTys)] StringTy) ->
|
||||
let mappings = unifySignatures originalStructTy concreteStructTy
|
||||
correctedMembers = replaceGenericTypeSymbolsOnMembers mappings membersXObjs
|
||||
@ -296,13 +296,13 @@ genericStr pathStrings originalStructTy@(StructTy typeName varTys) membersXObjs
|
||||
tokensForStr :: TypeEnv -> Env -> String -> [(String, Ty)] -> Ty -> [Token]
|
||||
tokensForStr typeEnv env typeName memberPairs concreteStructTy =
|
||||
(toTemplate $ unlines [ "$DECL {"
|
||||
, " // convert members to string here:"
|
||||
, " string temp = NULL;"
|
||||
, " // convert members to String here:"
|
||||
, " String temp = NULL;"
|
||||
, " int tempsize = 0;"
|
||||
, " (void)tempsize; // that way we remove the occasional unused warning "
|
||||
, calculateStructStrSize typeEnv env memberPairs concreteStructTy
|
||||
, " string buffer = CARP_MALLOC(size);"
|
||||
, " string bufferPtr = buffer;"
|
||||
, " String buffer = CARP_MALLOC(size);"
|
||||
, " String bufferPtr = buffer;"
|
||||
, ""
|
||||
, " snprintf(bufferPtr, size, \"(%s \", \"" ++ typeName ++ "\");"
|
||||
, " bufferPtr += strlen(\"" ++ typeName ++ "\") + 2;\n"
|
||||
|
@ -121,13 +121,13 @@ toC toCMode root = emitterSrc (execState (visit startingIndent root) (EmitterSta
|
||||
visitStr' indent str i =
|
||||
-- | This will allocate a new string every time the code runs:
|
||||
-- do let var = freshVar i
|
||||
-- appendToSrc (addIndent indent ++ "string " ++ var ++ " = strdup(\"" ++ str ++ "\");\n")
|
||||
-- appendToSrc (addIndent indent ++ "String " ++ var ++ " = strdup(\"" ++ str ++ "\");\n")
|
||||
-- return var
|
||||
-- | This will use the statically allocated string in the C binary (can't be freed):
|
||||
do let var = freshVar i
|
||||
varRef = freshVar i ++ "_ref";
|
||||
appendToSrc (addIndent indent ++ "static string " ++ var ++ " = \"" ++ escapeString str ++ "\";\n")
|
||||
appendToSrc (addIndent indent ++ "string *" ++ varRef ++ " = &" ++ var ++ ";\n")
|
||||
appendToSrc (addIndent indent ++ "static String " ++ var ++ " = \"" ++ escapeString str ++ "\";\n")
|
||||
appendToSrc (addIndent indent ++ "String *" ++ varRef ++ " = &" ++ var ++ ";\n")
|
||||
return varRef
|
||||
visitString indent (XObj (Str str) (Just i) _) = visitStr' indent str i
|
||||
visitString indent (XObj (Pattern str) (Just i) _) = visitStr' indent str i
|
||||
|
@ -4,6 +4,7 @@ module Parsing (parse, validCharacters, balance) where
|
||||
|
||||
import Text.Parsec ((<|>))
|
||||
import qualified Text.Parsec as Parsec
|
||||
import qualified Data.Char as Char
|
||||
import qualified Data.Set as Set
|
||||
import Obj
|
||||
import Types
|
||||
|
@ -131,9 +131,9 @@ generateTemplateFuncStrOrPrn :: String -> Ty -> (String, Binder)
|
||||
generateTemplateFuncStrOrPrn name funcTy = defineTemplate
|
||||
(SymPath [] name)
|
||||
(FuncTy [funcTy] StringTy)
|
||||
(toTemplate "string $NAME (void *f)")
|
||||
(toTemplate "String $NAME (void *f)")
|
||||
(toTemplate $ unlines ["$DECL {"
|
||||
," static string lambda = \"λ\";"
|
||||
," static String lambda = \"λ\";"
|
||||
," return String_copy(&lambda);"
|
||||
,"}"])
|
||||
(const [])
|
||||
|
@ -87,8 +87,8 @@ tyToCManglePtr _ BoolTy = "bool"
|
||||
tyToCManglePtr _ FloatTy = "float"
|
||||
tyToCManglePtr _ DoubleTy = "double"
|
||||
tyToCManglePtr _ LongTy = "long"
|
||||
tyToCManglePtr _ StringTy = "string"
|
||||
tyToCManglePtr _ PatternTy = "pattern"
|
||||
tyToCManglePtr _ StringTy = "String"
|
||||
tyToCManglePtr _ PatternTy = "Pattern"
|
||||
tyToCManglePtr _ CharTy = "char"
|
||||
tyToCManglePtr _ UnitTy = "void"
|
||||
tyToCManglePtr _ (VarTy x) = x
|
||||
|
Loading…
Reference in New Issue
Block a user