Get rid of side effects in the template function
By not setting the metatable for the substitution parameter we keep the
template function side-effect free. It should also behave better if the
substitution table already has a metatable set (though I can't think of
why we would want to do that).
@@ -4,13 +4,17 @@ local coder = {}
local codeexp, codestat
-local function output(code, vars)
- local substitutions = setmetatable(vars, {
- __index = function(_, k)
+-- Barebones string-based template function for generating C code.
+-- Replaces $VAR placeholders in the `code` template by the corresponding
+-- strings in the `substs` table.
+local function output(code, substs)
+ return (string.gsub(code, "$([%w_]+)", function(k)
+ local v = substs[k]
+ if not v then
error("Internal compiler error: missing template variable " .. k)
end
- })
- return (string.gsub(code, "$([%u%d_]+)", substitutions))
+ return v
+ end))
end
local function quotestr(s)
