Fix exit and show source line numbers

This commit is contained in:
Hybrid Dog 2017-04-13 23:05:50 +02:00
parent ee0859ce94
commit adc39aeb7f
2 changed files with 38 additions and 10 deletions

View File

@ -4,9 +4,13 @@ local function befehl_ausfuhren(faden)
local vars = faden.vars
local anw = faden.liste[faden.ip]
if not anw then
return false, "Invalid instruction pointer"
return false, "Invalid instruction pointer, previous line: " ..
faden.previous_line_number
end
local befehl, args = unpack(anw)
local line_number = anw[3] or -1
-- enable this line for tracing:
--~ print(line_number, befehl, args and table.concat(args, ", "))
for i = 1,#is do
local bfunk = is[i][befehl]
if bfunk then
@ -25,7 +29,8 @@ local function befehl_ausfuhren(faden)
end
local weiter, ergebnis = bfunk(fa, faden)
if not weiter then
return false, "Command " .. befehl .. ": " .. ergebnis
return false, "Command " .. befehl ..
" (" .. line_number .. "): " .. ergebnis
end
if args
and ergebnis ~= nil then
@ -43,16 +48,18 @@ local function befehl_ausfuhren(faden)
if faden.ip > #faden.liste then
return false, "Done"
end
faden.previous_line_number = line_number
return true
end
end
return false, 'Unknown command "' .. befehl .. '"'
return false, 'Unknown command "' .. befehl .. '" (' .. line_number .. ")"
end
local function programm_ausfuhren(faden)
local weiter,msg = befehl_ausfuhren(faden)
if not weiter then
faden.log = faden.log .. "Aborted (" .. faden.ip .. "): " .. msg .. "\n"
faden.log = faden.log ..
"Aborted (." .. faden.ip .. "): " .. msg .. "\n"
faden:exit()
return
end
@ -64,25 +71,26 @@ return function(faden_manip, parsed)
log = "",
vars = {pi = math.pi},
ip = 1,
last_line_number = 1,
sp = 3500,
sb = 3500,
strlen_max = 2000,
stack = {},
is = {pdisc.standard_befehlssatz},
suscitate = programm_ausfuhren,
suscitate = programm_ausfuhren, -- beim ersten Start
flush = function(self)
print(self.log)
self.log = ""
return true
end,
stop = function(self)
stop = function(self) -- beim vorrübergehenden Anhalten
self.stopped = true
end,
continue = function(self)
self.stopped = false
self:suscitate()
end,
try_rebirth = function(self)
try_rebirth = function(self) -- bei weiteren Starts
if minetest.get_us_time() >= self.rebirth then
self:continue()
return true
@ -90,6 +98,7 @@ return function(faden_manip, parsed)
return false
end,
exit = function(self)
self.stopped = false
self:flush()
end,
}

View File

@ -56,20 +56,39 @@ local function parse_zeile(marken, imms, z, ip)
return {befehl, args}
end
local function zeileniter(text)
return function()
if not text then
return
end
local umbruch = text:find"\n"
if not umbruch then
local rv = text
text = nil
return rv
end
local rv = text:sub(1, umbruch-1)
text = text:sub(umbruch+1)
return rv
end
end
return function(programm)
local imms = {}
local marken = {}
-- Programm erkennen
local zeilen = programm:split"\n"
local anz = 0
local liste = {}
for i = 1,#zeilen do
local befehl = parse_zeile(marken, imms, zeilen[i], anz + 1)
local zn = 1
for zeile in zeileniter(programm) do
local befehl = parse_zeile(marken, imms, zeile, anz + 1)
if befehl[1] then
befehl[3] = zn -- Zeilennummer für Fehlernachrichten
anz = anz + 1
liste[anz] = befehl
end
zn = zn+1
end
local immn = #imms+1