LuaPreprocess - straightforward preprocessor with simple syntax

Showcase your libraries, tools and other projects that help your fellow love users.
monolifed
Party member
Posts: 188
Joined: Sat Feb 06, 2016 9:42 pm

Re: LuaPreprocess - straightforward preprocessor with simple syntax

Post 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")]])
User avatar
pgimeno
Party member
Posts: 3541
Joined: Sun Oct 18, 2015 2:58 pm

Re: LuaPreprocess - straightforward preprocessor with simple syntax

Post 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.
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: LuaPreprocess - straightforward preprocessor with simple syntax

Post 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.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
pgimeno
Party member
Posts: 3541
Joined: Sun Oct 18, 2015 2:58 pm

Re: LuaPreprocess - straightforward preprocessor with simple syntax

Post 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.
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: LuaPreprocess - straightforward preprocessor with simple syntax

Post 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".
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
pgimeno
Party member
Posts: 3541
Joined: Sun Oct 18, 2015 2:58 pm

Re: LuaPreprocess - straightforward preprocessor with simple syntax

Post 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.
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: LuaPreprocess - straightforward preprocessor with simple syntax

Post 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)
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: LuaPreprocess - straightforward preprocessor with simple syntax

Post 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.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

LuaPreprocess update 1.13.1

Post by ReFreezed »

Update 1.13.1

Changes since 1.13:

Library: Command line program:
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 13 guests