Difference between revisions of "Macro Functions"

From GiderosMobile
m (Text replacement - "<source" to "<syntaxhighlight")
Line 7: Line 7:
 
=== <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.
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
name @ (| ...body... |)
 
name @ (| ...body... |)
 
</source>
 
</source>
Line 20: Line 20:
  
 
You can redefine a macro with @@:
 
You can redefine a macro with @@:
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
name @@ (| ...another_body... |)
 
name @@ (| ...another_body... |)
 
</source>
 
</source>
  
 
To call macro function use its name with parenthesis as with usual functions:
 
To call macro function use its name with parenthesis as with usual functions:
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
name(...arguments...)
 
name(...arguments...)
 
</source>
 
</source>
Line 31: Line 31:
 
=== <translate>Examples</translate> ===
 
=== <translate>Examples</translate> ===
 
'''Enumeration'''
 
'''Enumeration'''
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
enum @ (|
 
enum @ (|
 
local t = ...
 
local t = ...
Line 46: Line 46:
  
 
'''Turning off the print command'''
 
'''Turning off the print command'''
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
print @ (| return "" |)
 
print @ (| return "" |)
 
</source>
 
</source>
  
 
'''Unroll loops'''
 
'''Unroll loops'''
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
dotimes @ (|
 
dotimes @ (|
 
local times = table.remove(..., 1)
 
local times = table.remove(..., 1)
Line 63: Line 63:
  
 
'''A Sum macro example'''
 
'''A Sum macro example'''
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
-- comma counts as an argument meaning MYSUM(o1, o2) has three parameters
 
-- comma counts as an argument meaning MYSUM(o1, o2) has three parameters
 
MYSUM @ (| return "("..(...)[1] + (...)[3] ..")" |)
 
MYSUM @ (| return "("..(...)[1] + (...)[3] ..")" |)

Revision as of 14:28, 13 July 2023


Supported platforms: Platform android.pngPlatform ios.pngPlatform mac.pngPlatform pc.png
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. <syntaxhighlight lang="lua"> name @ (| ...body... |) </source>

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 @@: <syntaxhighlight lang="lua"> name @@ (| ...another_body... |) </source>

To call macro function use its name with parenthesis as with usual functions: <syntaxhighlight lang="lua"> name(...arguments...) </source>

Examples

Enumeration <syntaxhighlight lang="lua"> 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</source>

Turning off the print command <syntaxhighlight lang="lua"> print @ (| return "" |) </source>

Unroll loops <syntaxhighlight lang="lua"> dotimes @ (| local times = table.remove(..., 1) return (table.concat(..., " ").." "):rep(times) |)

local t = {}

dotimes(10 print "Boom!") </source>

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)) </source>

Methods

Events

Constants