looplist command

git-svn-id: svn://svn.code.sf.net/p/actiongame/code/trunk/ac@6534 e016b1c5-ed68-44aa-a2f7-31cac88cffde
This commit is contained in:
Lucas GAUTHERON
2011-06-14 11:20:13 +00:00
parent 22909543ad
commit 040ec8d482
3 changed files with 55 additions and 34 deletions
+5
View File
@@ -111,6 +111,11 @@ docargument [N] [the amount of loops] [] [0];
docargument [body] [the body to execute on each iteration] [] [0];
docremark [This command sets the alias you choose, as first argument, from 0 to N-1 for every iteration.];
docexample [loop i 10 [ echo $i ]] [];
docident [looplist] [Browses a list and executes a body for each element];
docargument [L] [the list] [0];
docargument [E] [the alias containing the current element] [0];
docargument [body] [the body to execute for each element] [0];
docexample [looplist "zero one two three" number [ echo $number ]] [];
docident [mod] [Performs a modulo operation.];
docargument [A] [the dividend] [] [0];
docargument [B] [the divisor] [] [0];
+19
View File
@@ -612,6 +612,25 @@
</references>
</command>
<command name="looplist">
<description>Browses a list and executes a body for each element</description>
<examples>
<example>
<code><![CDATA[looplist "zero one two three" number [ echo $number ]]]></code>
</example>
</examples>
<arguments>
<argument token="V" description="the list to browse"/>
<argument token="N" description="the alias containing the current element value."/>
<argument token="body" description="the body to execute on each iteration"/>
</arguments>
<references>
<identifierReference identifier="break"/>
<identifierReference identifier="continue"/>
<identifierReference identifier="loop"/>
</references>
</command>
<command name="break">
<description>Aborts a loop created with a 'loop' or 'while' command.</description>
<examples>
+31 -34
View File
@@ -1044,37 +1044,6 @@ void format(char **args, int numargs)
#define whitespaceskip s += strspn(s, "\n\t \r")
#define elementskip *s=='"' ? (++s, s += strcspn(s, "\"\n\0"), s += *s=='"') : s += strcspn(s, "\n\t \0")
void looplista(char *s, char *var, char *body)
{
int n = 0;
whitespaceskip;
loop_level++;
ident *id = newident(var, execcontext);
if(id->type!=ID_ALIAS) return;
char *buf = newstring(MAXSTRLEN);
for(; *s; n++)
{
char *e = strtok(newstring(s), "\"\n\0\t \r");
if(buf != id->action)
{
if(id->action != id->executing) delete[] id->action;
id->action = buf = newstring(MAXSTRLEN);
}
copystring(id->action, e);
execute(body);
if(loop_skip) loop_skip = false;
if(loop_break)
{
loop_break = false;
break;
}
elementskip, whitespaceskip;
}
loop_level--;
}
void explodelist(const char *s, vector<char *> &elems)
{
whitespaceskip;
@@ -1087,6 +1056,36 @@ void explodelist(const char *s, vector<char *> &elems)
}
}
void looplist(char *list, char *var, char *body)
{
ident *id = newident(var, execcontext);
if(id->type!=ID_ALIAS) return;
char *buf = newstring(MAXSTRLEN);
vector<char *> elems;
explodelist(list, elems);
loop_level++;
loopv(elems)
{
const char *elem = elems[i];
if(buf != id->action)
{
if(id->action != id->executing) delete[] id->action;
id->action = buf = newstring(MAXSTRLEN);
}
copystring(id->action, elem);
execute(body);
if(loop_skip) loop_skip = false;
if(loop_break)
{
loop_break = false;
break;
}
}
loop_level--;
}
char *indexlist(const char *s, int pos)
{
whitespaceskip;
@@ -1114,8 +1113,6 @@ int listlen(char *s)
return n;
}
void at(char *s, char *pos)
{
commandret = indexlist(s, ATOI(pos));
@@ -1271,7 +1268,7 @@ void strreplace (const char *source, const char *search, const char *replace)
COMMANDN(c, colora, ARG_1STR);
COMMANDN(loop, loopa, ARG_3STR);
COMMANDN(looplist, looplista, ARG_3STR);
COMMAND(looplist, ARG_3STR);
COMMANDN(while, whilea, ARG_2STR);
COMMANDN(break, breaka, ARG_NONE);
COMMANDN(continue, continuea, ARG_NONE);