## for(var,start,end,procedure) ## ## A "for" loop in the macro language m4. This "for" accepts "break" as an ## instruction to exit "for". The loop test is "var == end" NOT "var >= end". ## define(for,`undefine(`_break')define(`$1',`$2')dnl _for(`$1',`$2',`$3',`$4')')dnl define(_for,`$4`'ifelse($1,`$3',,dnl `ifdef(`_break',,dnl `define(`$1',incr($1))dnl _for(`$1',`$2',`$3',`$4')')')')dnl define(`break',`define(`_break')')dnl ## Test it: for(i,1,3,`variable `i' is i ')dnl ## define(`C',`dnl')dnl C This defined a FORTRAN style comment which does not get passed through m4. C C We now dissect the "for" macro. First note the "if" constructs in m4: C C ifdef(`name',true_clause,false_clause) C ifelse(strg1,strg2,true_clause, optional_false_clause) C ifelse(strg1,strg2,true_clause, ...args...) is the same as C ifelse(strg1,strg2,true_clause,ifelse(...args...)) C C Also note the trailing "dnl"s mean "delete the following new line" C C Define "for" by undefining private "_break", setting var($1) to start($2), C and expanding "for" to a private _for C define(for,`undefine(`_break')define(`$1',`$2')dnl _for(`$1',`$2',`$3',`$4')')dnl C C Definition of _for: C C Do procedure ($4). The trailing `' separates $4 from the rest. C define(_for,`$4`'dnl C If var == end then do no more expansions (the true clause is empty). ifelse($1,`$3',,dnl C else if var != end then do the following (the false clause): C if _break is defined do no more expansions `ifdef(`_break',,dnl C else if _break is not defined, increment var `define(`$1',incr($1))dnl C and expand "_for" with the new var value _for(`$1',`$2',`$3',`$4')')'dnl C End of ifelse and end of definition of "_for". )')dnl C C C When a public "break" is encountered in "procedure", define the C private break "_break". "for" will see the break request at C the appropriate stage. C define(`break',`define(`_break')')dnl C C Now test the "for" loop once again C undefine(`i')dnl for(i,1,3,`variable `i' is i ')dnl