Removing varargs from free var functions.

This commit is contained in:
Bruce Hill 2018-05-04 13:49:09 -07:00
parent a5bbce315d
commit 126f51f955
4 changed files with 33 additions and 27 deletions

View File

@ -201,7 +201,7 @@ do
local _class_0 local _class_0
local _parent_0 = Code local _parent_0 = Code
local _base_0 = { local _base_0 = {
add_free_vars = function(self, ...) add_free_vars = function(self, vars)
local seen local seen
do do
local _tbl_0 = { } local _tbl_0 = { }
@ -215,13 +215,14 @@ do
end end
seen = _tbl_0 seen = _tbl_0
end end
for i = 1, select("#", ...) do for _index_0 = 1, #vars do
local var = select(i, ...) local var = vars[_index_0]
if type(var) == 'userdata' and var.type == "Var" then if type(var) == 'userdata' and var.type == "Var" then
var = tostring(var:as_lua()) var = tostring(var:as_lua())
elseif type(var) ~= 'string' then elseif type(var) ~= 'string' then
var = tostring(var) var = tostring(var)
end end
assert(var:match("^[_a-zA-Z][_a-zA-Z0-9]*$"))
if not (seen[var]) then if not (seen[var]) then
self.free_vars[#self.free_vars + 1] = var self.free_vars[#self.free_vars + 1] = var
seen[var] = true seen[var] = true
@ -229,15 +230,16 @@ do
end end
self.__str = nil self.__str = nil
end, end,
remove_free_vars = function(self, ...) remove_free_vars = function(self, vars)
local removals = { } local removals = { }
for i = 1, select("#", ...) do for _index_0 = 1, #vars do
local var = select(i, ...) local var = vars[_index_0]
if type(var) == 'userdata' and var.type == "Var" then if type(var) == 'userdata' and var.type == "Var" then
var = tostring(var:as_lua()) var = tostring(var:as_lua())
elseif type(var) ~= 'string' then elseif type(var) ~= 'string' then
var = tostring(var) var = tostring(var)
end end
assert(var:match("^[_a-zA-Z][_a-zA-Z0-9]*$"))
removals[var] = true removals[var] = true
end end
local stack = { local stack = {
@ -292,6 +294,7 @@ do
local var = _list_0[_index_0] local var = _list_0[_index_0]
if not (seen[var]) then if not (seen[var]) then
seen[var] = true seen[var] = true
assert(var:match("^[_a-zA-Z][_a-zA-Z0-9]*$"))
to_declare[#to_declare + 1] = var to_declare[#to_declare + 1] = var
end end
end end
@ -306,9 +309,10 @@ do
gather_from(self) gather_from(self)
end end
if #to_declare > 0 then if #to_declare > 0 then
self:remove_free_vars(unpack(to_declare)) self:remove_free_vars(to_declare)
return self:prepend("local " .. tostring(concat(to_declare, ", ")) .. ";\n") self:prepend("local " .. tostring(concat(to_declare, ", ")) .. ";\n")
end end
return to_declare
end, end,
__tostring = function(self) __tostring = function(self)
if self.__str == nil then if self.__str == nil then

View File

@ -134,27 +134,27 @@ class Lua extends Code
lua.is_value = true lua.is_value = true
return lua return lua
add_free_vars: (...)=> add_free_vars: (vars)=>
seen = {[v]:true for v in *@free_vars} seen = {[v]:true for v in *@free_vars}
for i=1,select("#",...) for var in *vars
var = select(i, ...)
if type(var) == 'userdata' and var.type == "Var" if type(var) == 'userdata' and var.type == "Var"
var = tostring(var\as_lua!) var = tostring(var\as_lua!)
elseif type(var) != 'string' elseif type(var) != 'string'
var = tostring(var) var = tostring(var)
assert(var\match("^[_a-zA-Z][_a-zA-Z0-9]*$"))
unless seen[var] unless seen[var]
@free_vars[#@free_vars+1] = var @free_vars[#@free_vars+1] = var
seen[var] = true seen[var] = true
@__str = nil @__str = nil
remove_free_vars: (...)=> remove_free_vars: (vars)=>
removals = {} removals = {}
for i=1,select("#",...) for var in *vars
var = select(i, ...)
if type(var) == 'userdata' and var.type == "Var" if type(var) == 'userdata' and var.type == "Var"
var = tostring(var\as_lua!) var = tostring(var\as_lua!)
elseif type(var) != 'string' elseif type(var) != 'string'
var = tostring(var) var = tostring(var)
assert(var\match("^[_a-zA-Z][_a-zA-Z0-9]*$"))
removals[var] = true removals[var] = true
stack = {self} stack = {self}
@ -183,14 +183,16 @@ class Lua extends Code
for var in *@free_vars for var in *@free_vars
unless seen[var] unless seen[var]
seen[var] = true seen[var] = true
assert(var\match("^[_a-zA-Z][_a-zA-Z0-9]*$"))
to_declare[#to_declare+1] = var to_declare[#to_declare+1] = var
for bit in *@bits for bit in *@bits
if bit.__class == Lua if bit.__class == Lua
gather_from bit gather_from bit
gather_from self gather_from self
if #to_declare > 0 if #to_declare > 0
@remove_free_vars unpack(to_declare) @remove_free_vars to_declare
@prepend "local #{concat to_declare, ", "};\n" @prepend "local #{concat to_declare, ", "};\n"
return to_declare
__tostring: => __tostring: =>
if @__str == nil if @__str == nil

View File

@ -28,7 +28,7 @@ immediately
end end
local body_lua = \%lua:as_lua(nomsu); local body_lua = \%lua:as_lua(nomsu);
body_lua:convert_to_statements("return "); body_lua:convert_to_statements("return ");
body_lua:remove_free_vars(unpack(args)); body_lua:remove_free_vars(args);
body_lua:declare_locals(); body_lua:declare_locals();
lua:append(")\n ", body_lua, "\nend);"); lua:append(")\n ", body_lua, "\nend);");
return lua; return lua;
@ -60,7 +60,7 @@ immediately
end end
local body_lua = \%body:as_lua(nomsu); local body_lua = \%body:as_lua(nomsu);
body_lua:convert_to_statements("return "); body_lua:convert_to_statements("return ");
body_lua:remove_free_vars(unpack(args)); body_lua:remove_free_vars(args);
body_lua:declare_locals(); body_lua:declare_locals();
lua:append(")\n ", body_lua, "\nend);") lua:append(")\n ", body_lua, "\nend);")
return lua; return lua;
@ -127,14 +127,14 @@ immediately
lua:convert_to_statements(); lua:convert_to_statements();
return lua; return lua;
compile [declare locals in %tree] to compile [declare locals in %code] to
Lua "\(%tree as lua expr):declare_locals();" Lua value "\(%code as lua expr):declare_locals()"
compile [declare locals %locals in %tree] to compile [declare locals %locals in %code] to
Lua "\(%tree as lua expr):declare_locals(\(%locals as lua expr));" Lua value "\(%code as lua expr):declare_locals(\(%locals as lua expr))"
compile [remove free vars %vars from %tree] to compile [remove free vars %vars from %code] to
Lua "\(%tree as lua expr):remove_free_vars(unpack(\(%vars as lua expr)));" Lua "\(%code as lua expr):remove_free_vars(\(%vars as lua expr));"
action [%tree as value] action [%tree as value]
=lua "nomsu:tree_to_value(\%tree)" =lua "nomsu:tree_to_value(\%tree)"

View File

@ -54,7 +54,7 @@ immediately
lua> ".." lua> ".."
local lua = Lua(tree.source, \%var_lua, ' = ', \%value_lua, ';'); local lua = Lua(tree.source, \%var_lua, ' = ', \%value_lua, ';');
if \%var.type == 'Var' then if \%var.type == 'Var' then
lua:add_free_vars(\%var); lua:add_free_vars({\%var});
end end
return lua; return lua;
@ -72,7 +72,7 @@ immediately
local value_lua = value:as_lua(nomsu); local value_lua = value:as_lua(nomsu);
if not value_lua.is_value then error("Invalid value for assignment: "..value:get_src()); end if not value_lua.is_value then error("Invalid value for assignment: "..value:get_src()); end
if target.type == "Var" then if target.type == "Var" then
lhs:add_free_vars(target); lhs:add_free_vars({target});
end end
if i > 1 then if i > 1 then
lhs:append(", "); lhs:append(", ");
@ -93,7 +93,7 @@ immediately
compile [exporting %exported %body] to compile [exporting %exported %body] to
%body_lua <- (%body as lua statements) %body_lua <- (%body as lua statements)
lua> "\%body_lua:remove_free_vars(unpack(\(%exported.value)));" lua> "\%body_lua:remove_free_vars(\(%exported.value));"
return %body_lua return %body_lua
compile [with %assignments %body] to compile [with %assignments %body] to
@ -112,7 +112,7 @@ immediately
error("Invalid value for assignment: "..tostring(value.source:get_text())); error("Invalid value for assignment: "..tostring(value.source:get_text()));
end end
if target.type == "Var" then if target.type == "Var" then
lhs:add_free_vars(target); lhs:add_free_vars({target});
end end
if i > 1 then if i > 1 then
lhs:append(", "); lhs:append(", ");