• Announcement: Lua.org now officially recommends this forum as a meeting place for the Lua community
  • The forum is currently open to new registrations. The registration will close for a short period of time when reaching 500 active members, to upgrade the server resources.

Text macro (1 Viewer)

Herly Quijano

Newcomer
Joined
Mar 19, 2021
Messages
7
Reaction score
1
Hello, I'm new here and I starting to learn about Lua, something I wanna ask is a thing that is used in another programing language called text macro, that replicates a code with small difference any time, basically it works like this:
Code:
textmacro EXAMPLE("func1","X")
textmacro EXAMPLE("func2","Y")

runtextmacro EXAMPLE(FUNC_NAME,VAR_TYPE)

function $FUNC_NAME$(var)
    SetCoord$VAR_TYPE$(var)
end

endtextmacro
Is the same as:
Code:
function func1(var)
    SetCoordX(var)
end

function func2(var)
    SetCoordY(var)
end

Is there something similar in Lua or there is a way to replicate it, because I searched and I didn't find anything.
 

stetre

Member
Rank: I
Joined
Jan 8, 2020
Messages
45
Reaction score
28
Location
Italy
Website
github.com
Hi. Lua does not have macros, but there are ways to write functions that in turn define other functions. For example:

Code:
local function def(name, var)
   load("function "..name..var.."(val) print('"..var.."', val) end")()
end

def("Set", "X") -- defines the SetX() function
def("Set", "Y") -- defines the SetY() function
SetX(123)
SetY(456)
 

Herly Quijano

Newcomer
Joined
Mar 19, 2021
Messages
7
Reaction score
1
Hi. Lua does not have macros, but there are ways to write functions that in turn define other functions. For example:

Code:
local function def(name, var)
   load("function "..name..var.."(val) print('"..var.."', val) end")()
end

def("Set", "X") -- defines the SetX() function
def("Set", "Y") -- defines the SetY() function
SetX(123)
SetY(456)
Oh I see thank you.
 

stetre

Member
Rank: I
Joined
Jan 8, 2020
Messages
45
Reaction score
28
Location
Italy
Website
github.com
I see you used this ' why and how can I use it?

Lua has a few alternative ways to delimit literal strings, e.g. "hello", 'hello', [[hello]], [=[hello]=] are all valid ways to define a literal string containing the word hello (for more details, see Lua 5.3 Reference Manual ).

In the above example, I obtained a string containing valid Lua code by concatenating a few strings and the contents of the variables name and var. The character ' is part of the string, while the " are delimiters. Say for example that name="Set" and var="X", then the code

Code:
load("function "..name..var.."(val) print('"..var.."', val) end")()

would result in

Code:
load("function ".."Set".."X".."(val) print('".."X".."', val) end")()

and eventually in

Code:
load("function SetX(val) print('X', val) end")()

Note that I could as well have swapped the roles of the ' and the ", by writing:

Code:
load('function '..name..var..'(val) print('"..var.."', val) end')()

This would result in the following, which is equivalent:

Code:
load('function SetX(val) print("X", val) end')()
 

Herly Quijano

Newcomer
Joined
Mar 19, 2021
Messages
7
Reaction score
1
Question how manipulable is this? I mean Can I create or edit external variables? and did everything must be in the same line? and can I do some more things than create a function?
 

stetre

Member
Rank: I
Joined
Jan 8, 2020
Messages
45
Reaction score
28
Location
Italy
Website
github.com
Question how manipulable is this? I mean Can I create or edit external variables? and did everything must be in the same line? and can I do some more things than create a function?

It is actually pretty flexible and you can do almost everything. The key is to understand how the load function works. Not the easiest topic, but definitely worth studying if you want to leverage the power of Lua. It essentially gives you ways to execute code that you compose at runtime. Such code follows the very same rules that must be followed by the code you would write in an editor (this should implicitly answer your questions). The only difference is that you compose it at runtime, which allows you to do things like those we did in the example, e.g. define a function whose name is the concatenation of the values of two variables.
 

Herly Quijano

Newcomer
Joined
Mar 19, 2021
Messages
7
Reaction score
1
It is actually pretty flexible and you can do almost everything. The key is to understand how the load function works. Not the easiest topic, but definitely worth studying if you want to leverage the power of Lua. It essentially gives you ways to execute code that you compose at runtime. Such code follows the very same rules that must be followed by the code you would write in an editor (this should implicitly answer your questions). The only difference is that you compose it at runtime, which allows you to do things like those we did in the example, e.g. define a function whose name is the concatenation of the values of two variables.
Ok, where can I found information? Because when I searched I just found simple descriptions.
 

stetre

Member
Rank: I
Joined
Jan 8, 2020
Messages
45
Reaction score
28
Location
Italy
Website
github.com
Ok, where can I found information? Because when I searched I just found simple descriptions.

The major sources of information about any Lua topic are the reference manual and the book Programming in Lua (see Lua: documentation ). Better if you can get the newest edition (4th) of the latter, otherwise you can find an older edition available for free on the Lua site, which should be good enough as a starting point. The load function is covered in the chapter Compilation, Execution, and Errors .
 

Herly Quijano

Newcomer
Joined
Mar 19, 2021
Messages
7
Reaction score
1
The major sources of information about any Lua topic are the reference manual and the book Programming in Lua (see Lua: documentation ). Better if you can get the newest edition (4th) of the latter, otherwise you can find an older edition available for free on the Lua site, which should be good enough as a starting point. The load function is covered in the chapter Compilation, Execution, and Errors .
Thank you
 
Top