Last active
September 11, 2024 16:28
-
-
Save Alexior3000/3c17065cdccf74035e45dd9b9b90199e to your computer and use it in GitHub Desktop.
Chat GPT Version of the commodore basic (simple)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local variables = {} | |
local program = {} | |
local running = false | |
local totalMemory = 38911 -- Pamięć BASIC w Commodore 64 w bajtach | |
local basicBytesFree = totalMemory | |
local programName = "" | |
-- Funkcja do obliczania ilości wolnej pamięci | |
local function calculateFreeMemory() | |
local usedMemory = 0 | |
for _, code in pairs(program) do | |
usedMemory = usedMemory + #code + 5 -- Szacowanie użytej pamięci na linię kodu | |
end | |
return totalMemory - usedMemory | |
end | |
-- Funkcja do tokenizacji wejścia | |
local function tokenize(input) | |
local tokens = {} | |
for token in string.gmatch(input, "[^%s]+") do | |
table.insert(tokens, token) | |
end | |
return tokens | |
end | |
-- Funkcja do oceny wyrażeń | |
local function evaluateExpression(expression) | |
-- Zamiana nazw zmiennych na wartości | |
expression = expression:gsub("([A-Za-z]+)", function(var) | |
return variables[var] or 0 | |
end) | |
-- Zamiana operatorów BASIC na operatory Lua | |
expression = expression:gsub("([%+%-%*/])", function(op) | |
return op | |
end) | |
-- Usuwanie nadmiarowych znaków | |
expression = expression:gsub("[%s]", "") | |
-- Sprawdzanie poprawności wyrażenia | |
local func, err = load("return " .. expression) | |
if func then | |
local success, result = pcall(func) | |
if success then | |
return result | |
else | |
print("Błąd w wykonaniu wyrażenia: " .. result) | |
return 0 | |
end | |
else | |
print("Błąd w wyrażeniu: " .. err) | |
return 0 | |
end | |
end | |
-- Funkcja do wykonania jednej linii programu | |
local function runLine(line) | |
local tokens = tokenize(line) | |
local command = tokens[1] | |
if command == "PRINT" then | |
local expr = table.concat(tokens, " ", 2) | |
print(evaluateExpression(expr)) | |
elseif command == "LET" then | |
local varName = tokens[2] | |
local expr = table.concat(tokens, " ", 4) | |
variables[varName] = evaluateExpression(expr) | |
elseif command == "INPUT" then | |
local varName = tokens[2] | |
io.write(varName .. ": ") | |
variables[varName] = tonumber(io.read()) | |
elseif command == "GOTO" then | |
return tonumber(tokens[2]) | |
elseif command == "IF" then | |
local condition = table.concat(tokens, " ", 2, #tokens - 2) | |
local targetLine = tonumber(tokens[#tokens]) | |
-- Sprawdź poprawność warunku | |
condition = condition:gsub("([%a%d]+)%s*([%+%-%*/=<>)])%s*([%a%d]+)", "%1 %2 %3") | |
local result = evaluateExpression(condition) | |
local expectedValue = tokens[#tokens - 1] == "THEN" and targetLine or nil | |
if result ~= 0 then | |
return targetLine | |
end | |
elseif command == "LIST" then | |
for lineNumber, code in pairs(program) do | |
print(lineNumber .. " " .. code) | |
end | |
elseif command == "RUN" then | |
running = true | |
local currentLine = 1 | |
while running and currentLine do | |
local code = program[currentLine] | |
if code then | |
currentLine = runLine(code) or currentLine + 1 | |
else | |
currentLine = currentLine + 1 | |
end | |
end | |
elseif command == "NEW" then | |
programName = tokens[2]:gsub('"', '') | |
print("Creating new program: " .. programName) | |
program = {} | |
variables = {} | |
basicBytesFree = totalMemory | |
print("READY.") | |
io.write("Enter program lines (type SAVE to save, RESET to reset):\n") | |
while true do | |
io.write("] ") | |
local input = io.read() | |
if input:upper() == "SAVE" then | |
-- Save the program to a file | |
local file = io.open(programName .. ".bas", "w") | |
for lineNumber, code in pairs(program) do | |
file:write(lineNumber .. " " .. code .. "\n") | |
end | |
file:close() | |
print("Program saved as " .. programName .. ".bas") | |
elseif input:upper() == "RESET" then | |
-- Reset the emulator | |
return | |
elseif tonumber(string.sub(input, 1, 1)) then | |
local lineNumber, code = string.match(input, "(%d+)%s*(.+)") | |
program[tonumber(lineNumber)] = code | |
basicBytesFree = calculateFreeMemory() | |
end | |
end | |
elseif command == "LOAD" then | |
programName = tokens[2]:gsub('"', '') | |
local file = io.open(programName .. ".bas", "r") | |
if file then | |
print("Loading program: " .. programName) | |
program = {} | |
for line in file:lines() do | |
local lineNumber, code = string.match(line, "(%d+)%s*(.+)") | |
program[tonumber(lineNumber)] = code | |
end | |
file:close() | |
print("READY.") | |
else | |
print("File not found: " .. programName .. ".bas") | |
end | |
elseif command == "END" then | |
running = false | |
end | |
return nil | |
end | |
-- Funkcja do resetowania emulatora | |
local function resetEmulator() | |
program = {} | |
variables = {} | |
basicBytesFree = totalMemory | |
print("**** COMMODORE 64 BASIC V2 ****") | |
print("64K RAM SYSTEM " .. basicBytesFree .. " BASIC BYTES FREE") | |
print("\nREADY.") | |
end | |
-- Główna funkcja interaktywna (REPL) | |
local function prompt() | |
resetEmulator() | |
while true do | |
io.write("] ") | |
local input = io.read() | |
if tonumber(string.sub(input, 1, 1)) then | |
local lineNumber, code = string.match(input, "(%d+)%s*(.+)") | |
program[tonumber(lineNumber)] = code | |
basicBytesFree = calculateFreeMemory() | |
else | |
if input:upper() == "RESET" then | |
-- Resetowanie emulatora | |
resetEmulator() | |
else | |
runLine(input) | |
end | |
end | |
end | |
end | |
-- Uruchomienie konsoli | |
prompt() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment