-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpr.lua
More file actions
92 lines (81 loc) · 2.08 KB
/
Copy pathcpr.lua
File metadata and controls
92 lines (81 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
local M = {
Class = require "class",
dump = dump,
ipairs = ipairs,
pairs = pairs,
getmetatable = getmetatable,
setmetatable = setmetatable,
type = type,
concat = table.concat,
next = next,
remove = table.remove,
format = string.format,
-- not needed
print = print,
assert = assert,
idump = idump,
tassert = tassert,
}
M.disown = function(self)
if not self["@tag"] then
dump(self)
end
local m = assert(M[self["@tag"]], "no metatable for type")
if getmetatable(self) ~= m then
--print("disown", self["@tag"])
m(setmetatable(self, m))
end
return self
end
M.explain = function(self)
local tab = {self["@tag"]}
for k, v in pairs(self) do
tab[#tab+1] = " " .. v["@tag"]
end
print(table.concat(tab, "\n"))
end
local function deepcompare(t1,t2,ignore_mt)
local ty1 = type(t1)
local ty2 = type(t2)
if ty1 ~= ty2 then return false end
-- non-table types can be directly compared
if ty1 ~= 'table' and ty2 ~= 'table' then return t1 == t2 end
-- as well as tables which have the metamethod __eq
local mt = getmetatable(t1)
if not ignore_mt and mt and mt.__eq then return t1 == t2 end
if t1["@tag"] == "token" and t2["@tag"] == "token" then
return t1.value == t2.value
end
for k1,v1 in pairs(t1) do
local v2 = t2[k1]
if v2 == nil or not deepcompare(v1,v2) then return false end
end
for k2,v2 in pairs(t2) do
local v1 = t1[k2]
if v1 == nil or not deepcompare(v1,v2) then return false end
end
return true
end
M.deepcompare = deepcompare
local function import(name, env)
local f,e = loadfile(name)
if not f then
error(e, 2)
end
setfenv(f, env)
return f()
end
import("cpr-block.lua", M)
import("cpr-declaration.lua", M)
import("cpr-enum.lua", M)
import("cpr-environment.lua", M)
import("cpr-expression.lua", M)
import("cpr-function.lua", M)
import("cpr-initializer.lua", M)
import("cpr-namespace.lua", M)
import("cpr-source.lua", M)
import("cpr-statement.lua", M)
import("cpr-struct.lua", M)
import("cpr-type.lua", M)
import("cpr-interface.lua", M)
return M