yetneverdone wrote: ↑Sun Nov 21, 2021 1:59 am
Code: Select all
local a = 1
@switch(a)
@case 1: print(1)
@case 2: print(2)
No, that kind of syntax is the kind of thing I want to avoid adding. Note that LuaPreprocess doesn't know what you're outputting and thus wouldn't know where to put the last 'end' in the example.
Just for fun, this example does work (even though it's a terrible way of programming):
Code: Select all
!(
local conditionCode, isFirstCase
local function switch(_conditionCode)
conditionCode = _conditionCode
isFirstCase = true
end
local function case(valueCode)
if not isFirstCase then
outputLua("else")
end
outputLua("if ", conditionCode, " == ", valueCode, " then")
isFirstCase = false
end
local function switchend()
outputLua("end")
end
)
local a = 1
@@switch(a)
@@case(1) print(1)
@@case(2) print(2)
@@switchend()
-- Output:
local a = 1
if a == 1 then print(1)
elseif a == 2 then print(2)
end
You should really just write what's in the output directly - normal freaking Lua if-statements. lol
yetneverdone wrote: ↑Sun Nov 21, 2021 2:17 am
is there a significant difference (preprocessing time?) between using the two that achieves the same output?
You should use the feature that best suits the situation: $func is short and sweet but takes no arguments or anything, @@func() converts the arguments to Lua code strings before calling the function, and !!() allows any arbitrary expression to run in the metaprogram (nothing special happening). More clarification:
Code: Select all
!local function noop() return "" end
!!(noop("x", 7)) -- 1
@@noop("x", 7) -- 2
$noop -- 3 (No arguments for this method)
-- The above essentially does the same as this:
!(
-- 1: Nothing special happening.
outputLua(noop("x", 7))
-- 2: Arguments are converted to strings with Lua code.
outputLua(noop("\"x\"", "7"))
-- 3: The value is either outputted, or called with the returned value outputted instead.
if type(noop) == "function" then
outputLua(noop())
else
outputLua(noop)
end
You can inspect the generated metaprogram (*.meta.lua, if debug mode is enabled) to maybe get some better idea what's happening.
The preprocessing time is probably not very important here, but as the example above may suggest, the !!() method is the most simple, and @@macros() do the most amount of extra work.