more progress

master
Daniel Kolesa 2015-08-08 03:45:02 +01:00
parent 7d584bbfb1
commit 543c532d33
1 changed files with 67 additions and 48 deletions

View File

@ -3748,64 +3748,85 @@ void init_lib_list(CsState &cs) {
while (--len > 0 && parselist(s, start, end, qstart, qend)); while (--len > 0 && parselist(s, start, end, qstart, qend));
cs.result->set_str(dup_ostr(ostd::ConstCharRange(list, qend - list))); cs.result->set_str(dup_ostr(ostd::ConstCharRange(list, qend - list)));
}); });
}
/* cs.add_command("listfind", "rse", [](CsState &cs, Ident *id,
void listfind(CsState &cs, Ident *id, const char *list, const ostd::Uint32 *body) { const char *list,
if (id->type != ID_ALIAS) { const ostd::Uint32 *body) {
if (id->type != ID_ALIAS) {
cs.result->set_int(-1);
return;
}
IdentStack stack;
int n = -1;
for (const char *s = list, *start, *end; parselist(s, start, end);) {
++n;
cs_set_iter(*id, dup_ostr(ostd::ConstCharRange(start,
end - start)), stack);
if (cs.run_bool(body)) {
cs.result->set_int(n);
goto found;
}
}
cs.result->set_int(-1); cs.result->set_int(-1);
return;
}
IdentStack stack;
int n = -1;
for (const char *s = list, *start, *end; parselist(s, start, end);) {
++n;
setiter(*id, dup_ostr(ostd::ConstCharRange(start, end - start)), stack);
if (cs.run_bool(body)) {
cs.result->set_int(n);
goto found;
}
}
cs.result->set_int(-1);
found: found:
if (n >= 0) id->pop_arg(); if (n >= 0)
} id->pop_arg();
COMMAND(listfind, "rse"); });
void listassoc(CsState &cs, Ident *id, const char *list, const ostd::Uint32 *body) { cs.add_command("listassoc", "rse", [](CsState &cs, Ident *id,
if (id->type != ID_ALIAS) return; const char *list,
IdentStack stack; const ostd::Uint32 *body) {
int n = -1; if (id->type != ID_ALIAS)
for (const char *s = list, *start, *end, *qstart; parselist(s, start, end);) { return;
++n; IdentStack stack;
setiter(*id, dup_ostr(ostd::ConstCharRange(start, end - start)), stack); int n = -1;
if (cs.run_bool(body)) { const char *s = list, *start, *end, *qstart;
if (parselist(s, start, end, qstart)) cs.result->set_str(listelem(start, end, qstart).disown()); while (parselist(s, start, end)) {
break; ++n;
cs_set_iter(*id, dup_ostr(ostd::ConstCharRange(start,
end - start)), stack);
if (cs.run_bool(body)) {
if (parselist(s, start, end, qstart))
cs.result->set_str(listelem(start, end, qstart).disown());
break;
}
if (!parselist(s))
break;
} }
if (!parselist(s)) break; if (n >= 0)
} id->pop_arg();
if (n >= 0) id->pop_arg(); });
}
COMMAND(listassoc, "rse");
#define LISTFIND(name, fmt, type, init, cmp) \ #define CS_CMD_LIST_FIND(name, fmt, type, init, cmp) \
ICOMMAND(name, "s" fmt "i", (CsState &cs, char *list, type *val, int *skip), \ cs.add_command(name, "s" fmt "i", [](CsState &cs, char *list, \
{ \ type *val, int *skip) { \
int n = 0; \ int n = 0; \
init; \ init; \
for(const char *s = list, *start, *end, *qstart; parselist(s, start, end, qstart); n++) \ const char *s = list, *start, *end, *qstart; \
{ \ for (; parselist(s, start, end, qstart); n++) { \
if(cmp) { cs.result->set_int(n); return; } \ if (cmp) { \
for (int i = 0; i < *skip; ++i) { if(!parselist(s)) goto notfound; n++; } \ cs.result->set_int(n); \
return; \
} \
for (int i = 0; i < *skip; ++i) { \
if (!parselist(s)) \
goto notfound; \
++n; \
} \
} \ } \
notfound: \ notfound: \
cs.result->set_int(-1); \ cs.result->set_int(-1); \
}); });
LISTFIND(listfind=, "i", int, , parseint(start) == *val);
LISTFIND(listfind=f, "f", float, , parsefloat(start) == *val);
LISTFIND(listfind=s, "s", char, int len = (int)strlen(val), int(end - start) == len && !memcmp(start, val, len));
CS_CMD_LIST_FIND("listfind=", "i", int, , parseint(start) == *val);
CS_CMD_LIST_FIND("listfind=f", "f", float, , parsefloat(start) == *val);
CS_CMD_LIST_FIND("listfind=s", "s", char, int len = (int)strlen(val),
int(end - start) == len && !memcmp(start, val, len));
#undef CS_CMD_LIST_FIND
}
/*
#define LISTASSOC(name, fmt, type, init, cmp) \ #define LISTASSOC(name, fmt, type, init, cmp) \
ICOMMAND(name, "s" fmt, (CsState &cs, char *list, type *val), \ ICOMMAND(name, "s" fmt, (CsState &cs, char *list, type *val), \
{ \ { \
@ -4374,10 +4395,8 @@ void init_lib_string(CsState &cs) {
}); });
cs.add_command("format", "V", [](CsState &cs, TaggedValue *v, int n) { cs.add_command("format", "V", [](CsState &cs, TaggedValue *v, int n) {
if (n <= 0) { if (n <= 0)
cs.result->set_str(dup_ostr(""));
return; return;
}
ostd::Vector<char> s; ostd::Vector<char> s;
const char *f = v[0].get_str(); const char *f = v[0].get_str();
while (*f) { while (*f) {