Rename "ref" to "node", and update some comments
The NodeUpdate can be used for both ast.Exp and ast.Var nodes, so "ref" isn't a good name.
My editor also deleted a bunch of whitespace.
@@ -48,16 +48,13 @@ function converter.convert(prog_ast)
end
--- Encapsulates an update on an AST Node.
--- A Node update has two parts:
--- `ref`: An ast.Var that denotes the node that has to be updated.
--- `update_fn`: A `(ast.Var) -> ()` function that is used to update the node.
--- `ref`'s location is used to create a new AST Node. The new node is then used to replace the original
--- occurance of `ref` in the AST by calling `update_fn` on the newly created node.
-local function NodeUpdate(ref, update_fn)
+-- Encapsulates an update to an AST Node.
+-- `node`: The AST we will replace (we use it's location)
+-- `update_fn`: A `node -> ()` function that is used to update the node.
+local function NodeUpdate(node, update_fn)
return {
- ref = ref, -- ast.Var
- update_fn = update_fn, -- (ast.Var) -> ()
+ node = node,
+ update_fn = update_fn,
}
end
@@ -138,20 +135,23 @@ function Converter:apply_transformations()
assert(not decl._exported_as)
-- 1. Create a record type `$T` to hold this captured var.
- -- 2. Transform the ast.Decl node from `local x = value` to `local x: $T = { value = value }`
- -- 3. Go over all the references ever made to this variable and transform them from `ast.Var.Name`
- -- to ast.Var.Dot
- local typ = types.T.Record(self:type_name(decl.name), { "value" } , { value = decl._type } )
+ -- 2. Transform node from `local x = value` to `local x: $T = { value = value }`
+ -- 3. Transform all references to the var from `ast.Var.Name` to ast.Var.Dot
+ local typ = types.T.Record(
+ self:type_name(decl.name),
+ { "value" } ,
+ { value = decl._type }
+ )
self:add_box_type(decl.loc, typ)
decl._type = typ
local init_exp_update = self.update_init_exp_of_decl[decl]
if init_exp_update then
- local old_ref = init_exp_update.ref
+ local old_exp = init_exp_update.node
local update = init_exp_update.update_fn
- local new_node = ast.Exp.InitList(old_ref.loc, {{ name = "value", exp = old_ref }})
+ local new_node = ast.Exp.InitList(old_exp.loc, {{ name = "value", exp = old_exp }})
new_node._type = typ
update(new_node)
else
@@ -159,10 +159,11 @@ function Converter:apply_transformations()
end
for _, node_update in ipairs(self.update_ref_of_decl[decl]) do
- local old_ref = node_update.ref
+ local old_var = node_update.node
local update = node_update.update_fn
- local new_node = ast.Var.Dot(old_ref.loc, ast.Exp.Var(old_ref.loc, old_ref), "value")
+ local loc = old_var.loc
+ local new_node = ast.Var.Dot(loc, ast.Exp.Var(loc, old_var), "value")
new_node.exp._type = typ
update(new_node)
end
