1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- local function split(str)
- -- should return single string, flat table of strings, or nil
- local ast = {}
- local prev, space = 0, str:find(" ", 0, true)
- local ret
- while space do
- ret = str:sub(prev + 1, space - 1)
- if #ret > 0 then
- table.insert(ast, ret)
- end
- prev = space
- space = str:find(" ", prev + 1, true)
- end
- ret = str:sub(prev + 1)
- if #ret > 0 then
- table.insert(ast, ret)
- end
- if #ast < 1 then
- return nil
- elseif #ast == 1 then
- return ast[1]
- else
- return ast
- end
- end
-
- local table_tostring
- local function parse(str)
- -- should return table with strings and tables, etc, or nil
- local ast = {}
- local open, close = str:find("(", 0, true), str:find(")", 0, true)
- local ret
- while open and close and open < close do
- --print("---" .. str:sub(1, open - 1))
- table.insert(ast, split(str:sub(1, open - 1)))
- --print(str:sub(open + 1))
- ret, str = parse(str:sub(open + 1))
- --print("RET", ret, ret[1], ret[2])
- --print(table_tostring(ast))
- --print(ret, "'"..str.."'", #ret, ret[1], ret[2])
- table.insert(ast, ret)
- --print(table_tostring(ast))
- --print("--------------")
- open, close = str:find("(", 0, true), str:find(")", 0, true)
- end
- --print(";;"..str)
- if close then
- --print(str:sub(1, close - 1))
- table.insert(ast, split(str:sub(1, close - 1)))
- ret = str:sub(close + 1)
- if #ast < 1 then
- return nil, ret
- elseif #ast == 1 then
- return ast[1], ret
- else
- return ast, ret
- end
- end
- if #ast < 1 then
- return nil
- elseif #ast == 1 then
- return ast[1]
- else
- return ast
- end
- end
-
- -- (echo (: "value" ($ name))) -- true/false depending on $name == "value"
- -- (while ($ name) (run command)) -- while $name run(command)
-
- -- fns = {
- -- print, =, if, for, while
- -- }
-
- --local table_tostring
- table_tostring = function(tab, depth)
- local str = ""
- depth = depth or 0
- if "table" == type(tab) then
- for k,v in pairs(tab) do
- str = str .. table_tostring(k, depth) .. " =\n"
- str = str .. table_tostring(v, depth + 1) .. "\n"
- end
- return str
- else
- return string.rep(" ", depth) .. tostring(tab) .. " (" .. type(tab):sub(1, 1) .. ")"
- end
- end
-
- local ast, extra = parse("(echo (: \"value\" ($ name)))")
- --local ast, extra = parse("(while ($ name) (run command))")
- --local ast, extra = parse("b b (c c)")
-
- print(table_tostring(ast))
- print(extra)
|