Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local lastlog = ""
- local interactions = {}
- local selected = nil
- local vars = {}
- local funcs = {}
- local startup = {}
- local oevent = {}
- local updte = {}
- local cols = {}
- local on = true
- local blk = true
- local function setPixel(x, y, color)
- local oldx, oldy = term.getCursorPos()
- term.setCursorPos(x, y)
- term.setBackgroundColor(color)
- term.setCursorPos(oldx, oldy)
- if cols[x] == nil then
- cols[x] = {}
- end
- cols[x][y] = color
- end
- local function setPixelText(x, y, color)
- local oldx, oldy = term.getCursorPos()
- term.setCursorPos(x, y)
- term.setTextColor(color)
- term.setCursorPos(oldx, oldy)
- if cols[x] == nil then
- cols[x] = {}
- end
- cols[x][y] = color
- end
- local function getSetColour(x, y)
- local color = colors.black
- if cols[x] ~= nil then
- local xtab = cols[x]
- if xtab[y] ~= nil then
- color = xtab[y]
- end
- end
- return color
- end
- local function getColour(color)
- color = string.lower(color)
- if color == "white" then
- return colors.white
- elseif color == "orange" then
- return colors.orange
- elseif color == "magenta" then
- return colors.magenta
- elseif color == "lightblue" then
- return colors.lightblue
- elseif color == "yellow" then
- return colors.yellow
- elseif color == "lime" then
- return colors.lime
- elseif color == "pink" then
- return colors.pink
- elseif color == "gray" then
- return colors.gray
- elseif color == "lightgray" then
- return colors.lightgray
- elseif color == "cyan" then
- return colors.cyan
- elseif color == "purple" then
- return colors.purple
- elseif color == "blue" then
- return colors.blue
- elseif color == "brown" then
- return colors.brown
- elseif color == "green" then
- return colors.green
- elseif color == "red" then
- return colors.red
- elseif color == "black" then
- return colors.black
- else
- return colors.white
- end
- end
- local function containsKey(tab, key)
- for i=1,#tab do
- local var = tab[i]
- if var[1] == key then
- return true
- end
- end
- return false
- end
- local function setValue(tab, key, value)
- for i=1,#tab do
- local var = tab[i]
- if var[1] == key then
- var[2] = value
- end
- end
- end
- local function getValue(tab, key)
- for i=1,#tab do
- local var = tab[i]
- if var[1] == key then
- return var[2]
- end
- end
- return nil
- end
- local function putValue(tab, key, value)
- tab[#tab + 1] = {key, value}
- end
- local function delValue(tab, key)
- local nval = {}
- for i=1,#tab do
- local var = tab[i]
- if var[1] == key then else
- nval[#nval + 1] = var
- end
- end
- vars = nval
- end
- local function split(str, pat)
- local t = {} -- NOTE: use {n = 0} in Lua-5.0
- if str ~= nil then
- local fpat = "(.-)" .. pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- else
- print("##3DD ERROR failed to split ["..str.."] by:"..pat)
- end
- return t
- end
- local function setLog(lg)
- lastlog = lg
- term.setCursorPos(1,1)
- term.write(" [ERROR: "..lg.."]")
- end
- local function getStr(v)
- local value = v
- if string.sub(value, 1, 1) == '"' then
- if string.sub(value, -1, -1) == '"' then
- if string.len(value) > 2 then
- value = string.sub(value, 2, -2)
- else
- value = ""
- end
- else
- return {false, "UNF STRING"}
- end
- end
- return {true, value}
- end
- local function getInt(v)
- local value = v
- if string.sub(value, 1, 1) == "#" then
- if string.len(value) > 1 then
- local sn = string.sub(value, 2, -1)
- if string.sub(sn, 1, 3) == "RND" then
- if string.len(sn) > 3 then
- local splt = split(sn, "~")
- if #splt == 2 then
- local v1 = tonumber(splt[2])
- if v1 ~= nil then
- value = math.random(v1)
- else
- return {false, "INVALID RND"}
- end
- elseif #splt == 3 then
- local v1 = tonumber(splt[2])
- local v2 = tonumber(splt[3])
- if v1 ~= nil and v2 ~= nil then
- value = math.random(v1, v2)
- else
- return {false, "INVALID RND"}
- end
- else
- return {false, "INVALID RND"}
- end
- else
- value = math.random()
- end
- elseif tonumber(sn) ~= nil then
- value = tonumber(sn)
- else
- return {false, "INVALID VALUE"}
- end
- else
- return {false, "NO INT GIVEN"}
- end
- end
- return {true, value}
- end
- local function getOVal(v)
- local value = v
- if string.sub(value, 1, 1) == '$' then
- if string.len(value) > 1 then
- local nv = string.sub(value, 2, -1)
- if containsKey(vars, nv) then
- value = getValue(vars, nv)
- else
- return {false, "NONEXISTANT VAR"}
- end
- else
- return {false, "NO VALUE GIVEN"}
- end
- end
- return {true, value}
- end
- local function getTabl(v)
- local value = string.lower(v)
- if string.sub(value, 1, 1) == "<" and string.sub(value, 1, 1) == ">" then
- local splt = split(value, "|")
- local tabl = {}
- for i=1,#splt do
- tabl[#tabl + 1] = getVal(splt[i])
- end
- value = tabl
- end
- return value
- end
- local function getBool(v)
- local value = string.lower(v)
- if string.sub(value, 1, 1) == "%" then
- local yn = string.sub(2,-1)
- if yn == "y" or yn == "yes" or yn == "t" or yn == "true" or yn == "1" then
- value = true
- elseif yn == "n" or yn == "no" or yn == "f" or yn == "false" or yn == "0" then
- value = false
- else
- return {false, "INVALID BOOLEAN"}
- end
- end
- return {true, value}
- end
- function getVal(v)
- local value = v
- local done = false
- local vres1 = getStr(value)
- if vres1[1] then
- local oval = value
- value = vres1[2]
- if oval ~= value then done = true end
- else return {false, vres1[2]} end
- if not done then
- local vres2 = getInt(value)
- if vres2[1] then
- local oval = value
- value = vres2[2]
- if oval ~= value then done = true end
- else return {false, vres2[2]} end
- end
- if not done then
- local vres3 = getOVal(value)
- if vres3[1] then
- local oval = value
- value = vres3[2]
- if oval ~= value then done = true end
- else return {false, vres3[2]} end
- end
- if not done then
- local vres3 = getBool(value)
- if vres3[1] then
- local oval = value
- value = vres3[2]
- if oval ~= value then done = true end
- else return {false, vres3[2]} end
- end
- return {true, value}
- end
- local function doFunc(ds, dn)
- if containsKey(ds, dn) then
- local code = getValue(ds, dn)
- if not runC(code, 0, nil) then return {false, lastlog} end
- else
- return {false, "NONEXISTANT CODE "..(string.upper(dn).." ")}
- end
- return {true, ""}
- end
- local function ifFunc(ds, splt, is)
- local vals = {}
- local curi = 2
- local ison = true
- while splt[curi] ~= "DO" and ison do
- local val1 = nil
- local vtab1 = getVal(splt[curi])
- if vtab1[1] then val1 = vtab1[2] else return {false, vtab1[2]} end
- vals[#vals + 1] = val1
- curi = curi + 1
- if #splt < curi then ison = false end
- end
- if not ison then return {false, "NO END OF IF"} end
- if #vals < 2 then return {false, "NOT ENOUGH VALS"} end
- local isok = true
- for j=1,#vals do
- local k = j+1
- if k > #vals then k = 1 end
- if is then
- if vals[j] == vals[k] then else isok = false end
- else
- if vals[j] ~= vals[k] then else isok = false end
- end
- end
- if isok then
- local dn = splt[ #splt ]
- local result = doFunc(ds, dn)
- if not result[1] then return {false, result[2]} else return {true, true} end
- else
- return {true, false}
- end
- end
- local function doMath(val1, val2, sep)
- local eval = val1
- if sep == "+" then
- eval = val1 + val2
- elseif sep == "-" then
- eval = val1 - val2
- elseif sep == "/" then
- eval = val1 / val2
- elseif sep == "*" then
- eval = val1 * val2
- elseif sep == "^" then
- eval = val1 ^ val2
- end
- return eval
- end
- local function runCode(code, args)
- local parts = code
- local ds = {}
- local kon = false
- local cdn = nil
- local cd = {}
- local curf = {}
- for i=1,#parts do
- local part = parts[i]
- local splt = split(part, " ")
- if string.sub(part, 1, 2) == "##" or string.gsub(part, " ", "") == "" or part == "" then else
- if not kon then
- local key = splt[1]
- if key == "DEF" then
- local name = splt[2]
- local value = splt[3]
- if string.sub(value, 1, 1) == '"' and #splt > 3 then
- for j=4,#splt do
- value = value.." "..splt[j]
- end
- end
- local vtab = getVal(value)
- if vtab[1] then value = vtab[2] else return {vtab[2], i} end
- if containsKey(vars, name) then
- setValue(vars, name, value)
- else
- putValue(vars, name, value)
- end
- elseif key == "DEL" then
- local name = splt[2]
- if containsKey(vars, name) then
- delValue(vars, name)
- else
- return {"NONEXISTANT VAR", i}
- end
- elseif key == "SET" then
- local name = splt[2]
- local value = splt[3]
- if string.sub(value, 1, 1) == '"' and #splt > 3 then
- for j=4,#splt do
- value = value.." "..splt[j]
- end
- end
- local vtab = getVal(value)
- if vtab[1] then value = vtab[2] else return {vtab[2], i} end
- if containsKey(vars, name) then
- setValue(vars, name, value)
- else
- return {"NONEXISTANT VAR", i}
- end
- elseif key == "API" then
- local dir = splt[2]
- if #splt > 2 then
- for i=3,#splt do
- dir = dir.." "..splt[i]
- end
- end
- if fs.exists(dir) then
- loadMenu(dir)
- else
- return {"NONEXISTANT API FILE", i}
- end
- elseif key == "PRS" then
- local name = splt[2]
- if containsKey(funcs, name) then
- local func = getValue(funcs, name)
- if not runC(func, i) then return {"ERROR IN CODE", i} end
- else
- return {"NONEXISTANT FUNC", i}
- end
- elseif key == "RUN" then
- local name = splt[2]
- if #splt > 2 then
- local args = {}
- for i=3,#splt do
- args[#args + 1] = splt[i]
- end
- shell.run(name, args)
- else
- shell.run(name)
- end
- elseif key == "RUNLN" then
- local value = splt[2]
- if string.sub(value, 1, 1) == '"' and #splt > 3 then
- for j=4,#splt do
- value = value.." "..splt[j]
- end
- end
- local vtab = getVal(value)
- if vtab[1] then value = vtab[2] else return {vtab[2], i} end
- if not runC({value}, i) then return {"ERROR IN CODE LINE", i} end
- elseif key == "DO" then
- local dn = splt[2]
- if containsKey(ds, dn) then
- local code = getValue(ds, dn)
- if not runC(code, i) then return {"ERROR IN CODE", i} end
- else
- return {"NONEXISTANT CODE", i}
- end
- elseif key == "IF" then
- local result = ifFunc(ds, splt, true)
- if not result[1] then return {result[2], i} end
- elseif key == "IFNOT" then
- local result = ifFunc(ds, splt, false)
- if not result[1] then return {result[2], i} end
- elseif key == "FOR" then
- local val1 = nil
- local vtab1 = getVal(splt[2])
- if vtab1[1] then val1 = vtab1[2] else return {vtab1[2], i} end
- local val2 = nil
- local vtab2 = getVal(splt[3])
- if vtab2[1] then val2 = vtab2[2] else return {vtab2[2], i} end
- local val3 = nil
- local vtab3 = getVal(splt[4])
- if vtab3[1] then val3 = vtab3[2] else return {vtab3[2], i} end
- local cdname = splt[5]
- if val1 ~= nil and val2 ~= nil and val3 ~= nil then
- for i=val1,val2,val3 do
- local result = doFunc(ds, cdname)
- if not result[1] then return {result[2], i} end
- end
- else
- return {"FOUND NULL", i}
- end
- elseif key == "WHILE" then
- local result = ifFunc(ds, splt, true)
- local canGo = true
- if not result[1] then
- canGo = false
- return {result[2], i}
- else
- canGo = result[2]
- end
- while canGo do
- local result = ifFunc(ds, splt)
- local canGo = true
- if not result[1] then
- canGo = false
- return {result[2], i}
- else
- canGo = result[2]
- end
- end
- elseif key == "ART" then
- if term.isColor() then
- local cmd = splt[2]
- color = getColour(color)
- if cmd == "BACKGROUND" then
- local color = splt[3]
- local vtab = getVal(color)
- if vtab1[1] then color = vtab1[2] else return {vtab1[2], i} end
- local x = splt[4]
- local y = splt[5]
- local vtab1 = getVal(x)
- if vtab1[1] then x = vtab1[2] else return {vtab1[2], i} end
- local vtab2 = getVal(y)
- if vtab1[1] then y = vtab1[2] else return {vtab1[2], i} end
- setPixel(x, y, color)
- elseif cmd == "TEXT" then
- local color = splt[3]
- local x = splt[4]
- local y = splt[5]
- local vtab1 = getVal(x)
- if vtab1[1] then x = vtab1[2] else return {vtab1[2], i} end
- local vtab1 = getVal(y)
- if vtab1[1] then y = vtab1[2] else return {vtab1[2], i} end
- setTextPixel(x, y, color)
- elseif cmd == "COLOR" or cmd == "COLOUR" then
- local result = splt[3]
- local color = splt[4]
- if containsKey(vars, result) then
- setValue(vars, result, getColour(color))
- else
- return {"NONEXISTANT VAR", i}
- end
- elseif cmd == "GET" then
- local result = splt[3]
- local x = splt[4]
- local y = splt[5]
- local vtab1 = getVal(x)
- if vtab1[1] then x = vtab1[2] else return {vtab1[2], i} end
- local vtab1 = getVal(y)
- if vtab1[1] then y = vtab1[2] else return {vtab1[2], i} end
- if containsKey(vars, result) then
- setValue(vars, result, getSetColour(x, y))
- else
- return {"NONEXISTANT VAR", i}
- end
- end
- else
- return {"NON-COLOR COMPUTER", i}
- end
- elseif key == "WHILENOT" then
- local result = ifFunc(ds, splt, false)
- local canGo = true
- if not result[1] then
- canGo = false
- return {result[2], i}
- else
- canGo = result[2]
- end
- while canGo do
- local result = ifFunc(ds, splt)
- local canGo = true
- if not result[1] then
- canGo = false
- return {result[2], i}
- else
- canGo = result[2]
- end
- end
- elseif key == "TABLE" then
- local cmd = splt[2]
- local name = splt[3]
- if containsKey(vars, name) then
- local tabl = getValue(vars, name)
- if cmd == "LEN" then
- local setto = splt[4]
- if containsKey(vars, setto) then
- setValue(vars, setto, #tabl)
- else
- putValue(vars, setto, #tabl)
- end
- else
- local index = splt[4]
- local vtab1 = getVal(index)
- if vtab1[1] then index = vtab1[2] else return {vtab1[2], i} end
- if cmd == "SET" then
- local setto = splt[5]
- local vtab1 = getVal(setto)
- if vtab1[1] then setto = vtab1[2] else return {vtab1[2], i} end
- if string.sub(setto, 1, 1) == '"' and #splt > 3 then
- for j=4,#splt do
- setto = setto.." "..splt[j]
- end
- end
- tabl[index] = setto
- elseif cmd == "GET" then
- local setto = splt[5]
- if containsKey(vars, setto) then
- setValue(vars, setto, tabl[index])
- else
- return {"NONEXISTANT VAR", i}
- end
- end
- end
- else
- return {"NONEXISTANT TABLE", i}
- end
- elseif key == "EVENT" then
- local cmd = splt[2]
- local varname = splt[3]
- if containsKey(vars, varname) then
- if cmd == "TYPE" then
- setValue(vars, varname, args[1])
- elseif cmd == "VAR" then
- local n = tonumber(splt[4])
- setValue(vars, varname, args[n-1])
- else
- return {"INVALID EVENT COMMAND", i}
- end
- else
- return {"NONEXISTANT VARIABLE", i}
- end
- elseif key == "MATH" then
- local name = splt[2]
- local mvls = {}
- local mms = {}
- if #splt > 4 then
- local num = true
- for i=3,#splt do
- if num then
- local val1 = nil
- local vtab1 = getVal(splt[i])
- if vtab1[1] then val1 = vtab1[2] else return {vtab1[2], i} end
- if val1 ~= nil then
- if type(val1) == "number" then
- mvls[#mvls + 1] = val1
- else
- return {"NON-NUMBER FOUND", i}
- end
- else
- return {"NULL FOUND", i}
- end
- num = false
- else
- mms[#mms + 1] = splt[i]
- num = true
- end
- end
- local curv = mvls[1]
- local curs = 1
- for i=2,#mvls do
- curv = doMath(curv, mvls[i], mms[curs])
- curs = curs + 1
- end
- if containsKey(vars, name) then
- setValue(vars, name, curv)
- else
- return {"NONEXISTANT VAR", i}
- end
- else
- return {"INVALID MATH", i}
- end
- elseif key == "TERM" then
- local cmd = splt[2]
- if cmd == "PRINT" then
- local value = splt[3]
- if string.sub(value, 1, 1) == '"' and #splt > 3 then
- for j=4,#splt do
- value = value.." "..splt[j]
- end
- end
- local vtab = getVal(value)
- if vtab[1] then value = vtab[2] else return {vtab[2], i} end
- print(value)
- elseif cmd == "SIZE" then
- local x = splt[3]
- local y = splt[4]
- if containsKey(vars, x) and containsKey(vars, y) then
- local tx, ty = term.getSize()
- setValue(vars, x, tx)
- setValue(vars, y, ty)
- else
- return {"NONEXISTANT VAR(S)", i}
- end
- elseif cmd == "RESTORE" then
- term.restore()
- elseif cmd == "CLEARLN" then
- term.clearln()
- elseif cmd == "BLINK" then
- if blk then blk = false else blk = true end
- term.setCursorBlink(b)
- elseif cmd == "GETBLINK" then
- local value = splt[3]
- if containsKey(vars, value) then
- setValue(vars, balue, blk)
- else
- return {"NONEXISTANT VAR", i}
- end
- elseif cmd == "SCROLL" then
- local n = splt[3]
- local vtab = getVal(n)
- if vtab[1] then n = vtab[2] else return {vtab[2], i} end
- if type(n) == "number" then
- term.scroll(n)
- else
- return {"NON-NUMBER FOUND", i}
- end
- elseif cmd == "REDNET" then
- local rcmd = splt[3]
- if rcmd == "OPEN" then
- local side = splt[4]
- local vtab = getVal(side)
- if vtab[1] then side = vtab[2] else return {vtab[2], i} end
- rednet.open(side)
- elseif rcmd == "CLOSE" then
- local side = splt[4]
- local vtab = getVal(side)
- if vtab[1] then side = vtab[2] else return {vtab[2], i} end
- rednet.close(side)
- elseif rcmd == "ANC" then
- rednet.announce()
- elseif rcmd == "SEND" then
- local id = splt[4]
- local vtab = getVal(id)
- if vtab[1] then id = vtab[2] else return {vtab[2], i} end
- local message = splt[5]
- if string.sub(message, 1, 1) == '"' and #splt > 5 then
- for j=6,#splt do
- message =message.." "..splt[j]
- end
- end
- local vtab = getVal(message)
- if vtab[1] then message = vtab[2] else return {vtab[2], i} end
- rednet.send(id, message)
- elseif rcmd == "CAST" then
- local message = splt[4]
- if string.sub(message, 1, 1) == '"' and #splt > 4 then
- for j=5,#splt do
- message =message.." "..splt[j]
- end
- end
- local vtab = getVal(message)
- if vtab[1] then message = vtab[2] else return {vtab[2], i} end
- rednet.broadcast(message)
- elseif rcmd == "RECIEVE" then
- local timeout = splt[4]
- local vtab = getVal(timeout)
- if vtab[1] then timeout = vtab[2] else return {vtab[2], i} end
- local ntabl = splt[5]
- if containsKey(vars, tabl) then
- local tabl = getValue(vars, ntabl)
- if type(tabl) == "table" then
- local sid, message, dist = rednet.recieve(timeout)
- if sid == nil and message == nil and dist == nil then
- setValue(vars, ntabl, {sid, message, dist})
- end
- else
- return {"NON-TABLE FOUND", i}
- end
- else
- return {"NONEXISTANT TABLE", i}
- end
- end
- elseif cmd == "STOP" then
- on = false
- elseif cmd == "SLEEP" then
- local value = splt[3]
- local vtab = getVal(value)
- if vtab[1] then value = vtab[2] else return {vtab[2], i} end
- if type(value) == "number" then
- sleep(value)
- else
- return {"NONUMBER GIVEN", i}
- end
- elseif cmd == "TIMER" then
- local value = splt[3]
- local vtab = getVal(value)
- if vtab[1] then value = vtab[2] else return {vtab[2], i} end
- if type(value) == "number" then
- os.startTimer(value)
- else
- return {"NONUMBER GIVEN", i}
- end
- elseif cmd == "WRITE" then
- local value = splt[3]
- if string.sub(value, 1, 1) == '"' and #splt > 3 then
- for j=4,#splt do
- value = value.." "..splt[j]
- end
- end
- local vtab = getVal(value)
- if vtab[1] then value = vtab[2] else return {vtab[2], i} end
- term.write(value)
- elseif cmd == "SET" then
- local val1 = nil
- local vtab1 = getVal(splt[3])
- if vtab1[1] then val1 = vtab1[2] else return {vtab1[2], i} end
- local val2 = nil
- local vtab2 = getVal(splt[4])
- if vtab2[1] then val2 = vtab2[2] else return {vtab2[2], i} end
- if type(val1) == "number" and type(val2) == "number" then
- term.setCursorPos(val1, val2)
- else
- return ({"NON-NUMBER FOUND", i})
- end
- elseif cmd == "CLEAR" then
- term.clear()
- end
- -- comp: [type] [text] [loc]
- -- Button: [code] [states] [state]
- elseif key == "COMP" then
- local cmd = splt[2]
- local varname = splt[3]
- if containsKey(vars, varname) or cmd == "SET" then
- if cmd == "TEXT" then
- setValue(vars, varname, args[1][2])
- elseif cmd == "TYPE" then
- setValue(vars, varname, args[1][1])
- elseif cmd == "X" then
- setValue(vars, varname, args[1][3][1])
- elseif cmd == "Y" then
- setValue(vars, varname, args[1][3][2])
- elseif cmd == "WIDTH" or cmd == "W" then
- setValue(vars, varname, args[1][3][3])
- elseif cmd == "HEIGHT" or cmd == "H" then
- setValue(vars, varname, args[1][3][4])
- elseif cmd == "SET" then
- local cm = varname
- if containsKey(vars, splt[4]) then
- local value = splt[4]
- if string.sub(value, 1, 1) == '"' and #splt > 3 then
- for j=4,#splt do
- value = value.." "..splt[j]
- end
- end
- local vtab = getVal(value)
- if vtab[1] then value = vtab[2] else return {vtab[2], i} end
- if cmd == "TEXT" then
- args[1][2] = value
- elseif cmd == "TYPE" then
- args[1][1] = value
- elseif cmd == "X" then
- args[1][3][1] = value
- elseif cmd == "Y" then
- args[1][3][2] = value
- elseif cmd == "WIDTH" or cmd == "W" then
- args[1][3][3] = value
- elseif cmd == "HEIGHT" or cmd == "H" then
- args[1][3][4] = value
- else
- return {"INVALID COMP CMD", i}
- end
- else
- return {"NONEXISTANT VAR", i}
- end
- else
- return {"INVALID COMP CMD", i}
- end
- else
- return {"NONEXISTANT VAR", i}
- end
- elseif key == "SETFUNC" then
- local name = splt[2]
- local cname = splt[3]
- if containsKey(ds, cname) then
- local code = getValue(ds, cname)
- if containsKey(funcs, name) then
- setValue(funcs, name, code)
- else
- putValue(funcs, name, code)
- end
- else
- return {"NONEXISTANT CODE", i}
- end
- elseif key == "STRING" then
- local cmd = splt[2]
- if cmd == "CONCAT" then
- local result = splt[3]
- local vals = {}
- for i=4,#splt do
- vals[#vals + 1] = splt[i]
- end
- local str = ""
- for i=1,#vals do
- local value = vals[i]
- local vtab = getVal(value)
- if vtab[1] then value = vtab[2] else return {vtab[2], i} end
- if type(value) == "string" then
- if i == 1 then
- str = value
- else
- str = str..value
- end
- end
- end
- if containsKey(vars, result) then
- setValue(vars, result, str)
- else
- putValue(vars, result, str)
- end
- elseif cmd == "CUT" then
- local result = splt[3]
- local from = splt[4]
- local to = splt[5]
- local vtab1 = getVal(from)
- if vtab1[1] then from = vtab1[2] else return {vtab1[2], i} end
- local vtab2 = getVal(to)
- if vtab2[1] then to = vtab2[2] else return {vtab2[2], i} end
- local value = splt[6]
- if string.sub(value, 1, 1) == '"' and #splt > 4 then
- for j=5,#splt do
- value = value.." "..splt[j]
- end
- end
- local vtab = getVal(value)
- if vtab[1] then value = vtab[2] else return {vtab[2], i} end
- local res = string.sub(value, from, to)
- if containsKey(vars, result) then
- setValue(vars, result, res)
- else
- return {"INVALID VAR", i}
- end
- elseif cmd == "RPL" then
- local result = splt[3]
- local from = splt[4]
- local to = splt[5]
- local vtab1 = getVal(from)
- if vtab1[1] then from = vtab1[2] else return {vtab1[2], i} end
- local vtab2 = getVal(to)
- if vtab2[1] then to = vtab2[2] else return {vtab2[2], i} end
- local value = splt[6]
- if string.sub(value, 1, 1) == '"' and #splt > 6 then
- for j=7,#splt do
- value = value.." "..splt[j]
- end
- end
- local vtab = getVal(value)
- if vtab[1] then value = vtab[2] else return {vtab[2], i} end
- local res = string.gsub(value, from, to)
- if containsKey(vars, result) then
- setValue(vars, result, res)
- else
- return {"INVALID VAR", i}
- end
- elseif cmd == "SPLIT" then
- local varname = splt[3]
- local spltat = splt[4]
- local vtab1 = getVal(spltat)
- if vtab1[1] then from = vtab1[2] else return {vtab1[2], i} end
- local value = splt[5]
- if string.sub(value, 1, 1) == '"' and #splt > 6 then
- for j=7,#splt do
- value = value.." "..splt[j]
- end
- end
- local vtab = getVal(value)
- if vtab[1] then value = vtab[2] else return {vtab[2], i} end
- if containsKey(vars, varname) then
- setValue(vars, varname, split(value, spltat))
- else
- putValue(vars, varname, split(value, spltat))
- end
- elseif cmd == "ISPLIT" then
- local result = splt[3]
- local spltat = splt[4]
- local index = splt[5]
- local vtab1 = getVal(spltat)
- if vtab1[1] then from = vtab1[2] else return {vtab1[2], i} end
- local vtab2 = getVal(index)
- if vtab2[1] then from = vtab2[2] else return {vtab2[2], i} end
- local value = splt[6]
- if string.sub(value, 1, 1) == '"' and #splt > 6 then
- for j=7,#splt do
- value = value.." "..splt[j]
- end
- end
- local vtab = getVal(value)
- if vtab[1] then value = vtab[2] else return {vtab[2], i} end
- local spl = split(value, spltat)
- local res = spl[index]
- if containsKey(vars, result) then
- setValue(vars, result, res)
- else
- return {"INVALID VAR", i}
- end
- else
- return {"INVALID STRING CMD", i}
- end
- elseif key == "IO" then
- local cmd = splt[2]
- local fname = splt[3]
- if cmd == "CREATE" then
- local f = fs.open(fname, "a")
- f.close()
- elseif cmd == "DELETE" then
- fs.delete(fname)
- elseif cmd == "OPEN" then
- local styp = splt[4]
- local typ = "a"
- if styp == "write" then
- typ = "a"
- elseif styp == "read" then
- typ = "r"
- else
- return {"INVALID FILE TYPE", i}
- end
- local fn = splt[5]
- if containsKey(curf, fn) then
- return {"FILE ALREADY EXISTS", i}
- else
- curf[#curf + 1] = {fn, fs.open(fname, typ)}
- end
- elseif cmd == "CLOSE" then
- if containsKey(curf, fname) then
- local f = getValue(curf, fname)
- f.close()
- delValue(curf, fname)
- else
- return {"NONEXISTANT FILE", i}
- end
- elseif cmd == "WRITE" then
- if containsKey(curf, fname) then
- local f = getValue(curf, fname)
- local value = splt[4]
- if string.sub(value, 1, 1) == '"' and #splt > 4 then
- for j=5,#splt do
- value = value.." "..splt[j]
- end
- end
- local vtab = getVal(value)
- if vtab[1] then value = vtab[2] else return {vtab[2], i} end
- f.writeLine(value)
- else
- return {"NONEXISTANT FILE", i}
- end
- elseif cmd == "READ" then
- if containsKey(curf, fname) then
- local f = getValue(curf, fname)
- local var1 = splt[4]
- if containsKey(vars, var1) then
- local ln = f.readLine()
- if ln ~= nil then
- setValue(vars, var1, ln)
- else
- setValue(vars, var1, false)
- end
- else
- return {"NONEXISTANT VAR", i}
- end
- else
- return {"NONEXISTANT FILE", i}
- end
- elseif cmd == "READF" then
- if containsKey(furc, fname) then
- local f = getValue(curf, fname)
- local var1 = splt[4]
- if containsKey(vars, var1) then
- local tabl = {}
- local ln = f.readLine()
- while ln ~= nil do
- tabl[#tabl + 1] = ln
- ln = f.readLine()
- end
- setValue(vars, var1, tabl)
- else
- return {"NONEXISTANT VAR", i}
- end
- end
- else
- return {"INVALID IO COMMAND", i}
- end
- elseif key == "*" then
- kon = true
- if #splt >= 2 then
- cdn = splt[2]
- cd = {}
- else
- return {"NO CODE NAME", i}
- end
- else
- return {"INVALID COMMAND", i}
- end
- else
- if splt[1] == "*" or part == "*" then
- kon = false
- ds[#ds + 1] = {cdn, cd}
- cd = {}
- cdn = nil
- else
- cd[#cd + 1] = part
- end
- end
- end
- end
- return nil
- end
- function runC(code, index, args)
- if args == nil then args = {"NONE"} end
- local er = runCode(code, args)
- if er ~= nil then
- setLog(er[1].."@"..index..";"..er[2])
- return false
- end
- return true
- end
- local function setPixel(x, y, str)
- local oldx, oldy = term.getCursorPos()
- term.setCursorPos(x, y)
- term.write(str)
- term.setCursorPos(oldx, oldy)
- end
- local function interact(x, y)
- for i=1,#interactions do
- local comp = interactions[i]
- if #comp[3] >= 4 then
- if comp[3][1] <= x and comp[3][2] <= y and comp[3][3]+comp[3][1] >= x and comp[3][4]+comp[3][2] >= y then
- local type = comp[1]
- if comp == "button" then
- local code = comp[4]
- runC(code, i, {comp})
- elseif comp == "input" then
- selected = comp
- end
- end
- end
- end
- return false
- end
- local function inputChar(ch)
- if selected ~= nil then
- if selected[1] == "input" then
- selected[2] = selected[2]..ch
- if containsKey(vars, selected[4]) then
- setValue(vars, selected[4], selected[2])
- else
- putValue(vars, selected[4], selected[2])
- end
- end
- end
- end
- -- comp: [type] [text] [loc]
- -- Button: [code] [states] [state]
- -- Input: [varname] [states] [state] [cursor]
- function loadMenu(dir)
- if fs.exists(dir) then
- local file = fs.open(dir, "r")
- local line = file.readLine()
- local btyp = ""
- local name = ""
- local cur = {}
- local ison = false
- local cmt = true
- while line ~= nil do
- line = string.gsub(line, "\t", "")
- if ison and cmt and string.gsub(line, " ", "") ~= "" and line ~= "::" then
- local curl = ""
- local curn = 1
- while string.gsub(curl, " ", "") == "" do
- curl = string.sub(line, 1, curn)
- curn = curn + 1
- end
- local l = string.sub(line, curn-1, -1)
- if btyp == "" then
- if string.sub(l, 1, 4) == "func" then
- local splt = split(l, ":~:")
- name = splt[2]
- else
- btyp = string.lower(l)
- end
- else
- if btyp == "comp" then
- local splt = split(l, ":~:")
- local type = splt[1]
- if type == "type" then
- cur[1] = splt[2]
- elseif type == "text" then
- cur[2] = splt[2]
- elseif type == "loc" then
- cur[3] = {}
- local c = cur[3]
- for i=2,#splt do
- c[#c + 1] = tonumber(splt[i])
- end
- cur[3] = c
- elseif type == "code" then
- cur[4] = getValue(funcs, splt[2])
- elseif type == "states" then
- cur[5] = {}
- local c = cur[5]
- for i=2,#splt do
- c[#c + 1] = splt[i]
- end
- cur[5] = c
- elseif type == "state" then
- cur[6] = tonumber(splt[2])
- elseif type == "cursor" then
- cur[7] = tonumber(splt[2])
- elseif type == "varname" then
- cur[4] = splt[2]
- end
- elseif btyp == "func" then
- cur[#cur + 1] = l
- elseif btyp == "startup" then
- cur[#cur + 1] = l
- elseif btyp == "update" then
- cur[#cur + 1] = l
- elseif btyp == "event" then
- cur[#cur + 1] = l
- elseif btype == "end" then
- cur[#cur + 1] = l
- end
- end
- end
- if line == "#:" then
- cmt = true
- end
- if line == ":#" then
- cmt = false
- end
- if line == "::" then
- if ison then
- print("CLOSED:"..btyp)
- ison = false
- if btyp == "comp" then
- interactions[#interactions + 1] = cur
- elseif btyp == "func" then
- if containsKey(funcs, name) then
- setValue(funcs, name, cur)
- else
- putValue(funcs, name, cur)
- end
- elseif btyp == "startup" then
- print("LOADED STARTUP")
- startup = cur
- elseif btyp == "update" then
- updte = cur
- elseif btyp == "event" then
- oevent = cur
- elseif btyp == "end" then
- ecode = cur
- end
- btyp = ""
- name = ""
- cur = {}
- else ison = true print("OPENED") end
- end
- print(line)
- line = file.readLine()
- end
- file.close()
- return true
- end
- return false
- end
- local function drawComponent(comp)
- local type = comp[1]
- local text = comp[2]
- local loc = comp[3]
- local x = loc[1]
- local y = loc[2]
- if type == "button" then
- local w = loc[3]
- local h = loc[4]-1
- local states = comp[5]
- local state = states[comp[6]]
- local s = {}
- for i=1,string.len(state) do
- s[#s + 1] = string.sub(state, i, i)
- end
- setPixel(x, y, s[1])
- setPixel(x+w, y, s[2])
- setPixel(x, y+h, s[3])
- setPixel(x+w, y+h, s[4])
- for i=x+1, x+w-1, 1 do
- setPixel(i, y, s[5])
- setPixel(i, y+h, s[7])
- end
- for i=y+1, x+h-1, 1 do
- setPixel(x, i, s[6])
- setPixel(x+w, i, s[8])
- end
- local ry = y+(h/2)
- local rx = x+(w/2)-(string.len(text)/2)
- setPixel(rx, ry, text)
- elseif type == "input" then
- local states = comp[5]
- local s = states[comp[6]]
- local cur = comp[7]
- setPixel(x, y, s[1])
- setPixel(x+w, y, s[2])
- setPixel(x+1, y, s[3])
- local rtext = string.sub(text, cur, -1)
- if (w-3)+cur < string.len(rtext) then
- rtext = string.sub(rtext, cur, cur+(w-3))
- end
- setPixel(x+2, y, rtext)
- elseif type == "label" then
- setPixel(x, y, text)
- end
- end
- local stuff = {...}
- print("LOOKING AT STUFF")
- if #stuff == 1 then
- print("LOADING MENU: "..stuff[1])
- if loadMenu(stuff[1]) then
- print("OK")
- on = runC(startup, 0, nil)
- local timer = os.startTimer(1)
- local interval = 0.5
- local n = 0
- while on do
- if on then
- local event, p1, p2, p3 = os.pullEvent()
- local kon = true
- if event == "timer" and p1 == timer then
- if on then
- timer = os.startTimer(interval)
- --print(n)
- if #interactions > 0 then
- for i=1,#interactions do
- local comp = interactions[i]
- drawComponent(comp)
- end
- end
- if on then
- kon = runC(updte, 0, nil)
- end
- end
- elseif event == "char" then
- inputChar(p1)
- if on then kon = runC(oevent, 0, {"char", p1}) end
- elseif event ~= nil and event ~= "" then
- if on then
- if p2 == nil then
- kon = runC(oevent, 0, {event, p1})
- elseif p3 == nil then
- kon = runC(oevent, 0, {event, p1, p2})
- elseif p3 ~= nil then
- kon = runC(oevent, 0, {event, p1, p2, p3})
- end
- end
- end
- if on then
- on = kon
- end
- n = n + 1
- else
- break
- end
- --sleep(0)
- end
- if not runC(ecode, 0, nil) then
- term.clear()
- term.setCursorPos(1,1)
- end
- end
- else
- print("INVALID INPUT")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement