diff options
Diffstat (limited to 'lib/lace/compiler.lua')
-rw-r--r-- | lib/lace/compiler.lua | 48 |
1 files changed, 27 insertions, 21 deletions
diff --git a/lib/lace/compiler.lua b/lib/lace/compiler.lua index 05bf047..7b3c504 100644 --- a/lib/lace/compiler.lua +++ b/lib/lace/compiler.lua @@ -61,29 +61,19 @@ local function _setposition(context, ruleset, linenr) context._lace.linenr = linenr end -local function compile_one_line(compcontext, line) - -- The line is a rule, so we don't need to think about that. The - -- first entry is a command. - local cmdname = line.content[1].str - local cmdfn = _command(compcontext, cmdname) - if type(cmdfn) ~= "function" then - return err.error("Unknown command: " .. cmdname, {1}) - end - - local args = {} - local rules = {} - for i = 1, #line.content do - if line.content[i].sub then - -- We have encountered a subdefine - -- We thusly need to magically construct a define. - local sub = line.content[i].sub +local function transfer_args(compcontext, content, rules) + local args, err = {} + for i = 1, #content do + if content[i].sub then + local sub = content[i].sub local defnr = compcontext._lace.magic_define_nr local definename = "__autodef"..tostring(defnr) compcontext._lace.magic_define_nr = defnr + 1 local definefn = _command(compcontext, "define") - local subargs = {} - for j = 1, #sub do - subargs[j] = sub[j].str or "{}" + local subargs + rules, subargs = transfer_args(compcontext, sub, rules) + if type(rules) ~= "table" then + return rules, subargs end local definerule, err = definefn(compcontext, "define", definename, unpack(subargs)) @@ -92,13 +82,29 @@ local function compile_one_line(compcontext, line) err.words = {i} return definerule, err end - args[i] = definename + args[#args+1] = definename rules[#rules+1] = definerule else - args[i] = line.content[i].str + args[#args+1] = content[i].str end end + return rules, args +end + +local function compile_one_line(compcontext, line) + -- The line is a rule, so we don't need to think about that. The + -- first entry is a command. + local cmdname = line.content[1].str + local cmdfn = _command(compcontext, cmdname) + if type(cmdfn) ~= "function" then + return err.error("Unknown command: " .. cmdname, {1}) + end + local rules, args = transfer_args(compcontext, line.content, {}) + if type(rules) ~= "table" then + return rules, args + end + local linerule, err = cmdfn(compcontext, unpack(args)) if type(linerule) ~= "table" then return linerule, err |