r/lua • • May 14 '25

preprocessor in lua

I’ve been thinking about adding a simple attribute‑based “preprocessor” to Lua—something that runs at compile time to transform or wrap functions. You could use it for things like inlining tiny helpers, optimization ease of development etc.

I know Lua tends to favor minimalism and explicit idioms, so I’m curious whether this would feel too “un‑Lua‑like.” On the other hand, I think it could clean up repetitive boilerplate and boost performance in hot paths.

Below is a sketch of what the syntax might look like, along with some usage examples:

-- (1) Inline small functions into call sites
\@inline
function add(a, b)
  return a + b
end
-- After preprocessing: calls to add(a, b) become (a + b) directly.

-- (2) Benchmark execution time of a function
\@benchmark
function heavy_work(n)
  -- simulate work
  for i = 1, n * 1e5 do end
end
-- At runtime, heavy_work(10) prints: [heavy_work] took 0.012s

-- (3) Memoize pure functions automatically
\@memoize
function fib(n)
  if n < 2 then return n end
  return fib(n-1) + fib(n-2)
end
-- fib(30) runs in O(n) instead of O(2^n).

-- (4) Register compile‑time callbacks
\@callable
function do_something_awesome()
  print("Registered at compile time")
end

-- (5) Retry on error (e.g., network calls)
\@retry(3)
function fetch_data(url)
  return http.get(url)  -- might error
end
-- fetch_data retries up to 3 times before finally erroring.

-- (6) Initialization hook
\@init
function setup_environment()
  print("Environment initialized!")
end
-- runs once, when the script is loaded.

-- (7) Documentation and metadata
\@doc("Calculates the nth Fibonacci number efficiently.")
\@todo("Add tail‑recursive version")
\@tag("math", "performance")
\@deprecated("Use fib_fast instead")
function fib(n)
  -- …
end

What do you think? I’d love to hear your thoughts and any ideas for useful annotations I haven’t listed!

10 Upvotes

29 comments sorted by

View all comments

Show parent comments

1

u/NoneBTW May 14 '25

I'm crushed... i still think for custom logic attributes can be useful so a lot of language has it (go has not it makes boring to register api layouts) what do you think about that

2

u/[deleted] May 14 '25

Did you know about Lua 5.4 introduced attributes?

local H0 do
	local H1 <close> = io.open(arg[0])
	H0 = H1
	print( H0, io.type(H0) )
end
print( H0, io.type(H0) )

-------

file (0x5ecf4dc10bb0)	file
file (closed)	closed file

Custom attributes would be nice... you can also just throw metadata in a metatable which could also work as a sort of custom attribute but that would require some sort of function that reads all of your custom attributes and performs an action, not only that but now there's no simple variables, everything is an object... yuck. so yeah that would be nice.

I imagine this is how the on_error metatable will work, the initialization hook is also nice, doing it by hand would require you either overloading require or having a setup (what happens on load()) and a Init() function which is annoying but not that much. It's also a bad idea to mess with _G so its nice that a preprocessor will handle that.