aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2018-01-23 19:28:53 -0800
committerBruce Hill <bitbucket@bruce-hill.com>2018-01-23 19:29:11 -0800
commit49f0e9557167fcfd84d43d104ecf4ff1cf43eb45 (patch)
tree666e0da15cba624f32e14397905bf0e268cb089b /lib
parent6d11354b3f7226a9a417ae89f53de7a225434647 (diff)
Prevented redeclaration of function arguments as locals.
Diffstat (limited to 'lib')
-rw-r--r--lib/metaprogramming.nom37
1 files changed, 31 insertions, 6 deletions
diff --git a/lib/metaprogramming.nom b/lib/metaprogramming.nom
index eec99a0..8c50deb 100644
--- a/lib/metaprogramming.nom
+++ b/lib/metaprogramming.nom
@@ -34,11 +34,19 @@ immediately
lua> ".."
nomsu:define_compile_action("compile %names to %body", \(__line_no__), function(\%names, \%body)
local names, args = nomsu:parse_spec(\%names);
+ local declared_locals = {};
+ for i, arg in ipairs(args) do declared_locals[arg] = true; end
names, args = repr(names), table.concat(args, ", ");
local body_lua = nomsu:tree_to_lua(\%body);
local body_code = body_lua.statements or ("return "..body_lua.expr..";");
- if body_lua.locals and #body_lua.locals > 0 then
- body_code = "local "..table.concat(body_lua.locals, ", ")..";\\n"..body_code;
+ local undeclared_locals = {};
+ for i, body_local in ipairs(body_lua.locals or {}) do
+ if not declared_locals[body_local] then
+ table.insert(undeclared_locals, body_local);
+ end
+ end
+ if #undeclared_locals > 0 then
+ body_code = "local "..table.concat(undeclared_locals, ", ")..";\\n"..body_code;
end
local lua = ([[
do
@@ -55,11 +63,20 @@ immediately
lua> ".."
nomsu:define_compile_action("compile %names to code %body", \(__line_no__), function(\%names, \%body)
local names, args = nomsu:parse_spec(\%names);
+ local declared_locals = {};
+ for i, arg in ipairs(args) do declared_locals[arg] = true; end
names, args = repr(names), table.concat(args, ", ");
local body_lua = nomsu:tree_to_lua(\%body);
local body_code = body_lua.statements or ("return "..body_lua.expr..";");
- if body_lua.locals and #body_lua.locals > 0 then
- body_code = "local "..table.concat(body_lua.locals, ", ")..";\\n"..body_code;
+ local body_code = body_lua.statements or ("return "..body_lua.expr..";");
+ local undeclared_locals = {};
+ for i, body_local in ipairs(body_lua.locals or {}) do
+ if not declared_locals[body_local] then
+ table.insert(undeclared_locals, body_local);
+ end
+ end
+ if #undeclared_locals > 0 then
+ body_code = "local "..table.concat(undeclared_locals, ", ")..";\\n"..body_code;
end
local lua = ([[
do
@@ -78,11 +95,19 @@ immediately
compile [action %names %body] to code
lua> ".."
local names, args = nomsu:parse_spec(\%names);
+ local declared_locals = {};
+ for i, arg in ipairs(args) do declared_locals[arg] = true; end
names, args = repr(names), table.concat(args, ", ");
local body_lua = nomsu:tree_to_lua(\%body);
local body_code = body_lua.statements or ("return "..body_lua.expr..";");
- if body_lua.locals and #body_lua.locals > 0 then
- body_code = "local "..table.concat(body_lua.locals, ", ")..";\\n"..body_code;
+ local undeclared_locals = {};
+ for i, body_local in ipairs(body_lua.locals or {}) do
+ if not declared_locals[body_local] then
+ table.insert(undeclared_locals, body_local);
+ end
+ end
+ if #undeclared_locals > 0 then
+ body_code = "local "..table.concat(undeclared_locals, ", ")..";\\n"..body_code;
end
local src = nomsu:dedent(nomsu:source_code(0));
local def_lua = ([[