aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--code_obj.lua2
-rw-r--r--code_obj.moon2
-rw-r--r--lib/object.nom2
-rw-r--r--nomsu.lua15
-rwxr-xr-xnomsu.moon9
-rw-r--r--nomsu_tree.lua12
-rw-r--r--nomsu_tree.moon5
7 files changed, 22 insertions, 25 deletions
diff --git a/code_obj.lua b/code_obj.lua
index 2aba03d..848ed84 100644
--- a/code_obj.lua
+++ b/code_obj.lua
@@ -291,7 +291,7 @@ do
local _len_0 = 1
for _index_0 = 1, #to_declare do
local v = to_declare[_index_0]
- _accum_0[_len_0] = v:as_lua_id()
+ _accum_0[_len_0] = string.as_lua_id(v.value)
_len_0 = _len_0 + 1
end
return _accum_0
diff --git a/code_obj.moon b/code_obj.moon
index 17ec816..665d0f1 100644
--- a/code_obj.moon
+++ b/code_obj.moon
@@ -170,7 +170,7 @@ class Lua extends Code
gather_from self
if #to_declare > 0
@remove_free_vars to_declare
- @prepend "local #{concat [v\as_lua_id! for v in *to_declare], ", "};\n"
+ @prepend "local #{concat [string.as_lua_id(v.value) for v in *to_declare], ", "};\n"
return to_declare
__tostring: =>
diff --git a/lib/object.nom b/lib/object.nom
index cf0a809..07b3613 100644
--- a/lib/object.nom
+++ b/lib/object.nom
@@ -16,7 +16,7 @@ compile [as %instance %body] to
end
compile [object %classname %class_body] to
- %class_id <- (=lua "Var.as_lua_id({value=\(%classname as value)}):sub(2,-1)")
+ %class_id <- (=lua "string.as_lua_id(\(%classname as value)):sub(2,-1)")
if: %class_id is ""
%class_id <- "class"
%methods <-: Lua ""
diff --git a/nomsu.lua b/nomsu.lua
index b14c7ed..cccb1a6 100644
--- a/nomsu.lua
+++ b/nomsu.lua
@@ -64,6 +64,15 @@ do
Nomsu, Lua, Source = _obj_0.Nomsu, _obj_0.Lua, _obj_0.Source
end
local STDIN, STDOUT, STDERR = "/dev/fd/0", "/dev/fd/1", "/dev/fd/2"
+string.as_lua_id = function(str)
+ return "_" .. (str:gsub("%W", function(c)
+ if c == "_" then
+ return "__"
+ else
+ return ("_%x"):format(c:byte())
+ end
+ end))
+end
FILE_CACHE = setmetatable({ }, {
__index = function(self, filename)
local file = io.open(filename)
@@ -288,9 +297,7 @@ do
local _len_0 = 1
for _index_1 = 1, #stub_args do
local a = stub_args[_index_1]
- _accum_0[_len_0] = fn_arg_positions[Types.Var.as_lua_id({
- value = a
- })]
+ _accum_0[_len_0] = fn_arg_positions[string.as_lua_id(a)]
_len_0 = _len_0 + 1
end
arg_orders[stub] = _accum_0
@@ -767,7 +774,7 @@ do
elseif "Number" == _exp_0 then
return Lua.Value(tree.source, tostring(tree.value))
elseif "Var" == _exp_0 then
- return Lua.Value(tree.source, tree:as_lua_id())
+ return Lua.Value(tree.source, string.as_lua_id(tree.value))
else
return error("Unknown type: " .. tostring(tree.type))
end
diff --git a/nomsu.moon b/nomsu.moon
index 8475ced..1d2bff9 100755
--- a/nomsu.moon
+++ b/nomsu.moon
@@ -52,6 +52,10 @@ debug_getinfo = debug.getinfo
{:Nomsu, :Lua, :Source} = require "code_obj"
STDIN, STDOUT, STDERR = "/dev/fd/0", "/dev/fd/1", "/dev/fd/2"
+string.as_lua_id = (str)->
+ "_"..(str\gsub("%W", (c)-> if c == "_" then "__" else ("_%x")\format(c\byte!)))
+
+
-- TODO:
-- consider non-linear codegen, rather than doing thunks for things like comprehensions
-- type checking?
@@ -308,7 +312,7 @@ class NomsuCompiler
stub = concat(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[Types.Var.as_lua_id {value:a}] for a in *stub_args]
+ arg_orders[stub] = [fn_arg_positions[string.as_lua_id a] for a in *stub_args]
@environment.ARG_ORDERS[fn] = arg_orders
define_compile_action: (signature, fn)=>
@@ -641,12 +645,11 @@ class NomsuCompiler
Lua.Value(tree.source, tostring(tree.value))
when "Var"
- Lua.Value(tree.source, tree\as_lua_id!)
+ Lua.Value(tree.source, string.as_lua_id(tree.value))
else
error("Unknown type: #{tree.type}")
-
tree_to_nomsu: (tree, inline=false, can_use_colon=false)=>
switch tree.type
when "Action"
diff --git a/nomsu_tree.lua b/nomsu_tree.lua
index 9a492f3..dee5cc2 100644
--- a/nomsu_tree.lua
+++ b/nomsu_tree.lua
@@ -107,17 +107,7 @@ Tree("DictEntry", 'multi')
Tree("IndexChain", 'multi')
Tree("Number", 'single')
Tree("Comment", 'single')
-Tree("Var", 'single', {
- as_lua_id = function(self)
- return "_" .. (self.value:gsub("%W", function(c)
- if c == "_" then
- return "__"
- else
- return ("_%x"):format(c:byte())
- end
- end))
- end
-})
+Tree("Var", 'single')
Tree("Action", 'multi', {
get_stub = function(self, include_names)
if include_names == nil then
diff --git a/nomsu_tree.moon b/nomsu_tree.moon
index 5aa8f81..1e662d7 100644
--- a/nomsu_tree.moon
+++ b/nomsu_tree.moon
@@ -57,10 +57,7 @@ Tree "DictEntry", 'multi'
Tree "IndexChain", 'multi'
Tree "Number", 'single'
Tree "Comment", 'single'
-
-Tree "Var", 'single',
- as_lua_id: =>
- "_"..(@value\gsub("%W", (c)-> if c == "_" then "__" else ("_%x")\format(c\byte!)))
+Tree "Var", 'single'
Tree "Action", 'multi',
get_stub: (include_names=false)=>