Workarounds for preprocessor oddities?

General FreeBASIC programming questions.
Post Reply
shadow008
Posts: 107
Joined: Nov 26, 2013 2:43

Workarounds for preprocessor oddities?

Post by shadow008 »

I'm rather confused as to how to get the preprocessor to properly resolve tokens without resorting to trial and error.

Specifically I'm noticing that
1) #print and #error do not allow for token resolution unless that token is at the very beginning of the line.
2) The wiki description of typeof() is clearly omitting something about its actual behavior. The wiki states the following:
Typeof is a compiler intrinsic that replaces itself with the type of the variable passed to it.
But this "compiler intrinsic" does not behave like other compiler intrinsics (See other intrinsics: https://www.freebasic.net/wiki/CatPgDddefines) as it does not actually replace itself with the type of the variable passed to it in some circumstances.

I've noted the issues in the following code snippet:

Code: Select all

#define _TEST asdf

dim _TEST as integer

'Let's see if we can output details on both the
'variable being used, and its type...

'_TEST is not resolved in either instances.
'typeof is not resolved either
#print Variable: _TEST is of type typeof(_TEST)

'Nope
#print Variable: ##_TEST is of type typeof(##_TEST)

'Closer, but typeof is still not resolved
'This is rather excessive for an only partially working solution
__FB_UNQUOTE__(__FB_EVAL__("#print Variable: " & __FB_QUOTE__(_TEST) & " is of type " & __FB_QUOTE__(typeof(_TEST))))

'This is the most straightforward, minimal solution, but
'why does it have to be spread across 3 lines?
'This could be partially joined with the above solution
'for a 2 line solution but damn is it ugly.
#print Variable:
#print _TEST is of type
#print typeof(_TEST) 'Demands its own line because...?  It's special I guess

'Syntax error.  I'm not sure why, seems like it should be valid to me.
'#print __FB_EVAL__(typeof(_TEST))

'End-of-line what now?  This is both unexpected and bizarre.
'#print typeof(_TEST) should be an integer

'Cannot capture the actual type name in quotes because...?
'I would expect this to output "INTEGER", not "typeof(asdf)"
'AND JUST WHY is _TEST being resolved here but it wasn't being
'resolved above in the first couple examples??
#print __FB_QUOTE__(typeof(_TEST))
Is there a more elegant workaround to the fact that tokens are not being resolved mid-statement with #print and #error (unless those tokens are at the start of the line)?
Also are any of the noted issues with typeof() a bug or am I just using it wrong?
Post Reply