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