Page 2 of 2

Re: Expanded View of Macros

Posted: Thu Nov 14, 2013 9:34 am
by BorisTheOld
TI-994A wrote:To reiterate, if anyone knows of a way, or has some utility that could expand PureBasic macros within the source listing, please do share.
PB macros don't always expand the way one would expect. The PB macro processor has some interesting "features", so it's likely that no one has made the effort to write the kind of program you're looking for. Of course, you could write your own, but it seems like a lot of effort for very little return.

Other than doing what luis and I have suggested, your best bet might be to wait for Fred to update the compiler.

Re: Expanded View of Macros

Posted: Thu Nov 14, 2013 11:40 am
by luis
BorisTheOld wrote: Actually, it's PB's macro feature which is antiquated. It's close to being non-existant.
I agree, it's very 'basic'. A stronger macro language or the ability to have some kind of template coding would be very nice.
BorisTheOld wrote: your best bet might be to wait for Fred to update the compiler
http://www.purebasic.fr/english/viewtop ... 30#p312230

Let's hope. After all PB does all the work already, it should just need to collect the pieces and spit out a file.

Re: Expanded View of Macros

Posted: Thu Nov 14, 2013 1:43 pm
by TI-994A
BorisTheOld wrote:Actually, it's PB's macro feature which is antiquated. ...
BorisTheOld wrote:PB macros don't always expand the way one would expect. The PB macro processor has some interesting "features"...
Hi BorisTheOld. I don't know enough about the inner workings of PureBasic's macro system to comment on you earlier remark, but what do you mean when you say the way one would expect? :?

Re: Expanded View of Macros

Posted: Sat Nov 16, 2013 5:09 am
by BorisTheOld
TI-994A wrote:Hi BorisTheOld. I don't know enough about the inner workings of PureBasic's macro system to comment on you earlier remark, but what do you mean when you say the way one would expect? :?
I don't know anything about PB's macro processor. :) Two weeks ago I reported what looks like a bug in the macro processor, but Fred said it's not, and moved me to Feature Requests.

Here's the problem I reported. The first block of code works, but the second block fails.

The "Field" statement expands as follows: Field(x, typInt32) --> x.typInt32 --> x.i

Code: Select all

Macro typInt32
  i
EndMacro

Macro Field (DataName, DataType)
  DataName.DataType
EndMacro

Field(x, typInt32)   ; x.i
With this next block of code, the compiler complains about a missing structure named, "typInt32".

The "Field" statement expands as follows: Field(x, typInt32) --> x.typInt32 , then stops

This time the "typInt32" is not recognized as a macro, but in the first block of code the compiler appeared to continue scanning the text to resolve the "typInt32" macro. Obviously, under certain conditions, something is going on in the macro processor to terminate parsing before all possible macro references have been resolved.

Code: Select all

Macro typInt32
  i
EndMacro

Macro Field (DataName, DataType)
  DataName.typ#DataType
EndMacro

Field(x, Int32)   ; structure not found: typint32
All other macro processors I've worked with keep parsing the text until all macro references have been resolved. PB seems to be working only with the parameters, then dropping the expanded text into the macro body as a final step. This unusual behaviour severely limits what can be done with PB macros, and makes it difficult to write utility programs to expand macros the way PB does.

Re: Expanded View of Macros

Posted: Sat Nov 16, 2013 4:53 pm
by TI-994A
BorisTheOld wrote:The "Field" statement expands as follows: Field(x, typInt32) --> x.typInt32 , then stops...

Code: Select all

Macro typInt32
  i
EndMacro

Macro Field (DataName, DataType)
  DataName.typ#DataType
EndMacro

Field(x, Int32)   ; structure not found: typint32
Hello BorisTheOld. That's because concatenation in macros will only work with labels and keywords, and not data types.
The PureBasic Manual (Macros) wrote:The special concatenation character '#' can be used to create new labels or keyword...

Re: Expanded View of Macros

Posted: Sat Nov 16, 2013 10:24 pm
by BorisTheOld
TI-994A wrote:Hello BorisTheOld. That's because concatenation in macros will only work with labels and keywords, and not data types.
The PureBasic Manual (Macros) wrote:The special concatenation character '#' can be used to create new labels or keyword...
The "#" can be used to create data types, I do it all the time. In the following example, "QQBlock" is a dynamically created macro that specifies the class name. So I don't see "labels or keyword" as excluding data types.

Code: Select all

Macro BeginClassData
  Structure str#QQBlock
EndMacro
Also, the help file also says:
When a macro has some parameters, they are replaced in the macro code by the literal expression which is passed to the called macro. No evaluation is done as this stage, which is very important to understand: the evaluation of a line is started once all the macros found on this line are expanded.
This would imply that both my examples should work, not just the first. And that's why I say the behaviour is strange, since PB does not always seek out all macros before it compiles the code. But Fred says this behaviour is by design, so I just work around it.

Re: Expanded View of Macros

Posted: Sun Nov 17, 2013 3:14 am
by TI-994A
BorisTheOld wrote:The "#" can be used to create data types, I do it all the time. In the following example, "QQBlock" is a dynamically created macro that specifies the class name. So I don't see "labels or keyword" as excluding data types.

Code: Select all

Macro BeginClassData
  Structure str#QQBlock
EndMacro
Hello again. In the example above, the concatenation str#QQBlock is for a structure, and not a native data type. Rather than debating the ambiguity of the PureBasic manual, bottom line is, have you been able to define a native data type through the use of a concatenated macro?

Or perhaps, could it be that the macro names themselves could not be called by such concatenations?

Re: Expanded View of Macros

Posted: Sun Nov 17, 2013 5:19 pm
by BorisTheOld
TI-994A wrote:Rather than debating the ambiguity of the PureBasic manual, bottom line is, have you been able to define a native data type through the use of a concatenated macro?
Only by sleight of hand. :)

Since the anomalous behaviour is hard coded into the macro processor, the only way around the problem is to fake it.

So, although this doesn't work:

Code: Select all

Macro typInt32
  i
EndMacro

Macro Field (DataName, DataType)
  DataName.typ#DataType
EndMacro

Field(x, Int32)   ; structure not found: typint32
This does:

Code: Select all

Macro typInt32
  i
EndMacro

Macro FieldTyp (DataName, DataType)
  DataName.DataType
EndMacro

Macro Field (DataName, DataType)
  FieldTyp (DataName, typ#DataType)
EndMacro

Field(x, Int32) ; x.i
This allows me to dynamically combine text, such as "Int32", with other text so as to reference specific macros, such as "typInt32". It's not the most elegant solution, but it allows me to do what I want. Luckily, there are only a few macros where I have to use this technique.