How to translate a label to a string and concatenate a underscore in front of it.

General FreeBASIC programming questions.
D.J.Peters
Posts: 7659
Joined: May 28, 2005 3:28

How to translate a label to a string and concatenate a underscore in front of it.

Postby D.J.Peters » Jan 10, 2019 2:12

I wrote such macro in the past but I don't get it anymore today.

Does anything are changed in the macro syntax since V 0.16 or V0.24 ?

Or maybe I get only older :-(

Translate a label to a string and concatenate a underscore in front of it.

"_ " & #_label_
"_ " ## #_label_
"_ "###_label_
#_ ## #_label_
#_###_label_

I mean something like this:
#define declare_function(_name_) declare function _name_ alias ?!?

declare_function(test) (a as integer, b as integer) as Boolean
should produce:
declare function test alias "_test" (a as integer, b as integer) as Boolean

Thank you.

Joshy
counting_pine
Site Admin
Posts: 6096
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: How to translate a label to a string and concatenate a underscore in front of it.

Postby counting_pine » Jan 10, 2019 9:04

I'm not sure how it would have worked. I tried a few different things to get '_' working as a token, and they all just seemed to treat it as a line separator. (I don't know if that's changed from previous versions...)
Trying to use it as a string seems to get stuck, because the Alias can't be an expression, only a single string literal.

The best I can suggest is using a different prefix, such as two underscores:

Code: Select all

#define stringize(x) #x
#define decl(x) declare function x alias stringize(__##x)

decl(test) (a as integer) as integer

print test(3)

function test alias "__test" (a as integer) as integer
   return a*a
end function


Possibly what FB should do is allow any constant string expression for the Alias. (Or allow underscores to be used symbolically, but only under very specific circumstances...)
coderJeff
Site Admin
Posts: 2794
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: How to translate a label to a string and concatenate a underscore in front of it.

Postby coderJeff » Jan 10, 2019 19:28

Joshy, should work with fbc-0.16 to fbc-1.06. tested on win 32-bit. so should work either STDCALL or CDECL

Prefix ALIAS with '_':

Code: Select all

'' trick fbc to have an alias that starts with '_'
#define begin_() "_
#define prefix_(arg) begin_()##arg"

#define declare_function(n) declare function n alias prefix_(n)

declare_function(hello) (a as integer, b as integer) as integer

'' check: should be undefined reference to `_hello@8'
hello(1,2)


It should also help for working with __fastcall alias:
Prefix ALIAS with '@':

Code: Select all

'' trick fbc to have an alias that starts with '@'
#define begin_at() "@
#define prefix_at(arg) begin_at()##arg"

#define declare_fast_function(n) declare function n alias prefix_at(n)

declare_fast_function(ff_hello) (a as integer, b as integer) as integer
                 
'' check: should be undefined reference to `@ff_hello@8'
ff_hello(1,2)
coderJeff
Site Admin
Posts: 2794
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: How to translate a label to a string and concatenate a underscore in front of it.

Postby coderJeff » Jan 10, 2019 19:31

counting_pine wrote:Possibly what FB should do is allow any constant string expression for the Alias. (Or allow underscores to be used symbolically, but only under very specific circumstances...)

Or maybe an "unstringize()" function to unquote arguments in the pre-processor. Taking a literal string, or constant string expressions, and pasting it in to the parser, unquoted.
dodicat
Posts: 5555
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to translate a label to a string and concatenate a underscore in front of it.

Postby dodicat » Jan 10, 2019 20:15

Here is a cheesy way using counting pine's method.
direct to a .bi file which can be included

Code: Select all

 


#include "file.bi"
Function SAR(s0 As String,s1 As String,s2 As String) As String
    Dim s As String=s0
    var position=Instr(s,s1)
    While position>0
        s=Mid(s,1,position-1) & s2 & Mid(s,position+Len(s1))
        position=Instr(position+Len(s2),s,s1)
    Wend
    SAR=s
End Function


Sub savefile(filename As String,p As String)
    Dim As Integer n
    n=Freefile
    If Open (filename For Binary Access Write As #n)=0 Then
        Put #n,,p
        Close
    Else
        Print "Unable to save " + filename
    End If
End Sub

Function loadfile(file as string) as String
   If FileExists(file)=0 Then Print file;" not found":Sleep:end
   var  f=freefile
    Open file For Binary Access Read As #f
    Dim As String text
    If Lof(f) > 0 Then
      text = String(Lof(f), 0)
      Get #f, , text
    End If
    Close #f
    return text
end Function

sub AppendAndtweak(filename As String,txt As String)
    if fileexists(filename)=0 then savefile(filename," ")
    Dim As String s=loadfile(filename)
    If Len(s) Then savefile(filename,s+txt)
    s=loadfile(filename)
    s=SAR(s,"underscore","_")
    s=SAR(s,"$","")
    savefile(filename,s)
End sub


AppendAndTweak("declarations.bi"," ")'starter

#define stringize(x) #x
#define decl(x) declare function x alias stringize(underscore##x)

#macro record(z)
appendandTweak("declarations.bi", #z)
appendandTweak("declarations.bi", chr(10))
#endmacro
'  write out requirements
 record(decl(test) (a as integer) as integer)
 record(decl(add) (a as integer,b as integer) as integer)
 record(decl(subtract) (a as integer,b as integer) as integer)
 record(decl(multiply) (a as integer,b as integer) as integer)
 record(decl(create)(a as long,s as zstring ptr,g as any ptr) as any ptr)
 shell "type declarations.bi"
 
 'kill "declarations.bi"  ''for testing only
 print fileexists("declarations.bi")
 

 sleep

 
D.J.Peters
Posts: 7659
Joined: May 28, 2005 3:28

Re: How to translate a label to a string and concatenate a underscore in front of it.

Postby D.J.Peters » Jan 10, 2019 21:53

If it worked as macro with V0.15 or V0.16 or V0.24 I can't remember there must be something changed or a bug ?

Joshy
coderJeff
Site Admin
Posts: 2794
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: How to translate a label to a string and concatenate a underscore in front of it.

Postby coderJeff » Jan 10, 2019 22:19

Maybe you want just '#_name_' then? fbc automatically adds the underscore, and suffix, but only if it is needed for the target platform. Seems to be OK since fbc-0.14 -gen gas.

Code: Select all

#define declare_function(_name_) declare function _name_ alias #_name_

declare_function(func) () as integer


I don't know about inline ASM and -gen gcc on all versions of fbc, though. There's been a number of bug fixes for name mangling.
fxm
Posts: 8693
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: How to translate a label to a string and concatenate a underscore in front of it.

Postby fxm » Jan 11, 2019 7:12

Around that, the only change since fbc 0.24 which I remember is the following on fbc 0.90:
Preprocessor's macro parameter stringize operator # now produces an empty string ("") if the macro argument was empty, instead of producing nothing.
Tourist Trap
Posts: 2616
Joined: Jun 02, 2015 16:24

Re: How to translate a label to a string and concatenate a underscore in front of it.

Postby Tourist Trap » Jan 12, 2019 14:45

Hello all, I tried to use the chr(8) to eat a character because 2 underscores are allowed. But here is my first try, something goes weird no?

Code: Select all

#macro X(strg)
    #define strg##_##chr(8)##_     beep
#endMacro


X(a)
a   'authorized call?


? "no beep? no bug?"


(seems that means newline and ignore following...)

How can we evaluate an invisible literal and pass it as an active token to a macro? It's not possible I suppose ?

Code: Select all

#macro X(strg, eater)
    #print strg##x##(eater)
#endMacro

X(a, (chr(8)))

expected a, returned ax((chr(8)))...

Some addition. Spaces can be lost when stringize it seems.
#define p(exp) ? #exp, exp : var a = 9 : p(a - 1) : sleep

returns a -1. Space before 1 is gone.

Return to “General”

Who is online

Users browsing this forum: Google [Bot] and 3 guests