Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- BASIC-Emulator "RetroOS"
- local program = {}
- local vars = {}
- local loop_stack = {}
- local PIN = "1234" -- Dein PIN für LOGOUT
- local themes = {
- {name = "C64 Style", bg = colors.blue, fg = colors.white},
- {name = "CBM PET", bg = colors.black, fg = colors.lime},
- {name = "Apple II", bg = colors.black, fg = colors.green},
- {name = "Spectrum", bg = colors.white, fg = colors.black},
- {name = "DOS Mode", bg = colors.black, fg = colors.lightGray}
- }
- local currentTheme = themes[1]
- -- Grafische Hilfsfunktionen
- local function drawBox(w, h, title)
- term.clear()
- local tw, th = term.getSize()
- local x, y = math.floor((tw-w)/2), math.floor((th-h)/2)
- term.setBackgroundColor(colors.gray)
- term.setCursorPos(x, y)
- for i=1, h do
- term.setCursorPos(x, y + i - 1)
- term.write(string.rep(" ", w))
- end
- term.setCursorPos(x+1, y)
- term.write(title)
- end
- local function promptPin()
- drawBox(20, 5, " SYSTEM SPERRE ")
- term.setCursorPos(5, 3)
- term.write("PIN eingeben:")
- term.setCursorPos(5, 4)
- local input = read("*")
- if input == PIN then return true end
- return false
- end
- local function selectTheme()
- local selection = 1
- while true do
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1, 1)
- print("--- Waehle dein Retro-System ---")
- for i, t in ipairs(themes) do
- print((i == selection and "> " or " ") .. t.name)
- end
- local _, key = os.pullEvent("key")
- if key == keys.up then selection = math.max(1, selection - 1)
- elseif key == keys.down then selection = math.min(#themes, selection + 1)
- elseif key == keys.enter then
- currentTheme = themes[selection]
- break
- end
- end
- end
- -- Parser Funktionen
- local function execute()
- local lines = {}
- for k in pairs(program) do table.insert(lines, k) end
- table.sort(lines)
- local ip = 1
- while ip <= #lines do
- local cmd = program[lines[ip]]:upper()
- local tokens = {}
- for token in cmd:gmatch("%S+") do table.insert(tokens, token) end
- -- Befehlsverarbeitung
- if tokens[1] == "PRINT" then
- local text = cmd:sub(7)
- if vars[text] then print(vars[text])
- elseif text:sub(1,1) == "\"" then print(text:sub(2, -2))
- else print(text) end
- elseif tokens[1] == "LET" or tokens[2] == "=" then
- local var, val = cmd:match("(%a+)%s*=%s*(%d+)")
- vars[var] = tonumber(val)
- elseif tokens[1] == "IF" then
- local var, op, val, thenLine = cmd:match("IF%s+(%a+)%s*([<>=]+)%s*(%d+)%s+THEN%s+(%d+)")
- local condition = false
- if op == "=" and vars[var] == tonumber(val) then condition = true
- elseif op == ">" and vars[var] > tonumber(val) then condition = true
- elseif op == "<" and vars[var] < tonumber(val) then condition = true end
- if condition then
- for i, l in ipairs(lines) do if l == tonumber(thenLine) then ip = i - 1 end end
- end
- elseif tokens[1] == "GOTO" then
- local target = tonumber(tokens[2])
- for i, l in ipairs(lines) do if l == target then ip = i - 1 end end
- elseif tokens[1] == "CLS" then term.clear(); term.setCursorPos(1,1)
- elseif tokens[1] == "REM" then -- Kommentar (ignoriert)
- elseif tokens[1] == "END" then break
- end
- ip = ip + 1
- sleep(0.05)
- end
- end
- -- Hauptprogramm
- selectTheme()
- term.setBackgroundColor(currentTheme.bg)
- term.setTextColor(currentTheme.fg)
- term.clear()
- term.setCursorPos(1, 1)
- print("RetroOS v1.0 - BASIC Emulation")
- while true do
- write("> ")
- local input = read()
- if input:upper() == "RUN" then execute()
- elseif input:upper() == "LOGOUT" then
- if promptPin() then break
- else print("Falscher PIN!") end
- elseif input:upper() == "LIST" then
- for _, n in ipairs((function() local l={}; for k in pairs(program) do table.insert(l,k) end; table.sort(l); return l end)()) do
- print(n .. " " .. program[n])
- end
- else
- local lineNum = tonumber(input:match("^(%d+)"))
- if lineNum then program[lineNum] = input:match("^%d+%s+(.*)")
- else print("SYNTAX ERROR") end
- end
- end
- term.clear()
Advertisement