aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2017-12-09 15:34:52 -0800
committerBruce Hill <bitbucket@bruce-hill.com>2017-12-09 15:34:52 -0800
commit96441251c4bdbb2d9296866e6c84624ce85c2f45 (patch)
tree7b390648a594517fb66735c9824a4f9d2cca3bb6
parent28b6bc188370fe3c20b26c483b48ab70673bd35b (diff)
Pretty much everything is working??
-rw-r--r--lib/metaprogramming.nom14
-rw-r--r--nomsu.lua18
-rwxr-xr-xnomsu.moon13
3 files changed, 27 insertions, 18 deletions
diff --git a/lib/metaprogramming.nom b/lib/metaprogramming.nom
index d1236fe..4ea70c0 100644
--- a/lib/metaprogramming.nom
+++ b/lib/metaprogramming.nom
@@ -5,12 +5,15 @@
# Rule to make rules:
lua> ".."
|nomsu:defmacro("rule %signature = %body", (function(nomsu, vars)
- | local signature = nomsu:get_stubs(nomsu:typecheck(vars, "signature", "List").value);
+ | local signature = {};
+ | for i, alias in ipairs(nomsu:typecheck(vars, "signature", "List").value) do
+ | signature[i] = alias.src;
+ | end
| local body = nomsu:typecheck(vars, "body", "Thunk");
| return ([[
|nomsu:def(%s, %s, %s)
- |]]):format(nomsu:repr(signature), nomsu:tree_to_lua(body), nomsu:repr(\(__src__))), nil;
- |end));
+ |]]):format(nomsu:repr(signature), nomsu:tree_to_lua(body), nomsu:repr(nomsu:dedent(nomsu.defs["#macro_tree"].src))), nil;
+ |end), \(__src__));
# Rule to make nomsu macros:
rule [parse \%shorthand as \%longhand] =:
@@ -109,10 +112,7 @@ rule [help %rule] =:
|if not fn_def then
| nomsu:writeln("Rule not found: "..nomsu:repr(vars.rule));
|else
- | local template = fn_def.is_macro and "compile %s to%s" or "rule %s =%s";
- | local src = fn_def.src or ":\\n <unknown source code>";
- | if src:sub(1,1) ~= ":" and fn_def.is_macro then template = "parse %s as: %s"; end
- | nomsu:writeln(template:format(nomsu:repr(fn_def.stub), src));
+ | nomsu:writeln(fn_def.src or "<unknown source code>");
|end
# Compiler tools
diff --git a/nomsu.lua b/nomsu.lua
index 54a8cbd..5544f66 100644
--- a/nomsu.lua
+++ b/nomsu.lua
@@ -280,9 +280,7 @@ do
def_number = self.__class.def_number,
defs = self.defs
}
- local where_defs_go = (getmetatable(self.defs) or {
- __newindex = self.defs
- }).__newindex
+ local where_defs_go = (getmetatable(self.defs) or { }).__newindex or self.defs
for _index_0 = 1, #signature do
local _des_0 = signature[_index_0]
local stub, arg_names, escaped_args
@@ -325,7 +323,15 @@ do
end,
scoped = function(self, thunk)
local old_defs = self.defs
- self.defs = setmetatable({ }, {
+ local new_defs = {
+ ["#vars"] = setmetatable({ }, {
+ __index = self.defs["#vars"]
+ }),
+ ["#loaded_files"] = setmetatable({ }, {
+ __index = self.defs["#loaded_files"]
+ })
+ }
+ self.defs = setmetatable(new_defs, {
__index = old_defs
})
local ok, ret1, ret2 = pcall(thunk, self)
@@ -1249,8 +1255,8 @@ end)]]):format(concat(lua_bits, "\n"))
local bit = _list_0[_index_0]
if type(bit) == "string" then
insert(concat_parts, bit)
- elseif type(bit) == "table" and bit.type == "FunctionCall" and bit.src == "__src__" then
- insert(concat_parts, repr(self.defs["#macro_tree"].src))
+ elseif bit.src == '__src__' then
+ insert(concat_parts, repr(self:dedent(self.defs["#macro_tree"].src)))
else
local expr, statement = self:tree_to_lua(bit, filename)
if statement then
diff --git a/nomsu.moon b/nomsu.moon
index 285ce48..76cbb94 100755
--- a/nomsu.moon
+++ b/nomsu.moon
@@ -238,7 +238,7 @@ class NomsuCompiler
aliases = {}
@@def_number += 1
def = {:thunk, :src, :is_macro, aliases:{}, def_number:@@def_number, defs:@defs}
- where_defs_go = (getmetatable(@defs) or {__newindex:@defs}).__newindex
+ where_defs_go = (getmetatable(@defs) or {}).__newindex or @defs
for {stub, arg_names, escaped_args} 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)}"
@@ -261,7 +261,10 @@ class NomsuCompiler
scoped: (thunk)=>
old_defs = @defs
- @defs = setmetatable({}, {__index:old_defs})
+ new_defs =
+ ["#vars"]: setmetatable({}, {__index:@defs["#vars"]})
+ ["#loaded_files"]: setmetatable({}, {__index:@defs["#loaded_files"]})
+ @defs = setmetatable(new_defs, {__index:old_defs})
ok, ret1, ret2 = pcall thunk, @
@defs = old_defs
if not ok then @error(ret1)
@@ -825,15 +828,15 @@ end)]])\format(concat(lua_bits, "\n"))
for bit in *code.value
if type(bit) == "string"
insert concat_parts, bit
- elseif type(bit) == "table" and bit.type == "FunctionCall" and bit.src == "__src__"
- insert concat_parts, repr(@defs["#macro_tree"].src)
+ elseif bit.src == '__src__'
+ insert concat_parts, repr(@dedent @defs["#macro_tree"].src)
else
expr, statement = @tree_to_lua bit, filename
if statement
@error "Cannot use [[#{bit.src}]] as a string interpolation value, since it's not an expression."
insert concat_parts, expr
return concat(concat_parts)
-
+
-- Uses named local functions to help out callstack readability
lua_code = (vars)=>
lua = nomsu_string_as_lua(@, vars.code)