Code: Select all
!(
if debug then
ondebug = function(s) return s end
else
--ondebug = function(s) return "--[==["..s.."]==]" end
ondebug = function(s) return "" end
end
)
!!(ondebug[[print("hello",
"world")]])
Code: Select all
!(
if debug then
ondebug = function(s) return s end
else
--ondebug = function(s) return "--[==["..s.."]==]" end
ondebug = function(s) return "" end
end
)
!!(ondebug[[print("hello",
"world")]])
Code: Select all
!DEBUG=true
!(
function ASSERT(cond, msg)
if DEBUG then
if not msg then
msg = [["Assertion failed: " .. cond]]
end
outputLua("if not (", cond, ") then error(", msg, ") end")
end
end
)
!ASSERT([[i >= 1 and i <= 5]], [["i out of range, expected: 1<=i<=5, actual: " .. i]])
I was going to write a long reply about how bad C/C++ macros were, but instead I started experimenting with how macros actually would work in this preprocessor. Turns out I found a solution that doesn't look awful (in my opinion at least).
Code: Select all
!(
function assert(cond, msg)
if not DEBUG then return "" end
msg = msg or string.format("%q", "Assertion failed: "..cond)
-- Return the code instead of calling outputLua().
return "if not ("..cond..") then error("..msg..") end"
end
)
@insert assert(i >= 1 and i <= 5, "i out of range, expected: 1<=i<=5, actual: " .. i)
-- Output:
if not (i >= 1 and i <= 5) then error("i out of range, expected: 1<=i<=5, actual: " .. i) end
Code: Select all
-- Have we reached nirvana yet?
@@assert(i >= 1 and i <= 5, "i out of range, expected: 1<=i<=5, actual: " .. i)
Users browsing this forum: Ahrefs [Bot] and 3 guests