-
Notifications
You must be signed in to change notification settings - Fork 1
/
luayan.lua
1206 lines (1183 loc) · 33.4 KB
/
luayan.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
--[=[
LuaYan loader v0.2
The loader was written in lua 5.1 and was also tested in lua 5.3
It doesn't require any libraries.
Operators modifications and additions can be easily made
The loader adds the floor division operator "//"
as well as assignment operators
"+=", "-=", "^=", *=", "/=", "//=", "%=", "..=", "and=", "or="
This doesn't include bitwise operators.
]=]
local matchstr, sub, gsub, gmatch, char, repstr = string.match, string.sub, string.gsub, string.gmatch, string.char, string.rep
local max = math.max
local unpack = table.unpack
--[['#' OPERATOR / UNPACK FIX]]--
local function len(tab)
local i = 0
for k in next, tab do
i = type(k) == "number" and k or i
end
return i
end
do
local upk = unpack
unpack = function(tab, i, j)
return upk(tab, i or 1, j or len(tab))
end
end
--[[MANAGING ENVIRONMENTS]]--
local globals
local function getDefinedObject(env)
local defined = {}
for i in next, env do
defined[i] = true
end
for i in next, env["*nil"] do
defined[i] = true
end
return defined
end
local function newChildEnv(env, defined)
return setmetatable(
{
["*parentDefined"] = env~=globals and setmetatable(defined or getDefinedObject(env), {__index=env["*parentDefined"]}) or {},
["*nil"] = {},
["*parent"] = env
},
{
__index = function(tab,index)
if not rawget(tab,"*nil")[index] then
if rawget(tab,"*parentDefined")[index] then
return env[index]
else
return globals[index]
end
end
end,
__newindex = function(tab,key,value)
rawset(tab,key,value)
if value == nil then
tab["*nil"][key] = true
else
tab["*nil"][key] = nil
end
end
}
)
end
local function setDeclaredVariable(tab,index,value,scope)
if tab == scope then
while tab ~= globals do
if rawget(tab, index) ~= nil or tab["*nil"][index] then
if value == nil then
tab["*nil"][index] = true
end
break
else
tab = tab["*parent"]
end
end
end
tab[index] = value
end
local returnObject, breakObject = {}, {}
--[[OPERATORS]]--
local evaluate
local operators = {
["or"] = function(a,b,...) return evaluate(a,...) or evaluate(b,...) end,
["and"] = function(a,b,...) return evaluate(a,...) and evaluate(b,...) end,
["<"] = function(a,b,...) return evaluate(a,...) < evaluate(b,...) end,
[">"] = function(a,b,...) return evaluate(a,...) > evaluate(b,...) end,
["<="] = function(a,b,...) return evaluate(a,...) <= evaluate(b,...) end,
[">="] = function(a,b,...) return evaluate(a,...) >= evaluate(b,...) end,
["~="] = function(a,b,...) return evaluate(a,...) ~= evaluate(b,...) end,
["=="] = function(a,b,...) return evaluate(a,...) == evaluate(b,...) end,
[".."] = function(a,b,...) return evaluate(a,...) .. evaluate(b,...) end,
["+"] = function(a,b,...) return evaluate(a,...) + evaluate(b,...) end,
["-"] = function(a,b,...) return evaluate(a,...) - evaluate(b,...) end,
["*"] = function(a,b,...) return evaluate(a,...) * evaluate(b,...) end,
["/"] = function(a,b,...) return evaluate(a,...) / evaluate(b,...) end,
["//"] = function(a,b,...) return math.floor(evaluate(a,...)/evaluate(b,...)) end,
["%"] = function(a,b,...) return evaluate(a,...) % evaluate(b,...) end,
["^"] = function(a,b,...) return evaluate(a,...) ^ evaluate(b,...) end
}
local orderPatterns = {
"(.*%d)(or)(%d.*)",
"(.*%d)(and)(%d.*)",
"(.*%d)([<>~=]=?)(%d.*)",
"(.*%d)(%.%.)(%d.*)",
"(.*%d)([%+%-])(%d.*)",
"(.*%d)([%*/%%]/?)(%d.*)",
"(.*%d)(%^)(%d.*)"
}
local fastPatterns = {"^%s*([%+%-%*/%%%^=~><%.][=/%.]?)()","^%s*(and)()[^_%w]","^%s*(or)()[^_%w]"}
local unaryOperators = {
["#"] = function(a) return #a end,
["-"] = function(a) return -a end,
["not"] = function(a) return not a end
}
local unaryPattern = "[#%-]"
local opPrecedingUnaries = {"%^"} --List of operators that has superior precedence than unary operators.
local assignOperators = {
["="] = function(scope,var,value) return value end,
["+="] = function(scope,var,value) return scope[var]+value end,
["-="] = function(scope,var,value) return scope[var]-value end,
["*="] = function(scope,var,value) return scope[var]*value end,
["/="] = function(scope,var,value) return scope[var]/value end,
["^="] = function(scope,var,value) return scope[var]^value end,
["%="] = function(scope,var,value) return scope[var]%value end,
["..="] = function(scope,var,value) return scope[var]..value end,
["or="] = function(scope,var,value) return scope[var] or value end,
["and="] = function(scope,var,value) return scope[var] and value end,
["//="] = function(scope,var,value) return math.floor(scope[var]/value) end
}
--[[USEFUL FUNCTIONS]]--
local fullScriptLen, currentPosition, currentScript
local function unescapeString(str)
str = gsub(str,"\\(.)(%d?%d?)",function(a, b)
if a == "a" then return "\a"..b
elseif a == "b" then return "\b"..b
elseif a == "f" then return "\f"..b
elseif a == "n" then return "\n"..b
elseif a == "r" then return "\r"..b
elseif a == "t" then return "\t"..b
elseif a == "v" then return "\v"..b
elseif a == "\\" then return "\\"..b
elseif a == "\"" then return "\""..b
elseif a == "'" then return "'"..b
elseif a == "[" then return "["..b
elseif a == "]" then return "]"..b
elseif matchstr(a,"^%d$") then return char(tonumber(a..b))
else error("invalid escape sequence") end
end)
return str
end
local function unfinishedStrings(script)
while true do
local m = matchstr(script,"^[^\"']-%[(=-)%[") or matchstr(script,"^[^']-(\")") or matchstr(script,"'")
if m then
local suc
if m ~= '"' and m ~= "'" then
suc = matchstr(script,"%["..m.."%[.-%]"..m.."%]()")
else
local s; s, suc = matchstr(script,m.."(.-)"..m.."()")
if suc then
local c = 0
repeat
s = unescapeString(s.."0")
if sub(s,-1) == char(0) then
c = c + 1
s, suc = matchstr(script,m..'(.-'..repstr(m..'.-',c)..')'..m..'()')
else
break
end
until false
end
end
if suc then
script = sub(script,suc)
else
return m
end
else
return false
end
end
end
local function keepLines(a)
return a:gsub("[^\n]+", "")
end
local function removeComments(script, keep)
local n = 1
while true do
local scr = sub(script,n)
local z, block = matchstr(scr,"()%-%-(%[?=*%[?)")
if z then
local m = unfinishedStrings(sub(scr,1,z-1))
if not m then
local b = matchstr(block,"%[(=-)%[")
if b then
script = sub(script,1,n-1)..gsub(scr,"%-%-%["..b.."%[.-%]"..b.."%]", keep and keepLines or "", 1)
else
script = sub(script,1,n-1)..gsub(scr,"%-%-.-\r?\n", "\n", 1)
end
else
local is, d, s = sub(scr,z)
if m == "'" or m == '"' then
s, d = matchstr(is,"(.-)"..m.."()")
local c = 0
repeat
s = unescapeString(s.."0")
if sub(s,-1) == char(0) then
c = c + 1
s, d = matchstr(is,'(.-'..repstr(m..'.-',c)..')'..m..'()')
else
break
end
until false
else
d = matchstr(is,"%]"..m.."%]()")
end
n = n + z + d - 2
end
else
break
end
end
return script
end
local function returnTable(iter, ...)
return function(...)
local a = {iter(...)}
if a[1] then
return a[1], a
end
end, ...
end
local function matchWord(script)
local word, n = matchstr(script,"^%s*([_%a][_%w]*)()")
if word then
script = sub(script,n)
end
return script, word
end
local function checkReturn(ret, scopeType, scr, ln)
if ret[1] == returnObject then
if scopeType == "function" then
fullScriptLen, currentPosition = scr, ln
return unpack(ret,2)
elseif scopeType == "main" then
error("'return' used outside function")
else
fullScriptLen, currentPosition = scr, ln
return unpack(ret)
end
end
if ret[1] == breakObject then
if scopeType == "function" or scopeType == "main" then
error("'break' used outside loop")
else
fullScriptLen, currentPosition = scr, ln
return breakObject
end
end
end
--[[MAIN FUNCTIONS]]
local readExpression, readScript
local function readTable(tblstr, scope)
local c = 0
local a = tblstr:match("^%s*{value%[1%]%(unpack%(args,1,c%)%)")
local n, brk, e, _ = matchstr(tblstr,"^%s*{()%s*(}?)()")
local value = {}
while brk == "" do
if _ == "" then
error("error")
end
tblstr = sub(tblstr,n)
n = matchstr(tblstr,"^%s*%[()")
local index, val
if n then
tblstr, index = readExpression(sub(tblstr,n),scope)
n = matchstr(tblstr,"^%s*%]%s*=%s*()")
else
index, n = matchstr(tblstr,"^%s*([_%a][_%w]*)%s*=%s*()")
if not n then
c = c + 1
index = c
n = 1
end
end
local match = {readExpression(sub(tblstr,n),scope)}
tblstr = match[1]
if scope then
value[index] = match[2]
local k = c
if n == 1 then
for i = 3, len(match) do
k = k + 1
value[k] = match[i]
end
end
for i = k+1, len(value) do
value[i] = nil
end
end
_, n, brk, e = matchstr(tblstr,"^%s*(,?)()%s*(}?)()")
end
return sub(tblstr,e), value
end
local function matchScopeBody(script, addLen)
local len = readScript(script,nil,"",nil,true)
local body = sub(script,1,len)
script = sub(script,len+(addLen or 3)+1)
return script, body
end
function evaluate(value, scope, vars, inevaluated, counter)
local n
for i = 1, #orderPatterns do
value, n = gsub(value,orderPatterns[i], function(a,op,b) counter = counter + 1 vars[counter] = operators[op](a,b,scope,vars,inevaluated,counter) return tostring(counter) end)
if n ~= 0 then
break
end
end
local i = tonumber(matchstr(value,"%d+"))
if inevaluated[i] then
vars[i] = select(2,readExpression(inevaluated[i], scope,nil,nil,true))
inevaluated[i] = nil
end
return vars[i]
end
function readExpression(script, scope, isUnary, processCall, skipCurrent)
if not skipCurrent then
currentScript = script
end
local value, mustCall, callIsLast, isIndexable
if processCall then
value = processCall
isIndexable = true
else
value = {}
local word; script, word = matchWord(script)
if word then
if word == "function" then
local varNames, n, body = matchstr(script,"^%s*%((.-)%)()")
if not varNames then
error("error")
end
local initialScript = sub(script,n)
script, body = matchScopeBody(initialScript)
if scope then
local defined = getDefinedObject(scope)
local ln = currentPosition+fullScriptLen-#initialScript
value[1] = function(...)
local env = newChildEnv(scope,defined)
local params, c, isLast = {...}, 0, false
for arg in gmatch(varNames,"%s*([^,]+)%s*") do
if isLast then
error("error")
end
c = c + 1
if arg == "..." then
env["..."] = {unpack(params,c,select("#",...))}
isLast = true
else
env[arg] = params[c]
end
end
return readScript(body, env, "function", ln)
end
end
elseif unaryOperators[word] then
script, value[1] = readExpression(script, scope, true)
if scope then
value[1] = unaryOperators[word](value[1])
end
elseif word == "true" then
value[1] = true
elseif word == "false" then
value[1] = false
elseif word == "nil" then
value[1] = nil
else
if scope then
value[1] = scope[word]
end
isIndexable = true
end
else
local n; value[1], n = matchstr(script,"^%s*\"(.-)\"()")
if n then
local c = 0
repeat
value[1] = unescapeString(value[1].."0")
if sub(value[1],-1) == char(0) then
c = c + 1
value[1], n = matchstr(script,'^%s*"(.-'..repstr('".-',c)..')"()')
else
break
end
until false
value[1] = sub(value[1],1,-2)
script = sub(script,n)
else
value[1], n = matchstr(script,"^%s*'(.-)'()")
if n then
local c = 0
repeat
value[1] = unescapeString(value[1].."0")
if sub(value[1],-1) == char(0) then
c = c + 1
value[1], n = matchstr(script,'^%s*\'(.-'..repstr('\'.-',c)..')\'()')
else
break
end
until false
value[1] = sub(value[1],1,-2)
script = sub(script,n)
else
local pat; pat, value[1], n = matchstr(script,'^%s*%[(=-)%[(.-)%]%1%]()')
if n then
value[1], n = matchstr(script,'^%s*%['..pat..'%[(.-)%]'..pat..'%]()')
script = sub(script,n)
else
local p, z, n = matchstr(script,"^%s*(%.?)(%d+)()")
if z then
script = sub(script,n)
if p == "" then
local d, n = matchstr(script,"^%s*%.(%d+)()")
if d then
script = sub(script,n)
z = z .. "." .. d
else
d, n = matchstr(script,"^%s*x(%w+)()")
if d then
if z ~= "0" then
error("error")
end
script = sub(script,n)
z = z .. "x" .. d
end
end
value[1] = tonumber(z)
else
value[1] = tonumber("0."..z)
end
else
n = matchstr(script,"^%s*%(()")
if n then
script, value[1] = readExpression(sub(script,n),scope)
n = matchstr(script,"^%s*%)()")
script = sub(script,n)
isIndexable = true
elseif matchstr(script,"^%s*{") then
script, value[1] = readTable(script, scope)
else
n = matchstr(script,"^%s*%.%.%.()")
if n then
script = sub(script,n)
if scope then
if not scope["..."] then
error("error")
end
value = scope["..."]
end
else
local op, n = matchstr(script,"^%s*("..unaryPattern..")()")
if n then
if not unaryOperators[op] then
error("error")
end
script, value[1] = readExpression(sub(script,n),scope,true)
if scope then
value[1] = unaryOperators[op](value[1])
end
else
error("unexpected character")
end
end
end
end
end
end
end
end
end
while isIndexable do
callIsLast = true
local arg, n
if matchstr(script,"^%s*{") then
script, arg = readTable(script, scope)
if scope then
if mustCall then
value = { value[1](mustCall, arg) }
else
value = { value[1](arg) }
end
end
mustCall = false
else
arg, n = matchstr(script,"^%s*\"(.-)\"()")
if arg then
local c = 0
repeat
arg = unescapeString(arg.."0")
if sub(arg,-1) == char(0) then
c = c + 1
arg, n = matchstr(script,'^%s*"(.-'..repstr('".-',c)..')"()')
else
break
end
until false
arg = sub(arg,1,-2)
script = sub(script,n)
if scope then
if mustCall then
value = {value[1](mustCall, arg)}
else
value = {value[1](arg)}
end
end
mustCall = false
else
arg, n = matchstr(script,"^%s*'(.-)'()")
if arg then
local c = 0
repeat
arg = unescapeString(arg.."0")
if sub(arg,-1) == char(0) then
c = c + 1
arg, n = matchstr(script,'^%s*\'(.-'..repstr('\'.-',c)..')\'()')
else
break
end
until false
arg = sub(arg,1,-2)
script = sub(script,n)
if scope then
if mustCall then
value = {value[1](mustCall, arg)}
else
value = {value[1](arg)}
end
end
mustCall = false
else
local pat; pat, arg, n = matchstr(script,'^%s*%[(=-)%[(.-)%]%1%]()')
if arg then
script = sub(script,n)
if scope then
if mustCall then
value = {value[1](mustCall, arg)}
else
value = {value[1](arg)}
end
end
mustCall = false
elseif matchstr(script,"^%s*%(") then
local n = matchstr(script,"^%s*%(%s*%)()")
if n then
script = sub(script,n)
if scope then
if mustCall then
value = {value[1](mustCall)}
else
value = {value[1]()}
end
end
else
local args, k, c = {}, 0
if mustCall then
k = 1
args[k] = mustCall
end
n = matchstr(script,"^%s*%(()")
while n do
local match = {readExpression(sub(script,n),scope)}
script = match[1]
k = k + 1
c = k
for i = 2, len(match) do
c = k + i - 2
args[c] = match[i]
end
n = matchstr(script,"^%s*,()")
end
if scope then
value = {value[1](unpack(args,1,c))}
end
n = matchstr(script,"^%s*%)()")
script = sub(script,n)
end
mustCall = false
else
if mustCall then
error("error")
end
n = matchstr(script,"^%s*%[()")
if n then
local index; script, index = readExpression(sub(script,n),scope)
n = matchstr(script,"^%s*%]()")
script = sub(script,n)
if scope then
value[1] = value[1][index]
end
callIsLast = false
else
local name; name, n = matchstr(script,"^%s*%.%s*([_%a][_%w]*)()")
if n then
script = sub(script,n)
if scope then
value[1] = value[1][name]
end
callIsLast = false
else
name, n = matchstr(script,"^%s*:%s*([_%a][_%w]*)()")
if n then
script = sub(script,n)
if scope then
mustCall = value[1]
value[1] = value[1][name]
else
mustCall = true
end
callIsLast = false
else
break
end
end
end
end
end
end
end
end
if processCall then
if not callIsLast then
error("error")
end
return script
end
if not isUnary then
local vars, c, loop, valProcess, inevaluated = {value[1]}, 1, true, "1", {}
while loop do
loop = false
for i = 1, #fastPatterns do
local op, n = matchstr(script,fastPatterns[i])
if op then
if not operators[op] then
error("error")
end
loop = true
local iscript, exp = sub(script,n)
script = readExpression(iscript, nil, true)
exp = sub(iscript,1,#iscript-#script)
c = c + 1
inevaluated[c] = exp
if scope then
valProcess = valProcess .. op .. c
end
break
end
end
end
if scope and valProcess ~= "1" then
value = {evaluate(valProcess, scope, vars, inevaluated, c)}
end
else
for i = 1, #opPrecedingUnaries do
local op, n = matchstr(script,"^%s*("..opPrecedingUnaries[i]..")()")
if n then
local operant; script, operant = readExpression(sub(script,n),scope,true)
if scope then
value = {operators[op](value[1],operant)}
end
break
end
end
end
return script, unpack(value)
end
local function readLine(script, scope)
currentScript = script
local n = matchstr(script, "^%s*;()")
if n then
script = sub(script, n)
if matchstr(script, "^%s*$") then
return ""
end
end
local word; script, word = matchWord(script)
if word then
if word == "local" then
local varName; script, varName = matchWord(script)
if varName then
if varName == "function" then
local fName, args, n, fBody = matchstr(script,"^%s*([_%a][_%w]*)%s*%((.-)%)()")
local initialScript = sub(script,n)
script, fBody = matchScopeBody(initialScript)
if scope then
local ln = currentPosition+fullScriptLen-#initialScript
local defined = getDefinedObject(scope)
scope[fName] = function(...)
local env = newChildEnv(scope,defined)
local params = {...}
local c = 0
local isLast = false
for arg in gmatch(args,"%s*([^,]+)%s*") do
if isLast then
error("error")
end
c = c + 1
if arg == "..." then
env["..."] = {unpack(params,c,select('#',...))}
isLast = true
else
env[arg] = params[c]
end
end
return readScript(fBody, env, "function", ln)
end
end
else
local varNames = {}
local c = 1
varNames[c] = varName
local n = matchstr(script,"^%s*,()")
while n do
c = c + 1
script, varName = matchWord(sub(script,n))
varNames[c] = varName
n = matchstr(script,"^%s*,()")
end
local n = matchstr(script,"^%s*=()")
c = 0
local varValues, k = {}, 0
while n do
local match = {readExpression(sub(script,n),scope)}
script = match[1]
if scope then
c = c + 1
k = c
for i = 2, len(match) do
k = c + i - 2
varValues[k] = match[i]
end
end
n = matchstr(script,"^%s*,()")
end
if scope then
for i = 1, #varNames do
scope[varNames[i]] = i <= k and varValues[i] or nil
end
end
end
else
error("error")
end
elseif word == "function" then
local fName, args, n, fBody = matchstr(script,"^%s*([_%a][_%.:%s%w]*)%s*%(%s*(.-)%)()")
local initialScript = sub(script,n)
script, fBody = matchScopeBody(initialScript)
if scope then
local ln = currentPosition+fullScriptLen-#initialScript
local tab = scope
local last
for n, l in gmatch(fName,"([^%.%s]+)%s*%.%s*([^%.%s]+)") do
tab = tab[n]
last = l
end
if not last then
last = matchstr(fName,"[^:%s]+")
end
local method = matchstr(fName,"^%s*.-%s*:%s*(.*)%s*$")
if method then
tab = tab[last]
last = method
end
local defined = getDefinedObject(scope)
setDeclaredVariable(tab,last,function(...)
local env = newChildEnv(scope,defined)
if method then
args = args == "" and "self" or "self,"..args
end
local params = {...}
local c = 0
local isLast = false
for arg in gmatch(args,"%s*([^,]+)%s*") do
if isLast then
error("error")
end
c = c + 1
if arg == "..." then
env["..."] = {unpack(params,c,select('#',...))}
isLast = true
else
env[arg] = params[c]
end
end
return readScript(fBody, env, "function", ln)
end, scope)
end
elseif word == "while" then
local iscript = script
script = readExpression(script, nil)
local statement = sub(iscript,1,#iscript-#script)
local n = matchstr(script,"^%s*do()")
if n then
local initialScript = sub(script,n)
local body; script, body = matchScopeBody(initialScript)
if scope then
local ln = currentPosition+fullScriptLen-#initialScript
while select(2,readExpression(statement, scope)) do
local env = newChildEnv(scope)
local ret = {readScript(body, env, "breakable", ln)}
if ret[1] == returnObject then
return unpack(ret)
elseif ret[1] == breakObject then
return script
end
end
end
else
error("do expected")
end
elseif word == "for" then
local varName, n = matchstr(script,"^%s*([_%a][_%w]*)%s*=()")
if varName then
local init; script, init = readExpression(sub(script,n), scope)
n = matchstr(script,"^%s*,()")
if n then
local fin; script, fin = readExpression(sub(script,n), scope)
local inc
n = matchstr(script,"^%s*,()")
if n then
script, inc = readExpression(sub(script,n), scope)
if scope and not inc then
error("for step must be a number")
end
end
n = matchstr(script,"^%s*do()")
if n then
local initialScript = sub(script,n)
local body; script, body = matchScopeBody(initialScript)
if scope then
local ln = currentPosition+fullScriptLen-#initialScript
for i = init, fin, inc or 1 do
local env = newChildEnv(scope)
env[varName] = i
local ret = {readScript(body, env, "breakable", ln)}
if ret[1] == returnObject then
return unpack(ret)
elseif ret[1] == breakObject then
return script
end
end
end
else
error("error")
end
else
error("error")
end
else
varName, n = matchstr(script,"^%s*(.-)%s+in%s+()")
if not varName then
error("error")
end
local args = {}
local c, k = 0, 0
while n do
local match = {readExpression(sub(script,n), scope)}
script = match[1]
c = c + 1
k = c
for i = 2, len(match) do
k = c + i - 2
args[k] = match[i]
end
n = matchstr(script,"^%s*,()")
end
for i = k+1, len(args) do
args[i] = nil
end
n = matchstr(script,"^%s*do()")
if n then
local initialScript = sub(script,n)
local body; script, body = matchScopeBody(initialScript)
if scope then
local ln = currentPosition+fullScriptLen-#initialScript
for _, vars in returnTable(args[1],args[2],args[3]) do
local env = newChildEnv(scope)
local c = 0
for vname in gmatch(varName,"%s*([^,]+)%s*") do
c = c + 1
env[vname] = vars[c]
end
local ret = {readScript(body, env, "breakable", ln)}
if ret[1] == returnObject then
return unpack(ret)
elseif ret[1] == breakObject then
return script
end
end
end
else
error("error")
end
end
elseif word == "repeat" then
local body, condition, initialScript
local initialScript = script
script, body = matchScopeBody(script, 5)
condition = script
script = readExpression(condition, nil)
condition = sub(condition,1,#condition-#script)
if scope then
local ln = currentPosition+fullScriptLen-#initialScript
repeat
local env = newChildEnv(scope)
local ret = {readScript(body, env, "breakable", ln)}
if ret[1] == returnObject then
return unpack(ret)
elseif ret[1] == breakObject then
return script
end
until select(2, readExpression(condition,scope))
end
elseif word == "if" then
local condition; script, condition = readExpression(script, scope)
local n = matchstr(script,"^%s*then()")
local env
if scope then
env = newChildEnv(scope)
end
if n then
local initialScript = sub(script,n)
local body; script, body = matchScopeBody(initialScript, 0)
if condition then
if scope then
local ln = currentPosition+fullScriptLen-#initialScript
local ret = {readScript(body, env, "if", ln)}
if ret[1] == returnObject or ret[1] == breakObject then
return unpack(ret)
end
end
local el, n = matchstr(script,"^else(i?f?)()")
while n do
if el == "if" then
script = readExpression(sub(script,n),nil)
n = matchstr(script, "^%s*then()")
end
script = matchScopeBody(sub(script,n),0)
el, n = matchstr(script,"^else(i?f?)()")
end
script = sub(script,4)
else
n = matchstr(script,"^elseif()")
while n do
script, condition = readExpression(sub(script,n), scope)
n = matchstr(script,"^%s*then()")
if n then
initialScript = sub(script,n)
script, body = matchScopeBody(initialScript,0)
if condition then
if scope then
local ln = currentPosition+fullScriptLen-#initialScript
local ret = {readScript(body, env, "if", ln)}
if ret[1] == returnObject or ret[1] == breakObject then
return unpack(ret)
end
end
local el, n = matchstr(script,"^else(i?f?)()")
while n do
if el == "if" then
script = readExpression(sub(script,n),nil)
n = matchstr(script, "^%s*then()")
end
script = matchScopeBody(sub(script,n),0)
el, n = matchstr(script,"^else(i?f?)()")
end