-
Notifications
You must be signed in to change notification settings - Fork 0
/
rheo
893 lines (779 loc) · 29.2 KB
/
rheo
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
local lpeg = require("lulpeg")
local tArgs = {...}
local Scopes = { {} }
if #tArgs < 2 then
isFile = false
end
if tArgs[1] == "-rs" then
fl = shell.resolve(tArgs[2])
table.remove(tArgs, 1)
table.remove(tArgs, 2)
isFile = true
elseif tArgs[1] == "-h" then
term.setTextColor(colors.lime)
print("Rheo help:")
term.setTextColor(colors.blue)
print("-rs -- Runs a RheoScript file")
print("-cra -- Creates a RheArchive")
print("-ra -- Runs a RheoArchive file")
return
elseif tArgs[1] == "-ra" then
if #tArgs == 2 then
if fs.exists(tArgs[2]) then
local file = fs.open(shell.resolve(tArgs[2]), "r")
local handler = file.readAll()
file.close()
if file then
local func, err = loadstring(handler)
if err then
error("Corruped file: "..tArgs[2], 0)
else
func()
end
if files then
if not files["main.rs"] then
error("No main.rs file has been found!")
else
fs.makeDir("/.raTmp")
local curDir = shell.dir()
shell.setDir("/.raTmp")
for k, v in pairs(files) do
local file = fs.open("/.raTmp/"..k, "w")
file.write(v)
file.close()
end
shell.run(settings.get("rheo.home").."/rheo -rs", "main.rs")
shell.setDir(curDir)
fs.delete("/.raTmp")
end
end
end
end
else
error("Usage: rheo -ra <File>", 0)
end
return
elseif tArgs[1] == "-cra" then
if #tArgs < 3 then
error("Usage: rheo -cra <File> <ArchiveFile>", 0)
end
local files = {}
function readFile(dir, fl)
local file = fs.open(dir.."/"..fl, "r")
if file then
local handle = file.readAll()
if dir == tArgs[2] then
files[fl] = handle
else
local nDir = {}
for i in string.gmatch(dir, "[^/]+") do
table.insert(nDir, i)
end
table.remove(nDir, 1)
local dir = table.concat(nDir, "/")
print(dir)
files[dir.."/"..fl] = handle
end
file.close()
else
printError("An error occured while reading: "..fl)
end
end
function readDir(dir)
for _, file in ipairs(fs.list(dir)) do
if fs.isDir(file) then
readDir(dir.."/"..file)
else
readFile(dir, file)
end
end
end
if fs.isDir(tArgs[2]) then
readDir(tArgs[2])
else
error("Directory expected, got file!")
end
local file = fs.open(tArgs[3]..".ra", "w")
file.write("files = "..textutils.serialize(files))
file.close()
return
else
error("Unrecognized: "..tArgs[1].."\n use rheo -h")
end
function eval_expr(expr)
local accum = eval(expr[2])
for i = 3, #expr, 2 do
local operator = expr[i]
local num2 = eval(expr[i+1])
if operator == '+' then
accum = accum + num2
elseif operator == '-' then
accum = accum - num2
elseif operator == '*' then
accum = accum * num2
elseif operator == '/' then
accum = accum / num2
else
throw("Attempt to var (assign) ?")
end
end
return accum
end
function eval_bool(expr)
local num1 = eval(expr[2])
local operator = expr[3]
local num2 = eval(expr[4])
if operator == '<' then
return num1 < num2
elseif operator == '=<' then
return num1 <= num2
elseif operator == '>' then
return num1 > num2
elseif operator == '=>' then
return num1 >= num2
elseif operator == '==' then
return num1 == num2
elseif operator == '!=' then
return num1 ~= num2
end
end
function eval_if(if_ast)
local last_bool
local isElse = false
for i=1, #if_ast do
if isElse == true then
break
end
if if_ast[i] == "if" then
if eval_bool(if_ast[i+1]) then
last_bool = eval_bool(if_ast[i+1])
return eval(if_ast[i+2])
end
elseif if_ast[i][1] == "elseif" then
if last_bool then
elseif eval_bool(if_ast[i][2]) then
last_bool = eval_bool(if_ast[i][2])
return eval(if_ast[i][3])
end
elseif if_ast[i][1] == "else" then
if last_bool then
else
isElse = true
return eval(if_ast[i][2])
end
end
end
end
function eval(ast)
if type(ast) == "number" then
return ast
elseif type(ast) == "string" then
if string.find(ast, "%%#br") then
ast = ast:gsub("%%#br", "\n")
end
if string.find(ast, "%%#dQ") then
ast = ast:gsub("%%#dQ", "\"")
end
if string.find(ast, "%%#dQ") then
ast = ast:gsub("%%#dQ", "\'")
end
if string.find(ast, "%%#") then
ast = ast:gsub("%%#", "\\")
end
return ast
elseif ast[1] == "string" then
return eval(ast[2])
elseif ast[1] == "expr" or ast[1] == "term" then
return eval_expr(ast)
elseif ast[1] == "none" then
return nil
elseif ast[1] == "true" then
return true
elseif ast[1] == "false" then
return false
elseif ast[1] == "array" then
local new = {}
for _, el in ipairs(ast[2]) do
table.insert(new, eval(el))
end
return new
elseif ast[1] == "ref" then
return lookup(ast)
elseif ast[1] == "assign" then
return assign(ast[2], eval(ast[3]))
elseif ast[1] == "list" then
local last = nil
for i = 2, #ast do
last = eval(ast[i])
end
return last
elseif ast[1] == "if" then
return eval_if(ast)
elseif ast[1] == "while" then
while eval_bool(ast[2]) do
eval(ast[3])
end
elseif ast[1] == "func" then
return {args = ast[2],
code = ast[3],
scope = Scopes[#Scopes],
reVal = ast[4] }
elseif ast[1] == "funcdef" then
return assign(ast[2], { args = ast[3], code = ast[4], scope = Scopes[#Scopes], reVal = "function" })
elseif ast[1] == "call" then
local fn = eval(ast[2])
if not fn then
throw("<Call> Undefined function")
return
end
if type(fn) ~= "function" then
throw("<Call> Function expected, got "..type())
end
scope = setmetatable({}, {__index=fn.scope})
for i, name in ipairs(fn.args) do
scope[name] = eval(ast[3][i])
end
table.insert(Scopes, scope)
eval(fn.code)
local reVal = eval(fn.reVal)
table.remove(Scopes)
return reVal
elseif ast[1] == "print" then
local str = eval(ast[2])
if type(str) ~= "string" then
throw("Attempt to print "..type(str)..": expected String")
else
print(str)
end
elseif ast[1] == "random" then
local num1 = eval_expr(ast[2])
local num2 = eval_expr(ast[3])
if type(num1) == "number" and type(num2) == "number" then
return math.random(num1, num2)
else
error("Number expected, got "..type(num2)..", "..type(num3))
end
elseif ast[1] == "input" then
local input = io.read()
return eval(input)
elseif ast[1] == "execute" then
local str = eval(ast[2])
if type(str) == "string" then
shell.run(fs.getDir(shell.getRunningProgram()).."/"..str)
end
elseif ast[1] == "wait" then
local time = os.clock() + eval_expr(ast[2])
repeat until os.clock() > time
elseif ast[1] == "makestring" then
local str = eval(ast[2])
if type(str) == "number" then
return tostring(str)
else
throw("<makestring> Number expected, got "..type(str))
end
elseif ast[1] == "makenumber" then
local num = eval(ast[2])
if type(ast[2]) == "string" then
return tonumber(ast[2])
else
throw("<makenumber> String expected, got "..type(num))
end
elseif ast[1] == "throw" then
local str = eval(ast[2])
if type(str) == "string" then
throw(str)
else
throw("<throw> String expected, got "..type(str))
end
elseif ast[1] == "for" then
local list = eval(ast[3])
if type(list) == "number" then
for i=1, list do
assign(ast[2], eval(i))
eval(ast[4])
assign(ast[2], eval({"none"}))
end
else
throw("<For> expected number to iterate, got "..type(table))
end
elseif ast[1] == "importfile" then
local file = eval(ast[2])
if type(file) == "string" then
import(file)
else
throw("<import>: String expected, got"..type(file))
end
elseif ast[1] == "importfolder" then
local folder = eval(ast[3])
local file = eval(ast[2])
if type(folder) == "string" then
if type(file) == "string" then
importFolder(folder.."/"..file)
else
throw("<Import> <From>: Module expected, got "..type(file))
end
else
throw("<Import> <From>: Library as string expected, got "..type(file)..", "..type(folder))
end
elseif ast[1] == "argument" then
local num = eval(ast[2])
if type(num) == "number" then
for i=1, #tArgs do
if i == num then
return tArgs[i]
end
end
else
throw("<Arguments> Number expected, got "..type(num))
end
elseif ast[1] == "return" then
return eval(ast[2])
elseif ast[1] == "func_assign" then
local fn = eval(ast[3])
if type(fn) ~= "function" then
throw("<Assign> function expected, got "..type(fn))
end
return assign(ast[2], fn)
elseif ast[1] == "str_assign" then
local str = eval(ast[3])
if type(str) ~= "string" then
throw("<Assign> string expected, got "..type(str))
end
return assign(ast[2], str)
elseif ast[1] == "int_assign" then
local int = eval(ast[3])
if type(int) ~= "number" then
throw("<Assign> number expected, got "..type(int))
end
if int ~= math.floor(int) then
throw("<Assign> integer expected, got float")
end
return assign(ast[2], int)
elseif ast[1] == "flt_assign" then
local float = eval(ast[3])
if type(float) ~= "number" then
throw("<Assign> number expected, got "..type(float))
end
return assign(ast[2], float)
elseif ast[1] == "list_assign" then
local list = eval(ast[3])
if type(list) ~= "list" then
throw("<Assign> list expected, got "..type(list))
return
end
return assign(ast[2], list)
elseif ast[1] == "bool_assign" then
local bool = eval(ast[3])
if type(bool) ~= "boolean" then
throw("<Assign> boolean expected, got "..type(bool))
return nil
end
return assign(ast[2], bool)
elseif ast[1] == "none_assign" then
return assign(ast[2], eval({"none"}))
elseif ast[1] == "length" then
local len = eval(ast[2])
if type(len) == "number" then
throw("<Length> Attempt to get length of number")
end
return #len
elseif ast[1] == "type" then
return type(eval(ast[2]))
elseif ast[1] == "this" then
return fl
elseif ast[1] == "conc" then
if #ast[2] <= 2 and type(eval(ast[2][1])) == "list" then
if type(eval(ast[2][2])) == "string" then
return assign(ast[2][1], table.concat(eval(ast[2][1]), eval(ast[2][2])))
else
throw("<Conc> Attempt to: string expected #2, got "..type(eval(ast[2][1])))
end
else
local val = {}
for _, el in ipairs(ast[2]) do
local el = eval(el)
if type(el) ~= "string" then
throw("Attempt to: String expected, got "..type(el))
else
table.insert(val, eval(el))
end
end
return table.concat(val, "")
end
elseif ast[1] == "split" then
args = {}
for _, el in ipairs(ast[2]) do
table.insert(args, eval(el))
end
if #args ~= 2 then
throw("<Split> Attempt to: #1+#2 value expected!")
else
if type(args[1]) ~= "String" and type(args[2]) ~= "string" then
throw("<Split> Attempt to: #1+#2 string expected!")
elseif type(args[1]) ~= "string" and type(args[2]) == "string" then
throw("<Split> Attempt to: #1 string expected!")
elseif type(args[1]) == "string" and type(args[2]) ~= "string" then
throw("<Split> Attempt to: #2 string expected!")
elseif type(args[1]) == "string" and type(args[2]) == "string" then
ls = {}
for i in string.gmatch(args[1], args[2]) do
table.insert(ls, i)
end
return ls
end
end
elseif ast[1] == "dolua" then
local str = eval(ast[2])
if type(str) == "string" then
local func, err = loadstring(str)
if not err then
func()
else
throw("<dolua> Attempt to:LuaError: "..err)
end
else
throw("<dolua> Attempt to: string expected, got "..type(str))
end
elseif ast[1] == "do" then
local str = eval(ast[2])
if type(str) == "string" then
local oLine = curLine
local curLine = 1
local exec = {}
for i in string.gmatch(str, "[^\r\n]+") do
table.insert(exec, i)
end
parser:match(table.concat(exec, " %%#n "))
curLine = oLine
else
throw("<do> Attempt to: string expected, got "..type(str))
end
elseif ast[1] == "parseDict" then
local str = eval(ast[2])
local dict = {}
if type(str) == "list" then
for i=1, #str do
table.insert(dict, tostring(str[i]).." => ".."true\n")
end
return table.concat(dict, "")
elseif type(str) == "string" then
local list = {}
for i in string.gmatch(str, "%S+") do
table.insert(list, i)
end
for i=1, #list do
if list[i] == "=>" then
table.remove(list, i)
table.remove(list, i)
end
end
return list
else
throw("Attempt to parse dict: string or list expected, got "..type(str))
end
elseif ast[1] == "readFile" then
local str = eval(ast[2])
if type(str) == "string" then
local file = fs.open(fs.getDir(fl).."/"..str, "r")
if file then
local inp = file.readAll()
file.close()
return inp
else
throw("File not found!")
end
else
throw("Attept to read "..type(str))
end
elseif ast[1] == "writeFile" then
local str = eval(ast[2])
local input = eval(ast[3])
if type(str) ~= "string" then
throw("Attempt to write "..type(str))
return
end
if type(input) ~= "string" then
throw("Expected string to write, got "..type(input))
return
end
if type(str) == "string" then
local file = fs.open(fs.getDir(fl).."/"..str, "w")
file.write(input)
file.close()
end
elseif ast[1] == "event" then
local str = eval(ast[2])
if type(str) ~= "string" then
throw("Attempt to pullEvent: string expected, got "..type(str))
end
local nStr = {}
for i in string.gmatch(str, "[^:]+") do
table.insert(nStr, i)
end
if #nStr < 2 or #nStr > 2 then
throw("Attempt to event: unexpected pattern")
end
if #nStr == 2 then
local executor = nStr[1]
local event = nStr[2]
if executor == "Input" then
if event == "mouse_click" then
local event, button, x, y = os.pullEvent("mouse_click")
if button == 1 then
button = "left"
elseif button == 2 then
button = "right"
elseif button == 3 then
button = "middle"
end
return {button, x, y}
elseif event == "key" then
local event, key, isHeld = os.pullEvent("key")
return {keys.getName(key), isHeld}
else
throw("Attempt to except unknown event")
end
else
throw("Attempt to call unknown class <executor> (?)")
end
end
elseif ast[1] == "comment" then
return nil
elseif ast[1] == "eol" then
curLine = curLine+1
else
error("Unexpected: "..ast[1])
end
end
function importFolder(ifl)
local ifile = io.open(fs.dir(shell.getRunningProgram()).."/lib/"..ifl..".lib", "r")
if ifile then
local oLine = curLine
curLine = 1
local oldthrow = throw
ilines = {}
function throw(msg)
error(ifl..": "..iline..": "..msg)
end
for line in ifile:lines() do
table.insert(ilines, line)
end
parser:match(table.concat(ilines, " %%#n "))
curLine = oLine
throw = oldthrow
else
throw(ifl.." does not exists in the Rheo library!")
end
end
function import(ifl)
local ilines = {}
local ifile = io.open(fs.getDir(shell.getRunningProgram()).."/lib/"..ifl..".lib", "r")
if ifile then
local oLine = curLine
curLine = 0
local oldthrow = throw;
function throw(msg)
error(ifl..": "..iline..": "..msg, 0)
end
for line in ifile:lines() do
table.insert(ilines, line)
end
parser:match(table.concat(ilines, " %%#n "))
curLine = oLine
throw = oldthrow;
else
throw(ifl.." does not exists in the Rheo library!")
end
end
function assign(ref, value)
local current = Scopes[#Scopes]
for i = 2, #ref do
local next_index = ref[i]
if type(next_index) == "list" then
next_index = eval(next_index)
end
if i == #ref then
while current[next_index] and not rawget(current, next_index) do
current = getmetatable(current).__index
end
current[next_index] = value
return value
else
current = current[next_index]
end
end
end
function lookup(ref)
local current = Scopes[#Scopes]
for i = 2, #ref do
local next_index = ref[i]
if type(next_index) == "list" then
next_index = eval(next_index)
end
current = current[next_index]
end
return current
end
local spc = lpeg.S("\t\n ")^0
local digit = lpeg.R('09')
local number = lpeg.C( (lpeg.P("-") + digit) *
digit^0 *
( lpeg.P('.') * digit^0 )^-1 ) / tonumber * spc
local lparen = "(" * spc
local rparen = ")" * spc
local lbrack = "[" * spc
local rbrack = "]" * spc
local lcurly = "{" * spc
local rcurly = "}" * spc
local comma = "," * spc
local expr_op = lpeg.C( lpeg.S('+-') ) * spc
local term_op = lpeg.C( lpeg.S('*/') ) * spc
local letter = lpeg.R('AZ','az')
local name = lpeg.C( letter*( digit+letter+"_"+".")^0 ) * spc
local doublequoted_string = lpeg.P('"') * lpeg.C( ( name+" "+"+"+"/"+"!"+"§"+"$"+"%"+"&"+"("+")"+"="+"?"+"`"+"°"+"^"+"*"+"#"+"~"+"´"+"ä"+"ö"+"ü"+"²"+"³"+"{"+"}"+"["+"]"+"'"+":"+";"+","+"-"+"<"+">"+"|"+"µ"+"?"+"@")^0 ) * lpeg.P('"') * spc
local singlequoted_string = lpeg.P("'") * lpeg.C( ( name+" "+"+"+"/"+"!"+"§"+"$"+"%"+"&"+"("+")"+"="+"?"+"`"+"°"+"^"+"*"+"#"+"~"+"´"+"ä"+"ö"+"ü"+"²"+"³"+"{"+"}"+"["+"]"+":"+";"+","+"-"+"\""+"<"+">"+"|"+"µ"+"?"+"@")^0 ) * lpeg.P("'") * spc
local comment = lpeg.P("//") * lpeg.C( ( name+" "+"+"+"!"+"§"+"$"+"%"+"&"+"("+")"+"="+"?"+"`"+"°"+"^"+"*"+"#"+"~"+"´"+"ä"+"ö"+"ü"+"²"+"³"+"{"+"}"+"["+"]"+"'"+":"+";"+","+"-"+"<"+">"+"|"+"µ"+"?"+"@")^0 ) * lpeg.P("//") * spc
local string = singlequoted_string + doublequoted_string
local keywords = ( lpeg.P("if") + lpeg.P("else") + lpeg.P("elseif") + lpeg.P("while") + lpeg.P("func") + lpeg.P("print") + lpeg.P("random") + lpeg.P("input") + lpeg.P("true") + lpeg.P("none") + lpeg.P("false") + lpeg.P("execute") + lpeg.P("wait") + lpeg.P("makestring") + lpeg.P("makenumber") + lpeg.P("throw") + lpeg.P("for") + lpeg.P("import") + lpeg.P("return") + lpeg.P("type") + lpeg.P("this") + lpeg.P("var") + lpeg.P("Static") + lpeg.P("Int") + lpeg.P("Float") + lpeg.P("String") + lpeg.P("List") + lpeg.P("conc") + lpeg.P("split") + lpeg.P("dolua") + lpeg.P("event") + lpeg.P("parseDict") + lpeg.P("writeFile") + lpeg.P("readFile") ) * spc
local name = name - keywords
local boolean = lpeg.C( lpeg.S("<>") + "<=" + ">=" + "!=" + "==" ) * spc
stmt = spc * lpeg.P{
"LIST";
LIST =
lpeg.Ct( lpeg.Cc("list") *
( lpeg.V("STMT") * spc )^0 ),
STMT =
lpeg.V("STR_ASSIGN") + lpeg.V("INT_ASSIGN") + lpeg.V("FLOAT_ASSIGN") + lpeg.V("FUNC_ASSIGN") + lpeg.V("LIST_ASSIGN") + lpeg.V("BOOL_ASSIGN") + lpeg.V("NONE_ASSIGN") +
lpeg.V("EOL") +
lpeg.V("IF") +
lpeg.V("COMMENT") +
lpeg.V("IMPORTFOLDER") +
lpeg.V("IMPORTFILE") +
lpeg.V("DOLUA") +
lpeg.V("DO") +
lpeg.V("WRITEFILE") +
lpeg.V("FUNCDEF") +
lpeg.V("WAIT") +
lpeg.V("WHILE") +
lpeg.V("FOR") +
lpeg.V("PRINT") +
lpeg.V("CALL") +
lpeg.V("THROW") +
lpeg.V("EXECUTE") +
lpeg.V("STRING") +
lpeg.V("INPUT"),
STR_ASSIGN = lpeg.Ct( lpeg.Cc("str_assign") * "var" * spc * "String" * spc * lpeg.V("REF") * ":" * spc * lpeg.V("VAL") ),
INT_ASSIGN = lpeg.Ct( lpeg.Cc("int_assign") * "var" * spc * "Int" * spc * lpeg.V("REF") * ":" * spc * lpeg.V("VAL") ),
FLOAT_ASSIGN = lpeg.Ct( lpeg.Cc("flt_assign") * "var" * spc * "Float" * spc * lpeg.V("REF") * ":" * spc * lpeg.V("VAL") ),
FUNC_ASSIGN = lpeg.Ct( lpeg.Cc("func_assign") * "var" * spc * "Static" * spc * lpeg.V("REF") * ":" * spc * lpeg.V("VAL") ),
LIST_ASSIGN = lpeg.Ct( lpeg.Cc("list_assign") * "var" * spc * "List" * spc * lpeg.V("REF") * ":" * spc * lpeg.V("VAL") ),
BOOL_ASSIGN = lpeg.Ct( lpeg.Cc("bool_assign") * "var" * spc * "Boolean" * spc * lpeg.V("REF") * ":" * spc * lpeg.V("VAL") ),
NONE_ASSIGN = lpeg.Ct( lpeg.Cc("none_assign") * "var" * spc * "None" * spc * lpeg.V("REF") ),
REF = lpeg.Ct( lpeg.Cc("ref") * name * ( lbrack * lpeg.V("VAL") * rbrack )^0 ),
EXPR = lpeg.Ct( lpeg.Cc("expr") * lpeg.V("TERM") * ( expr_op * lpeg.V("TERM") )^0 ),
TERM = lpeg.Ct( lpeg.Cc("term") * lpeg.V("FACT") * ( term_op * lpeg.V("FACT") )^0 ),
FACT =
number +
lparen * lpeg.V("EXPR") * rparen +
lpeg.V("REF"),
STRING = lpeg.Ct( lpeg.Cc("string") * string * spc ),
ARRAY = lpeg.Ct( lpeg.Cc("array") * lbrack * spc * lpeg.V("VAL_LIST") * spc * rbrack ),
VAL_LIST = lpeg.Ct( ( lpeg.V("VAL") * comma^-1 * spc )^0 ),
VAL = lpeg.V("CALL") + lpeg.V("ARGUMENT") + lpeg.V("ARRAY") + lpeg.V("FUNC") + lpeg.V("STRING") + lpeg.V("EXPR") + lpeg.V("RANDOM") + lpeg.V("INPUT") + lpeg.V("MAKESTRING") + lpeg.V("MAKENUMBER") + lpeg.V("BOOLS") + lpeg.V("LEN") + lpeg.V("TYPE") + lpeg.V("THIS") + lpeg.V("CONC") + lpeg.V("SPLIT") + lpeg.V("EVENT") + lpeg.V("PARSEDICT") + lpeg.V("READFILE"),
BOOL = lpeg.Ct( lpeg.Cc("bool") * ( lpeg.V("REF") + lpeg.V("VAL") ) * spc * boolean * spc * ( lpeg.V("REF") + lpeg.V("VAL") ) ),
IF = lpeg.Ct( lpeg.C("if") * lparen * spc * lpeg.V("BOOL") * spc * rparen * ":" * spc * lpeg.V("LIST") * spc * ( lpeg.V("ELSEIF") + lpeg.V("ELSE") )^0 * ";" ),
ELSE = lpeg.Ct( lpeg.C("else") * ":" * spc * lpeg.V("LIST") ),
ELSEIF = lpeg.Ct( lpeg.C("elseif") * lparen * spc * lpeg.V("BOOL") * spc * rparen * ":" * spc * lpeg.V("LIST") ),
WHILE = lpeg.Ct( lpeg.C("while") * lparen * spc * lpeg.V("BOOL") * spc * rparen * ":" * spc * lpeg.V("LIST") * spc ),
FOR = lpeg.Ct( lpeg.C("for") * lparen * spc * lpeg.V("REF") * "in" * spc * lpeg.V("VAL") * spc * rparen * ":" * spc * lpeg.V("LIST") * ";" ),
CALL = lpeg.Ct( lpeg.Cc("call") * lpeg.V("REF") * spc * lparen * lpeg.V("VAL_LIST") * rparen ),
FUNC = lpeg.Ct( lpeg.C("func") * lparen * lpeg.V("NAME_LIST") * rparen * ":" * spc * lpeg.V("LIST") * spc * lpeg.V("RETURN") ),
FUNCDEF = lpeg.Ct( lpeg.Cc("funcdef") * "func" * spc * lpeg.V("REF") * spc * lparen * spc * lpeg.V("NAME_LIST") * spc * rparen * ":" * spc * lpeg.V("LIST") * spc * ";"),
PRINT = lpeg.Ct( lpeg.C("print") * lparen * lpeg.V("VAL") * rparen ),
RANDOM = lpeg.Ct( lpeg.C("random") * lparen * spc * lpeg.V("VAL") * comma^0 * spc * lpeg.V("VAL") * spc * rparen ),
MAKESTRING = lpeg.Ct( lpeg.C("makestring") * lparen * spc * lpeg.V("VAL") * spc * rparen ),
MAKENUMBER = lpeg.Ct( lpeg.C("makenumber") * lparen * spc * lpeg.V("VAL") * spc * rparen ),
THROW = lpeg.Ct( lpeg.C("throw") * spc * lparen * spc * lpeg.V("VAL") * spc * rparen ),
INPUT = lpeg.Ct( lpeg.C("input") * lparen * spc * rparen),
BOOLS = lpeg.V("NONE") + lpeg.V("TRUE") + lpeg.V("FALSE"),
NONE = lpeg.Ct( lpeg.C("none") ),
TRUE = lpeg.Ct( lpeg.C("true") ),
FALSE = lpeg.Ct( lpeg.C("false") ),
THIS = lpeg.Ct( lpeg.C("this") ),
EXECUTE = lpeg.Ct( lpeg.C("execute") * lparen * spc * lpeg.V("VAL") * spc * rparen ),
WAIT = lpeg.Ct( lpeg.C("wait") * lparen * spc * lpeg.V("VAL") * spc * rparen ),
IMPORTFILE = lpeg.Ct( lpeg.Cc("importfile") * "import" * spc * lpeg.V("VAL") ),
IMPORTFOLDER = lpeg.Ct( lpeg.Cc("importfolder") * "import" * spc * lpeg.V("VAL") * spc * "from" * spc * lpeg.V("VAL") ),
ARGUMENT = lpeg.Ct( lpeg.C("argument") * lbrack * lpeg.V("EXPR") * rbrack ),
COMMENT = lpeg.Ct( lpeg.Cc("comment") * comment ),
EOL = lpeg.Ct( lpeg.Cc("eol") * "%%#n" ),
RETURN = lpeg.Ct( lpeg.C("return") * spc * lpeg.V("VAL") ),
LEN = lpeg.Ct( lpeg.Cc("length") * "~" * lpeg.V("VAL") ),
TYPE = lpeg.Ct( lpeg.C("type") * lparen * spc * lpeg.V("VAL") * spc * rparen ),
CONC = lpeg.Ct( lpeg.C("conc") * lparen * spc * lpeg.V("VAL_LIST") * spc * rparen ),
SPLIT = lpeg.Ct( lpeg.C("split") * lparen * spc * lpeg.V("VAL_LIST") * rparen ),
DOLUA = lpeg.Ct( lpeg.C("dolua") * lparen * spc * lpeg.V("VAL") * spc * rparen ),
DO = lpeg.Ct( lpeg.C("do") * lparen * spc * lpeg.V("VAL") * spc * rparen ),
PARSEDICT = lpeg.Ct( lpeg.C("parseDict") * lparen * spc * lpeg.V("VAL") * spc * rparen ),
EVENT = lpeg.Ct( lpeg.C("event") * lparen * spc * lpeg.V("VAL") * spc * rparen ),
WRITEFILE = lpeg.Ct( lpeg.C("writeFile") * lparen * spc * lpeg.V("VAL") * "," * spc * lpeg.V("VAL") * spc * rparen ),
READFILE = lpeg.Ct( lpeg.C("readFile") * lparen * spc * lpeg.V("VAL") * spc * rparen ),
NAME_LIST = lpeg.Ct( ( name * comma^0 )^0 )
}
local oldType = type;
function type(val)
if oldType(val) == "table" then
if val.args and val.code and val.scope then
return "function"
elseif val["x"] and val["y"] then
return "vector"
else
return "list"
end
elseif oldType(val) == "nil" then
return "none"
else
return oldType(val)
end
end
parser = stmt / eval
if isFile == true then
function throw(msg)
error(fl..": "..curLine..": "..msg, 0)
end
file = io.open(fl, "r")
if file then
curLine = 1
lines = {}
for line in file:lines() do
table.insert(lines, line)
end
file:close()
code = table.concat(lines, " %%#n ")
parser:match(code)
else
error(fl.." does not exists!",0)
end
else
term.setTextColor(colors.yellow)
print("Rheo v1.0")
print(" ")
print("Rheo interactive interpreter")
print("Type exit() to leave the program")
print("")
print("")
while true do
function throw(msg)
printError("<303>:: Error while parsing: Attempt to: "..msg)
end
term.setTextColor(colors.lime)
term.write("> ")
term.setTextColor(colors.white)
local input = io.read()
if input == "exit()" then
error()
else
parser:match(input)
end
end
end