Moved as_lua_id() onto 'string'

This commit is contained in:
Bruce Hill 2018-05-30 13:46:40 -07:00
parent 0afcb416bc
commit 624fcdcc7e
7 changed files with 22 additions and 25 deletions

View File

@ -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

View File

@ -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: =>

View File

@ -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 ""

View File

@ -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

View File

@ -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"

View File

@ -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

View File

@ -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)=>