extract functions
This commit is contained in:
parent
32afec0b36
commit
80dba4c7c7
71
init.lua
71
init.lua
|
@ -263,34 +263,30 @@ local function parse_element(el,pc)
|
||||||
return el, prefix
|
return el, prefix
|
||||||
end
|
end
|
||||||
|
|
||||||
--TODO: make functions handling the lambdas
|
local function store_space(sp,pc)
|
||||||
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
|
|
||||||
if pc.optassign then
|
if pc.optassign then
|
||||||
if pc.optassign ~= true then
|
if pc.optassign ~= true then
|
||||||
pc.optassign = pc.optassign .. sp
|
pc.optassign = pc.optassign .. sp
|
||||||
end
|
end
|
||||||
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
|
else
|
||||||
for st in vp_util.optmatch(sp,non_space_elements) do
|
pc.deccheck = pc.deccheck .. sp
|
||||||
if pc.slcomm then
|
return false
|
||||||
pl = pl .. st
|
end
|
||||||
else
|
else
|
||||||
local el,pre = parse_element(st,pc)
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function handle_prefix(el,p)
|
||||||
|
local pre = p
|
||||||
local lpre
|
local lpre
|
||||||
if pre then
|
if pre then
|
||||||
while pre:match("\n") do
|
while pre:match("\n") do
|
||||||
|
@ -315,12 +311,13 @@ local function parse_line(l,pc)
|
||||||
print("prel:" .. el)
|
print("prel:" .. el)
|
||||||
end
|
end
|
||||||
--]]
|
--]]
|
||||||
if el == "" then
|
return vp_util.concat_optnil(pre,el),lpre
|
||||||
el = pre
|
|
||||||
elseif pre ~= "" then
|
|
||||||
el = pre .. " " .. el
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
return el
|
||||||
|
end
|
||||||
|
|
||||||
|
local function store_lambargs(e,pc)
|
||||||
|
local el = e
|
||||||
if pc.newlamb then
|
if pc.newlamb then
|
||||||
if pc.lambargs then
|
if pc.lambargs then
|
||||||
el = pc.lambargs .. el
|
el = pc.lambargs .. el
|
||||||
|
@ -339,6 +336,10 @@ local function parse_line(l,pc)
|
||||||
--print("notl:", el)
|
--print("notl:", el)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
return el
|
||||||
|
end
|
||||||
|
|
||||||
|
local function store_optassign(el,pc)
|
||||||
if pc.optassign and el ~= "" then
|
if pc.optassign and el ~= "" then
|
||||||
if pc.linestart and el:match(elements.names) then
|
if pc.linestart and el:match(elements.names) then
|
||||||
if pc.optassign == true then
|
if pc.optassign == true then
|
||||||
|
@ -350,6 +351,24 @@ local function parse_line(l,pc)
|
||||||
pc.optassign = false
|
pc.optassign = false
|
||||||
end
|
end
|
||||||
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
|
if lpre then
|
||||||
pl = pl .. lpre .. el
|
pl = pl .. lpre .. el
|
||||||
else
|
else
|
||||||
|
|
|
@ -25,7 +25,7 @@ local function for_in_test()
|
||||||
for i,el in pairs(testt) do
|
for i,el in pairs(testt) do
|
||||||
reft[i] = el
|
reft[i] = el
|
||||||
end
|
end
|
||||||
assert(vp_util.dftc(reft, testt) )
|
assert(vp_util.dftc(reft, testt))
|
||||||
|
|
||||||
reft = {}
|
reft = {}
|
||||||
for _,el in pairs(testt) do
|
for _,el in pairs(testt) do
|
||||||
|
@ -36,7 +36,7 @@ local function for_in_test()
|
||||||
for _, el in pairs(testt ) do
|
for _, el in pairs(testt ) do
|
||||||
table.insert(reft2,el)
|
table.insert(reft2,el)
|
||||||
end
|
end
|
||||||
assert(vp_util.dftc(reft, reft2) )
|
assert(vp_util.dftc(reft, reft2))
|
||||||
end
|
end
|
||||||
|
|
||||||
for_in_test()
|
for_in_test()
|
||||||
|
@ -56,7 +56,7 @@ local function shadow_test()
|
||||||
local function a()
|
local function a()
|
||||||
return "function"
|
return "function"
|
||||||
end
|
end
|
||||||
assert(a() =="function")
|
assert(a()=="function")
|
||||||
|
|
||||||
local reft = {}
|
local reft = {}
|
||||||
do
|
do
|
||||||
|
@ -76,7 +76,7 @@ local function shadow_test()
|
||||||
end
|
end
|
||||||
assert(n == 12)
|
assert(n == 12)
|
||||||
|
|
||||||
assert(a() =="function")
|
assert(a()=="function")
|
||||||
end
|
end
|
||||||
|
|
||||||
shadow_test()
|
shadow_test()
|
||||||
|
@ -84,13 +84,13 @@ shadow_test()
|
||||||
local function t()
|
local function t()
|
||||||
return "hi"
|
return "hi"
|
||||||
end
|
end
|
||||||
assert(t() =="hi")
|
assert(t()=="hi")
|
||||||
|
|
||||||
local function t2()
|
local function t2()
|
||||||
return "also hi"
|
return "also hi"
|
||||||
end
|
end
|
||||||
assert(type(t2) =="function")
|
assert(type(t2)=="function")
|
||||||
assert(t2() =="also hi")
|
assert(t2()=="also hi")
|
||||||
|
|
||||||
local b = true
|
local b = true
|
||||||
if (true) then
|
if (true) then
|
||||||
|
@ -163,7 +163,7 @@ local function decj()
|
||||||
j = j - 1
|
j = j - 1
|
||||||
return j-- not a decrement, only returns n, this is a comment
|
return j-- not a decrement, only returns n, this is a comment
|
||||||
end
|
end
|
||||||
assert(decj() ==1)
|
assert(decj()==1)
|
||||||
assert(j == 1)
|
assert(j == 1)
|
||||||
|
|
||||||
local function reti()
|
local function reti()
|
||||||
|
@ -201,6 +201,6 @@ local function concatsub(t)
|
||||||
end
|
end
|
||||||
return ret
|
return ret
|
||||||
end
|
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")
|
print("venus test end")
|
||||||
|
|
40
vp_util.lua
40
vp_util.lua
|
@ -125,6 +125,46 @@ function vp_util.dftc(t1,t2)
|
||||||
return true
|
return true
|
||||||
end
|
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.
|
--The unit tests for the vp utilities.
|
||||||
local function tests()
|
local function tests()
|
||||||
|
|
Loading…
Reference in New Issue