From 406f079621b9f4ad55ab080def2d051038a0125e Mon Sep 17 00:00:00 2001 From: Henri DF Date: Mon, 22 Feb 2016 20:27:57 -0800 Subject: [PATCH] Deep copy macro ASTs when expanding So that we can individually tag expressions that originate from the same macro (see next commit). --- userspace/digwatch/lua/compiler.lua | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/userspace/digwatch/lua/compiler.lua b/userspace/digwatch/lua/compiler.lua index 2a587f7a..b562e269 100644 --- a/userspace/digwatch/lua/compiler.lua +++ b/userspace/digwatch/lua/compiler.lua @@ -339,6 +339,14 @@ end --]] function expand_macros(node, defs, changed) + + function copy(obj) + if type(obj) ~= 'table' then return obj end + local res = {} + for k, v in pairs(obj) do res[copy(k)] = copy(v) end + return res + end + if (node.type == "Rule") then macros = expand_macros(node.filter, defs, changed) elseif node.type == "Filter" then @@ -346,7 +354,7 @@ function expand_macros(node, defs, changed) if (defs[node.value.value] == nil) then error("Undefined macro '".. node.value.value .. "' used in filter.") end - node.value = defs[node.value.value] + node.value = copy(defs[node.value.value]) changed = true return changed end @@ -358,7 +366,7 @@ function expand_macros(node, defs, changed) if (defs[node.left.value] == nil) then error("Undefined macro '".. node.left.value .. "' used in filter.") end - node.left = defs[node.left.value] + node.left = copy(defs[node.left.value]) changed = true end @@ -366,7 +374,7 @@ function expand_macros(node, defs, changed) if (defs[node.right.value] == nil) then error("Undefined macro ".. node.right.value .. "used in filter.") end - node.right = defs[node.right.value] + node.right = copy(defs[node.right.value]) changed = true end @@ -379,7 +387,7 @@ function expand_macros(node, defs, changed) if (defs[node.argument.value] == nil) then error("Undefined macro ".. node.argument.value .. "used in filter.") end - node.argument = defs[node.argument.value] + node.argument = copy(defs[node.argument.value]) changed = true end return expand_macros(node.argument, defs, changed)