aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/metaprogramming.nom8
-rw-r--r--nomsu.lua41
-rwxr-xr-xnomsu.moon16
-rw-r--r--nomsu_tree.lua12
-rw-r--r--nomsu_tree.moon7
-rw-r--r--tests/metaprogramming.nom2
6 files changed, 23 insertions, 63 deletions
diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom
index eafe583..475cdeb 100644
--- a/core/metaprogramming.nom
+++ b/core/metaprogramming.nom
@@ -20,7 +20,7 @@ immediately
lua:append("function(tree");
local args = {};
for i,tok in ipairs(\%actions.value[1].value) do
- if tok.type == "Var" then args[#args+1] = nomsu:var_to_lua_identifier(tok.value); end
+ if tok.type == "Var" then args[#args+1] = tok:as_lua(nomsu); end
end
for i, arg in ipairs(args) do
lua:append(", ");
@@ -52,7 +52,7 @@ immediately
lua:append("function(");
local args = {};
for i,tok in ipairs(\%actions.value[1].value) do
- if tok.type == "Var" then args[#args+1] = nomsu:var_to_lua_identifier(tok.value); end
+ if tok.type == "Var" then args[#args+1] = tok:as_lua(nomsu); end
end
for i, arg in ipairs(args) do
lua:append(arg);
@@ -83,7 +83,7 @@ immediately
lua:append("function(tree");
local args = {};
for i,tok in ipairs(\%shorthand.value[1].value) do
- if tok.type == "Var" then args[#args+1] = nomsu:var_to_lua_identifier(tok.value); end
+ if tok.type == "Var" then args[#args+1] = tostring(tok:as_lua(nomsu)); end
end
for i, arg in ipairs(args) do
lua:append(", ");
@@ -157,7 +157,7 @@ immediately
immediately
compile [nomsu] to: Lua value "nomsu"
- compile [%var as lua identifier] to: Lua value "nomsu:var_to_lua_identifier(\(%var as lua expr))"
+ compile [%var as lua identifier] to: Lua value "\(%var as lua expr):as_lua(nomsu)"
# Compiler tools
immediately
diff --git a/nomsu.lua b/nomsu.lua
index 1231b57..e835562 100644
--- a/nomsu.lua
+++ b/nomsu.lua
@@ -277,28 +277,6 @@ do
elseif type(signature) ~= 'table' then
error("Invalid signature, expected list of strings, but got: " .. tostring(repr(signature)), 0)
end
- local stubs
- do
- local _accum_0 = { }
- local _len_0 = 1
- for _index_0 = 1, #signature do
- local alias = signature[_index_0]
- _accum_0[_len_0] = assert(stub_pattern:match(alias))
- _len_0 = _len_0 + 1
- end
- stubs = _accum_0
- end
- local stub_args
- do
- local _accum_0 = { }
- local _len_0 = 1
- for _index_0 = 1, #signature do
- local alias = signature[_index_0]
- _accum_0[_len_0] = assert(var_pattern:match(alias))
- _len_0 = _len_0 + 1
- end
- stub_args = _accum_0
- end
local fn_info = debug_getinfo(fn, "u")
assert(not fn_info.isvararg, "Vararg functions aren't supported. Sorry, use a list instead.")
local fn_arg_positions
@@ -313,14 +291,14 @@ do
for _index_0 = 1, #signature do
local alias = signature[_index_0]
local stub = assert(stub_pattern:match(alias))
- stub_args = assert(var_pattern:match(alias));
+ local stub_args = assert(var_pattern:match(alias));
(is_compile_action and self.environment.COMPILE_ACTIONS or self.environment.ACTIONS)[stub] = fn
do
local _accum_0 = { }
local _len_0 = 1
for _index_1 = 1, #stub_args do
local a = stub_args[_index_1]
- _accum_0[_len_0] = fn_arg_positions[self:var_to_lua_identifier(a)]
+ _accum_0[_len_0] = fn_arg_positions[Types.Var.as_lua_id(a)]
_len_0 = _len_0 + 1
end
arg_orders[stub] = _accum_0
@@ -428,6 +406,7 @@ do
local file = FILE_CACHE[lua_filename]
if file then
ret = self:run_lua(Lua(Source(filename), file))
+ remove(_running_files)
_continue_0 = true
break
end
@@ -515,7 +494,7 @@ do
do
local _tbl_0 = { }
for k, v in pairs(replacements) do
- _tbl_0[self:var_to_lua_identifier(k)] = v
+ _tbl_0[tostring(k:as_lua(self))] = v
end
replacements = _tbl_0
end
@@ -529,18 +508,6 @@ do
end
end)
end,
- var_to_lua_identifier = function(self, var)
- if Types.Var:is_instance(var) then
- var = var.value
- end
- return "_" .. (var:gsub("%W", function(verboten)
- if verboten == "_" then
- return "__"
- else
- return ("_%x"):format(verboten:byte())
- end
- end))
- end,
initialize_core = function(self)
local nomsu = self
self:define_compile_action("immediately %block", function(self, _block)
diff --git a/nomsu.moon b/nomsu.moon
index 79ecd63..bd15a08 100755
--- a/nomsu.moon
+++ b/nomsu.moon
@@ -290,9 +290,6 @@ class NomsuCompiler
elseif type(signature) != 'table'
error("Invalid signature, expected list of strings, but got: #{repr signature}", 0)
- stubs = [assert(stub_pattern\match(alias)) for alias in *signature]
- stub_args = [assert(var_pattern\match(alias)) for alias in *signature]
-
fn_info = debug_getinfo(fn, "u")
assert(not fn_info.isvararg, "Vararg functions aren't supported. Sorry, use a list instead.")
fn_arg_positions = {debug.getlocal(fn, i), i for i=1,fn_info.nparams}
@@ -301,7 +298,7 @@ class NomsuCompiler
stub = assert(stub_pattern\match(alias))
stub_args = assert(var_pattern\match(alias))
(is_compile_action and @environment.COMPILE_ACTIONS or @environment.ACTIONS)[stub] = fn
- arg_orders[stub] = [fn_arg_positions[@var_to_lua_identifier(a)] for a in *stub_args]
+ arg_orders[stub] = [fn_arg_positions[Types.Var.as_lua_id(a)] for a in *stub_args]
@environment.ARG_ORDERS[fn] = arg_orders
define_compile_action: (signature, fn)=>
@@ -370,6 +367,7 @@ class NomsuCompiler
file = FILE_CACHE[lua_filename]
if file
ret = @run_lua(Lua(Source(filename), file))
+ remove _running_files
continue
file = file or FILE_CACHE[filename]
if not file
@@ -427,21 +425,13 @@ class NomsuCompiler
tree_with_replaced_vars: (tree, replacements)=>
return tree unless next(replacements)
if next(replacements).type == "Var"
- replacements = {@var_to_lua_identifier(k),v for k,v in pairs(replacements)}
+ replacements = {tostring(k\as_lua(@)),v for k,v in pairs(replacements)}
tree\map (t)->
if t.type == "Var"
id = tostring(t\as_lua(self))
if replacements[id] != nil
return replacements[id]
- var_to_lua_identifier: (var)=>
- -- Converts arbitrary nomsu vars to valid lua identifiers by replacing illegal
- -- characters with escape sequences
- if Types.Var\is_instance(var)
- var = var.value
- "_"..(var\gsub "%W", (verboten)->
- if verboten == "_" then "__" else ("_%x")\format(verboten\byte!))
-
initialize_core: =>
-- Sets up some core functionality
nomsu = self
diff --git a/nomsu_tree.lua b/nomsu_tree.lua
index 1fd1687..1132dac 100644
--- a/nomsu_tree.lua
+++ b/nomsu_tree.lua
@@ -801,15 +801,17 @@ Tree("Number", {
end
})
Tree("Var", {
- as_lua = function(self, nomsu)
- local lua_id = "_" .. (self.value:gsub("%W", function(verboten)
- if verboten == "_" then
+ as_lua_id = function(v)
+ return "_" .. (v:gsub("%W", function(c)
+ if c == "_" then
return "__"
else
- return ("_%x"):format(verboten:byte())
+ return ("_%x"):format(c:byte())
end
end))
- return Lua.Value(self.source, lua_id)
+ end,
+ as_lua = function(self, nomsu)
+ return Lua.Value(self.source, self.as_lua_id(self.value))
end,
as_nomsu = function(self, inline)
if inline == nil then
diff --git a/nomsu_tree.moon b/nomsu_tree.moon
index f63f036..dd3bb28 100644
--- a/nomsu_tree.moon
+++ b/nomsu_tree.moon
@@ -481,10 +481,11 @@ Tree "Number",
map: (fn)=> fn(self) or self
Tree "Var",
+ as_lua_id: (v)->
+ "_"..(v\gsub("%W", (c)-> if c == "_" then "__" else ("_%x")\format(c\byte!)))
+
as_lua: (nomsu)=>
- lua_id = "_"..(@value\gsub "%W", (verboten)->
- if verboten == "_" then "__" else ("_%x")\format(verboten\byte!))
- Lua.Value(@source, lua_id)
+ Lua.Value(@source, self.as_lua_id(@value))
as_nomsu: (inline=false)=>
return Nomsu(@source, "%", @value)
diff --git a/tests/metaprogramming.nom b/tests/metaprogramming.nom
index 395f4c6..e94a78f 100644
--- a/tests/metaprogramming.nom
+++ b/tests/metaprogramming.nom
@@ -50,7 +50,7 @@ assume ((type of {}) = "table") or barf "type of failed."
assume ((nomsu) = (=lua "nomsu")) or barf "nomsu failed"
-assume (("x" as lua identifier) = (\%x as lua identifier)) or barf "converting to identifier failed."
+assume (((\%x as lua identifier) as text) = "_x") or barf "converting to identifier failed."
assume ((run "return 99") = 99) or barf "run % failed."