Skip to content

Commit

Permalink
fix: remove all goto from lua code
Browse files Browse the repository at this point in the history
enhance compatibility with Lua 5.1 base standard
  • Loading branch information
jaromil committed Aug 11, 2023
1 parent 4316ddd commit 5a422f4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 80 deletions.
2 changes: 1 addition & 1 deletion src/lua/.luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ globals = {
'require', 'require_once','fif', 'deepmap', 'luatype', 'sort_pairs',
'empty', 'have', 'initkeyring', 'havekey', 'zenguard', 'exitcode',
'deprecated', 'mayhave', 'parse_prefix', 'strtok', 'strcasecmp',
'uscore', 'debug_traceback',
'uscore', 'debug_traceback', 'isnumber'
'G2','ABC','ECDH',
'check_codec', 'ZKP_challenge', 'SHA256', 'SHA512', 'sha256', 'sha512',
'printerr', 'act', 'notice', 'warn', 'error', 'xxx', 'fatal', 'trim', 'serialize', 'iszen',
Expand Down
12 changes: 4 additions & 8 deletions src/lua/inspect.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,13 @@ local function export_arr(object, format)
local ft = type(format)
if format and ft == 'function' then
conv_f = format
goto ok
end
if format and ft == 'string' then
elseif format and ft == 'string' then
conv_f = get_encoding_function(format)
goto ok
end
if not CONF.output.encoding then
elseif not CONF.output.encoding then
error('CONF.output.encoding is not configured', 2)
else
conv_f = CONF.debug.encoding.fun -- fallback to configured conversion function
end
conv_f = CONF.debug.encoding.fun -- fallback to configured conversion function
::ok::
ZEN.assert(
type(conv_f) == 'function',
'export_arr conversion function not configured'
Expand Down
16 changes: 6 additions & 10 deletions src/lua/zencode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,7 @@ function zencode:parse(text)
for line in zencode_newline_iter(text) do
linenum = linenum + 1
local tline = trim(line) -- saves trims in isempty / iscomment
if zencode_isempty(tline) then goto continue end
if zencode_iscomment(tline) then goto continue end
if not zencode_isempty(tline) and not zencode_iscomment(tline) then
-- xxx('Line: '.. text, 3)
-- max length for single zencode line is #define MAX_LINE
-- hard-coded inside zenroom.h
Expand Down Expand Up @@ -599,7 +598,8 @@ function zencode:parse(text)
assert(fm(self.machine, { msg = tline, Z = self }),
line.."\n "..
"Invalid transition from: "..self.machine.current)
::continue::
end
-- continue
end
collectgarbage'collect'
return true
Expand Down Expand Up @@ -777,12 +777,7 @@ function zencode:run()
local x = self.AST[ZEN.current_instruction]
ZEN.next_instruction = ZEN.next_instruction + 1
-- ZEN:trace(x.source)
if manage_branching(x) then
goto continue
end
if manage_foreach(x) then
goto continue
end
if not manage_branching(x) and not manage_foreach(x) then
-- trigger upon switch to when or then section
if x.from == 'given' and x.to ~= 'given' then
-- delete IN memory
Expand All @@ -808,7 +803,8 @@ function zencode:run()
fatal(x.source) -- traceback print inside
end
collectgarbage 'collect'
::continue::
end
-- ::continue::
end
-- PRINT output
ZEN:trace('--- Zencode execution completed')
Expand Down
119 changes: 58 additions & 61 deletions src/lua/zencode_data.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,67 +34,64 @@

-- Used in scenario's schema declarations to cast to zenroom. type
ZEN.get = function(obj, key, conversion, encoding)
if type(key) ~= 'string' then
error('ZEN.get key is not a string', 2) end
if conversion and type(conversion) ~= 'function' then
error('ZEN.get invalid conversion function', 2) end
if encoding and type(encoding) ~= 'function' then
error('ZEN.get invalid encoding function', 2) end
local k
if not obj then -- take from IN root
-- does not support to pick in WHO (use of 'my')
k = KIN[key] or IN[key]
else
if key == '.' then
k = obj
else
k = obj[key]
end
end
if not k then
error('Key not found in object conversion: ' .. key, 2)
end
local res = nil
local t = type(k)
-- BIG/INT default export is decimal
if conversion == INT.new and not encoding then
-- compare function address and use default
res = INT.from_decimal(k)
goto ok
end
if iszen(t) and conversion then
res = conversion(k)
goto ok
end
if iszen(t) and not conversion then
res = k
goto ok
end
if t == 'string' then
if encoding then
res = encoding(k)
else
res = CONF.input.encoding.fun(k)
end
if conversion then
res = conversion(res)
end
goto ok
end
if t == 'number' then
res = k
end
if t == 'table' then
res = deepmap(encoding or CONF.input.encoding.fun, k)
if conversion then
res = deepmap(conversion, res)
end
end
::ok::
assert(
ZEN.OK and res,
'ZEN.get on invalid key: ' .. key .. ' (' .. t .. ')', 2)
return res
if type(key) ~= 'string' then
error('ZEN.get key is not a string', 2)
end
if conversion and type(conversion) ~= 'function' then
error('ZEN.get invalid conversion function', 2)
end
if encoding and type(encoding) ~= 'function' then
error('ZEN.get invalid encoding function', 2)
end
local k
if not obj then -- take from IN root
-- does not support to pick in WHO (use of 'my')
k = KIN[key] or IN[key]
else
if key == '.' then
k = obj
else
k = obj[key]
end
end
if not k then
error('Key not found in object conversion: ' .. key, 2)
end
local res = nil
local t = type(k)
-- BIG/INT default export is decimal
if conversion == INT.new and not encoding then
-- compare function address and use default
res = INT.from_decimal(k)
elseif iszen(t) and conversion then
res = conversion(k)
elseif iszen(t) and not conversion then
res = k
elseif t == 'string' then
if encoding then
res = encoding(k)
else
res = CONF.input.encoding.fun(k)
end
if conversion then
res = conversion(res)
end
else
if t == 'number' then
res = k
end
if t == 'table' then
res = deepmap(encoding or CONF.input.encoding.fun, k)
if conversion then
res = deepmap(conversion, res)
end
end
end

assert(
ZEN.OK and res,
'ZEN.get on invalid key: ' .. key .. ' (' .. t .. ')', 2)
return res
end

-- return leftmost and rightmost if definition string indicates
Expand Down

0 comments on commit 5a422f4

Please sign in to comment.