Macro Functions: Difference between revisions
From GiderosMobile
No edit summary |
m Text replacement - "</source>" to "</syntaxhighlight>" |
||
| (6 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
<languages /> | |||
<!-- GIDEROSOBJ:Macro Functions --> | |||
'''<translate>Supported platforms</translate>:''' [[File:Platform android.png]][[File:Platform ios.png]][[File:Platform mac.png]][[File:Platform pc.png]]<br/> | '''<translate>Supported platforms</translate>:''' [[File:Platform android.png]][[File:Platform ios.png]][[File:Platform mac.png]][[File:Platform pc.png]]<br/> | ||
'''<translate>Available since</translate>:''' Gideros 2017.10<br/> | '''<translate>Available since</translate>:''' Gideros 2017.10<br/> | ||
=== <translate>Description</translate> === | === <translate>Description</translate> === | ||
Macro Functions receive a list of tokens and output a string which will be pasted into code at compile time. | |||
Macro Functions receive a list of tokens and output a string which will be pasted into code at compile time.< | <syntaxhighlight lang="lua"> | ||
name @ (| ...body... |) | |||
</syntaxhighlight> | |||
You should use parenthesis around markers - '|' is the preferred marker. | |||
The same marker should be used to close macro body with closing parenthesis right after it. | |||
You can use any of these markers: | |||
'''\`~ ! # $ % ^ & * / + = |''' | |||
\`~ ! # $ % ^ & * / + = | | |||
You can redefine a macro with @@ | You can redefine a macro with @@: | ||
name @@ (| ...another_body... |)< | <syntaxhighlight lang="lua"> | ||
name @@ (| ...another_body... |) | |||
</syntaxhighlight> | |||
To call macro function use its name with parenthesis as with usual functions: | |||
<syntaxhighlight lang="lua"> | |||
name(...arguments...) | |||
</syntaxhighlight> | |||
=== <translate>Examples</translate> === | === <translate>Examples</translate> === | ||
'''Enumeration''' | '''Enumeration''' | ||
< | <syntaxhighlight lang="lua"> | ||
enum @ (| | enum @ (| | ||
local t = ... | local t = ... | ||
| Line 29: | Line 41: | ||
return table.concat(r, " ") | return table.concat(r, " ") | ||
|) | |) | ||
enum(apple, orange, melon) | enum(apple, orange, melon) | ||
print(apple, orange, melon) --> 1 2 3</ | print(apple, orange, melon) --> 1 2 3</syntaxhighlight> | ||
'''Turning off the print command''' | |||
< | '''Turning off the print command''' | ||
print @ (| return "" |)</ | <syntaxhighlight lang="lua"> | ||
'''Unroll loops''' | print @ (| return "" |) | ||
< | </syntaxhighlight> | ||
'''Unroll loops''' | |||
<syntaxhighlight lang="lua"> | |||
dotimes @ (| | dotimes @ (| | ||
local times = table.remove(..., 1) | local times = table.remove(..., 1) | ||
return (table.concat(..., " ").." "):rep(times) | return (table.concat(..., " ").." "):rep(times) | ||
|) | |) | ||
local t = {} | local t = {} | ||
dotimes(10 print "Boom!")</ | dotimes(10 print "Boom!") | ||
</syntaxhighlight> | |||
'''A Sum macro example''' | |||
<syntaxhighlight lang="lua"> | |||
-- comma counts as an argument meaning MYSUM(o1, o2) has three parameters | |||
MYSUM @ (| return "("..(...)[1] + (...)[3] ..")" |) | |||
print (MYSUM(1,2) * MYSUM(3,1)) | |||
</syntaxhighlight> | |||
{|- | {|- | ||
| style="width: 50%; vertical-align:top;"| | | style="width: 50%; vertical-align:top;"| | ||
=== <translate>Methods</translate> === | === <translate>Methods</translate> === | ||
| style="width: 50%; vertical-align:top;"| | | style="width: 50%; vertical-align:top;"| | ||
| Line 52: | Line 77: | ||
=== <translate>Constants</translate> === | === <translate>Constants</translate> === | ||
|} | |} | ||
{{GIDEROS IMPORTANT LINKS}} | |||
Latest revision as of 13:31, 13 July 2023
Supported platforms: ![]()
![]()
![]()
![]()
Available since: Gideros 2017.10
Description
Macro Functions receive a list of tokens and output a string which will be pasted into code at compile time.
name @ (| ...body... |)
You should use parenthesis around markers - '|' is the preferred marker.
The same marker should be used to close macro body with closing parenthesis right after it.
You can use any of these markers:
\`~ ! # $ % ^ & * / + = |
You can redefine a macro with @@:
name @@ (| ...another_body... |)
To call macro function use its name with parenthesis as with usual functions:
name(...arguments...)
Examples
Enumeration
enum @ (|
local t = ...
local r = {}
for i = 1, #t, 2 do
table.insert(r, t[i] .. " @ " .. i // 2 + 1)
end
print(table.concat(r, " "))
return table.concat(r, " ")
|)
enum(apple, orange, melon)
print(apple, orange, melon) --> 1 2 3
Turning off the print command
print @ (| return "" |)
Unroll loops
dotimes @ (|
local times = table.remove(..., 1)
return (table.concat(..., " ").." "):rep(times)
|)
local t = {}
dotimes(10 print "Boom!")
A Sum macro example
-- comma counts as an argument meaning MYSUM(o1, o2) has three parameters
MYSUM @ (| return "("..(...)[1] + (...)[3] ..")" |)
print (MYSUM(1,2) * MYSUM(3,1))
Methods |
EventsConstants |