Difference between revisions of "Macro Functions"

From GiderosMobile
(formatting + added another example from @perrochon)
Line 4: Line 4:
 
'''<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> ===
<translate><br />
+
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.<br /><br />
+
<source 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:
  
name @ (| ...body... |)<br /><br />
+
'''\`~ ! # $ % ^ & * / + = |'''
  
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 redefine a macro with @@:
You can use any of these markers.<br />
+
<source lang="lua">
\`~ ! # $ % ^ & * / + = |<br /><br />
+
name @@ (| ...another_body... |)
 +
</source>
  
You can redefine a macro with @@.<br />
+
To call macro function use its name with parenthesis as with usual functions:
name @@ (| ...another_body... |)<br /><br />
+
<source lang="lua">
 +
name(...arguments...)
 +
</source>
  
To call macro function use it's name with parenthesis as with usual functions:<br />
 
name(...arguments...)<br /><br /></translate>
 
 
=== <translate>Examples</translate> ===
 
=== <translate>Examples</translate> ===
'''Enumeration'''<br/>
+
'''Enumeration'''
<source lang="lua">  
+
<source lang="lua">
 
enum @ (|
 
enum @ (|
 
local t = ...
 
local t = ...
Line 31: 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</source>
 
print(apple, orange, melon) --> 1 2 3</source>
'''Turning off the print command'''<br/>
+
 
<source lang="lua">  
+
'''Turning off the print command'''
print @ (| return "" |)</source>
+
<source lang="lua">
'''Unroll loops'''<br/>
+
print @ (| return "" |)
<source lang="lua">  
+
</source>
 +
 
 +
'''Unroll loops'''
 +
<source 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!")</source>
+
dotimes(10 print "Boom!")
 +
</source>
 +
 
 +
'''A Sum macro example'''
 +
<source lang="lua">
 +
-- comma counts as an argument meaning SUM(o1, o2) has three parameters
 +
SUM @ (| return "("..(...)[1] + (...)[3] ..")" |)
 +
print (SUM(1,2) * SUM(3,1))
 +
</source>
 +
 
 
{|-
 
{|-
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|

Revision as of 23:05, 20 June 2020


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.

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 SUM(o1, o2) has three parameters
SUM @ (| return "("..(...)[1] + (...)[3] ..")" |)
print (SUM(1,2) * SUM(3,1))

Methods

Events

Constants