-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.lua
576 lines (509 loc) · 16.3 KB
/
compile.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
directIncludes = {}
indirectIncludes = {}
alreadyIncluded = {}
constants = {}
arrays = {}
functions = {}
variables = {}
callingDepth = 0
target = nil
function fileExists(name)
local f=io.open(name,"r")
if f~=nil then io.close(f) return true else return false end
end
function findIndirectInclude(include)
for _,dir in ipairs(indirectIncludes) do
if fileExists(dir..include..'.fen') then
return dir..include..'.fen'
end
if fileExists(dir..'/'..include..'.fen') then
return dir..'/'..include..'.fen'
end
end
end
function basicAssert(cond, msg)
return not cond and error(msg) or true
end
function main()
local platform = nil
local infile = nil
local outfile = nil
local argi = 1
while argi <= #arg do
if arg[argi] == '-i' then
basicAssert(argi+1 <= #arg or arg[argi+1]:match('^%-'), "-i <infile>: empty field infile")
basicAssert(not infile, "-i <infile>: infile already given")
infile = arg[argi+1]
elseif arg[argi] == '-o' then
basicAssert(argi+1 <= #arg or arg[argi+1]:match('^%-'), "-o <outfile>: empty field outfile")
basicAssert(not outfile, "-o <outfile>: outfile already given")
outfile = arg[argi+1]
elseif arg[argi] == '-l' then
basicAssert(argi+1 <= #arg or arg[argi+1]:match('^%-'), "-l <directory>: empty field directory")
table.insert(indirectIncludes, arg[argi+1])
elseif arg[argi] == '-L' then
basicAssert(argi+1 <= #arg or arg[argi+1]:match('^%-'), "-L <conversion>: empty field conversion")
local key,value = arg[argi+1]:match('^([^=]+)=([^=]+)$')
basicAssert(key and value, "-L <conversion>: conversion invalid")
directIncludes[key] = value
elseif arg[argi] == '-p' then
basicAssert(argi+1 <= #arg or arg[argi+1]:match('^%-'), "-p <platform>: empty field platform")
basicAssert(not platform, "-p <platform>: platform already given")
platform = arg[argi+1]
end
if arg[argi]:match('^%-[ioplL]$') then
argi = argi + 2
else
error(arg[argi]..": invalid tag")
end
end
basicAssert(platform, "no platform provided")
basicAssert(infile, "no infile provided")
basicAssert(outfile, "no outfile provided")
target = require(platform..'/target')
constants["WORD_SIZE"] = {type="number", value=target["WORD_SIZE"]}
compile(tokeniser(infile))
target.finish(outfile)
end
function compile(tokens)
while not tokens.eof() do
tokens.assert(declaration(tokens), 'invalid declaration')
end
end
function declaration(tokens)
return objectDeclaration(tokens)
or constantDeclaration(tokens)
or compilerDeclaration(tokens)
end
function statement(tokens)
return blockStatement(tokens)
or returnStatement(tokens)
or letStatement(tokens)
or callingDepth == 0 and allocateStatement(tokens)
or ifStatement(tokens)
or whileStatement(tokens)
or functionCall(tokens)
or implicitLet(tokens)
or numericalLiteral(tokens)
or stringLiteral(tokens)
end
function expression(tokens)
return blockStatement(tokens)
or functionCall(tokens)
or variableGet(tokens)
or numericalLiteral(tokens)
or stringLiteral(tokens)
end
function objectDeclaration(tokens)
local keyword = tokens.keyword('extern', 'intern', 'public', 'private')
if keyword then
return functionHeader(keyword, tokens) or arrayHeader(keyword, tokens)
end
end
function functionHeader(keyword, tokens)
if tokens.symbol("(") then
local name = tokens.name()
tokens.assert(name, "function name invalid")
tokens.assert(not target.builtin(name), "attempt to redefine builtin '"..name.."' as a function")
tokens.assert(not constants[name], "attempt to redefine constant '"..name.."' as a function")
tokens.assert(not functions[name] or not functions[name].defined, "attempt to redefine function '"..name.."' as a new function")
tokens.assert(not arrays[name], "attempt to redefine array '"..name.."' as a function")
variables = {}
local namedArgumentCount, allocatedCount = 0,0
local vararg = false
local allocating = false
while not tokens.symbol(")") do
if tokens.symbol(";") then
tokens.assert(not allocating, "more than one semicolon in function header for '"..name.."'")
allocating = true
else
local variable = tokens.name()
tokens.assert(variable, "function argument name '"..variable.."' invalid")
if tokens.symbol("...") then
vararg = true
allocating = true
tokens.symbol(";")
end
if allocating then
allocatedCount = allocatedCount + 1
else
namedArgumentCount = namedArgumentCount + 1
end
variables[variable] = {id = allocating and allocatedCount or namedArgumentCount, allocated=allocating}
end
end
functions[name] = {required = namedArgumentCount, moreAllowed = vararg, defined=(keyword=='private' or keyword=='public')}
if keyword == "public" then
target.public(name)
elseif keyword == "extern" then
target.extern(name)
end
if keyword == "public" or keyword == "private" then
target.functionDefinition(name, allocatedCount, vararg and namedArgumentCount)
tokens.assert(statement(tokens), "definition of function '"..name.."' is invalid")
target.numlit(0)
target.ret()
end
return true
end
end
function arrayHeader(keyword, tokens)
local name = tokens.name()
if name then
tokens.assert(not target.builtin(name), "attempt to reuse builtin '"..name.."' as an array")
tokens.assert(not constants[name], "attempt to redefine constant '"..name.."' as an array")
tokens.assert(not functions[name], "attempt to redefine function '"..name.."' as an array")
tokens.assert(not arrays[name], "attempt to redefine array '"..name.."' as a new array")
if keyword == "public" then
target.public(name)
elseif keyword == "extern" then
target.extern(name)
end
local isSized = tokens.symbol("[") and true
tokens.assert(isSized or keyword == 'extern' or keyword == 'intern', "array is '"..keyword.."' and does not have a defined size")
if not isSized then
return true
end
local size = nil
while not tokens.symbol("]") do
local value
value = tokens.number()
if value then
size = (size or 1) * tonumber(value)
else
value = tokens.name()
tokens.assert(value and constants[value] and constants[value].type=='number', "provided array cardinal for array '"..name.."' isn't a numerical constant")
size = (size or 1) * tonumber(constants[value].value)
tokens.assert(not tokens.eof(), "encountered EOF in array definition for '"..name.."'")
end
end
tokens.assert(size, "size is empty for array definition '"..name.."'")
arrays[name] = size
if keyword == "public" or keyword == "private" then
target.arrayDefinition(name, size)
end
return true
end
end
function constantDeclaration(tokens)
if tokens.keyword('constant') then
local name = tokens.name()
tokens.assert(name, "constant name is invalid")
tokens.assert(not target.builtin(name), "attempt to redefine builtin '"..name.."' as a constant")
tokens.assert(not constants[name], "attempt to redefine constant '"..name.."' as a new constant")
tokens.assert(not functions[name], "attempt to redefine function '"..name.."' as a constant")
tokens.assert(not arrays[name], "attempt to redefine array '"..name.."' as a constant")
tokens.assert(tokens.symbol('='), "'constant' is missing equals sign")
local value
value = tokens.number()
if value then constants[name] = {type = 'number', value = value} return true end
value = tokens.string()
if value then constants[name] = {type = 'string', value = value} return true end
value = tokens.name()
tokens.assert(value and constants[value], "provided value starting with '"..value.."' for constant '"..name.."' is invalid")
constants[name] = constants[value]
return true
end
end
function compilerDeclaration(tokens)
if tokens.keyword('include') then
local include = tokens.string()
if not include then
local name = tokens.name()
tokens.assert(name and constants[name] and constants[name].type == 'string', "'include' is missing library name")
include = constants[name].value
end
if not alreadyIncluded[include] then
local library = directIncludes[include] or findIndirectInclude(include)
tokens.assert(library, "'"..include.."' is not a valid library ")
alreadyIncluded[include] = library
compile(tokeniser(library))
end
return true
elseif tokens.keyword('link') then
local link = tokens.string()
if not link then
local name = tokens.name()
tokens.assert(name and constants[name] and constants[name].type == 'string', "'link' is missing library name")
end
return true
end
end
function returnStatement(tokens)
if tokens.keyword('return') then
tokens.assert(expression(tokens), "invalid expression in 'return'")
target.ret()
return true
end
end
function letStatement(tokens)
if tokens.keyword('let') then
local name = tokens.name()
tokens.assert(name, "'let' destination invalid")
tokens.assert(variables[name], "'let' destination variable '"..name.."' undefined")
tokens.assert(tokens.symbol('='), "'let' missing '=' after variable")
tokens.assert(expression(tokens), "invalid expression in 'let' expression")
target.store(variables[name])
return true
end
end
function implicitLet(tokens)
if tokens.keyword('extern', 'intern', 'public', 'private', 'constant') then
return
end
local name = tokens.name()
if name then
if tokens.symbol(':=') then
tokens.assert(variables[name], "implicit 'let' destination variable '"..name.."' undefined")
tokens.assert(expression(tokens), "invalid expression in implicicit 'let' assignment")
target.store(variables[name])
return true
else
if constants[name] then
if constants[name].type == 'number' then
target.numlit(constants[name].value)
elseif constants[name].type == 'string' then
target.stringlit(constants[name].value)
end
elseif arrays[name] then
target.arrayPointer(name)
else
tokens.assert(variables[name], "variable '"..name.."' does not exist")
target.load(variables[name])
end
return true
end
end
end
function ifStatement(tokens)
if tokens.keyword('if') then
tokens.assert(expression(tokens), "invalid expression in 'if' condition")
target.ifthen()
tokens.assert(statement(tokens), "invalid statement in 'if' body")
if tokens.keyword('else') then
target.ifelse()
tokens.assert(statement(tokens), "invalid statement in 'if' else clause")
end
target.ifend()
return true
end
end
function whileStatement(tokens)
if tokens.keyword('while') then
target.whileif()
tokens.assert(expression(tokens), "invalid expression in 'while' condition")
target.whiledo()
tokens.assert(statement(tokens), "invalid statement in 'while' body")
target.whileend()
return true
end
end
function allocateStatement(tokens)
if tokens.keyword('allocate') then
local name = tokens.name()
tokens.assert(name, "'allocate' invalid name")
tokens.assert(variables[name], "'allocate' destination variable '"..name.."' undefined")
tokens.assert(tokens.symbol("["), "'allocate' missing open square bracket")
tokens.assert(expression(tokens), "invalid expression in 'allocate' size")
tokens.assert(tokens.symbol("]"), "'allocate' missing closing bracket")
target.allocate(variables[name])
return true
end
end
function expressionStatement(tokens)
if tokens.keyword('then') then
tokens.assert(expression(tokens), "invalid expression in 'then' statement")
return true
end
end
function blockStatement(tokens)
if tokens.symbol('{') then
while not tokens.symbol('}') do
tokens.assert(statement(tokens), "invalid statement in block")
tokens.assert(not tokens.eof(), "encountered EOF in block")
end
return true
end
end
function functionCall(tokens)
if tokens.symbol('(') then
local name = tokens.name()
local func = target.builtin(name) or functions[name]
tokens.assert(func, "attempt to call undeclared function '"..name.."'")
callingDepth = callingDepth + 1
target.call_init()
while not tokens.symbol(')') do
tokens.assert(expression(tokens), "invalid expression in call of "..name)
tokens.assert(not tokens.eof(), "encountered EOF in function call for "..name)
target.pass()
end
local passed = target.call_fini(name)
tokens.assert(
passed == func.required or func.moreAllowed and passed > func.required,
"function '"..name.."' called with wrong number of arguments, should be "..(func.moreAllowed and "at least " or "")..func.required.." arguments"
)
callingDepth = callingDepth - 1
return true
end
end
function variableGet(tokens)
local name = tokens.name()
if name then
if constants[name] then
if constants[name].type == 'number' then
target.numlit(constants[name].value)
elseif constants[name].type == 'string' then
target.stringlit(constants[name].value)
end
elseif arrays[name] then
target.arrayPointer(name)
else
tokens.assert(variables[name], "variable '"..name.."' does not exist")
target.load(variables[name])
end
return true
end
end
function numericalLiteral(tokens)
local number = tokens.number()
if number then
target.numlit(number)
return true
end
end
function stringLiteral(tokens)
local string = tokens.string()
if string then
target.stringlit(string)
return true
end
end
function tokeniser(path)
local line = 1
local file = io.open(path, 'r')
local program = file:read('a')
file:close()
local function cerr(msg)
error("fennec compiler error at line "..line.." in file "..path..": "..msg)
end
local function assert(cond, msg)
if not cond then
cerr(msg)
end
end
local function removeJunk()
if program:match('^%s') then
local whitespace
whitespace, program = program:match('^(%s+)(.*)$')
_, whitespace = whitespace:gsub("\n", "")
line = line + whitespace
removeJunk()
elseif program:match('^//[^\n]*') then
program = program:match('^//[^\n]*(.*)$')
removeJunk()
end
end
local function pullSymbol(...)
removeJunk()
for _,option in ipairs(table.pack(...)) do
if #program >= #option and program:sub(1,#option) == option then
program = program:sub(#option+1)
return option
end
end
end
local function pullKeyword(...)
removeJunk()
if program:match('^[a-zA-Z_]') then
local keyword = program:match('^([a-zA-Z_][a-zA-Z0-9_]*)')
for _,option in ipairs(table.pack(...)) do
if keyword == option then
program = program:sub(#option+1)
return option
end
end
end
end
local escapes = {
['b'] = 8,
['t'] = 9,
['n'] = 10,
['r'] = 13,
['e'] = 27
}
local function pullName()
removeJunk()
if program:match('^[a-zA-Z_]') then
local name
name, program = program:match('^([a-zA-Z_][a-zA-Z0-9_]*)(.*)$')
return name
end
end
local function pullNumber()
removeJunk()
local token = nil
if program:match("^'\\.'") then
token = escapes[program:sub(3,3)] or string.byte(program,3)
program = program:sub(5)
elseif program:match("^'.'") then
token = string.byte(program, 2)
program = program:sub(4)
elseif program:match('^0x[0-9a-fA-F]') then
token, program = program:match("^0x([0-9a-fA-F]+)(.*)$")
token = tonumber(token, 16)
elseif program:match('^0b[01]') then
token, program = program:match("^0b([01]+)(.*)$")
token = tonumber(token, 2)
elseif program:match('^[0-9]') then
token, program = program:match("^([0-9]+)(.*)$")
token = tonumber(token)
elseif program:match('^%-0x[0-9a-fA-F]') then
token, program = program:match("^%-0x([0-9a-fA-F]+)(.*)$")
token = -tonumber(token, 16)
elseif program:match('^%-0b[01]') then
token, program = program:match("^%-0b([01]+)(.*)$")
token = -tonumber(token, 2)
elseif program:match('^%-[0-9]') then
token, program = program:match("^%-([0-9]+)(.*)$")
token = -tonumber(token)
end
return token
end
local function pullString()
removeJunk()
if program:match('^"') then
local i = 2
local string = ""
while program:sub(i,i) ~= '"' and i <= #program do
if program:sub(i,i) == '\\' then
i = i + 1
if i > #program then break end
if escapes[program:sub(i,i)] then
string = string .. string.char(escapes[program:sub(i,i)])
else
string = string .. program:sub(i,i)
end
else
string = string .. program:sub(i,i)
end
i = i + 1
end
if i > #program then
cerr("no end of string")
end
program = program:sub(i+1)
return string
end
end
return {
['error'] = cerr,
['assert'] = assert,
['symbol'] = pullSymbol,
['keyword'] = pullKeyword,
['name'] = pullName,
['number'] = pullNumber,
['string'] = pullString,
['eof'] = function() removeJunk() return #program == 0 end
}
end
main()