Throwaway Suggestion: #tempdef for #macros? (Clarified)

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Throwaway Suggestion: #tempdef for #macros? (Clarified)

Post by anonymous1337 »

http://freebasic.net/forum/viewtopic.php?f=7&t=22369

See the macro'ized version. I use #define to define very temporary helpers, then #undef to undefine them so I can use the macro with multiple types and still use the same #define names.

It would be nifty (although, I'm not sure if essential) if #macros had the concept of scope, and #tempdef's were automatically #undef'd at the end of the macro. Maybe something more macro-frenzied like fb-ext could make use of it?

I wonder if this would complicate the detection of recursive macro calls... say, if #tempdef could somehow allow more powerful preprocessing depending on how scope is implemented. (Ex: Nested macro calls.) What would it take to make our preprocessor Turing-complete? :)

I want to turn this:

Code: Select all

#macro MakeGlobalStackOf(T)

#define GlobalStackOfName GlobalStackOf##T
#define global_stack_array_name global_stack_array_of_##T

type GlobalStackOfName
...
end type

sub GlobalStackOfName.push( byref value as T )
    global_stack_array_name( index ) = value
end sub

#undef GlobalStackOfName
#undef global_stack_array_name

#endmacro
Into this:

Code: Select all

#macro MakeGlobalStackOf(T)

#tempdef GlobalStackOfName GlobalStackOf##T
#tempdef global_stack_array_name global_stack_array_of_##T

type GlobalStackOfName
...
end type

sub GlobalStackOfName.push( byref value as T )
    global_stack_array_name( index ) = value
end sub

#endmacro
Last edited by anonymous1337 on Mar 21, 2014 16:49, edited 2 times in total.
MOD
Posts: 555
Joined: Jun 11, 2009 20:15

Re: Throwaway Suggestion: #tempdef for #macros?

Post by MOD »

Have a look at the include files of mdTypes. You don't need fancy new scoped macros but simply use the existing ones to reach your goals.
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Re: Throwaway Suggestion: #tempdef for #macros?

Post by anonymous1337 »

MOD:

I am looking, but I am only seeing what I tried to avoid:

Code: Select all

Function mdCollection##E##P.add(ByRef element As E P) As Boolean
	Dim As mdBasicNode##E##P Ptr currentNode = @This.node
Which I would reduce to mdCollectionName by creating a #define for it.

Is there a specific example you think would help me understand?
MOD
Posts: 555
Joined: Jun 11, 2009 20:15

Re: Throwaway Suggestion: #tempdef for #macros?

Post by MOD »

It will be reduced. If you use mdCollection(String) the macro will create a mdCollectionString. So later, you can use both mdCollection(String) and mdCollectionString, because it's the same. This way you have one macro for all types - it's generic.

Have a look at the examples and use -pp to see what the macros are generating.
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Re: Throwaway Suggestion: #tempdef for #macros?

Post by anonymous1337 »

MOD, I feel you are misunderstanding me.

I already know the macro will go through text substitution during pre-precossing. I've already looked at mdTypes, and it is doing exactly what I was doing before and didn't like. Have you looked at the Macro'ized version of the Stack I wrote? I think it would demonstrate that I know this about macros already.

Before, I was doing this (what mdTypes has all over the place):

Code: Select all

#macro MakeGlobalStackOf(T)

type GlobalStackOf##T
...
end type

sub GlobalStackOf##T.push( byref value as T )
    global_stack_array_of_##T( index ) = value
end sub

#endmacro
But I changed that to:

Code: Select all

#macro MakeGlobalStackOf(T)

#define GlobalStackOfName GlobalStackOf##T
#define global_stack_array_name global_stack_array_of_##T

type GlobalStackOfName
...
end type

sub GlobalStackOfName.push( byref value as T )
    global_stack_array_name( index ) = value
end sub

#undef GlobalStackOfName
#undef global_stack_array_name

#endmacro
Because I feel like Whatever##X##Y all over code makes it ugly and really difficult to read. This is why I would want something like #tempdef

I see some #defines being used in mdTypes how I would want to use #tempdef at the top of source code files, but then there's also this. Removing code like this is why I want #tempdef:

Code: Select all

Function mdQueue##E##P.iterator() As mdIterator##E##P
	Dim As mdIterator##E##P newIterator = This.node.nex
	Return newIterator
End Function
Post Reply