From bd67ac89a3dfeedf1a9393218e05de5ac73cc787 Mon Sep 17 00:00:00 2001 From: Lucas GAUTHERON Date: Fri, 20 May 2011 20:50:22 +0000 Subject: [PATCH] "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 --- config/docs.cfg | 7 +++--- docs/reference.xml | 48 +++++++++++++++++++++++++++--------------- source/src/command.cpp | 24 +++++++++++++-------- 3 files changed, 49 insertions(+), 30 deletions(-) diff --git a/config/docs.cfg b/config/docs.cfg index 38586c2f..0beba62f 100644 --- a/config/docs.cfg +++ b/config/docs.cfg @@ -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]; diff --git a/docs/reference.xml b/docs/reference.xml index ccd3f298..225573dc 100644 --- a/docs/reference.xml +++ b/docs/reference.xml @@ -554,6 +554,11 @@ + + + + + @@ -573,34 +578,43 @@ + + + + + Aborts a loop created with a 'loop' or 'while' command. - - - 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. - - - - output: 0 1 2 3 4 - - - - output: 0 1 2 3 4 - Note how the result is no different than in the example before. - - - + output: 0 1 2 3 + + + + + + + Skip current loop iteration. + + + + output: 0 1 3 4 + + + + + + + + + Concatenates multiple strings with spaces inbetween. diff --git a/source/src/command.cpp b/source/src/command.cpp index dd44bea3..b2d84c0b 100644 --- a/source/src/command.cpp +++ b/source/src/command.cpp @@ -12,7 +12,9 @@ bool contextsealed = false; bool contextisolated[IEXC_NUM] = { false }; int execcontext; -hashtable *idents = NULL; // contains ALL vars/commands/aliases +bool loop_break = false, loop_skip = false; // break or continue (skip) current loop + +hashtable *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);