-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.lua
More file actions
190 lines (148 loc) · 4.27 KB
/
basic.lua
File metadata and controls
190 lines (148 loc) · 4.27 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
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
local parser = require("parser")
local syntax = require("syntax")
local emitter = require("nasm_emitter")
local dbg = require("debugger")
local IR = require("IR_translator")
local adaptivestream = require("adaptivetokenstream")
--local f = adaptivestream.new("mysrc.b")
local num_cb = function(n) n.value = tonumber(n.content); return n end
local op_cb = function(n)
if n.content == "=" then n.op_type = "binary"
n.precedence = 0
elseif n.content == "+=" then n.op_type = "binary"
n.precedence = 0
elseif n.content == "-=" then n.op_type = "binary"
n.precedence = 0
elseif n.content == "+" then n.op_type = "either"
n.precedence = 1
elseif n.content == "-" then n.op_type = "either"
n.precedence = 1
elseif n.content == "*" then n.op_type = "binary"
n.precedence = 2
elseif n.content == "/" then n.op_type = "binary"
n.precedence = 2
elseif n.content == "%" then n.op_type = "binary"
n.precedence = 2
elseif n.content == "++" then n.op_type = "unary"
n.precedence = 3
elseif n.content == "--" then n.op_type = "unary"
n.precedence = 3
end
n.op = n.content
return n
end
local start = os.clock()
f = adaptivestream.new("mysrc.b")
f:add_token_match("%a%w*_*%w*", "identifier")
f:add_token_match("%d+", "number", num_cb)
f:add_token_match("%+%+", "operator", op_cb)
f:add_token_match("%+", "operator", op_cb)
f:add_token_match("%-%-", "operator", op_cb)
f:add_token_match("%-", "operator", op_cb)
f:add_token_match("%*", "operator", op_cb)
f:add_token_match("%/", "operator", op_cb)
f:add_token_match("%(", "lparen")
f:add_token_match("%)", "rparen")
f:add_token_match("%[", "lsqbracket")
f:add_token_match("%]", "rsqbracket")
f:add_token_match("%=%=", "operator", op_cb)
f:add_token_match("%=", "operator", op_cb)
f:add_token_match("%+%=", "operator", op_cb)
f:add_token_match("%-%=", "operator", op_cb)
f:add_token_match("%;", "semicolon")
f:add_token_match("%:", "colon")
f:add_token_match("%.%.%.", "tdot")
f:add_token_match("%.%.", "ddot")
f:add_token_match("%.", "dot")
f:add_token_match("%,", "comma")
f:add_token_match("%s", "whitespace")
f:add_token_match("\".*\"","string")
----------------------------------------------------------------
-- Token verbose output
f:push()
local n = f:peek()
while n ~= nil and n.type ~= "EOF" do
local line = n.content
line = line:gsub("\n", "\\n")
io.write("token: "..n.type.." offset: "..tostring(n.line_pos)..
" content: ["..line.."] ")
if n.op_type ~= nil then print("type: "..(n.op_type or "n/a")) else io.write("\n") end
f:consume(n.type)
if n.type == "newline" then
io.write("Parsed from line "..n.line_nr..":\"")
io.write(n.line_txt)
print("\"")
end
n = f:peek()
end
f:pop()
----------------------------------------------------------------
--f:push()
--local tmp = parser.parse(f)
--f:pop()
local ast = parser.parse(f)
local res = syntax.verify(ast)
delta = os.clock() - start
print("Compile time "..(delta*1000).." ms")
io.write("Syntax: ")
if res then print("OK") else print("ERROR") end
print("")
parser.printAST(ast)
--[[
local nasm_cmd = "nasm -f win64 build.asm -o build.o -Wall"
local gcc_cmd = "gcc build.o -o build.exe -Wall"
-- Delete old build
os.execute("@del build.exe")
local ir = IR.translate(ast)
for k,v in pairs(ir.code) do
print(v.type.." "..tostring(v.reg1 or v.size).. " "..tostring(v.reg2 or "").." "..tostring(v.tag or ""))
end
print("Emitting asm")
emitter.emit(ir, "build")
--emitter.compile(ast)
print("Emitted build.asm")
local handle = io.popen(nasm_cmd)
local result = handle:read("*a")
handle:close()
handle = io.popen(gcc_cmd)
result = handle:read("*a")
handle:close()
print("Running application")
handle = io.popen("build.exe")
result = handle:read("*a")
handle:close()
print(tostring(result))
--]]
print("Done")
--[[
chunk, stat, block, laststat, varlist, explist
optional means only *one* instance may exist while
repeat means 0 or more may exist.
chunk.description = {
[1] = {
{_stat, optional = false, repeat = true},
{_laststat, optional = true, repeat = false}
}
}
_stat.desc = {
[1] = {
{stat, repeat = true},
{";", optional = true}
}
}
_laststat.desc = {
[1] = {
{laststat, optional = true},
{";", optional = true}
}
}
laststat.desc = {
[1] = {
{"return"},
{explist, optional = true},
},
[2] = {
{"break"}
}
}
]]