"break" command changed + new "continue" command

git-svn-id: svn://svn.code.sf.net/p/actiongame/code/trunk/ac@6451 e016b1c5-ed68-44aa-a2f7-31cac88cffde
This commit is contained in:
Lucas GAUTHERON
2011-05-20 20:50:22 +00:00
parent 8bb842309e
commit bd67ac89a3
3 changed files with 49 additions and 30 deletions
+3 -4
View File
@@ -197,10 +197,9 @@ docexample [akimboendaction 0] [none];
docexample [akimboendaction 1] [switch to primary];
docexample [akimboendaction 2] [switch to grenades, else switch to primary];
docident [break] [Aborts a loop created with a 'loop' or 'while' command.];
docexample [loop i 10 [ echo $i; if (= $i 4) break ]] [Output: 0 1 2 3 4];
docexample [loop i 10 [ if (= $i 4) break; echo $i ]] [Note how the result is no different than in the example before.];
docexample [loop i 10 [ if (= $i 4) [break] [echo $i] ]] [Output: 0 1 2 3];
docremark [Note: As opposed to (almost) any other scripting language, this command does NOT abort execution of the current iteration. Instead, it aborts the loop after the execution of that iteration has finished. If you want to break out of a loop without executing the rest of the code, use an if-else construction as in the last example.];
docexample [loop i 10 [ if (= $i 4) [ break ]; echo $i]] [Output: 0 1 2 3];
docident [continue] [Skip current loop iteration.]
docexample [loop i 5 [ if (= $i 2) [ continue ]; echo $i]] [output: 0 1 3 4]
docident [checkalias] [Determines if the argument given is an existing alias or not.];
docargument [A] [the alias to check for] [] [0];
docexample [hello = ""; echo (checkalias hello)] [Output: 1];
+31 -17
View File
@@ -554,6 +554,11 @@
<argument token="N" description="the amount of loops"/>
<argument token="body" description="the body to execute on each iteration"/>
</arguments>
<references>
<identifierReference identifier="break"/>
<identifierReference identifier="continue"/>
<identifierReference identifier="while"/>
</references>
</command>
<command name="while">
@@ -573,34 +578,43 @@
<argument token="cond" description="the condition" valueNotes="0 (false) or anything else (true)"/>
<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>
<remarks>
<remark>
Note: As opposed to (almost) any other scripting language, this command does NOT abort execution of the current iteration.
Instead, it aborts the loop after the execution of that iteration has finished.
If you want to break out of a loop without executing the rest of the code, use an if-else construction as in the last example.
</remark>
</remarks>
<examples>
<example>
<code><![CDATA[loop i 10 [ echo $i; if (= $i 4) break ]]]></code>
<explanation>output: 0 1 2 3 4</explanation>
</example>
<example>
<code><![CDATA[loop i 10 [ if (= $i 4) break; echo $i ]]]></code>
<explanation>output: 0 1 2 3 4</explanation>
<explanation>Note how the result is no different than in the example before.</explanation>
</example>
<example>
<code><![CDATA[loop i 10 [ if (= $i 4) [break] [echo $i] ]]]></code>
<code><![CDATA[loop i 10 [ if (= $i 4) [ break ]; echo $i]]]></code>
<explanation>output: 0 1 2 3</explanation>
</example>
</examples>
<references>
<identifierReference identifier="continue"/>
<identifierReference identifier="loop"/>
<identifierReference identifier="while"/>
</references>
</command>
<command name="continue">
<description>Skip current loop iteration.</description>
<examples>
<example>
<code><![CDATA[loop i 5 [ if (= $i 2) [ continue ]; echo $i]]]></code>
<explanation>output: 0 1 3 4</explanation>
</example>
</examples>
<references>
<identifierReference identifier="break"/>
<identifierReference identifier="loop"/>
<identifierReference identifier="while"/>
</references>
</command>
<command name="concat">
<description>Concatenates multiple strings with spaces inbetween.</description>
<examples>
+15 -9
View File
@@ -12,7 +12,9 @@ bool contextsealed = false;
bool contextisolated[IEXC_NUM] = { false };
int execcontext;
hashtable<const char *, ident> *idents = NULL; // contains ALL vars/commands/aliases
bool loop_break = false, loop_skip = false; // break or continue (skip) current loop
hashtable<const char *, ident> *idents = NULL; // contains ALL vars/commands/aliases
bool persistidents = true;
@@ -506,6 +508,7 @@ char *executeret(const char *p) // all evaluation hap
{
for(bool cont = true; cont;) // for each ; seperated statement
{
if(loop_skip) break;
int numargs = MAXWORDS, infix = 0;
loopi(MAXWORDS) // collect all argument values
{
@@ -947,8 +950,6 @@ COMMAND(execdir,ARG_1STR);
// below the commands that implement a small imperative language. thanks to the semantics of
// () and [] expressions, any control construct can be defined trivially.
bool breakout = false;
void ifthen(char *cond, char *thenp, char *elsep) { commandret = executeret(cond[0]!='0' ? thenp : elsep); }
void loopa(char *var, char *times, char *body)
{
@@ -959,7 +960,8 @@ void loopa(char *var, char *times, char *body)
char *buf = newstring("0", 16);
pushident(*id, buf);
execute(body);
if(breakout) breakout = false;
if(loop_skip) loop_skip = false;
if(loop_break) loop_break = false;
else
{
loopi(t-1)
@@ -971,9 +973,10 @@ void loopa(char *var, char *times, char *body)
}
itoa(id->action, i+1);
execute(body);
if(breakout)
if(loop_skip) loop_skip = false;
if(loop_break)
{
breakout = false;
loop_break = false;
break;
}
}
@@ -985,14 +988,16 @@ void whilea(char *cond, char *body)
while(execute(cond))
{
execute(body);
if(breakout)
if(loop_skip) loop_skip = false;
if(loop_break)
{
breakout = false;
loop_break = false;
break;
}
}
}
void breaka() { breakout = true; }
void breaka() { loop_skip = loop_break = true; }
void continuea() { loop_skip = true; }
void concat(char *s) { result(s); }
void concatword(char *s) { result(s); }
@@ -1201,6 +1206,7 @@ COMMANDN(c, colora, ARG_1STR);
COMMANDN(loop, loopa, ARG_3STR);
COMMANDN(while, whilea, ARG_2STR);
COMMANDN(break, breaka, ARG_NONE);
COMMANDN(continue, continuea, ARG_NONE);
COMMANDN(if, ifthen, ARG_3STR);
COMMAND(exec, ARG_1STR);
COMMAND(concat, ARG_CONC);