[solved] var name automation

General FreeBASIC programming questions.
Post Reply
dafhi
Posts: 1640
Joined: Jun 04, 2005 9:51

[solved] var name automation

Post by dafhi »

I want to do something like this

Code: Select all

#Macro foo(my_int)
  var my_int = 
  var my_int_ = 'name with extra char
#EndMacro
Last edited by dafhi on May 10, 2018 12:53, edited 1 time in total.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: var name automation

Post by fxm »

I do not understand what you are trying to do.
Can you clarify your request?
sancho3
Posts: 358
Joined: Sep 30, 2017 3:22

Re: var name automation

Post by sancho3 »

I also don't understand the idea here.
However I can warn you that macro's and underscores don't play well.
See here.
That may be affecting your issue.
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: var name automation

Post by badidea »

I think it is not possible to create a variable (and give it a name) at run-time. The compiler wants to know all variable names.

Alternative suggestions:
1) The python like dictionary Paul doe is working on (https://freebasic.net/forum/viewtopic.php?f=7&t=26646)
2) Write a program that writes .bas files. Compilation in 2 steps.
3) Use a type (struct). my_var.0 , my_var._
4) Use an array myvar(0 to 1)
5) ...

Option 2 I used once to generate a list of constants form a tab-separated-file.
dafhi
Posts: 1640
Joined: Jun 04, 2005 9:51

Re: var name automation

Post by dafhi »

I want to create multiple "instances" of a variable, with a common name base, like
var x =
var x1 =
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: var name automation

Post by fxm »

Maybe create multiple references to the same variable (but does not work with arrays and non static UDT fields):

Code: Select all

Dim As Integer I

Var Byref myI1 = I
Var Byref myI2 = I

I = 123
Print I, myI1, myI2

myI1 = 456
Print I, myI1, myI2

myI2 = 789
Print I, myI1, myI2

Sleep
A reference can also be declared only within a local scope structure compared to the variable, so that its usage is only local.
dafhi
Posts: 1640
Joined: Jun 04, 2005 9:51

Re: var name automation

Post by dafhi »

I've seen ### used in macro params, but had a hard time understanding.

here's my actual code

Code: Select all

    #macro def_xy(scale_var,xy,wh,_wh,off_var,int_var)
      var scale_var = _wh / dest->wh
      var off_var = scale_var * ( int_var - xy )
    #EndMacro

    def_xy( scale_x, x, w, _w, offx_src, intx )
    def_xy( scale_y, y, h, _h, offy_src, inty )
Thanks for all the examples guys! I did learn some new things
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: var name automation

Post by dodicat »

Hi dafhi
Your original (which is easier)

Code: Select all


    
    #Macro foo(my_int,HELLO)
  var my_int = HELLO
  var my_int_ = #my_int + #hello
#EndMacro

foo(my_int,13)
print my_int
print my_int_


#print typeof(my_int)
#print typeof(my_int_)

sleep  
Which doesn't seem much use for anything.
sorry.
dafhi
Posts: 1640
Joined: Jun 04, 2005 9:51

Re: var name automation

Post by dafhi »

this works!

Code: Select all

#macro foo(name_base,name_append)
  var name_base##name_append = name_append
#endmacro

foo(angel_hair,1)
? angel_hair1
what i'm now using in my proto. thanks a lot guys

Code: Select all

sub imvars.resize(byref dest as imvars ptr=0, x as single=0, y as single=0, parm_w as single=0, parm_h as single=0)
  
    #macro def_xy(coordinate, dimension)
      var scale_##coordinate = parm_##dimension / dest->dimension
      ' .. more stuff
    #EndMacro

    def_xy( x, w )
    def_xy( y, h )
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: [solved] var name automation

Post by fxm »

I finally understood that I had not understand anything at your request !
(a well-posed problem is already half solved)
dafhi
Posts: 1640
Joined: Jun 04, 2005 9:51

Re: [solved] var name automation

Post by dafhi »

I never knew about var byref though :D
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: [solved] var name automation

Post by badidea »

dafhi wrote:I never knew about var byref though :D
In C one would use pointers, here we can do both.
Even possible with 'normal' variables (I just learned): https://freebasic.net/wiki/wikka.php?wa ... fVariables
Post Reply