diff --git a/lib/metaprogramming.nom b/lib/metaprogramming.nom index 65a625d..ac45574 100644 --- a/lib/metaprogramming.nom +++ b/lib/metaprogramming.nom @@ -9,8 +9,8 @@ lua> ".." | local body = nomsu:typecheck(vars, "body", "Thunk"); | return ([[ |nomsu:def(%s, %s, %s) - |]]):format(nomsu:repr(signature), nomsu:tree_to_lua(body), nomsu:repr(body.src)), nil; - |end), ""); + |]]):format(nomsu:repr(signature), nomsu:tree_to_lua(body), nomsu:repr(("rule %s\\n..=%s"):format(vars.signature.src, vars.body.src))), nil; + |end)); # Rule to make nomsu macros: rule [escaped parse %shorthand as %longhand] =: @@ -33,7 +33,7 @@ rule [escaped parse %shorthand as %longhand] =: | end | return nil, table.concat(lua_bits, "\\n"); |end - |nomsu:defmacro(aliases, parsing_as, template.src); + |nomsu:defmacro(aliases, parsing_as, ("parse %s\\n..as %s"):format(vars.shorthand.src, vars.longhand.src)); escaped parse \[parse %shorthand as %longhand] as \: escaped parse \%shorthand as \%longhand # Rule to make lua macros: @@ -42,14 +42,14 @@ rule [escaped compile %macro_def to %body] =: |local aliases = nomsu:get_stubs(nomsu:typecheck(vars, "macro_def", "List").value); |local body = nomsu:typecheck(vars, "body", "Thunk"); |local thunk = nomsu:tree_to_value(body); - |nomsu:defmacro(aliases, thunk, body.src); + |nomsu:defmacro(aliases, thunk, ("compile %s\\n..to %s"):format(vars.macro_def.src, body.src)); rule [escaped compile %macro_def to code %body] =: lua> ".." |local aliases = nomsu:get_stubs(nomsu:typecheck(vars, "macro_def", "List").value); |local body = nomsu:typecheck(vars, "body", "Thunk"); |local thunk = nomsu:tree_to_value(body); |local thunk_wrapper = function(nomsu, vars) return nil, thunk(nomsu, vars); end - |nomsu:defmacro(aliases, thunk_wrapper, body.src); + |nomsu:defmacro(aliases, thunk_wrapper, ("compile %s\\n..to code %s"):format(vars.macro_def.src, body.src)); parse [compile %macro_def to %body] as: escaped compile \%macro_def to \%body parse [compile %macro_def to code %body] as: escaped compile \%macro_def to code \%body diff --git a/nomsu.lua b/nomsu.lua index b00560c..055c8a2 100644 --- a/nomsu.lua +++ b/nomsu.lua @@ -242,6 +242,7 @@ do assert(type(thunk) == 'function', "Bad thunk: " .. tostring(repr(thunk))) local canonical_args = nil local aliases = { } + self.def_number = self.def_number + 1 for _index_0 = 1, #signature do local _des_0 = signature[_index_0] local stub, arg_names @@ -269,13 +270,33 @@ do arg_names = arg_names, src = src, is_macro = is_macro, - aliases = aliases + aliases = aliases, + def_number = self.def_number } end end, defmacro = function(self, signature, thunk, src) return self:def(signature, thunk, src, true) end, + serialize_defs = function(self, after) + if after == nil then + after = 0 + end + defs = { } + for _, def in pairs(self.defs) do + defs[def.def_number] = def.src or "" + end + local keys = utils.keys(defs) + table.sort(keys) + local buff = { } + for _index_0 = 1, #keys do + local i = keys[_index_0] + if i > after and #defs[i] > 0 then + insert(buff, defs[i]) + end + end + return concat(buff, "\n") + end, call = function(self, stub, line_no, ...) local def = self.defs[stub] if def and def.is_macro and self.callstack[#self.callstack] ~= "#macro" then @@ -1063,6 +1084,7 @@ end)]]):format(concat(lua_bits, "\n")) self.defs = setmetatable({ }, { __index = parent and parent.defs }) + self.def_number = 0 self.callstack = { } self.debug = false self.utils = utils diff --git a/nomsu.moon b/nomsu.moon index 0c45258..90b017c 100755 --- a/nomsu.moon +++ b/nomsu.moon @@ -194,6 +194,7 @@ class NomsuCompiler @write = (...)=> io.write(...) @write_err = (...)=> io.stderr\write(...) @defs = setmetatable({}, {__index:parent and parent.defs}) + @def_number = 0 @callstack = {} @debug = false @utils = utils @@ -219,6 +220,7 @@ class NomsuCompiler assert type(thunk) == 'function', "Bad thunk: #{repr thunk}" canonical_args = nil aliases = {} + @def_number += 1 for {stub, arg_names} in *signature assert stub, "NO STUB FOUND: #{repr signature}" if @debug then @writeln "#{colored.bright "DEFINING RULE:"} #{colored.underscore colored.magenta repr(stub)} #{colored.bright "WITH ARGS"} #{colored.dim repr(arg_names)}" @@ -228,10 +230,22 @@ class NomsuCompiler assert utils.equivalent(utils.set(arg_names), canonical_args), "Mismatched args" else canonical_args = utils.set(arg_names) insert aliases, stub - @defs[stub] = {:thunk, :stub, :arg_names, :src, :is_macro, :aliases} + @defs[stub] = {:thunk, :stub, :arg_names, :src, :is_macro, :aliases, def_number:@def_number} defmacro: (signature, thunk, src)=> @def(signature, thunk, src, true) + + serialize_defs: (after=0)=> + defs = {} + for _, def in pairs(@defs) + defs[def.def_number] = def.src or "" + keys = utils.keys(defs) + table.sort(keys) + buff = {} + for i in *keys + if i > after and #defs[i] > 0 + insert buff, defs[i] + return concat buff, "\n" call: (stub,line_no,...)=> def = @defs[stub]