Page 4 of 8

Re: LuaPreprocess - straightforward preprocessor with simple syntax

Posted: Mon May 10, 2021 12:19 pm
by monolifed
Nice. using !! and multiline strings you can keep multiline statements intact

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")]])

Re: LuaPreprocess - straightforward preprocessor with simple syntax

Posted: Mon May 10, 2021 9:38 pm
by pgimeno
Thanks. So there doesn't seem to be a way around embedding the Lua code for the parameters and copy it verbatim to the output.

The problem that I'm trying to solve is this: http://lua.space/general/assert-usage-caveat . In the article, my proposed solution is a hack around the lack of a preprocessor. That's why I tried to use LuaPreprocess for this purpose.

So, here's a solution that meets the requirements of an assert function, except it needs strings instead of operations:

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]])
Any plans to include something like C/C++ preprocessor macro parameters? That is, to not need to pass the parameters as strings in the first place.

Re: LuaPreprocess - straightforward preprocessor with simple syntax

Posted: Wed May 12, 2021 1:07 am
by ReFreezed
pgimeno wrote: Mon May 10, 2021 9:38 pm Any plans to include something like C/C++ preprocessor macro parameters? That is, to not need to pass the parameters as strings in the first place.
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).

Until now you've been able to say @insert "filename" to insert a file (or something else if params.onInsert() is defined). Now you can also say `@insert foo()` which will invoke the function foo() in the metaprogram (just like `!!(foo())`) but all arguments will automatically be converted to individual strings containing the apparent Lua code, like this:

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
As long as you define assert() in the metaprogram like this, all you have to do is prepend @insert to all relevant assert() calls. I think that solves the problem pretty nicely.

Re: LuaPreprocess - straightforward preprocessor with simple syntax

Posted: Wed May 12, 2021 11:10 am
by pgimeno
That sounds pretty good, thank you! It would be even cooler to be able to do something like: @assert(...) instead of @insert assert(...) but that is already a nice solution.

Re: LuaPreprocess - straightforward preprocessor with simple syntax

Posted: Wed May 12, 2021 5:18 pm
by ReFreezed
Because @blah is already used for preprocessor keywords I though about adding @@func() as new syntax (for what could be seen as a "user-defined" keyword), but I think reusing @insert like this makes more sense as it has similar functionality already, and I don't have to add a new kind of "symbol".

Re: LuaPreprocess - straightforward preprocessor with simple syntax

Posted: Wed May 12, 2021 6:12 pm
by pgimeno
I see, but it would be so nice if expandable macros are minimally penalized in typing. There are still some symbols free, like $ or &, or even ~ when not followed by =, that could be used for this purpose. Or maybe an identifier followed by "(" without a space could have a special meaning and not be interpreted as a preprocessor keyword. The latter may be a bit of a stretch, admittedly.

Re: LuaPreprocess - straightforward preprocessor with simple syntax

Posted: Thu May 13, 2021 1:32 am
by ReFreezed
Hmm, I think I agree with you, so I added @@ as an alias for @insert.

Code: Select all

-- Have we reached nirvana yet?
@@assert(i >= 1 and i <= 5, "i out of range, expected: 1<=i<=5, actual: " .. i)

Re: LuaPreprocess - straightforward preprocessor with simple syntax

Posted: Thu May 13, 2021 1:46 am
by pgimeno
Great, thanks!

Re: LuaPreprocess - straightforward preprocessor with simple syntax

Posted: Fri May 14, 2021 8:37 pm
by ReFreezed
Update 1.13

Changes since 1.12:

Library: Command line program:
  • Fixed additional stack overflow error when there's an error in the "fileerror" message handler.

LuaPreprocess update 1.13.1

Posted: Sun May 16, 2021 8:48 pm
by ReFreezed
Update 1.13.1

Changes since 1.13:

Library: Command line program: