Repository navigation
Expand file tree
/
Copy pathtype.lua
More file actions
165 lines (141 loc) · 4.95 KB
/
Copy pathtype.lua
File metadata and controls
165 lines (141 loc) · 4.95 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
local util = require('util')
local Type = {}
-- pointers have a "points_to" field that is a Type object
-- arrays have a length field
Type.INVERTED_KINDS = {
"VOID",
"CHAR",
"INT",
"LONG",
"STRUCT",
"UNION",
"POINTER",
"ARRAY",
"FUNCTION"
}
Type.BASE_KINDS = {
["VOID"]=1,
["CHAR"]=1,
["INT"]=1,
["LONG"]=1,
["SIGNED"]=1,
["UNSIGNED"]=1,
["FUNCTION"]=1,
["STRUCT"]=1,
["UNION"]=1,
["ENUM"]=1
}
Type.KINDS = util.invert_table(Type.INVERTED_KINDS)
Type.__index = Type
function Type:new(t)
local o = setmetatable(t or {}, Type)
return o
end
function Type.same_type_chain(type1, type2, allow_length_mismatch)
allow_length_mismatch = allow_length_mismatch or false
while(type1 ~= nil and type2 ~= nil) do
if(type1.kind ~= type2.kind or (not allow_length_mismatch and type1.length ~= type2.length)) then
return false
end
type1 = type1.points_to
type2 = type2.points_to
end
return type1 == type2 -- both are nil
end
function Type.base(kind)
if(type(kind) == "table") then
if(#kind == 1) then
return Type:new({kind = Type.KINDS[string.upper(kind[1])], signed = true})
elseif(#kind == 2) then
-- first kind is signedness
-- second kind is the base type
local first_kind = string.upper(kind[1])
local second_kind = string.upper(kind[2])
assert((first_kind == "SIGNED" or first_kind == "UNSIGNED"), "Invalid type specifier")
return Type:new({kind = Type.KINDS[second_kind], signed = first_kind == "SIGNED"})
else
error()
end
else
return Type:new({kind = Type.KINDS[string.upper(kind)], signed = true})
end
end
function Type.pointer(target_type)
return Type:new({kind = Type.KINDS["POINTER"], points_to = target_type})
end
function Type.array(length, target_type)
return Type:new({kind = Type.KINDS["ARRAY"], length = length, points_to = target_type})
end
function Type.func(return_type, parameter_types)
return Type:new({kind = Type.KINDS["FUNCTION"], return_type = return_type, parameter_types = parameter_types})
end
function Type.struct(id, members)
return Type:new({kind = Type.KINDS["STRUCT"], id = id, members = members or {}})
end
function Type.enum(id, members)
return Type:new({kind = Type.KINDS["ENUM"], id = id, members = members or {}})
end
function Type.union(id, members)
return Type:new({kind = Type.KINDS["UNION"], id = id, members = members or {}})
end
function Type.is_base_type(type)
return Type.BASE_KINDS[string.upper(Type.INVERTED_KINDS[type.kind])]
end
function Type.to_string(type)
if(type == nil) then
return "?"
end
if(type.kind == Type.KINDS["POINTER"]) then
return Type.to_string(type.points_to) .. "*"
elseif(type.kind == Type.KINDS["ARRAY"]) then
return Type.to_string(type.points_to) .. "[" .. (type.length >= 0 and type.length or "?") .. "]"
else
return Type.INVERTED_KINDS[type.kind]
end
end
function Type.to_string_pretty(type)
if(type == nil) then
return "?"
end
local chain = {}
while(type ~= nil) do
table.insert(chain, type)
type = type.points_to
end
local result = ""
local i = #chain
type = chain[i]
while(type ~= nil) do
local array_string = ""
while(type ~= nil and type.kind == Type.KINDS["ARRAY"]) do
array_string = "[" .. (type.length >= 0 and type.length or "?") .. "]" .. array_string
i = i - 1
type = chain[i]
end
result = result .. array_string
if(type == nil) then
break
elseif(type.kind == Type.KINDS["POINTER"]) then
result = result .. "*"
elseif(type.kind == Type.KINDS["FUNCTION"]) then
result = result .. "FUNCTION(("
for _, param in ipairs(type.parameter_types) do
result = result .. Type.to_string_pretty(param) .. ", "
end
result = result .. ") -> " .. Type.to_string_pretty(type.return_type) .. ")"
elseif(Type.INVERTED_KINDS[type.kind]) then
result = result .. Type.INVERTED_KINDS[type.kind]
else
local potential_symbol = get_symbol(type.kind, symbol_table.ordinary)
if(potential_symbol) then
result = result .. Type.to_string_pretty(potential_symbol.type)
else
error()
end
end
i = i - 1
type = chain[i]
end
return result
end
return Type