extract functions

This commit is contained in:
theFox6 2020-02-19 09:12:51 +01:00
parent 32afec0b36
commit 80dba4c7c7
Signed by: theFox6
GPG Key ID: C884FE8D3BCE128A
3 changed files with 147 additions and 88 deletions

View File

@ -263,34 +263,30 @@ local function parse_element(el,pc)
return el, prefix
end
--TODO: make functions handling the lambdas
local function parse_line(l,pc)
local pl = ""
local i = 0
for sp,s in vp_util.optmatch(l,elements.spaces) do
if s then
if pc.lambargs then
pc.lambargs = pc.lambargs .. sp
elseif pc.deccheck then
if pc.deccheck == true then
pc.deccheck = sp
else
pc.deccheck = pc.deccheck .. sp
end
else
pl = pl .. sp
end
local function store_space(sp,pc)
if pc.optassign then
if pc.optassign ~= true then
pc.optassign = pc.optassign .. sp
end
end
if pc.lambargs then
pc.lambargs = pc.lambargs .. sp
return false
elseif pc.deccheck then
if pc.deccheck == true then
pc.deccheck = sp
return false
else
for st in vp_util.optmatch(sp,non_space_elements) do
if pc.slcomm then
pl = pl .. st
pc.deccheck = pc.deccheck .. sp
return false
end
else
local el,pre = parse_element(st,pc)
return true
end
end
local function handle_prefix(el,p)
local pre = p
local lpre
if pre then
while pre:match("\n") do
@ -315,12 +311,13 @@ local function parse_line(l,pc)
print("prel:" .. el)
end
--]]
if el == "" then
el = pre
elseif pre ~= "" then
el = pre .. " " .. el
end
return vp_util.concat_optnil(pre,el),lpre
end
return el
end
local function store_lambargs(e,pc)
local el = e
if pc.newlamb then
if pc.lambargs then
el = pc.lambargs .. el
@ -339,6 +336,10 @@ local function parse_line(l,pc)
--print("notl:", el)
end
end
return el
end
local function store_optassign(el,pc)
if pc.optassign and el ~= "" then
if pc.linestart and el:match(elements.names) then
if pc.optassign == true then
@ -350,6 +351,24 @@ local function parse_line(l,pc)
pc.optassign = false
end
end
end
local function parse_line(l,pc)
local pl = ""
local i = 0
for sp,s in vp_util.optmatch(l,elements.spaces) do
if s then
if store_space(sp,pc) then
pl = pl .. sp
end
else
for st in vp_util.optmatch(sp,non_space_elements) do
if pc.slcomm then
pl = pl .. st
else
local el,lpre = handle_prefix(parse_element(st,pc))
el = store_lambargs(el,pc)
store_optassign(el,pc)
if lpre then
pl = pl .. lpre .. el
else

View File

@ -25,7 +25,7 @@ local function for_in_test()
for i,el in pairs(testt) do
reft[i] = el
end
assert(vp_util.dftc(reft, testt) )
assert(vp_util.dftc(reft, testt))
reft = {}
for _,el in pairs(testt) do
@ -36,7 +36,7 @@ local function for_in_test()
for _, el in pairs(testt ) do
table.insert(reft2,el)
end
assert(vp_util.dftc(reft, reft2) )
assert(vp_util.dftc(reft, reft2))
end
for_in_test()
@ -56,7 +56,7 @@ local function shadow_test()
local function a()
return "function"
end
assert(a() =="function")
assert(a()=="function")
local reft = {}
do
@ -76,7 +76,7 @@ local function shadow_test()
end
assert(n == 12)
assert(a() =="function")
assert(a()=="function")
end
shadow_test()
@ -84,13 +84,13 @@ shadow_test()
local function t()
return "hi"
end
assert(t() =="hi")
assert(t()=="hi")
local function t2()
return "also hi"
end
assert(type(t2) =="function")
assert(t2() =="also hi")
assert(type(t2)=="function")
assert(t2()=="also hi")
local b = true
if (true) then
@ -163,7 +163,7 @@ local function decj()
j = j - 1
return j-- not a decrement, only returns n, this is a comment
end
assert(decj() ==1)
assert(decj()==1)
assert(j == 1)
local function reti()
@ -201,6 +201,6 @@ local function concatsub(t)
end
return ret
end
assert(vp_util.dftc(concatsub(tt) ,{"hello there", "venus test"}))
assert(vp_util.dftc(concatsub(tt),{"hello there", "venus test"}))
print("venus test end")

View File

@ -125,6 +125,46 @@ function vp_util.dftc(t1,t2)
return true
end
---
--concatenate strings
--if one string is nil the other is returned
--
--@function [parent=#vp_util] concat_optnil
--@param #string fstr the first string to be concatenated
--@param #string lstr the second string to be concatenated
--@param #string sep the seperator to be added between the strings if both are present
--@param #string retstr The string returned if both strings are empty.
-- Can be true to return an empty string.
function vp_util.concat_optnil(fstr,lstr,sep,retstr)
if fstr then
if lstr then
if sep then
if fstr == "" or lstr == "" then
return fstr..lstr
else
return fstr..sep..lstr
end
else
return fstr..lstr
end
else
return fstr
end
else
if lstr then
return lstr
else
if retstr == true then
return ""
elseif retstr then
return retstr
else
return nil
end
end
end
end
---
--The unit tests for the vp utilities.
local function tests()