Lua scripts and notes for programming within Minetest.

fargor.lua 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. local ptable
  2. ptable = function(t,d)
  3. local str = ""
  4. d = d or 0
  5. for k,v in pairs(t) do
  6. str = str .. string.rep(" ", d) .. tostring(k) .. " = " .. tostring(v) .. "\n"
  7. if type(v) == "table" then str = str .. ptable(v, d+1) end
  8. end
  9. return str
  10. end
  11. local split
  12. split = function(str)
  13. --print("split<"..tostring(str)..">")
  14. local ast = {}
  15. local prev = 0
  16. local cur = str:find(" ", 0, true)
  17. local ret
  18. while cur do
  19. ret = str:sub(prev + 1, cur - 1)
  20. if ret and #ret > 0 then
  21. table.insert(ast, ret)
  22. end
  23. prev = cur
  24. cur = str:find(" ", prev + 1, true)
  25. end
  26. ret = str:sub(prev + 1)
  27. if ret and #ret > 0 then
  28. table.insert(ast, ret)
  29. end
  30. --print("split's result:")
  31. --print(ptable(ast))
  32. if #ast == 1 then
  33. --print("split '" .. str .. "' returns '" .. ast[1] .. "'")
  34. return ast[1]
  35. elseif #ast > 1 then
  36. --print("split '" .. str .. "' returns '" .. table.concat(ast, ",") .. "'")
  37. return ast
  38. end
  39. end
  40. local brace
  41. brace = function(str)
  42. --print("brace<"..tostring(str)..">")
  43. local ast = {}
  44. local open, close = str:find("(", 0, true), str:find(")", 0, true)
  45. local ret
  46. while open and close and open < close do
  47. -- we want what came before the next level starts
  48. ret = split(str:sub(1, open - 1))
  49. if ret then
  50. table.insert(ast, ret)
  51. end
  52. -- we want the object formed from the next level
  53. ret, str = brace(str:sub(open + 1))
  54. if ret then
  55. table.insert(ast, ret)
  56. end
  57. -- and now we need to find what is next
  58. open, close = str:find("(", 0, true), str:find(")", 0, true)
  59. end
  60. if close then
  61. -- we want what came before our close
  62. ret = split(str:sub(1, close - 1))
  63. if ret then
  64. table.insert(ast, ret)
  65. end
  66. --print("CLOSING \""..str:sub(close + 1).."\"")
  67. -- return our result + what is still unparsed
  68. return ast, str:sub(close + 1)
  69. end
  70. -- if there is anything left, add it (we should be on the top level only here)
  71. ret = split(str)
  72. --print("STR->RET", str, ret)
  73. if ret then
  74. table.insert(ast, ret)
  75. end
  76. --print("brace's result:")
  77. --print(ptable(ast))
  78. if #ast == 1 then
  79. return ast[1]
  80. elseif #ast > 1 then
  81. return ast
  82. end
  83. end
  84. local a = "(echo (: 'value' ($ name)))"
  85. ast = brace(a)
  86. print(ptable(ast))
  87. print(ast[2][1][1])
  88. --print(ast)
  89. --(
  90. -- 1echo
  91. -- 2(
  92. -- 1:
  93. -- 2'value'
  94. -- 3(
  95. -- 1$
  96. -- 2name
  97. -- )
  98. -- )
  99. --)