Guest User

Untitled

a guest
Sep 26th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. -- Reads a dotted key from t
  2. local function read(t, key)
  3. local b,s,l = string.byte,string.sub,1
  4. for i=1, key:len()+1 do
  5. local c = b(key, i)
  6. if c==nil or c==46 then
  7. if l==i then
  8. error( "Invalid key: [" .. tostring(key) .. "] at character position: " .. tostring(l) )
  9. end
  10. t,l = type(t)~="table" and nil or t[s(key, l, i-1)],i+1
  11. end
  12. end
  13. return t
  14. end
  15.  
  16. local t = { one = { two = "12", t = true, f = false } }
  17. assert( read(t, "one.two") == "12" )
  18. assert( read(t, "one.t") == true )
  19. assert( read(t, "one.t1") == nil )
  20. assert( read(t, "one.f") == false )
  21. assert( read(t, "one.f1") == nil )
  22. assert( type(read(t, "one"))=="table" )
  23. --assert( read(read(t,"one1",read(t,"one")), "two") == "12")
  24. print( read(t,"one.two"))
  25. print( read(t,"blah"))
  26.  
  27. -- Evals a boolean "AND" expression of dotted keys
  28. local function eval(t, expr)
  29. local b,s,r,l,plus = string.byte,string.sub,read
  30. for i=1, expr:len()+1 do
  31. local c = b(expr, i)
  32. if c==nil or c==43 or c==45 then
  33. if l==i or c==nil and l==nil then
  34. error( "Invalid expression" )
  35. end
  36. if l~=nil then
  37. local k = s(expr, l, i-1)
  38. local v = r(t, k)
  39. local f = v==plus
  40. if c==nil or not(f) then
  41. print(k,v,f,plus)
  42. return f
  43. end
  44. end
  45. plus,l = c==43,i+1
  46. end
  47. end
  48. assert(nil)
  49. end
  50.  
  51. local f = { a = { b = { c = false } } }
  52. assert( eval(f, "-a.b.c") == false )
  53. assert( eval(f, "+a.b.c") == true )
  54.  
  55. -- Splits a single line into word table. Handles and preserves quotes (' " `)
  56. local function split(l)
  57. local t,n, b,s, p,q = {},1, string.byte,string.sub
  58. for i=1, l:len() do
  59. local c = b(l, i)
  60. if not(q) then
  61. if c==9 or c==32 then
  62. if p then
  63. t[n],n, p = s(l, p, i - 1),n + 1
  64. end
  65. else
  66. q = (c==34 or c==39 or c==96) and c
  67. p = p or i
  68. end
  69. elseif q==c then
  70. q = nil
  71. end
  72. end
  73. t[n] = p and s(l,p)
  74. return t
  75. end
  76.  
  77. -- Splits each line from lines
  78. -- Then finds all boolean expressions
  79. -- And based on them leaves only these lines
  80. local function expand(t, words)
  81. local more_expr, accepted = true
  82. local out, b = {}, string.byte
  83. for i=1, #words do
  84. local w = words[i]
  85. local c = b(w)
  86. if more_expr and (c==43 or c==45) then
  87. if not(accepted) then
  88. accepted = eval(t,w)
  89. print('AA',accepted,w)
  90. end
  91. elseif accepted~=false then
  92. out[ #out+1 ] = w
  93. more_expr = false
  94. else
  95. break
  96. end
  97. end
  98. return out
  99. end
  100.  
  101. do
  102. local t = { debug=false }
  103. for l in io.lines('z.lib.build') do
  104. -- print(l)
  105. print(table.concat(expand(t,split(l)), ' .. '))
  106. end
  107. end
Add Comment
Please, Sign In to add comment