-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.lua
More file actions
60 lines (53 loc) · 1.75 KB
/
Copy pathParser.lua
File metadata and controls
60 lines (53 loc) · 1.75 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
Parser = {}
function Parser:New(strParam)
local PS = {};
PS.line = 0;
PS.position = 0;
PS.curtokem = 0;
PS.token = {};
if not strParam or type(strParam) ~= 'string' then
print("Parser recebou arquivo nao valido "..tostring(strParam));
return nil;
end
PS.vmfile = io.open(strParam, "r");
if not PS.vmfile then
print("nao foi possivel abri o arquivo "..tostring(strParam));
return nil;
end
function IsCommand(cmd)
local comands = {"pop", "push", "add", "sub", "neg", "eq", "gt", "lt", "and", "or", "not", "label", "if-goto", "function", "return", "call", "goto"};
for i, v in pairs(comands) do
--print("comapar "..tostring(cmd).." "..i);
if v == cmd then return true
end
end
return false;
end
function PS:Advance()
local line = PS.vmfile:read();
while line do
PS.token.cmd, PS.token.segment, PS.token.index = line:match('^(.-)%s(.-)%s(%d+)');
if not PS.token.cmd and not line:match("^(//)")then
local s= {};
for str in line:gmatch("([^%s]+)") do
table.insert(s, str);
end
PS.token.cmd, PS.token.segment = s[1], s[2];
end
if IsCommand(PS.token.cmd) then
PS.line = PS.line + 1;
return true;
else
PS.token.cmd = line:match('^(.+)');
if IsCommand(PS.token.cmd) then
PS.line = PS.line + 1;
return true;
end
end
line = PS.vmfile:read();
end
return nil;
end
--print("retornando")
return PS;
end