forked from PAT457/Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LuaIDE.lua
2205 lines (1933 loc) · 58.3 KB
/
LuaIDE.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
--
-- Lua IDE
-- Made by GravityScore
--
-- -------- Variables
-- Version
local version = "1.0"
local args = {...}
-- Editing
local w, h = term.getSize()
local tabWidth = 2
local autosaveInterval = 20
local allowEditorEvent = true
local keyboardShortcutTimeout = 0.4
-- Clipboard
local clipboard = nil
-- Theme
local theme = {}
-- Language
local languages = {}
local curLanguage = {}
-- Events
local event_distract = "luaide_distractionEvent"
-- Locations
local updateURL = "https://raw.github.com/GravityScore/LuaIDE/master/luaide.lua"
local ideLocation = "/" .. shell.getRunningProgram()
local themeLocation = "/.LuaIDE-Theme"
local function isAdvanced() return term.isColor and term.isColor() end
-- -------- Utilities
local function modRead(properties)
local w, h = term.getSize()
local defaults = {replaceChar = nil, history = nil, visibleLength = nil, textLength = nil,
liveUpdates = nil, exitOnKey = nil}
if not properties then properties = {} end
for k, v in pairs(defaults) do if not properties[k] then properties[k] = v end end
if properties.replaceChar then properties.replaceChar = properties.replaceChar:sub(1, 1) end
if not properties.visibleLength then properties.visibleLength = w end
local sx, sy = term.getCursorPos()
local line = ""
local pos = 0
local historyPos = nil
local function redraw(repl)
local scroll = 0
if properties.visibleLength and sx + pos > properties.visibleLength + 1 then
scroll = (sx + pos) - (properties.visibleLength + 1)
end
term.setCursorPos(sx, sy)
local a = repl or properties.replaceChar
if a then term.write(string.rep(a, line:len() - scroll))
else term.write(line:sub(scroll + 1, -1)) end
term.setCursorPos(sx + pos - scroll, sy)
end
local function sendLiveUpdates(event, ...)
if type(properties.liveUpdates) == "function" then
local ox, oy = term.getCursorPos()
local a, data = properties.liveUpdates(line, event, ...)
if a == true and data == nil then
term.setCursorBlink(false)
return line
elseif a == true and data ~= nil then
term.setCursorBlink(false)
return data
end
term.setCursorPos(ox, oy)
end
end
term.setCursorBlink(true)
while true do
local e, but, x, y, p4, p5 = os.pullEvent()
if e == "char" then
local s = false
if properties.textLength and line:len() < properties.textLength then s = true
elseif not properties.textLength then s = true end
local canType = true
if not properties.grantPrint and properties.refusePrint then
local canTypeKeys = {}
if type(properties.refusePrint) == "table" then
for _, v in pairs(properties.refusePrint) do
table.insert(canTypeKeys, tostring(v):sub(1, 1))
end
elseif type(properties.refusePrint) == "string" then
for char in properties.refusePrint:gmatch(".") do
table.insert(canTypeKeys, char)
end
end
for _, v in pairs(canTypeKeys) do if but == v then canType = false end end
elseif properties.grantPrint then
canType = false
local canTypeKeys = {}
if type(properties.grantPrint) == "table" then
for _, v in pairs(properties.grantPrint) do
table.insert(canTypeKeys, tostring(v):sub(1, 1))
end
elseif type(properties.grantPrint) == "string" then
for char in properties.grantPrint:gmatch(".") do
table.insert(canTypeKeys, char)
end
end
for _, v in pairs(canTypeKeys) do if but == v then canType = true end end
end
if s and canType then
line = line:sub(1, pos) .. but .. line:sub(pos + 1, -1)
pos = pos + 1
redraw()
end
elseif e == "key" then
if but == keys.enter then break
elseif but == keys.left then if pos > 0 then pos = pos - 1 redraw() end
elseif but == keys.right then if pos < line:len() then pos = pos + 1 redraw() end
elseif (but == keys.up or but == keys.down) and properties.history then
redraw(" ")
if but == keys.up then
if historyPos == nil and #properties.history > 0 then
historyPos = #properties.history
elseif historyPos > 1 then
historyPos = historyPos - 1
end
elseif but == keys.down then
if historyPos == #properties.history then historyPos = nil
elseif historyPos ~= nil then historyPos = historyPos + 1 end
end
if properties.history and historyPos then
line = properties.history[historyPos]
pos = line:len()
else
line = ""
pos = 0
end
redraw()
local a = sendLiveUpdates("history")
if a then return a end
elseif but == keys.backspace and pos > 0 then
redraw(" ")
line = line:sub(1, pos - 1) .. line:sub(pos + 1, -1)
pos = pos - 1
redraw()
local a = sendLiveUpdates("delete")
if a then return a end
elseif but == keys.home then
pos = 0
redraw()
elseif but == keys.delete and pos < line:len() then
redraw(" ")
line = line:sub(1, pos) .. line:sub(pos + 2, -1)
redraw()
local a = sendLiveUpdates("delete")
if a then return a end
elseif but == keys["end"] then
pos = line:len()
redraw()
elseif properties.exitOnKey then
if but == properties.exitOnKey or (properties.exitOnKey == "control" and
(but == 29 or but == 157)) then
term.setCursorBlink(false)
return nil
end
end
end
local a = sendLiveUpdates(e, but, x, y, p4, p5)
if a then return a end
end
term.setCursorBlink(false)
if line ~= nil then line = line:gsub("^%s*(.-)%s*$", "%1") end
return line
end
-- -------- Themes
local defaultTheme = {
background = "gray",
backgroundHighlight = "lightGray",
prompt = "cyan",
promptHighlight = "lightBlue",
err = "red",
errHighlight = "pink",
editorBackground = "gray",
editorLineHightlight = "lightBlue",
editorLineNumbers = "gray",
editorLineNumbersHighlight = "lightGray",
editorError = "pink",
editorErrorHighlight = "red",
textColor = "white",
conditional = "yellow",
constant = "orange",
["function"] = "magenta",
string = "red",
comment = "lime"
}
local normalTheme = {
background = "black",
backgroundHighlight = "black",
prompt = "black",
promptHighlight = "black",
err = "black",
errHighlight = "black",
editorBackground = "black",
editorLineHightlight = "black",
editorLineNumbers = "black",
editorLineNumbersHighlight = "white",
editorError = "black",
editorErrorHighlight = "black",
textColor = "white",
conditional = "white",
constant = "white",
["function"] = "white",
string = "white",
comment = "white"
}
local availableThemes = {
{"Water (Default)", "https://raw.github.com/GravityScore/LuaIDE/master/themes/default.txt"},
{"Fire", "https://raw.github.com/GravityScore/LuaIDE/master/themes/fire.txt"},
{"Sublime Text 2", "https://raw.github.com/GravityScore/LuaIDE/master/themes/st2.txt"},
{"Midnight", "https://raw.github.com/GravityScore/LuaIDE/master/themes/midnight.txt"},
{"TheOriginalBIT", "https://raw.github.com/GravityScore/LuaIDE/master/themes/bit.txt"},
{"Superaxander", "https://raw.github.com/GravityScore/LuaIDE/master/themes/superaxander.txt"},
{"Forest", "https://raw.github.com/GravityScore/LuaIDE/master/themes/forest.txt"},
{"Night", "https://raw.github.com/GravityScore/LuaIDE/master/themes/night.txt"},
{"Original", "https://raw.github.com/GravityScore/LuaIDE/master/themes/original.txt"},
}
local function loadTheme(path)
local f = io.open(path)
local l = f:read("*l")
local config = {}
while l ~= nil do
local k, v = string.match(l, "^(%a+)=(%a+)")
if k and v then config[k] = v end
l = f:read("*l")
end
f:close()
return config
end
-- Load Theme
if isAdvanced() then theme = defaultTheme
else theme = normalTheme end
-- -------- Drawing
local function centerPrint(text, ny)
if type(text) == "table" then for _, v in pairs(text) do centerPrint(v) end
else
local x, y = term.getCursorPos()
local w, h = term.getSize()
term.setCursorPos(w/2 - text:len()/2 + (#text % 2 == 0 and 1 or 0), ny or y)
print(text)
end
end
local function title(t)
term.setTextColor(colors[theme.textColor])
term.setBackgroundColor(colors[theme.background])
term.clear()
term.setBackgroundColor(colors[theme.backgroundHighlight])
for i = 2, 4 do term.setCursorPos(1, i) term.clearLine() end
term.setCursorPos(3, 3)
term.write(t)
end
local function centerRead(wid, begt)
local function liveUpdate(line, e, but, x, y, p4, p5)
if isAdvanced() and e == "mouse_click" and x >= w/2 - wid/2 and x <= w/2 - wid/2 + 10
and y >= 13 and y <= 15 then
return true, ""
end
end
if not begt then begt = "" end
term.setTextColor(colors[theme.textColor])
term.setBackgroundColor(colors[theme.promptHighlight])
for i = 8, 10 do
term.setCursorPos(w/2 - wid/2, i)
term.write(string.rep(" ", wid))
end
if isAdvanced() then
term.setBackgroundColor(colors[theme.errHighlight])
for i = 13, 15 do
term.setCursorPos(w/2 - wid/2 + 1, i)
term.write(string.rep(" ", 10))
end
term.setCursorPos(w/2 - wid/2 + 2, 14)
term.write("> Cancel")
end
term.setBackgroundColor(colors[theme.promptHighlight])
term.setCursorPos(w/2 - wid/2 + 1, 9)
term.write("> " .. begt)
return modRead({visibleLength = w/2 + wid/2, liveUpdates = liveUpdate})
end
-- -------- Prompt
local function prompt(list, dir, isGrid)
local function draw(sel)
for i, v in ipairs(list) do
if i == sel then term.setBackgroundColor(v.highlight or colors[theme.promptHighlight])
else term.setBackgroundColor(v.bg or colors[theme.prompt]) end
term.setTextColor(v.tc or colors[theme.textColor])
for i = -1, 1 do
term.setCursorPos(v[2], v[3] + i)
term.write(string.rep(" ", v[1]:len() + 4))
end
term.setCursorPos(v[2], v[3])
if i == sel then
term.setBackgroundColor(v.highlight or colors[theme.promptHighlight])
term.write(" > ")
else term.write(" - ") end
term.write(v[1] .. " ")
end
end
local key1 = dir == "horizontal" and 203 or 200
local key2 = dir == "horizontal" and 205 or 208
local sel = 1
draw(sel)
while true do
local e, but, x, y = os.pullEvent()
if e == "key" and but == 28 then
return list[sel][1]
elseif e == "key" and but == key1 and sel > 1 then
sel = sel - 1
draw(sel)
elseif e == "key" and but == key2 and ((err == true and sel < #list - 1) or (sel < #list)) then
sel = sel + 1
draw(sel)
elseif isGrid and e == "key" and but == 203 and sel > 2 then
sel = sel - 2
draw(sel)
elseif isGrid and e == "key" and but == 205 and sel < 3 then
sel = sel + 2
draw(sel)
elseif e == "mouse_click" then
for i, v in ipairs(list) do
if x >= v[2] - 1 and x <= v[2] + v[1]:len() + 3 and y >= v[3] - 1 and y <= v[3] + 1 then
return list[i][1]
end
end
end
end
end
local function scrollingPrompt(list)
local function draw(items, sel, loc)
for i, v in ipairs(items) do
local bg = colors[theme.prompt]
local bghigh = colors[theme.promptHighlight]
if v:find("Back") or v:find("Return") then
bg = colors[theme.err]
bghigh = colors[theme.errHighlight]
end
if i == sel then term.setBackgroundColor(bghigh)
else term.setBackgroundColor(bg) end
term.setTextColor(colors[theme.textColor])
for x = -1, 1 do
term.setCursorPos(3, (i * 4) + x + 4)
term.write(string.rep(" ", w - 13))
end
term.setCursorPos(3, i * 4 + 4)
if i == sel then
term.setBackgroundColor(bghigh)
term.write(" > ")
else term.write(" - ") end
term.write(v .. " ")
end
end
local function updateDisplayList(items, loc, len)
local ret = {}
for i = 1, len do
local item = items[i + loc - 1]
if item then table.insert(ret, item) end
end
return ret
end
-- Variables
local sel = 1
local loc = 1
local len = 3
local disList = updateDisplayList(list, loc, len)
draw(disList, sel, loc)
-- Loop
while true do
local e, key, x, y = os.pullEvent()
if e == "mouse_click" then
for i, v in ipairs(disList) do
if x >= 3 and x <= w - 11 and y >= i * 4 + 3 and y <= i * 4 + 5 then return v end
end
elseif e == "key" and key == 200 then
if sel > 1 then
sel = sel - 1
draw(disList, sel, loc)
elseif loc > 1 then
loc = loc - 1
disList = updateDisplayList(list, loc, len)
draw(disList, sel, loc)
end
elseif e == "key" and key == 208 then
if sel < len then
sel = sel + 1
draw(disList, sel, loc)
elseif loc + len - 1 < #list then
loc = loc + 1
disList = updateDisplayList(list, loc, len)
draw(disList, sel, loc)
end
elseif e == "mouse_scroll" then
os.queueEvent("key", key == -1 and 200 or 208)
elseif e == "key" and key == 28 then
return disList[sel]
end
end
end
function monitorKeyboardShortcuts()
local ta, tb = nil, nil
local allowChar = false
local shiftPressed = false
while true do
local event, char = os.pullEvent()
if event == "key" and (char == 42 or char == 52) then
shiftPressed = true
tb = os.startTimer(keyboardShortcutTimeout)
elseif event == "key" and (char == 29 or char == 157 or char == 219 or char == 220) then
allowEditorEvent = false
allowChar = true
ta = os.startTimer(keyboardShortcutTimeout)
elseif event == "key" and allowChar then
local name = nil
for k, v in pairs(keys) do
if v == char then
if shiftPressed then os.queueEvent("shortcut", "ctrl shift", k:lower())
else os.queueEvent("shortcut", "ctrl", k:lower()) end
sleep(0.005)
allowEditorEvent = true
end
end
if shiftPressed then os.queueEvent("shortcut", "ctrl shift", char)
else os.queueEvent("shortcut", "ctrl", char) end
elseif event == "timer" and char == ta then
allowEditorEvent = true
allowChar = false
elseif event == "timer" and char == tb then
shiftPressed = false
end
end
end
-- -------- Saving and Loading
local function download(url, path)
for i = 1, 3 do
local response = http.get(url)
if response then
local data = response.readAll()
response.close()
if path then
local f = io.open(path, "w")
f:write(data)
f:close()
end
return true
end
end
return false
end
local function saveFile(path, lines)
local dir = path:sub(1, path:len() - fs.getName(path):len())
if not fs.exists(dir) then fs.makeDir(dir) end
if not fs.isDir(path) and not fs.isReadOnly(path) then
local a = ""
for _, v in pairs(lines) do a = a .. v .. "\n" end
local f = io.open(path, "w")
f:write(a)
f:close()
return true
else return false end
end
local function loadFile(path)
if not fs.exists(path) then
local dir = path:sub(1, path:len() - fs.getName(path):len())
if not fs.exists(dir) then fs.makeDir(dir) end
local f = io.open(path, "w")
f:write("")
f:close()
end
local l = {}
if fs.exists(path) and not fs.isDir(path) then
local f = io.open(path, "r")
if f then
local a = f:read("*l")
while a do
table.insert(l, a)
a = f:read("*l")
end
f:close()
end
else return nil end
if #l < 1 then table.insert(l, "") end
return l
end
-- -------- Languages
languages.lua = {}
languages.brainfuck = {}
languages.none = {}
-- Lua
languages.lua.helpTips = {
"A function you tried to call doesn't exist.",
"You made a typo.",
"The index of an array is nil.",
"The wrong variable type was passed.",
"A function/variable doesn't exist.",
"You missed an 'end'.",
"You missed a 'then'.",
"You declared a variable incorrectly.",
"One of your variables is mysteriously nil."
}
languages.lua.defaultHelpTips = {
2, 5
}
languages.lua.errors = {
["Attempt to call nil."] = {1, 2},
["Attempt to index nil."] = {3, 2},
[".+ expected, got .+"] = {4, 2, 9},
["'end' expected"] = {6, 2},
["'then' expected"] = {7, 2},
["'=' expected"] = {8, 2}
}
languages.lua.keywords = {
["and"] = "conditional",
["break"] = "conditional",
["do"] = "conditional",
["else"] = "conditional",
["elseif"] = "conditional",
["end"] = "conditional",
["for"] = "conditional",
["function"] = "conditional",
["if"] = "conditional",
["in"] = "conditional",
["local"] = "conditional",
["not"] = "conditional",
["or"] = "conditional",
["repeat"] = "conditional",
["return"] = "conditional",
["then"] = "conditional",
["until"] = "conditional",
["while"] = "conditional",
["true"] = "constant",
["false"] = "constant",
["nil"] = "constant",
["print"] = "function",
["write"] = "function",
["sleep"] = "function",
["pairs"] = "function",
["ipairs"] = "function",
["loadstring"] = "function",
["loadfile"] = "function",
["dofile"] = "function",
["rawset"] = "function",
["rawget"] = "function",
["setfenv"] = "function",
["getfenv"] = "function",
}
languages.lua.parseError = function(e)
local ret = {filename = "unknown", line = -1, display = "Unknown!", err = ""}
if e and e ~= "" then
ret.err = e
if e:find(":") then
ret.filename = e:sub(1, e:find(":") - 1):gsub("^%s*(.-)%s*$", "%1")
-- The "" is needed to circumvent a CC bug
e = (e:sub(e:find(":") + 1) .. ""):gsub("^%s*(.-)%s*$", "%1")
if e:find(":") then
ret.line = e:sub(1, e:find(":") - 1)
e = e:sub(e:find(":") + 2):gsub("^%s*(.-)%s*$", "%1") .. ""
end
end
ret.display = e:sub(1, 1):upper() .. e:sub(2, -1) .. "."
end
return ret
end
languages.lua.getCompilerErrors = function(code)
code = "local function ee65da6af1cb6f63fee9a081246f2fd92b36ef2(...)\n\n" .. code .. "\n\nend"
local fn, err = loadstring(code)
if not err then
local _, e = pcall(fn)
if e then err = e end
end
if err then
local a = err:find("]", 1, true)
if a then err = "string" .. err:sub(a + 1, -1) end
local ret = languages.lua.parseError(err)
if tonumber(ret.line) then ret.line = tonumber(ret.line) end
return ret
else return languages.lua.parseError(nil) end
end
languages.lua.run = function(path, ar)
local fn, err = loadfile(path)
setfenv(fn, getfenv())
if not err then
_, err = pcall(function() fn(unpack(ar)) end)
end
return err
end
-- Brainfuck
languages.brainfuck.helpTips = {
"Well idk...",
"Isn't this the whole point of the language?",
"Ya know... Not being able to debug it?",
"You made a typo."
}
languages.brainfuck.defaultHelpTips = {
1, 2, 3
}
languages.brainfuck.errors = {
["No matching '['"] = {1, 2, 3, 4}
}
languages.brainfuck.keywords = {}
languages.brainfuck.parseError = function(e)
local ret = {filename = "unknown", line = -1, display = "Unknown!", err = ""}
if e and e ~= "" then
ret.err = e
ret.line = e:sub(1, e:find(":") - 1)
e = e:sub(e:find(":") + 2):gsub("^%s*(.-)%s*$", "%1") .. ""
ret.display = e:sub(1, 1):upper() .. e:sub(2, -1) .. "."
end
return ret
end
languages.brainfuck.mapLoops = function(code)
-- Map loops
local loopLocations = {}
local loc = 1
local line = 1
for let in string.gmatch(code, ".") do
if let == "[" then
loopLocations[loc] = true
elseif let == "]" then
local found = false
for i = loc, 1, -1 do
if loopLocations[i] == true then
loopLocations[i] = loc
found = true
end
end
if not found then
return line .. ": No matching '['"
end
end
if let == "\n" then line = line + 1 end
loc = loc + 1
end
return loopLocations
end
languages.brainfuck.getCompilerErrors = function(code)
local a = languages.brainfuck.mapLoops(code)
if type(a) == "string" then return languages.brainfuck.parseError(a)
else return languages.brainfuck.parseError(nil) end
end
languages.brainfuck.run = function(path)
-- Read from file
local f = io.open(path, "r")
local content = f:read("*a")
f:close()
-- Define environment
local dataCells = {}
local dataPointer = 1
local instructionPointer = 1
-- Map loops
local loopLocations = languages.brainfuck.mapLoops(content)
if type(loopLocations) == "string" then return loopLocations end
-- Execute code
while true do
local let = content:sub(instructionPointer, instructionPointer)
if let == ">" then
dataPointer = dataPointer + 1
if not dataCells[tostring(dataPointer)] then dataCells[tostring(dataPointer)] = 0 end
elseif let == "<" then
if not dataCells[tostring(dataPointer)] then dataCells[tostring(dataPointer)] = 0 end
dataPointer = dataPointer - 1
if not dataCells[tostring(dataPointer)] then dataCells[tostring(dataPointer)] = 0 end
elseif let == "+" then
if not dataCells[tostring(dataPointer)] then dataCells[tostring(dataPointer)] = 0 end
dataCells[tostring(dataPointer)] = dataCells[tostring(dataPointer)] + 1
elseif let == "-" then
if not dataCells[tostring(dataPointer)] then dataCells[tostring(dataPointer)] = 0 end
dataCells[tostring(dataPointer)] = dataCells[tostring(dataPointer)] - 1
elseif let == "." then
if not dataCells[tostring(dataPointer)] then dataCells[tostring(dataPointer)] = 0 end
if term.getCursorPos() >= w then print("") end
write(string.char(math.max(1, dataCells[tostring(dataPointer)])))
elseif let == "," then
if not dataCells[tostring(dataPointer)] then dataCells[tostring(dataPointer)] = 0 end
term.setCursorBlink(true)
local e, but = os.pullEvent("char")
term.setCursorBlink(false)
dataCells[tostring(dataPointer)] = string.byte(but)
if term.getCursorPos() >= w then print("") end
write(but)
elseif let == "/" then
if not dataCells[tostring(dataPointer)] then dataCells[tostring(dataPointer)] = 0 end
if term.getCursorPos() >= w then print("") end
write(dataCells[tostring(dataPointer)])
elseif let == "[" then
if dataCells[tostring(dataPointer)] == 0 then
for k, v in pairs(loopLocations) do
if k == instructionPointer then instructionPointer = v end
end
end
elseif let == "]" then
for k, v in pairs(loopLocations) do
if v == instructionPointer then instructionPointer = k - 1 end
end
end
instructionPointer = instructionPointer + 1
if instructionPointer > content:len() then print("") break end
end
end
-- None
languages.none.helpTips = {}
languages.none.defaultHelpTips = {}
languages.none.errors = {}
languages.none.keywords = {}
languages.none.parseError = function(err)
return {filename = "", line = -1, display = "", err = ""}
end
languages.none.getCompilerErrors = function(code)
return languages.none.parseError(nil)
end
languages.none.run = function(path) end
-- Load language
curLanguage = languages.lua
-- -------- Run GUI
local function viewErrorHelp(e)
title("LuaIDE - Error Help")
local tips = nil
for k, v in pairs(curLanguage.errors) do
if e.display:find(k) then tips = v break end
end
term.setBackgroundColor(colors[theme.err])
for i = 6, 8 do
term.setCursorPos(5, i)
term.write(string.rep(" ", 35))
end
term.setBackgroundColor(colors[theme.prompt])
for i = 10, 18 do
term.setCursorPos(5, i)
term.write(string.rep(" ", 46))
end
if tips then
term.setBackgroundColor(colors[theme.err])
term.setCursorPos(6, 7)
term.write("Error Help")
term.setBackgroundColor(colors[theme.prompt])
for i, v in ipairs(tips) do
term.setCursorPos(7, i + 10)
term.write("- " .. curLanguage.helpTips[v])
end
else
term.setBackgroundColor(colors[theme.err])
term.setCursorPos(6, 7)
term.write("No Error Tips Available!")
term.setBackgroundColor(colors[theme.prompt])
term.setCursorPos(6, 11)
term.write("There are no error tips available, but")
term.setCursorPos(6, 12)
term.write("you could see if it was any of these:")
for i, v in ipairs(curLanguage.defaultHelpTips) do
term.setCursorPos(7, i + 12)
term.write("- " .. curLanguage.helpTips[v])
end
end
prompt({{"Back", w - 8, 7}}, "horizontal")
end
local function run(path, lines, useArgs)
local ar = {}
if useArgs then
title("LuaIDE - Run " .. fs.getName(path))
local s = centerRead(w - 13, fs.getName(path) .. " ")
for m in string.gmatch(s, "[^ \t]+") do ar[#ar + 1] = m:gsub("^%s*(.-)%s*$", "%1") end
end
saveFile(path, lines)
term.setCursorBlink(false)
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
term.clear()
term.setCursorPos(1, 1)
local err = curLanguage.run(path, ar)
term.setBackgroundColor(colors.black)
print("\n")
if err then
if isAdvanced() then term.setTextColor(colors.red) end
centerPrint("The program has crashed!")
end
term.setTextColor(colors.white)
centerPrint("Press any key to return to LuaIDE...")
while true do
local e = os.pullEvent()
if e == "key" then break end
end
-- To prevent key from showing up in editor
os.queueEvent(event_distract)
os.pullEvent()
if err then
if curLanguage == languages.lua and err:find("]") then
err = fs.getName(path) .. err:sub(err:find("]", 1, true) + 1, -1)
end
while true do
title("LuaIDE - Error!")
term.setBackgroundColor(colors[theme.err])
for i = 6, 8 do
term.setCursorPos(3, i)
term.write(string.rep(" ", w - 5))
end
term.setCursorPos(4, 7)
term.write("The program has crashed!")
term.setBackgroundColor(colors[theme.prompt])
for i = 10, 14 do
term.setCursorPos(3, i)
term.write(string.rep(" ", w - 5))
end
local formattedErr = curLanguage.parseError(err)
term.setCursorPos(4, 11)
term.write("Line: " .. formattedErr.line)
term.setCursorPos(4, 12)
term.write("Error:")
term.setCursorPos(5, 13)
local a = formattedErr.display
local b = nil
if a:len() > w - 8 then
for i = a:len(), 1, -1 do
if a:sub(i, i) == " " then
b = a:sub(i + 1, -1)
a = a:sub(1, i)
break
end
end
end
term.write(a)
if b then
term.setCursorPos(5, 14)
term.write(b)
end
local opt = prompt({{"Error Help", w/2 - 15, 17}, {"Go To Line", w/2 + 2, 17}},
"horizontal")
if opt == "Error Help" then
viewErrorHelp(formattedErr)
elseif opt == "Go To Line" then
-- To prevent key from showing up in editor
os.queueEvent(event_distract)
os.pullEvent()
return "go to", tonumber(formattedErr.line)
end
end
end
end
-- -------- Functions
local function goto()
term.setBackgroundColor(colors[theme.backgroundHighlight])
term.setCursorPos(2, 1)
term.clearLine()
term.write("Line: ")
local line = modRead({visibleLength = w - 2})
local num = tonumber(line)
if num and num > 0 then return num
else
term.setCursorPos(2, 1)
term.clearLine()
term.write("Not a line number!")
sleep(1.6)
return nil
end
end
local function setsyntax()
local opts = {
"[Lua] Brainfuck None ",
" Lua [Brainfuck] None ",
" Lua Brainfuck [None]"
}
local sel = 1
term.setCursorBlink(false)
term.setBackgroundColor(colors[theme.backgroundHighlight])