Skip to content

Commit 1fabad2

Browse files
committed
Macros!
Also, better code preview in file error messages.
1 parent 52e4988 commit 1fabad2

File tree

3 files changed

+386
-97
lines changed

3 files changed

+386
-97
lines changed

misc/quickTest.lua2p

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ a = a..!(b)..a -- Comment, string concat.
2222
!local bool = --[[ Just a multiline
2323
comment here...]] true
2424

25+
26+
2527
!(
2628
-- Preprocessor block.
2729
local function sum(x, y)
@@ -34,6 +36,8 @@ local str = !(run("misc/quickTestInclude.lua", "what"))
3436

3537
_G.!!("global"..math.random(5)) = 99
3638

39+
40+
3741
-- Extended preprocessor line. (Balanced brackets.)
3842
!local arr = {
3943
10,
@@ -46,6 +50,8 @@ local z = !(z)
4650

4751
!local justTheLetterD = "d"
4852

53+
54+
4955
-- Dual code. (Outputs both both preprocessor code and normal Lua. Can only be used for assignment statements.)
5056
!!local alpha = "[%a_]"
5157
!!local alphanum = "[%a%d_]"
@@ -58,6 +64,8 @@ print(s:match(ident)) -- "hello5"
5864
print(s:match(num)) -- "2"
5965
print(s:match(funcCall)) -- "hello5( foo )"
6066

67+
68+
6169
-- File inserts.
6270
!(
6371
@insert "misc/quickTest.luapart"
@@ -66,6 +74,40 @@ print("Metaprogram - someString: "..someString)
6674
local uhh = !(@insert"misc/quickTest.txt")
6775
print("Final program - uhh: "..uhh)
6876

77+
78+
79+
-- Macros.
80+
!local function BLARGH() return 'print("Blargh!")' end
81+
@insert BLARGH()
82+
-- !@insert BLARGH() -- Error: Preprocessor token inside metaprogram.
83+
-- @insert BLARGH( @insert BLARGH() ) -- Error: Preprocessor code not supported in macros.
84+
-- @insert BLARGH(function() return 1,2 end) -- Syntax error! Caused by the comma in the return statement. (This would work if the preprocessor was smarter.)
85+
86+
!local function WHERE(filename, ln) return "print(string.format('We are at %s:%d!', "..filename..", "..ln.."))" end
87+
@insert WHERE(@file, @line)
88+
89+
!(
90+
local DEBUG = 1==1
91+
92+
local function ASSERT(cond, message)
93+
if not DEBUG then return "" end
94+
95+
message = message or "'Asertion failed!'"
96+
97+
return "if not ("..cond..") then error("..message..") end"
98+
end
99+
)
100+
101+
local ok = 1==1
102+
103+
@insert ASSERT ( ok , "Oh "..tonumber("7",10).." noes!" )
104+
-- @insert ASSERT ( 1 1 ) -- Syntax error!
105+
-- @insert ASSERT ( ok , ) -- Syntax error!
106+
-- @insert ASSERT ( , ok ) -- Syntax error!
107+
-- @insert ASSERT ( --[[]] , ok ) -- Syntax error!
108+
109+
110+
69111
-- Misc.
70112
print(!("dataFromCommandLine: "..tostring(dataFromCommandLine)))
71113
print(!(("This file and line: %s:%d"):format(@file, @line)))
@@ -83,6 +125,8 @@ local NAN = !(0/0)
83125

84126
print("The end.")
85127

128+
129+
86130
!(
87131
--[==[ Test token stuff.
88132
for i, token in ipairs(assert(tokenize(assert(getFileContents"misc/quickTestInclude.lua")))) do
@@ -144,3 +188,5 @@ for _, token in ipairs{
144188
end
145189
--]==]
146190
)
191+
192+

misc/runTests.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ doTest("Preprocessor keywords", function()
213213
end,
214214
})
215215
assertCodeOutput(luaOut, [[v = "bar"]])
216+
217+
-- @Incomplete: Test macros: @insert foo()
216218
end)
217219

218220

0 commit comments

Comments
 (0)