Removing dead code and string.as_lua_id() (now just set on string
metametatable)
This commit is contained in:
parent
989e133a57
commit
9e10c8bf00
@ -379,28 +379,6 @@ do
|
||||
return text_methods[i]
|
||||
end
|
||||
end
|
||||
assert(("abc"):matches_1("ab"))
|
||||
local _ = [==[ text_metatable =
|
||||
__mul: (other)=>
|
||||
assert(type(other) == 'number', "Invalid type for multiplication")
|
||||
return rep(@, other)
|
||||
__index: (i)=>
|
||||
-- Use [] for accessing text characters, or s[{3,4}] for s:sub(3,4)
|
||||
if type(i) == 'number' then return sub(@, i, i)
|
||||
elseif type(i) == 'table' then return sub(@, i[1], i[2])
|
||||
else return text_methods[i]
|
||||
__tostring: => @_coalesce![1]
|
||||
__len: => #tostring(@)
|
||||
__concat: (other)=> tostring(@), tostring(other)
|
||||
__len: => #tostring(@)
|
||||
__eq: (other)=>
|
||||
type(@) == type(other) and getmetatable(@) == getmetatable(other) and tostring(@) == tostring(other)
|
||||
__lt: (other)=> tostring(@) < tostring(other)
|
||||
__le: (other)=> tostring(@) <= tostring(other)
|
||||
__newindex: => error("Cannot modify Text")
|
||||
|
||||
Text = (s)-> setmetatable(s, text_metatable)
|
||||
]==]
|
||||
end
|
||||
return {
|
||||
List = List,
|
||||
|
@ -174,30 +174,5 @@ do
|
||||
if type(i) == 'number' then return sub(@, i, i)
|
||||
elseif type(i) == 'table' then return sub(@, i[1], i[2])
|
||||
else return text_methods[i]
|
||||
|
||||
assert(("abc")\matches_1("ab"))
|
||||
|
||||
[==[
|
||||
text_metatable =
|
||||
__mul: (other)=>
|
||||
assert(type(other) == 'number', "Invalid type for multiplication")
|
||||
return rep(@, other)
|
||||
__index: (i)=>
|
||||
-- Use [] for accessing text characters, or s[{3,4}] for s:sub(3,4)
|
||||
if type(i) == 'number' then return sub(@, i, i)
|
||||
elseif type(i) == 'table' then return sub(@, i[1], i[2])
|
||||
else return text_methods[i]
|
||||
__tostring: => @_coalesce![1]
|
||||
__len: => #tostring(@)
|
||||
__concat: (other)=> tostring(@), tostring(other)
|
||||
__len: => #tostring(@)
|
||||
__eq: (other)=>
|
||||
type(@) == type(other) and getmetatable(@) == getmetatable(other) and tostring(@) == tostring(other)
|
||||
__lt: (other)=> tostring(@) < tostring(other)
|
||||
__le: (other)=> tostring(@) <= tostring(other)
|
||||
__newindex: => error("Cannot modify Text")
|
||||
|
||||
Text = (s)-> setmetatable(s, text_metatable)
|
||||
]==]
|
||||
|
||||
return {:List, :Dict, :Text}
|
||||
|
@ -11,7 +11,7 @@ lua> ".."
|
||||
local my_mangle_index = mangle_index
|
||||
mangle_index = mangle_index + 1
|
||||
return function(varname)
|
||||
return string.as_lua_id(varname..(("\\3%X"):format(my_mangle_index)))
|
||||
return (varname..(("\\3%X"):format(my_mangle_index))):as_lua_id()
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -109,13 +109,13 @@ test:
|
||||
assume ((foo 1) == "outer")
|
||||
compile [local action %actions %body] to:
|
||||
lua> ".."
|
||||
local fn_name = string.as_lua_id(\%actions[1].stub)
|
||||
local fn_name = \%actions[1].stub:as_lua_id()
|
||||
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 = string.as_lua_id(alias.stub)
|
||||
local alias_name = alias.stub:as_lua_id()
|
||||
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, " = ")
|
||||
@ -140,7 +140,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 string.as_lua_id(a.stub) end))
|
||||
lua:remove_free_vars(table.map(\%actions, function(a) return a.stub:as_lua_id() end))
|
||||
return lua
|
||||
|
||||
test:
|
||||
@ -224,7 +224,7 @@ compile [%tree as lua return] to (..)
|
||||
Lua value "nomsu:compile(\(%tree as lua expr)):as_statements('return ')"
|
||||
|
||||
compile [remove action %action] to (..)
|
||||
Lua "\(=lua "string.as_lua_id(\(%action.stub))") = nil"
|
||||
Lua "\(=lua "(\(%action.stub)):as_lua_id()") = nil"
|
||||
|
||||
test:
|
||||
assume ("\(\(foo \%x) as nomsu)" == "foo %x") or barf ".."
|
||||
@ -237,9 +237,9 @@ 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 AST.is_syntax_tree(\%var, 'Var') then return string.as_lua_id(\%var[1])
|
||||
elseif AST.is_syntax_tree(\%var, 'Action') then return string.as_lua_id(\%var.stub)
|
||||
if type(\%var) == 'string' then return \%var:as_lua_id()
|
||||
elseif AST.is_syntax_tree(\%var, 'Var') then return \%var[1]:as_lua_id()
|
||||
elseif AST.is_syntax_tree(\%var, 'Action') then return \%var.stub:as_lua_id()
|
||||
else error("Unknown type: "..tostring(\%var))
|
||||
end
|
||||
|
||||
|
@ -44,7 +44,7 @@ test:
|
||||
assume ((%d::bark) == "Bark! Bark!")
|
||||
compile [my action %actions %body] to:
|
||||
lua> ".."
|
||||
local fn_name = string.as_lua_id(\%actions[1].stub)
|
||||
local fn_name = \%actions[1].stub:as_lua_id()
|
||||
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 = string.as_lua_id(alias.stub)
|
||||
local alias_name = alias.stub:as_lua_id()
|
||||
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, " = ")
|
||||
@ -84,9 +84,9 @@ compile [object %classname extends %parent %class_body] to:
|
||||
return inst
|
||||
end,
|
||||
})
|
||||
nomsu[string.as_lua_id("new "..class.name)] = class
|
||||
nomsu[string.as_lua_id("new "..class.name.." 1")] = class
|
||||
nomsu[string.as_lua_id(class.name)] = function() return class end
|
||||
nomsu[("new "..class.name):as_lua_id()] = class
|
||||
nomsu[("new "..class.name.." 1"):as_lua_id()] = class
|
||||
nomsu[class.name:as_lua_id()] = 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[string.as_lua_id(stub)]
|
||||
class[metamethod] = class[stub:as_lua_id()]
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -36,19 +36,6 @@ end
|
||||
local AST = require("syntax_tree")
|
||||
local Parser = require("parser")
|
||||
SOURCE_MAP = { }
|
||||
string.as_lua_id = function(str)
|
||||
str = gsub(str, "^\3*$", "%1\3")
|
||||
str = gsub(str, "x([0-9A-F][0-9A-F])", "x78%1")
|
||||
str = gsub(str, "%W", function(c)
|
||||
if c == ' ' then
|
||||
return '_'
|
||||
else
|
||||
return format("x%02X", byte(c))
|
||||
end
|
||||
end)
|
||||
str = str:gsub("^_*%d", "_%1")
|
||||
return str
|
||||
end
|
||||
table.map = function(t, fn)
|
||||
return setmetatable((function()
|
||||
local _accum_0 = { }
|
||||
@ -475,10 +462,10 @@ do
|
||||
NomsuCompiler.compile = function(self, tree)
|
||||
if tree.version then
|
||||
do
|
||||
local get_version = self[string.as_lua_id("Nomsu version")]
|
||||
local get_version = self[("Nomsu version"):as_lua_id()]
|
||||
if get_version then
|
||||
do
|
||||
local upgrade = self[string.as_lua_id("1 upgraded from 2 to 3")]
|
||||
local upgrade = self[("1 upgraded from 2 to 3"):as_lua_id()]
|
||||
if upgrade then
|
||||
tree = upgrade(tree, tree.version, get_version())
|
||||
end
|
||||
@ -521,7 +508,7 @@ do
|
||||
lua:append("(", target_lua, "):")
|
||||
end
|
||||
end
|
||||
lua:append(string.as_lua_id(stub), "(")
|
||||
lua:append((stub):as_lua_id(), "(")
|
||||
local args = { }
|
||||
for i, tok in ipairs(tree) do
|
||||
local _continue_0 = false
|
||||
@ -709,7 +696,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, (tree[1]):as_lua_id())
|
||||
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
|
||||
|
@ -29,26 +29,6 @@ Parser = require("parser")
|
||||
export SOURCE_MAP
|
||||
SOURCE_MAP = {}
|
||||
|
||||
-- Convert an arbitrary string into a valid Lua identifier. This function is injective,
|
||||
-- but not idempotent, i.e. if (x != y) then (as_lua_id(x) != as_lua_id(y)),
|
||||
-- but as_lua_id(x) is not necessarily equal to as_lua_id(as_lua_id(x))
|
||||
string.as_lua_id = (str)->
|
||||
-- Empty strings are not valid lua identifiers, so treat them like "\3",
|
||||
-- and treat "\3" as "\3\3", etc. to preserve injectivity.
|
||||
str = gsub str, "^\3*$", "%1\3"
|
||||
-- Escape 'x' when it precedes something that looks like an uppercase hex sequence.
|
||||
-- This way, all Lua IDs can be unambiguously reverse-engineered, but normal usage
|
||||
-- of 'x' won't produce ugly Lua IDs.
|
||||
-- i.e. "x" -> "x", "oxen" -> "oxen", but "Hex2Dec" -> "Hex782Dec" and "He-ec" -> "Hex2Dec"
|
||||
str = gsub str, "x([0-9A-F][0-9A-F])", "x78%1"
|
||||
-- Map spaces to underscores, and everything else non-alphanumeric to hex escape sequences
|
||||
str = gsub str, "%W", (c)->
|
||||
if c == ' ' then '_'
|
||||
else format("x%02X", byte(c))
|
||||
-- Lua IDs can't start with numbers, so map "1" -> "_1", "_1" -> "__1", etc.
|
||||
str = str\gsub "^_*%d", "_%1"
|
||||
return str
|
||||
|
||||
table.map = (t, fn)-> setmetatable([fn(v) for _,v in ipairs(t)], getmetatable(t))
|
||||
table.fork = (t, values)-> setmetatable(values or {}, {__index:t})
|
||||
table.copy = (t)-> setmetatable({k,v for k,v in pairs(t)}, getmetatable(t))
|
||||
@ -310,8 +290,8 @@ with NomsuCompiler
|
||||
|
||||
.compile = (tree)=>
|
||||
if tree.version
|
||||
if get_version = @[string.as_lua_id("Nomsu version")]
|
||||
if upgrade = @[string.as_lua_id("1 upgraded from 2 to 3")]
|
||||
if get_version = @[("Nomsu version")\as_lua_id!]
|
||||
if upgrade = @[("1 upgraded from 2 to 3")\as_lua_id!]
|
||||
tree = upgrade(tree, tree.version, get_version!)
|
||||
switch tree.type
|
||||
when "Action"
|
||||
@ -333,7 +313,7 @@ with NomsuCompiler
|
||||
lua\append target_lua, ":"
|
||||
else
|
||||
lua\append "(", target_lua, "):"
|
||||
lua\append(string.as_lua_id(stub),"(")
|
||||
lua\append((stub)\as_lua_id!,"(")
|
||||
args = {}
|
||||
for i, tok in ipairs tree
|
||||
if type(tok) == "string" then continue
|
||||
@ -472,7 +452,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, (tree[1])\as_lua_id!)
|
||||
|
||||
when "FileChunks"
|
||||
error("Cannot convert FileChunks to a single block of lua, since each chunk's "..
|
||||
|
Loading…
Reference in New Issue
Block a user