Feature request: For i In var1, var2, ... varN

General FreeBASIC programming questions.
Post Reply
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Feature request: For i In var1, var2, ... varN

Post by fzabkar »

I recently had a need for something akin to an MSDOS For In Do batch command:

Code: Select all

For i In var1, expression2, ... 

   execute something using i

Next i
I realise this can be achieved by prefilling an array and then looping through each element of the array, but ISTM that this might be a useful feature nevertheless.
Lost Zergling
Posts: 534
Joined: Dec 02, 2011 22:51
Location: France

Re: Feature request: For i In var1, var2, ... varN

Post by Lost Zergling »

Using lzle, you might have a syntax close to your requirement, handling strings.
Dim MyList As List
MyList.BlindTag(var1)
MyList.BlindTag(var2)
...
For i=1 to MyList.AllOf : MyList.BlindStep
? MyList.Tag
Next i
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Feature request: For i In var1, var2, ... varN

Post by coderJeff »

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:

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 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.
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: Feature request: For i In var1, var2, ... varN

Post by fzabkar »

Many thanks. It looks like I have more reading to do.

I presume this is the best resource for LZLE:

viewtopic.php?f=9&t=26551
Lost Zergling
Posts: 534
Joined: Dec 02, 2011 22:51
Location: France

Re: Feature request: For i In var1, var2, ... varN

Post by Lost Zergling »

Yes, lots of more stuff would be usefull, usable status.
Solution will be depending on your expectations. Lzle pretty 'heavy', lots of features.
Designed to target code 'industrialization'
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Feature request: For i In var1, var2, ... varN

Post by fxm »

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:

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 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.
I don't see how to simplify this code by using '__FB_IIF__', because of 'arg...' (variable argument) ?
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Feature request: For i In var1, var2, ... varN

Post by coderJeff »

fxm wrote: Jun 07, 2022 11:38I don't see how to simplify this code by using '__FB_IIF__', because of 'arg...' (variable argument) ?
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
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Re: Feature request: For i In var1, var2, ... varN

Post by angros47 »

Speaking of that, I read that the latest version of Freebasic supports some preprocessor directives for templates. How is the current situation, regarding templayes/generic support?
adeyblue
Posts: 299
Joined: Nov 07, 2019 20:08

Re: Feature request: For i In var1, var2, ... varN

Post by adeyblue »

Same as always I think. There are better facilities to deal with macro arguments and sequences (like RIGHTOF, LEFTOF, ARG_COUNT shown above etc), but it's still just the preprocessor and various degrees and complexity of token pasting with all the inherent simplicity/drawbacks that has.
Post Reply