-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.lua
180 lines (165 loc) · 5.15 KB
/
parser.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
--Whitespace interpreter
--Labels have not yet been tested
--[[
Program:
{
"01201", "012010", 234, "0112"
}
The commands are encoded according to the SPACE, LF, and TAB constants, if an instruction needs a number after it, it'll be there
If an instruction uses a label, it will be there in number form, check the 'label number' for where the pc should be
Labels:
Labels[labelNumber] = newProgramcounter
]]
--#region TokenDefinitions
tokenType = {
space = 0, --0 and 1 for easy binary-nes
tab = 1, --If these are changed, anarchy will ensue
linefeed = 2 --This one too
}
SPACE = tokenType.space;
TAB = tokenType.tab;
LINEFEED = tokenType.linefeed;
LF = LINEFEED;
--#endregion
--#region LookupTables
--From char byte to token type
local lookupTable = {}
lookupTable[string.byte(" ")] = tokenType.space;
lookupTable[string.byte(" ")] = tokenType.tab;
lookupTable[string.byte("\n")] = tokenType.linefeed;
--Turns argsToCommand(SPACE, SPACE) into "00", entirely here for convenience
function argsToCommand(...)
local tab = {...};
local ret = "";
for i, v in pairs(tab) do
ret = ret .. tostring(v);
end
return ret;
end
--Tables of actions
local actionsWithNumbers = {
argsToCommand(SPACE, SPACE), argsToCommand(SPACE, TAB, SPACE), argsToCommand(SPACE, TAB, LF)
}
local actionsWithLabels = {
argsToCommand(LF, SPACE, SPACE), argsToCommand(LF, SPACE, TAB),
argsToCommand(LF, SPACE, LF), argsToCommand(LF, TAB, SPACE),
argsToCommand(LF, TAB, TAB)
}
--The one command that's not a command it just defines a label and so doesn't need to be in the output as a command
local labelDefAction = argsToCommand(LF, SPACE, SPACE);
local actionsWithoutArgs = {
argsToCommand(SPACE, LF, SPACE), argsToCommand(SPACE, LF, TAB),
argsToCommand(SPACE, LF, LF), argsToCommand(TAB, SPACE, SPACE, SPACE),
argsToCommand(TAB, SPACE, SPACE, TAB), argsToCommand(TAB, SPACE, SPACE, LF),
argsToCommand(TAB, SPACE, TAB, SPACE), argsToCommand(TAB, SPACE, TAB, TAB),
argsToCommand(TAB, TAB, SPACE), argsToCommand(TAB, TAB, TAB),
argsToCommand(LF, TAB, LF), argsToCommand(LF, LF, LF),
argsToCommand(TAB, LF, SPACE, SPACE), argsToCommand(TAB, LF, SPACE, TAB),
argsToCommand(TAB, LF, TAB, SPACE), argsToCommand(TAB, LF, TAB, TAB)
}
--Returns true or false if x is in table t
local function isInTable(t, x)
for i, v in pairs(t) do
if v == x then return true end;
end
return false;
end
--#endregion
--Takes a string in the form "101001010" and returns the number in decimal
local function binaryStringToInt(str)
local ret = 0;
for q = 1, str:len() do
if str:sub(q,q) == "1" then
ret = ret + math.pow(2, str:len() - q);
end
end
return ret;
end
--Gets a number from a whitespace str, assumes that index is the start of the number
--Also returns the new index in the string
local function getNumber(str, index)
local number = "";
local sign = "";
--Go over the chars starting at index
for q = index, str:len() do
index = q;
local token = lookupTable[str:sub(q,q):byte()];
if token == LF then break; end
if token and q > 1 then
number = number .. tostring(token);
elseif q == 1 and token == TAB then --Set the sign for the first "bit"
sign = "-";
end
end
return binaryStringToInt(sign .. number), index;
end
local module = {};
--Parse the input file (in whitespace)
--Returns a "program" and a list of labels
function module.parse(filename)
--Array of stuff to return
local output = {};
--Array of labels
local labels = {};
--Read the file
local file = io.open(filename, "r");
local text = file:read("*a");
local currentAction = "";
--For every char
local q = 1;
while true do
if q > text:len() then break end;
local token = lookupTable[text:sub(q,q):byte()];
--If it's not in the lookup table, ignore it
if token then
currentAction = currentAction .. tostring(token);
if currentAction:len() >= 5 then
error("Current action is 5 or greater");
end
--Check if we've found an action
if currentAction then
if isInTable(actionsWithNumbers, currentAction) then
output[#output+1] = currentAction;
local num;
num, q = getNumber(text, q + 1);
output[#output+1] = num;
currentAction = "";
elseif isInTable(actionsWithLabels, currentAction) then
local num;
num, q = getNumber(text, q + 1);
--If just a label definition
if currentAction == labelDefAction then
--Set this label to point to the next instruction
labels[num] = #output + 1;
else --We do something with the label
output[#output+1] = currentAction;
output[#output+1] = num;
end
currentAction = "";
elseif isInTable(actionsWithoutArgs, currentAction) then
output[#output+1] = currentAction;
currentAction = "";
end
end
end
q = q + 1;
end
file:close();
return output, labels;
end
--Removes all non-LF, TAB, ans SPACE chars from the infile
--and writes the output to outFile
function module.removeComments(inFilename, outFilename)
local inFile = io.open(inFilename, "r");
local outFile = io.open(outFilename, "w");
local text = inFile:read("*a");
for q = 1, text:len() do
local token = lookupTable[text:sub(q,q):byte()];
if token then
outFile:write(text:sub(q,q));
end
end
inFile:close();
outFile:close();
end
return module;