Code: Select all
For i In var1, expression2, ...
execute something using i
Next i
Code: Select all
For i In var1, expression2, ...
execute something using i
Next i
Code: Select all
#if( __FB_VERSION__ < "1.08.0" )
#error "foreach macro requires fbc 1.08.0 or later"
#endif
#macro foreach ? ( varname, args... )
__fb_uniqueid_push__( __foreach_array )
#if( __FB_ARG_COUNT__( args ) = 0 )
dim __fb_uniqueid__( __foreach_array ) (0 to ...) _
as __FB_ARG_RIGHTOF__( _
__FB_ARG_LEFTOF__( varname, IN, varname ), AS, integer ) _
= { __FB_ARG_RIGHTOF__( varname, IN ) }
#else
dim __fb_uniqueid__( __foreach_array ) (0 to ...) _
as __FB_ARG_RIGHTOF__( _
__FB_ARG_LEFTOF__( varname, IN, varname ), AS, integer ) _
= { __FB_ARG_RIGHTOF__( varname, IN ), args }
#endif
for __foreach_index as integer = _
lbound( __fb_uniqueid__( __foreach_array ) ) _
to ubound( __fb_uniqueid__( __foreach_array ) )
var byref __FB_ARG_LEFTOF__( varname, AS, varname ) = _
__fb_uniqueid__( __foreach_array )( __foreach_index )
#endmacro
foreach x as integer in 3, 1, 4, 1, 5, 9, 2, 6
print x
next
print
var a = "variable"
foreach s as string in a, "hello", a, "there", a
print s
next
print
'' nested too
foreach x as integer in 1, 2, 3
foreach y as string in "a", "b", "c"
print x, y
next
next
print
'' and functions
foreach x as double in rnd(), rnd(), rnd()
print x
next
print
'' how about pointers?
foreach x as zstring ptr in @"a", @"b", @"c"
print *x
next
print
sleep
I don't see how to simplify this code by using '__FB_IIF__', because of 'arg...' (variable argument) ?coderJeff wrote: ↑May 15, 2022 1:43 A little preprocessor #macro tomfoolery might work for simple stuff. Syntax errors are sure to be cryptic, sorry.
foreach variable as datatype in expr [, ...] macro with simple examples:I had plans to add a __FB_IIF__( expr, true-result, false-result ) and if I had, the macro could have been a little more compact.Code: Select all
#if( __FB_VERSION__ < "1.08.0" ) #error "foreach macro requires fbc 1.08.0 or later" #endif #macro foreach ? ( varname, args... ) __fb_uniqueid_push__( __foreach_array ) #if( __FB_ARG_COUNT__( args ) = 0 ) dim __fb_uniqueid__( __foreach_array ) (0 to ...) _ as __FB_ARG_RIGHTOF__( _ __FB_ARG_LEFTOF__( varname, IN, varname ), AS, integer ) _ = { __FB_ARG_RIGHTOF__( varname, IN ) } #else dim __fb_uniqueid__( __foreach_array ) (0 to ...) _ as __FB_ARG_RIGHTOF__( _ __FB_ARG_LEFTOF__( varname, IN, varname ), AS, integer ) _ = { __FB_ARG_RIGHTOF__( varname, IN ), args } #endif for __foreach_index as integer = _ lbound( __fb_uniqueid__( __foreach_array ) ) _ to ubound( __fb_uniqueid__( __foreach_array ) ) var byref __FB_ARG_LEFTOF__( varname, AS, varname ) = _ __fb_uniqueid__( __foreach_array )( __foreach_index ) #endmacro foreach x as integer in 3, 1, 4, 1, 5, 9, 2, 6 print x next print var a = "variable" foreach s as string in a, "hello", a, "there", a print s next print '' nested too foreach x as integer in 1, 2, 3 foreach y as string in "a", "b", "c" print x, y next next print '' and functions foreach x as double in rnd(), rnd(), rnd() print x next print '' how about pointers? foreach x as zstring ptr in @"a", @"b", @"c" print *x next print sleep
Indeed, so you can imagine my disappointment when I finally realized why this couldn't simplify. I still added the __FB_IIF__() macro though since it has it's uses for hacking on fbc token / source text. But in some places we are at the preprocessor's limits of lexical capability since #macro's and source code are both defined and invoked with parens and commas