-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
1324 lines (1111 loc) · 36.4 KB
/
init.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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local ffi = require "ffi"
local math = require "math"
local string = require "string"
local table = require "table"
local bit = require "bit"
local core = require "luvit.core"
local debug = require "debug"
local traceback = debug.traceback
local function isAlpha(c)
if type(c) == "nil" then return false end
return (c >= 65 and c <= 90) or (c >= 97 and c <= 122)
end
local function isNumeric(c)
if type(c) == "nil" then return false end
return (c >= 48 and c <= 57)
end
local function isAlphaNumeric(c)
if type(c) == "nil" then return false end
return (c >= 48 and c <= 57) or (c >= 65 and c <= 90) or (c >= 97 and c <= 122)
end
local function isLineSeperator(c)
if type(c) == "nil" then return false end
return c == 10 or c == 13
end
local function isWhiteSpace(c)
if type(c) == "nil" then return false end
return c == 20 or c == 32 or c == 10 or c == 13 or c == 9 or c == 11 or c == 12 -- space, \n, \r, \t, \v, \f
end
local function ___cut_trace___(f, ...)
return f(...)
end
local Buffer = core.Object:extend()
function Buffer:initialize(string, startLine, fileInfo)
self.i = 0
self.line = startLine or 1
self.file = fileInfo
if ffi.istype ("const char *", string) or ffi.istype ("char *", string) or ffi.istype ("char []", string) then
-- assume \0 terminated string
local s = string
while s[0] ~= 0 do s = s + 1 end
self.length = tonumber(s - string)
self.buffer = string
elseif type(string) ~= "string" then
error (("No string was given for argument #1 (given: %s (%s))"):format(tostring(string), type(string)))
else
-- Lua string as argument, create new buffer
self.length = #string
self.buffer = ffi.new("char[?]", self.length + 1, string)
assert(ffi.sizeof(self.buffer) == self.length + 1, "Did not allocate proper buffer length!")
end
end
function Buffer:getCharPointer(i)
assert((i or self.i) <= self.length, "Getting out of bounds char pointer")
assert((i or self.i) >= 0, "Getting out of bounds char pointer")
return self.buffer + (i or self.i) - 1
end
function Buffer:skipChar()
self.i = self.i + 1
end
function Buffer:getChar()
self:skipChar()
if self.i > self.length or self.i < 0 then
return nil
end
local c = self.buffer[self.i-1]
if isLineSeperator(c) and (c ~= 13 --[[\r]] or self:peekChar() ~= 10 --[[\n]]) then
self.line = self.line + 1
end
return c
end
function Buffer:peekChar(peek)
peek = peek or 1
if self.i + peek > self.length or self.i < 0 then
return nil
end
return self.buffer[self.i + peek - 1]
end
function Buffer:undoChar()
self.i = self.i - 1
return self:peekChar(0)
end
local tokenType = {
comment = "comment",
string = "string",
word = "word",
alias = "alias",
macro = "macro",
endStatement= "endStatement",
operator = "operator",
seperator = "seperator",
endOfBuffer = "endOfBuffer",
call = "call",
number = "number"
}
local Token = core.Object:extend()
function Token:initialize(tokenType)
self.type = tokenType
end
local Lexer = core.Object:extend()
function Lexer:initialize()
end
function Lexer:error(message, wrongChar, buffer)
local location = buffer.i
local fileLocation = buffer.file or "<unkown file>"
local lineLocation = buffer.line or 0
local line = ""
while not isLineSeperator(buffer:undoChar()) do
if not buffer:peekChar() then
break
end
end
local offset = 0
local char = buffer:getChar()
while char and not isLineSeperator(char) do
line = line .. self:getChar(char, buffer)
char = buffer:getChar()
if buffer.i <= location then
offset = offset + 1
end
end
local arrow = ("."):rep(offset) .. "^"
if wrongChar then
local char
if type(wrongChar) ~= "string" then
wrongChar = tonumber(wrongChar)
if isLineSeperator(wrongChar) then
char = "<eol>"
elseif wrongChar < 0 then
char = "<utf8>"
else
char = string.char(tonumber(wrongChar) or 0)
end
end
message = ("%s:%i %s got: %s (%i)"):format(fileLocation, lineLocation, message, char, wrongChar)
end
message = message .. "\n"..line .. "\n" .. arrow
error (message)
end
function Lexer:isSeperator(c)
return (c == 59 or isLineSeperator(c)) -- ; \n or \r
end
function Lexer:skipWhiteSpace(buffer)
while isWhiteSpace(buffer:peekChar()) and not isLineSeperator(buffer:peekChar()) do
buffer:skipChar()
end
end
function Lexer:readUntilWhiteSpace(buffer)
local s = ""
while not isWhiteSpace(buffer:peekChar()) do
local c = self:getChar(nil, buffer)
if c then
s = s .. c
else
break
end
end
return s
end
function Lexer:readEscapeSequence(buffer)
local char = buffer:getChar()
local escaped = ({
[102] = 12, -- \f
[110] = 10, -- \n
[114] = 13, -- \r
})[char]
if not escaped then
self:error("Unkown escape sequence ^"..char, char, buffer)
end
return escaped
end
function Lexer:getChar(char, buffer)
if not char then
char = buffer:getChar()
end
if not char then
return nil
end
if char < 0 then --utf8 char
local len
local modus = bit.band(char, 224)
if modus == 192 then --2 byte
len = 2
else
modus = bit.band(char, 240)
if modus == 224 then -- 3 byte
len = 3
else
modus = bit.band(char, 248)
if modus == 240 then -- 4 byte
len = 4
end
end
end
if not len then
self:error("Invalid UTF8 char", char, buffer)
end
local c = buffer:getCharPointer()
for i = 0, len do
if c == 0 then
self:error("Premature buffer end while reading UTF8 char", char, buffer)
end
end
local v = ffi.string(buffer:getCharPointer(), len)
buffer.i = buffer.i + len - 1
return v
else
return string.char(char)
end
end
function Lexer:parseString(buffer)
local stringType = buffer:getChar()
if stringType == 34 then -- "
local token = Token:new(tokenType.string)
local value = {}
local char = buffer:getChar()
while char do
if char == 94 then -- ^
char = self:readEscapeSequence(buffer)
elseif char == stringType then
break
end
value[#value + 1] = self:getChar(char, buffer)
char = buffer:getChar()
end
token.value = table.concat(value, "")
if char ~= stringType then
self:error("Expected closing "..string.char(stringType), char, buffer)
end
char = buffer:peekChar()
if char and not isWhiteSpace(char) and not self:isSeperator(char) then
buffer:skipChar()
self:error("Expected whitespace after string.", char, buffer)
end
return token
else
local token = Token:new(tokenType.string)
local parts = {}
local stack = 1
local char = buffer:peekChar()
while char do
if char == 64 then -- @
local oldI = buffer.i
local oldLine = buffer.line
char = self:parseMacro(buffer)
if char.stack < stack then
buffer.i = oldI
buffer.line = oldLine
char = buffer:getChar()
end
else
char = buffer:getChar()
if char == 93 then -- ]
stack = stack - 1
if stack == 0 then
break
end
elseif char == 91 then -- [
stack = stack + 1
end
end
if type(char) ~= "table" then
parts[#parts+1] = self:getChar(char, buffer)
else
parts[#parts+1] = char
end
char = buffer:peekChar()
end
token.value = {}
local currentString = nil
for k, v in pairs(parts) do
if type(v) == "string" then
currentString = (currentString or "") .. v
else
if currentString then
table.insert(token.value, currentString)
currentString = nil
end
table.insert(token.value, v)
end
end
if currentString then
table.insert(token.value, currentString)
currentString = nil
end
return token
end
end
function Lexer:parseAlias(buffer)
local token = Token:new(tokenType.alias)
local char = buffer:getChar()
local word = self:parseWord(buffer)
token.value = word.value
return token
end
function Lexer:parseWord(buffer)
local token = Token:new(tokenType.word)
local char = buffer:peekChar()
token.value = {}
local stringValue = ""
while char and not isWhiteSpace(char) and not self:isSeperator(char) do
char = buffer:getChar()
--buffer:skipChar()
if char == 64 then -- @
table.insert(token.value, stringValue)
stringValue = nil
buffer:undoChar()
table.insert(token.value, self:parseMacro(buffer))
else
stringValue = (stringValue or "") .. self:getChar(char, buffer)
end
char = buffer:peekChar()
end
if stringValue then
table.insert(token.value, stringValue)
end
return token
end
function Lexer:isNumber(buffer)
local char = buffer:peekChar()
local next = buffer:peekChar(2)
if isNumeric(char) then
--check if the first 2 chars are valid number chars, bit hacky, don't use numbers at the start of a word...
return not next or isWhiteSpace(next) or isNumeric(next) or (
char == 48 --"0"
and (
next == 120 or next == 88 --x or X
or next == 100 or next == 68 -- d or D
or next == 98 or next == 66 -- b or B
) and isNumeric(buffer:peekChar(3))
) or (
next == 46 -- .
and isNumeric(buffer:peekChar(3))
)
elseif char == 45 or char == 46 then -- - or .
return isNumeric(next)
else
return false
end
end
function Lexer:parseNumber(buffer)
local token = Token:new(tokenType.number)
local char = buffer:getChar()
token.value = 0
local negative = false
if char == 45 or char == 43 then -- - or +
negative = char == 45
char = buffer:getChar()
end
local base = 10
local numberType = "d"
if char == 48 and isAlpha(buffer:peekChar()) then
numberType = self:getChar(nil, buffer)
char = buffer:getChar()
end
if numberType == "d" then
elseif numberType == "x" then
base = 16
elseif numberType == "b" then
base = 2
else
self:error("Invalid base", string.byte(numberType), buffer)
end
local finalShift = 0
local noPeek = false
while char and not isWhiteSpace(char) do
if noPeek then
buffer:skipChar()
end
local value = ({
[48] = 0,
[49] = 1,
[50] = 2,
[51] = 3,
[52] = 4,
[53] = 5,
[54] = 6,
[55] = 7,
[56] = 8,
[57] = 9,
[97] = 10, --a
[65] = 10, --A
[98] = 11, --b
[66] = 11, --B
[99] = 12, --c
[67] = 12, --C
[100] = 13,--d
[68] = 13,--D
[101] = 14,--e
[69] = 14,--E
[102] = 15, --f
[70] = 15 --F
})[char]
if char == 46 then -- .
finalShift = 1
elseif type(value) == "nil" then
self:error("Invalid number", char, buffer)
end
if finalShift > 0 then
finalShift = finalShift + 1
end
if value then
token.value = token.value * base + value
end
char = buffer:peekChar()
noPeek = true
end
if not noPeek then
self:error("Invalid number (premature end)", char, buffer)
end
if negative then
token.value = -token.value
end
if finalShift > 0 then
token.value = token.value / (math.pow(base, finalShift-2))
end
return token
end
function Lexer:parseMacro(buffer)
local char = buffer:peekChar()
local token = Token:new(tokenType.macro)
token.stack = 0
while char == 64 do --@
token.stack = token.stack + 1
buffer:skipChar()
char = buffer:peekChar()
end
if token.stack == 0 then
self:error("Expecting start of macro", char, buffer)
end
if char == 91 then -- [
token.value = self:parseString(buffer)
elseif char == 40 then -- (
token.value = self:parseCall(buffer)
else
token.value = ""
char = buffer:peekChar()
while char and isAlphaNumeric(char) do
char = buffer:getChar()
token.value = token.value .. self:getChar(char, buffer)
char = buffer:peekChar()
end
end
return token
end
function Lexer:parseCall(buffer)
--TODO: macros!
local open = buffer:getChar()
assert(open == 40) --(
local token = Token:new(tokenType.call)
token.value = ""
local stack = 1
local char = buffer:getChar()
while char do
if char == 40 then -- (
stack = stack + 1
elseif char == 41 then -- )
stack = stack - 1
if stack == 0 then
break
end
end
token.value = token.value .. self:getChar(char, buffer)
char = buffer:getChar()
end
if char ~= 41 then -- )
self:error("Expected closing )", char, buffer)
end
return token
end
Lexer.operators = {
["61"] = "=",
}
function Lexer:isOperator(buffer)
local i = 1
local char = buffer:peekChar(i)
local op = {}
while char and not isWhiteSpace(char) do
op[#op + 1] = tostring(char)
i = i + 1
char = buffer:peekChar(i)
end
op = table.concat(op, "-")
return self.operators[op]
end
function Lexer:parseOperator(buffer)
local op = self:readUntilWhiteSpace(buffer)
if op ~= "=" then
self:error("Unkown operator: ", op, buffer)
end
local token = Token:new(tokenType.operator)
token.value = "="
return token
end
function Lexer:parseSeperator(buffer)
local seperator = buffer:getChar()
assert(self:isSeperator(seperator))
return Token:new(tokenType.seperator)
end
function Lexer:parseComment(buffer)
local char = buffer:getChar()
local token = Token:new(tokenType.comment)
token.value = ""
if --[[char == "#" or]] char == 47 and buffer:peekChar() == 47 then -- /
token.inline = true
if char == 47 then
buffer:getChar() -- skip the second slash
end
char = buffer:getChar()
while char and not isLineSeperator(char) do
token.value = token.value .. self:getChar(char, buffer)
char = buffer:getChar()
end
elseif char == 47 and buffer:peekChar() == 42 then
buffer:skipChar() -- skip the *
char = buffer:getChar()
while char and not (char == 42 and buffer:peekChar() == 47) do
token.value = token.value .. char
char = buffer:getChar()
end
buffer:skipChar() -- skip the ending /
else
self:error ("Malformatted comment", char, buffer)
end
return token
end
function Lexer:parseEndOfBuffer()
return Token:new(tokenType.endOfBuffer)
end
local _valid_var_chars = {
[33] = true, -- !
[38] = true, -- &
[42] = true, -- *
[43] = true, -- +
[45] = true, -- -
[46] = true, -- .
[47] = true, -- /
[60] = true, -- <
[61] = true, -- =
[62] = true, -- >
[63] = true, -- ?
[95] = true, -- _
[124] = true, -- |
}
function Lexer:validVariableChar(buffer)
local char = buffer:peekChar()
return isAlpha(char) or isNumeric(char) or _valid_var_chars[char]
end
function Lexer:lexizeSingleToken(buffer)
self:skipWhiteSpace(buffer)
local char = buffer:peekChar()
if char == 91 or char == 34 then -- [ or "
return self:parseString(buffer)
elseif char == 36 then -- $
return self:parseAlias(buffer)
elseif char == 64 then -- @
return self:parseMacro(buffer)
elseif char == 40 then -- (
return self:parseCall(buffer)
elseif char == 47 and (buffer:peekChar(2) == 47 or buffer:peekChar(2) == 42) then -- / and (/ or *)
return self:parseComment(buffer)
elseif self:isSeperator(char) then
return self:parseSeperator(buffer)
elseif self:isOperator(buffer) then
return self:parseOperator(buffer)
elseif self:isNumber(buffer) then
return self:parseNumber(buffer)
elseif self:validVariableChar(buffer) then
return self:parseWord(buffer)
elseif char then
return self:error("Unexpected character", char, buffer)
else
return self:parseEndOfBuffer()
end
end
function Lexer:lexize(buffer)
local tokens = {}
while buffer:peekChar() do
local line = buffer.line
local token = self:lexizeSingleToken(buffer)
token.line = line
token.file = buffer.file
table.insert(tokens, token)
end
return tokens
end
local LexerStack = core.Object:extend()
function LexerStack:initialize(lex)
self.lex = lex
self.i = 0
end
function LexerStack:next()
self.i = self.i + 1
return self.lex[self.i]
end
function LexerStack:peek(amount)
amount = amount or 1
return self.lex[self.i + amount]
end
function LexerStack:backPeek(amount)
return self:peek(-amount)
end
local Statement = core.Object:extend()
function Statement:initialize()
self.type = nil
self.arguments = {}
end
local statementType = {
assignment = "assignment",
call = "call",
lookup = "lookup"
}
local Parser = core.Object:extend()
function Parser:initialize()
end
function Parser:skipComments(lex)
while lex:peek() do
local token = lex:peek()
if token.type == tokenType.comment or token.type == tokenType.seperator or token.type == tokenType.endOfBuffer then
lex:next()
else
break
end
end
end
function Parser:parseArguments(lex, statement)
while lex:peek() do
local token = lex:peek()
if token and token.type ~= tokenType.seperator and token.type ~= tokenType.endOfBuffer and (token.type ~= tokenType.comment and not token.inline) then
if token.type ~= tokenType.comment then
table.insert(statement.arguments, lex:next())
else
lex:next() -- skip non inline comments
end
else
break
end
end
end
function Parser:parseOne(lex)
self:skipComments(lex)
local token = lex:next()
if not token then
return false
end
local statement = Statement:new()
statement.type = statementType.call
statement.line = token.line
statement.file = token.file
table.insert(statement.arguments, token)
-- Check if argument 1 would be valid in a call
local nextIsCall = token.type == tokenType.number or token.type == tokenType.word or token.type == tokenType.string or token.type == tokenType.operator or token.type == tokenType.macro or token.type == tokenType.call
if token.type == tokenType.alias then
local i = 1
repeat
local nextToken = lex:peek(i)
i = i + 1
if not nextToken or nextToken.type == tokenType.seperator or nextToken.type == tokenType.endOfBuffer then
nextIsCall = false
break
elseif nextToken.type == tokenType.comment then
--skip
else
nextIsCall = true
break
end
until false
end
if nextIsCall then -- (= ....)
if token.type == tokenType.operator then
token.type = tokenType.word --Convert
end
if lex:peek() and lex:peek().type == tokenType.operator then
local operator = lex:next()
if operator.value == "=" then
statement.type = statementType.assignment
end
end
self:parseArguments(lex, statement)
elseif token.type == tokenType.alias then
statement.type = statementType.lookup
else
p(lex)
error("Unexpected type: "..token.type)
end
return statement
end
function Parser:parse(lex)
local statements = {}
while lex:peek() do
local statement = self:parseOne(lex)
if statement then
table.insert(statements, statement)
end
end
return statements
end
local Trace = core.Object:extend()
function Trace:initialize()
self.i = 1
self.trace = {}
end
function Trace:add(obj)
self.trace[self.i] = obj
self.i = self.i + 1
end
function Trace:pop()
self.i = self.i - 1
local ret = self.trace[self.i]
self.trace[self.i] = nil
return ret
end
function Trace:copy()
local new = Trace:new()
new.i = self.i
for i = 1, self.i do
new.trace[i] = self.trace[i]
end
return new
end
local metaScope = {}
function metaScope:__index(key)
if key:sub(1, 1) == "#" then return end
local v = rawget(self, key)
local parent = rawget(self, "#parent")
if type(v) == "nil" and parent then
return parent[key]
else
return v
end
end
function metaScope:__newindex(key, value)
if key:sub(1, 1) == "#" then return end
local haveLocal = rawget(self, key)
local isNil = type(value) == "nil"
local parent = rawget(self, "#parent")
local preferlocal = rawget(self, "preferlocal")
if haveLocal or preferlocal then
if isNil then
rawset(self, key, "") -- Keep locals
else
rawset(self, key, value)
end
elseif parent then
parent[key] = value
else
rawset(self, key, value)
end
end
local function makeScope(parent, preferlocal)
return setmetatable({["#parent"] = parent, ["#preferlocal"] = preferlocal}, metaScope)
end
local Environment = core.Object:extend()
function Environment:initialize(scope)
self.lexer = nil
self.parser = nil
self.globalScope = scope or makeScope(nil)
end
function Environment:createTokenLocationTrace(token)
return tostring(token.file)..":"..tostring(token.line)
end
function Environment:createTraceMessage(msg)
if type(msg) == "string" then
return msg
elseif type(msg) == "table" then
if msg.argument then
return self:createTokenLocationTrace(msg.argument).." in argument preprocessing"
elseif msg.macro then
return self:createTokenLocationTrace(msg.macro).." in macro preprocessing"
elseif msg.statement then
if msg.type == "assignment" then
return self:createTokenLocationTrace(msg.statement).." in assginment "..tostring(msg.name)
elseif msg.type == "run.args" then
return self:createTokenLocationTrace(msg.statement).." in argument calling."
else
return self:createTokenLocationTrace(msg.statement).." in call to function "..tostring(msg.name)
end
elseif msg.callbackFunction then
return self:createTokenLocationTrace(msg).. " in callback function"
elseif msg.globalScope then
return self:createTokenLocationTrace(msg).. " in global scope"
else
local str = ""
for k, v in pairs(msg) do
str = str .. tostring(k) .. "=" .. tostring(v).. ", "
end
return str
end
else
return "Weird trace msg of type "..type(msg)
end
end
function Environment:throwError(message, trace)
local err = setmetatable({}, errorMeta)
err.message = message
local back = {}
local traceMessage = trace:pop()
while traceMessage do
local v = self:createTraceMessage(traceMessage)
if type(v) == "table" then
shift = v
else
table.insert(back, v)
end
traceMessage = trace:pop()
end
err.trace = back
err.message = err.message .."\n\t"..table.concat(back, "\n\t")
if self.debug == true then
err.message = err.message .. "\n\nDebug info: \n"..traceback()
end
error(err)
end
function Environment:makeBuffer(string, startLine, fileInfo)
return Buffer:new(string, startLine, fileInfo)
end
function Environment:tokenize(buf)
return self.lexer:lexize(buf)
end
function Environment:makeTokenStack(tokens)
return LexerStack:new(tokens)
end
function Environment:parse(stack)
return self.parser:parse(stack)
end
function Environment:executeCallback(callback, scope, trace, meta)
if type(callback) == "number" then
return callback --TODO: is this the place where we should put this?
elseif type(callback) == "string" and type(meta) == "number" then
local metaArg = rawget(scope, "#args")[meta]
if metaArg then
callback = {
value = callback,
file = metaArg.file,
line = metaArg.line
}