Switched "as_lua_id" to not add an underscore by default, and just add
it manually to variables and A_ as a prefix to actions. Now, by default, classes and method calls don't use the A_ prefix, which means nomsu can more easily play nice with regular Lua objects.
This commit is contained in:
parent
436982c6ba
commit
e64632be1a
@ -94,13 +94,13 @@ test:
|
||||
assume ((foo 1) == "outer")
|
||||
compile [local action %actions %body] to:
|
||||
lua> ".."
|
||||
local fn_name = "A"..string.as_lua_id(\%actions[1].stub)
|
||||
local fn_name = "A_"..string.as_lua_id(\%actions[1].stub)
|
||||
local \%args = table.map(\%actions[1]:get_args(), function(a) return tostring(nomsu:compile(a)) end)
|
||||
local lua = LuaCode(tree.source, fn_name, " = ", \(compile as (%args -> %body)))
|
||||
lua:add_free_vars({fn_name})
|
||||
for i=2,#\%actions do
|
||||
local alias = \%actions[i]
|
||||
local alias_name = "A"..string.as_lua_id(alias.stub)
|
||||
local alias_name = "A_"..string.as_lua_id(alias.stub)
|
||||
lua:add_free_vars({alias_name})
|
||||
local \%alias_args = table.map(alias:get_args(), function(a) return tostring(nomsu:compile(a)) end)
|
||||
lua:append("\\n", alias_name, " = ")
|
||||
@ -125,7 +125,7 @@ test:
|
||||
compile [action %actions %body] to (..)
|
||||
lua> ".."
|
||||
local lua = \(compile as (local action %actions %body))
|
||||
lua:remove_free_vars(table.map(\%actions, function(a) return "A"..string.as_lua_id(a.stub) end))
|
||||
lua:remove_free_vars(table.map(\%actions, function(a) return "A_"..string.as_lua_id(a.stub) end))
|
||||
return lua
|
||||
|
||||
test:
|
||||
@ -209,7 +209,7 @@ compile [%tree as lua return] to (..)
|
||||
Lua value "nomsu:compile(\(%tree as lua expr)):as_statements('return ')"
|
||||
|
||||
compile [remove action %action] to (..)
|
||||
Lua "A\(=lua "string.as_lua_id(\(%action.stub))") = nil"
|
||||
Lua "A_\(=lua "string.as_lua_id(\(%action.stub))") = nil"
|
||||
|
||||
test:
|
||||
assume ("\(\(foo \%x) as nomsu)" == "foo %x") or barf ".."
|
||||
@ -223,8 +223,8 @@ compile [%tree as inline nomsu] to (..)
|
||||
action [%var as lua identifier, %var as lua id] (..)
|
||||
lua> ".."
|
||||
if type(\%var) == 'string' then return string.as_lua_id(\%var)
|
||||
elseif \%var.type == 'Var' then return string.as_lua_id(\%var[1])
|
||||
elseif \%var.type == 'Action' then return "A"..string.as_lua_id(\%var.stub)
|
||||
elseif \%var.type == 'Var' then return "_"..string.as_lua_id(\%var[1])
|
||||
elseif \%var.type == 'Action' then return "A_"..string.as_lua_id(\%var.stub)
|
||||
end
|
||||
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
@ -44,7 +44,7 @@ test:
|
||||
assume ((%d::bark) == "Bark! Bark!")
|
||||
compile [my action %actions %body] to:
|
||||
lua> ".."
|
||||
local fn_name = "A"..string.as_lua_id(\%actions[1].stub)
|
||||
local fn_name = string.as_lua_id(\%actions[1].stub)
|
||||
local \%args = table.map(\%actions[1]:get_args(), function(a) return tostring(nomsu:compile(a)) end)
|
||||
table.insert(\%args, \(\%me as lua id))
|
||||
local lua = LuaCode(tree.source, "class.", fn_name, " = ", \(..)
|
||||
@ -52,7 +52,7 @@ compile [my action %actions %body] to:
|
||||
..)
|
||||
for i=2,#\%actions do
|
||||
local alias = \%actions[i]
|
||||
local alias_name = "A"..string.as_lua_id(alias.stub)
|
||||
local alias_name = string.as_lua_id(alias.stub)
|
||||
local \%alias_args = table.map(alias:get_args(), function(a) return tostring(nomsu:compile(a)) end)
|
||||
table.insert(\%alias_args, \(\%me as lua id))
|
||||
lua:append("\\nclass.", alias_name, " = ")
|
||||
@ -78,15 +78,15 @@ compile [object %classname extends %parent %class_body] to:
|
||||
__tostring=function(cls) return cls.name end,
|
||||
__call=function(cls, inst)
|
||||
inst = setmetatable(inst or {}, cls)
|
||||
if inst.A_set_up then
|
||||
inst:A_set_up()
|
||||
if inst.set_up then
|
||||
inst:set_up()
|
||||
end
|
||||
return inst
|
||||
end,
|
||||
})
|
||||
nomsu["A"..string.as_lua_id("new "..class.name)] = class
|
||||
nomsu["A"..string.as_lua_id("new "..class.name.." 1")] = class
|
||||
nomsu["A"..string.as_lua_id(class.name)] = function() return class end
|
||||
nomsu["A_"..string.as_lua_id("new "..class.name)] = class
|
||||
nomsu["A_"..string.as_lua_id("new "..class.name.." 1")] = class
|
||||
nomsu["A_"..string.as_lua_id(class.name)] = function() return class end
|
||||
class.__index = class
|
||||
class.class = class
|
||||
class.__tostring = function(inst)
|
||||
@ -104,7 +104,7 @@ compile [object %classname extends %parent %class_body] to:
|
||||
["__ipairs"]="__ipairs", ["__pairs"]="__pairs",
|
||||
}
|
||||
for stub,metamethod in pairs(metamethod_map) do
|
||||
class[metamethod] = class["A"..string.as_lua_id(stub)]
|
||||
class[metamethod] = class[string.as_lua_id(stub)]
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -40,7 +40,7 @@ string.as_lua_id = function(str)
|
||||
return format("x%02X", byte(c))
|
||||
end
|
||||
end)
|
||||
return '_' .. str
|
||||
return str
|
||||
end
|
||||
table.map = function(self, fn)
|
||||
local _accum_0 = { }
|
||||
@ -540,10 +540,10 @@ do
|
||||
NomsuCompiler.compile = function(self, tree)
|
||||
if tree.version then
|
||||
do
|
||||
local get_version = self['A' .. string.as_lua_id("Nomsu version")]
|
||||
local get_version = self['A_' .. string.as_lua_id("Nomsu version")]
|
||||
if get_version then
|
||||
do
|
||||
local upgrade = self['A' .. string.as_lua_id("1 upgraded from 2 to 3")]
|
||||
local upgrade = self['A_' .. string.as_lua_id("1 upgraded from 2 to 3")]
|
||||
if upgrade then
|
||||
tree = upgrade(tree, tree.version, get_version())
|
||||
end
|
||||
@ -577,7 +577,13 @@ do
|
||||
return ret
|
||||
end
|
||||
end
|
||||
local lua = LuaCode.Value(tree.source, "A", string.as_lua_id(stub), "(")
|
||||
local lua = LuaCode.Value(tree.source)
|
||||
if tree.target then
|
||||
lua:append(self:compile(tree.target), ":")
|
||||
else
|
||||
lua:append("A_")
|
||||
end
|
||||
lua:append(string.as_lua_id(stub), "(")
|
||||
local args = { }
|
||||
for i, tok in ipairs(tree) do
|
||||
local _continue_0 = false
|
||||
@ -603,10 +609,6 @@ do
|
||||
end
|
||||
lua:concat_append(args, ", ")
|
||||
lua:append(")")
|
||||
if tree.target then
|
||||
local target_lua = self:compile(tree.target)
|
||||
lua:prepend(target_lua, ":")
|
||||
end
|
||||
return lua
|
||||
elseif "EscapedNomsu" == _exp_0 then
|
||||
local lua = LuaCode.Value(tree.source, tree[1].type, "{")
|
||||
@ -769,7 +771,7 @@ do
|
||||
elseif "Number" == _exp_0 then
|
||||
return LuaCode.Value(tree.source, tostring(tree[1]))
|
||||
elseif "Var" == _exp_0 then
|
||||
return LuaCode.Value(tree.source, string.as_lua_id(tree[1]))
|
||||
return LuaCode.Value(tree.source, "_", string.as_lua_id(tree[1]))
|
||||
elseif "FileChunks" == _exp_0 then
|
||||
return error("Cannot convert FileChunks to a single block of lua, since each chunk's " .. "compilation depends on the earlier chunks")
|
||||
else
|
||||
|
@ -35,7 +35,7 @@ string.as_lua_id = (str)->
|
||||
str = gsub str, "%W", (c)->
|
||||
if c == ' ' then '_'
|
||||
else format("x%02X", byte(c))
|
||||
return '_'..str
|
||||
return str
|
||||
|
||||
table.map = (fn)=> [fn(v) for _,v in ipairs(@)]
|
||||
table.fork = (t, values)-> setmetatable(values or {}, {__index:t})
|
||||
@ -338,8 +338,8 @@ with NomsuCompiler
|
||||
|
||||
.compile = (tree)=>
|
||||
if tree.version
|
||||
if get_version = @['A'..string.as_lua_id("Nomsu version")]
|
||||
if upgrade = @['A'..string.as_lua_id("1 upgraded from 2 to 3")]
|
||||
if get_version = @['A_'..string.as_lua_id("Nomsu version")]
|
||||
if upgrade = @['A_'..string.as_lua_id("1 upgraded from 2 to 3")]
|
||||
tree = upgrade(tree, tree.version, get_version!)
|
||||
switch tree.type
|
||||
when "Action"
|
||||
@ -354,7 +354,12 @@ with NomsuCompiler
|
||||
"Compile-time action:\n%s\nfailed to produce any Lua"
|
||||
return ret
|
||||
|
||||
lua = LuaCode.Value(tree.source, "A",string.as_lua_id(stub),"(")
|
||||
lua = LuaCode.Value(tree.source)
|
||||
if tree.target
|
||||
lua\append @compile(tree.target), ":"
|
||||
else
|
||||
lua\append "A_"
|
||||
lua\append(string.as_lua_id(stub),"(")
|
||||
args = {}
|
||||
for i, tok in ipairs tree
|
||||
if type(tok) == "string" then continue
|
||||
@ -372,9 +377,6 @@ with NomsuCompiler
|
||||
insert args, arg_lua
|
||||
lua\concat_append args, ", "
|
||||
lua\append ")"
|
||||
if tree.target
|
||||
target_lua = @compile(tree.target)
|
||||
lua\prepend(target_lua, ":")
|
||||
return lua
|
||||
|
||||
when "EscapedNomsu"
|
||||
@ -496,7 +498,7 @@ with NomsuCompiler
|
||||
return LuaCode.Value(tree.source, tostring(tree[1]))
|
||||
|
||||
when "Var"
|
||||
return LuaCode.Value(tree.source, string.as_lua_id(tree[1]))
|
||||
return LuaCode.Value(tree.source, "_", string.as_lua_id(tree[1]))
|
||||
|
||||
when "FileChunks"
|
||||
error("Cannot convert FileChunks to a single block of lua, since each chunk's "..
|
||||
|
Loading…
Reference in New Issue
Block a user