-
Notifications
You must be signed in to change notification settings - Fork 1
/
luac.lua
80 lines (74 loc) · 2.12 KB
/
luac.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
---@author: Xpecya
local syntax = require "syntax";
local __GENERATOR__ = require "generator";
local function getReader(fileName)
local file = io.open(fileName);
local line = 1;
local position = 1;
local newLine;
if file == nil then
error("source file doesn't exist!");
end
return {
next = function()
local result = file:read(1);
if newLine then
line = line + 1;
position = 1;
newLine = nil;
end
if result == "\n" then
newLine = true;
else
position = position + 1;
end
return result;
end,
error = function(message)
error(string.format("error at line %d, position %d: %s!\r\n", line, position, message));
end
};
end
local function toString(table, number)
local result = "{"
local addComma = false;
local spaces = "\r\n";
if number == nil then
number = 1;
end
for i = 1, number - 1 do
spaces = spaces .. "\t";
end
local startSpaces = spaces .. "\t";
for k, v in pairs(table) do
if addComma then
result = result .. ',';
else
addComma = true;
end
result = result .. startSpaces .. '"' .. k .. '" = ';
if v == true or v == false then
result = result .. tostring(v)
elseif v == nil then
result = result .. "nil"
else
local vType = type(v);
if vType == "number" then
result = result .. v;
elseif vType == "string" then
result = result .. '"' .. v .. '"';
elseif vType == "table" then
result = result .. toString(v, number + 1);
end
end
end
result = result .. spaces .. "}"
return result;
end
local fileName = arg[1];
local reader = getReader(fileName);
local tree = syntax(reader);
print(toString(tree));
local start = fileName:find("%.lua");
local resultFileName = fileName:sub(1, start - 1) .. ".ll";
__GENERATOR__(tree, resultFileName);