From adc39aeb7f6c863659e1c278ee928ab22746b244 Mon Sep 17 00:00:00 2001 From: Hybrid Dog Date: Thu, 13 Apr 2017 23:05:50 +0200 Subject: [PATCH] Fix exit and show source line numbers --- faden.lua | 23 ++++++++++++++++------- parser.lua | 25 ++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/faden.lua b/faden.lua index 3885844..36d5810 100644 --- a/faden.lua +++ b/faden.lua @@ -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, } diff --git a/parser.lua b/parser.lua index dab9f97..aefe561 100644 --- a/parser.lua +++ b/parser.lua @@ -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