-
Notifications
You must be signed in to change notification settings - Fork 0
/
Erlang.lua
225 lines (194 loc) · 5.33 KB
/
Erlang.lua
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
Erlang = (function()
TAG = {
VERSION = 131,
COMPRESSED_ZLIB = 80,
NEW_FLOAT_EXT = 70,
BIT_BINARY_EXT = 77,
ATOM_CACHE_REF = 78,
NEW_PID_EXT = 88,
NEW_PORT_EXT = 89,
NEWER_REFERENCE_EXT = 90,
SMALL_INTEGER_EXT = 97,
INTEGER_EXT = 98,
FLOAT_EXT = 99,
ATOM_EXT = 100,
REFERENCE_EXT = 101,
PORT_EXT = 102,
PID_EXT = 103,
SMALL_TUPLE_EXT = 104,
LARGE_TUPLE_EXT = 105,
NIL_EXT = 106,
STRING_EXT = 107,
LIST_EXT = 108,
BINARY_EXT = 109,
SMALL_BIG_EXT = 110,
LARGE_BIG_EXT = 111,
NEW_FUN_EXT = 112,
EXPORT_EXT = 113,
NEW_REFERENCE_EXT = 114,
SMALL_ATOM_EXT = 115,
MAP_EXT = 116,
FUN_EXT = 117,
ATOM_UTF8_EXT = 118,
SMALL_ATOM_UTF8_EXT = 119
};
local function unpack(fmt, s)
local value, next = string.unpack('>' .. fmt, s);
local rest = s:sub(next);
return value, rest;
end;
local atoms = {};
local function atom(str)
if(#str == 0 or str == nil) then error('Invalid atom'); end;
if(not atoms[str]) then
atoms[str] = setmetatable(
{ ATOM = str },
{
__tostring = function() return 'Erlang Atom :' .. str; end,
__index = { __type = 'atom' }
}
);
end;
return atoms[str];
end
local function apply(old, meta)
local new = setmetatable({}, { __index = meta });
for k,v in pairs(old) do new[k] = v; end;
return new;
end;
local function tuple(t)
return apply(t, { __type = 'tuple' });
end;
local function map(t)
return apply(t, { __type = 'map' });
end;
local function list(t)
local tbl = apply({}, { __type = 'list' });
-- copy numeric keys only
for i,v in ipairs(t) do tbl[i] = v; end;
return tbl;
end;
local function typeof(t)
return type(t) == 'table' and t.__type or type(t);
end;
local function hex(s)
return s:gsub('.', function(c) return ('%02x'):format(string.byte(c)) end);
end;
local Decoders = {
[TAG.BINARY_EXT] = 's4',
[TAG.NEW_FLOAT_EXT] = 'd',
[TAG.SMALL_INTEGER_EXT] = 'B',
[TAG.INTEGER_EXT] = 'I4'
};
local function decodeTag(s)
local tag, s = unpack('B', s);
if(Decoders[tag]) then
if(type(Decoders[tag]) == 'function') then
return Decoders[tag](s);
elseif(type(Decoders[tag]) == 'string') then
return unpack(Decoders[tag], s);
end;
else
for k,v in pairs(TAG) do
if(v == tag) then
error('Unimplemented tag: ' .. k);
end;
end;
error('Unrecognised tag: ' .. tag);
end;
end;
Decoders[TAG.ATOM_EXT] = function(s)
local str, s = unpack('s2', s);
return atom(str), s;
end;
-- Decoders[TAG.NEW_PID_EXT] = function(s)
-- local node, id, serial, creation = string.unpack('>xs2I4I4I4', s);
-- print(node, id, serial, creation);
-- end;
local function decodeTuple(s, sizeFmt)
local elements, s = unpack(sizeFmt, s);
local t, elem = {};
for i=1, elements do
elem, s = decodeTag(s);
table.insert(t, elem);
end;
return tuple(t), s;
end;
Decoders[TAG.NIL_EXT] = function(s)
return {}, s;
end;
Decoders[TAG.SMALL_TUPLE_EXT] = function(s)
return decodeTuple(s, 'B');
end;
Decoders[TAG.LARGE_TUPLE_EXT] = function(s)
return decodeTuple(s, 'I4');
end;
Decoders[TAG.MAP_EXT] = function(s)
local elements, s = unpack('I4', s);
local t,k,v = {};
for i=1, elements do
k, s = decodeTag(s);
v, s = decodeTag(s);
t[k] = v;
end;
return map(t), s;
end;
Decoders[TAG.LIST_EXT] = function(s)
local elements, s = unpack('I4', s);
local t, elem = {};
for i=1, elements do
elem, s = decodeTag(s);
table.insert(t, elem);
end;
local tail, s = decodeTag(s);
if(type(tail) ~= 'table' or next(tail) ~= nil) then
table.insert(t, tail);
end;
return list(t), s;
end;
Decoders[TAG.STRING_EXT] = function(s)
local length, s = unpack('I2', s);
local elements = {string.unpack(string.rep('B', length), s)};
s = s:sub(table.remove(elements));
return list(elements), s;
end;
Decoders[TAG.SMALL_BIG_EXT] = function(s)
local length, s = unpack('B', s);
local sign, s = unpack('B', s);
local num, byte = 0 - sign;
for i=0, length-1 do
byte, s = unpack('B', s);
num = num + byte << (i*8);
end;
return num, s;
end;
-- [[ Unsupported due to lack of bignum support ]]
-- Decoders[TAG.LARGE_BIG_EXT] = function(s)
-- local length, s = unpack('I4', s);
-- print('bignum length', length);
-- local sign, s = unpack('B', s);
-- local num, byte = 0 - sign;
-- for i=0, length-1 do
-- byte, s = unpack('B', s);
-- num = num + byte << (i*8);
-- print(num)
-- end;
-- return num, s;
-- end;
function decode(s)
if(#s == 0) then return nil; end;
local version, s = unpack('B', s);
if(version ~= TAG.VERSION) then
error(("Wrong Erlang version: %d"):format(version));
end;
return (decodeTag(s));
end
return {
decode = decode,
atom = atom,
tuple = tuple,
list = list,
map = map,
type = typeof
};
end)();