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

169
init.lua
View File

@ -263,93 +263,112 @@ local function parse_element(el,pc)
return el, prefix
end
--TODO: make functions handling the lambdas
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
pc.deccheck = pc.deccheck .. sp
return false
end
else
return true
end
end
local function handle_prefix(el,p)
local pre = p
local lpre
if pre then
while pre:match("\n") do
if lpre then
lpre = lpre .. pre:sub(1,pre:find("\n"))
else
lpre = pre:sub(1,pre:find("\n"))
end
pre = pre:sub(pre:find("\n")+1)
end
local pres = pre:match("^%s*") or ""
if lpre then
lpre = lpre .. pres
else
lpre = pres
end
pre = pre:sub(#pres+1)
--[[
if (pre ~= "") then
print("pre:".. pre..":")
else
print("prel:" .. 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
end
pc.lambargs = pc.newlamb
pc.newlamb = false
--print("newl:", pc.lambargs, el)
elseif pc.lambargs then
if el:match(elements.names) or el:match(elements.lambda_args) then
pc.lambargs = pc.lambargs .. el
el = ""
elseif el ~= "" then
el = pc.lambargs .. el
pc.lambargs = false
pc.lambend = false
--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
pc.optassign = el
else
pc.optassign = pc.optassign .. el
end
elseif el ~= "--" then
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 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
if store_space(sp,pc) then
pl = pl .. sp
end
if pc.optassign then
if pc.optassign ~= true then
pc.optassign = pc.optassign .. sp
end
end
else
for st in vp_util.optmatch(sp,non_space_elements) do
if pc.slcomm then
pl = pl .. st
else
local el,pre = parse_element(st,pc)
local lpre
if pre then
while pre:match("\n") do
if lpre then
lpre = lpre .. pre:sub(1,pre:find("\n"))
else
lpre = pre:sub(1,pre:find("\n"))
end
pre = pre:sub(pre:find("\n")+1)
end
local pres = pre:match("^%s*") or ""
if lpre then
lpre = lpre .. pres
else
lpre = pres
end
pre = pre:sub(#pres+1)
--[[
if (pre ~= "") then
print("pre:".. pre..":")
else
print("prel:" .. el)
end
--]]
if el == "" then
el = pre
elseif pre ~= "" then
el = pre .. " " .. el
end
end
if pc.newlamb then
if pc.lambargs then
el = pc.lambargs .. el
end
pc.lambargs = pc.newlamb
pc.newlamb = false
--print("newl:", pc.lambargs, el)
elseif pc.lambargs then
if el:match(elements.names) or el:match(elements.lambda_args) then
pc.lambargs = pc.lambargs .. el
el = ""
elseif el ~= "" then
el = pc.lambargs .. el
pc.lambargs = false
pc.lambend = false
--print("notl:", el)
end
end
if pc.optassign and el ~= "" then
if pc.linestart and el:match(elements.names) then
if pc.optassign == true then
pc.optassign = el
else
pc.optassign = pc.optassign .. el
end
elseif el ~= "--" then
pc.optassign = false
end
end
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

@ -22,13 +22,13 @@ local function for_in_test()
}
local reft = {}
for i,el in pairs(testt) do
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
for _,el in pairs(testt) do
table.insert(reft,el)
end
@ -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,16 +84,16 @@ 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
if (true) then
b = "weewoo"
end
assert(b == "weewoo")
@ -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()
@ -172,7 +172,7 @@ local function reti()
end
i = i + 1
assert(reti() == 2)
assert(reti() == 2)
-- () => {}
@ -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()