Renamed replaced_vars() to be more descriptive.
This commit is contained in:
parent
b4a0267f71
commit
90c72074db
@ -100,7 +100,7 @@ immediately:
|
|||||||
local lua_code = ([[
|
local lua_code = ([[
|
||||||
nomsu:define_compile_action(%s, %s, (function(%s)
|
nomsu:define_compile_action(%s, %s, (function(%s)
|
||||||
local template = nomsu:parse(%s, %s);
|
local template = nomsu:parse(%s, %s);
|
||||||
local replacement = nomsu:replaced_vars(template, %s);
|
local replacement = nomsu:tree_with_replaced_vars(template, %s);
|
||||||
return nomsu:tree_to_lua(replacement);
|
return nomsu:tree_to_lua(replacement);
|
||||||
end), %s)]]):format(names, repr(\%shorthand:get_line_no()), args, template,
|
end), %s)]]):format(names, repr(\%shorthand:get_line_no()), args, template,
|
||||||
repr(\%shorthand:get_line_no()), replacements, repr(nomsu:source_code(0)));
|
repr(\%shorthand:get_line_no()), replacements, repr(nomsu:source_code(0)));
|
||||||
@ -143,7 +143,7 @@ compile [nomsu] to: "nomsu"
|
|||||||
compile [nomsu's %key] to: "nomsu[\(%key as lua)]"
|
compile [nomsu's %key] to: "nomsu[\(%key as lua)]"
|
||||||
compile [nomsu %method %args] to: "nomsu[\(%method as lua)](nomsu, unpack(\(%args as lua)))"
|
compile [nomsu %method %args] to: "nomsu[\(%method as lua)](nomsu, unpack(\(%args as lua)))"
|
||||||
compile [tree %tree with %replacements] to: ".."
|
compile [tree %tree with %replacements] to: ".."
|
||||||
nomsu:replaced_vars(\(%tree as lua), \(%replacements as lua))
|
nomsu:tree_with_replaced_vars(\(%tree as lua), \(%replacements as lua))
|
||||||
|
|
||||||
parse [action %names] as:
|
parse [action %names] as:
|
||||||
(nomsu's "defs")->(nomsu "get_stub" [\%names])
|
(nomsu's "defs")->(nomsu "get_stub" [\%names])
|
||||||
|
16
nomsu.lua
16
nomsu.lua
@ -996,17 +996,17 @@ do
|
|||||||
end
|
end
|
||||||
return concat(bits, "\n")
|
return concat(bits, "\n")
|
||||||
end,
|
end,
|
||||||
replaced_vars = function(self, tree, vars)
|
tree_with_replaced_vars = function(self, tree, replacements)
|
||||||
if type(tree) ~= 'table' then
|
if type(tree) ~= 'table' then
|
||||||
return tree
|
return tree
|
||||||
end
|
end
|
||||||
local _exp_0 = tree.type
|
local _exp_0 = tree.type
|
||||||
if "Var" == _exp_0 then
|
if "Var" == _exp_0 then
|
||||||
if vars[tree.value] ~= nil then
|
if replacements[tree.value] ~= nil then
|
||||||
tree = vars[tree.value]
|
tree = replacements[tree.value]
|
||||||
end
|
end
|
||||||
elseif "File" == _exp_0 or "Nomsu" == _exp_0 or "Block" == _exp_0 or "List" == _exp_0 or "FunctionCall" == _exp_0 or "Text" == _exp_0 then
|
elseif "File" == _exp_0 or "Nomsu" == _exp_0 or "Block" == _exp_0 or "List" == _exp_0 or "FunctionCall" == _exp_0 or "Text" == _exp_0 then
|
||||||
local new_value = self:replaced_vars(tree.value, vars)
|
local new_value = self:tree_with_replaced_vars(tree.value, replacements)
|
||||||
if new_value ~= tree.value then
|
if new_value ~= tree.value then
|
||||||
do
|
do
|
||||||
local _tbl_0 = { }
|
local _tbl_0 = { }
|
||||||
@ -1019,10 +1019,10 @@ do
|
|||||||
end
|
end
|
||||||
elseif "Dict" == _exp_0 then
|
elseif "Dict" == _exp_0 then
|
||||||
local dirty = false
|
local dirty = false
|
||||||
local replacements = { }
|
replacements = { }
|
||||||
for i, e in ipairs(tree.value) do
|
for i, e in ipairs(tree.value) do
|
||||||
local new_key = self:replaced_vars(e.dict_key, vars)
|
local new_key = self:tree_with_replaced_vars(e.dict_key, replacements)
|
||||||
local new_value = self:replaced_vars(e.dict_value, vars)
|
local new_value = self:tree_with_replaced_vars(e.dict_value, replacements)
|
||||||
dirty = dirty or (new_key ~= e.dict_key or new_value ~= e.dict_value)
|
dirty = dirty or (new_key ~= e.dict_key or new_value ~= e.dict_value)
|
||||||
replacements[i] = {
|
replacements[i] = {
|
||||||
dict_key = new_key,
|
dict_key = new_key,
|
||||||
@ -1043,7 +1043,7 @@ do
|
|||||||
local new_values = { }
|
local new_values = { }
|
||||||
local any_different = false
|
local any_different = false
|
||||||
for k, v in pairs(tree) do
|
for k, v in pairs(tree) do
|
||||||
new_values[k] = self:replaced_vars(v, vars)
|
new_values[k] = self:tree_with_replaced_vars(v, replacements)
|
||||||
any_different = any_different or (new_values[k] ~= tree[k])
|
any_different = any_different or (new_values[k] ~= tree[k])
|
||||||
end
|
end
|
||||||
if any_different then
|
if any_different then
|
||||||
|
14
nomsu.moon
14
nomsu.moon
@ -677,14 +677,14 @@ class NomsuCompiler
|
|||||||
insert bits, close
|
insert bits, close
|
||||||
return concat(bits)
|
return concat(bits)
|
||||||
|
|
||||||
replaced_vars: (tree, vars)=>
|
tree_with_replaced_vars: (tree, replacements)=>
|
||||||
if type(tree) != 'table' then return tree
|
if type(tree) != 'table' then return tree
|
||||||
switch tree.type
|
switch tree.type
|
||||||
when "Var"
|
when "Var"
|
||||||
if vars[tree.value] ~= nil
|
if replacements[tree.value] ~= nil
|
||||||
tree = vars[tree.value]
|
tree = replacements[tree.value]
|
||||||
when "File", "Nomsu", "Block", "List", "FunctionCall", "Text"
|
when "File", "Nomsu", "Block", "List", "FunctionCall", "Text"
|
||||||
new_value = @replaced_vars tree.value, vars
|
new_value = @tree_with_replaced_vars tree.value, replacements
|
||||||
if new_value != tree.value
|
if new_value != tree.value
|
||||||
tree = {k,v for k,v in pairs(tree)}
|
tree = {k,v for k,v in pairs(tree)}
|
||||||
tree.value = new_value
|
tree.value = new_value
|
||||||
@ -692,8 +692,8 @@ class NomsuCompiler
|
|||||||
dirty = false
|
dirty = false
|
||||||
replacements = {}
|
replacements = {}
|
||||||
for i,e in ipairs tree.value
|
for i,e in ipairs tree.value
|
||||||
new_key = @replaced_vars e.dict_key, vars
|
new_key = @tree_with_replaced_vars e.dict_key, replacements
|
||||||
new_value = @replaced_vars e.dict_value, vars
|
new_value = @tree_with_replaced_vars e.dict_value, replacements
|
||||||
dirty or= new_key != e.dict_key or new_value != e.dict_value
|
dirty or= new_key != e.dict_key or new_value != e.dict_value
|
||||||
replacements[i] = {dict_key:new_key, dict_value:new_value}
|
replacements[i] = {dict_key:new_key, dict_value:new_value}
|
||||||
if dirty
|
if dirty
|
||||||
@ -703,7 +703,7 @@ class NomsuCompiler
|
|||||||
new_values = {}
|
new_values = {}
|
||||||
any_different = false
|
any_different = false
|
||||||
for k,v in pairs tree
|
for k,v in pairs tree
|
||||||
new_values[k] = @replaced_vars v, vars
|
new_values[k] = @tree_with_replaced_vars v, replacements
|
||||||
any_different or= (new_values[k] != tree[k])
|
any_different or= (new_values[k] != tree[k])
|
||||||
if any_different
|
if any_different
|
||||||
tree = new_values
|
tree = new_values
|
||||||
|
Loading…
Reference in New Issue
Block a user