123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 | local match, sub, gsub, format, byte, find
do
local _obj_0 = string
match, sub, gsub, format, byte, find = _obj_0.match, _obj_0.sub, _obj_0.gsub, _obj_0.format, _obj_0.byte, _obj_0.find
end
local LuaCode, Source
do
local _obj_0 = require("code_obj")
LuaCode, Source = _obj_0.LuaCode, _obj_0.Source
end
local SyntaxTree = require("syntax_tree")
local Files = require("files")
local fail_at
fail_at = require('nomsu_compiler').fail_at
local unpack = unpack or table.unpack
local MAX_LINE = 80
local compile_definitions
compile_definitions = function(self, body)
local statements = body.type == "Block" and body or {
body
}
local names, seen, rest = { }, { }, { }
local add_name
add_name = function(name)
if not (seen[name]) then
names[#names + 1] = name
seen[name] = true
end
end
for _index_0 = 1, #statements do
local statement = statements[_index_0]
if statement.type == "Var" then
add_name(self:compile(statement):text())
elseif statement.type == "Table" and #statement > 0 and #(function()
local _accum_0 = { }
local _len_0 = 1
for _index_1 = 1, #statement do
local v = statement[_index_1]
if v.type == "Var" then
_accum_0[_len_0] = v
_len_0 = _len_0 + 1
end
end
return _accum_0
end)() == #statement then
for _index_1 = 1, #statement do
local v = statement[_index_1]
add_name(self:compile(v):text())
end
else
rest[#rest + 1] = statement
end
end
local lua = LuaCode:from(body.source)
if #rest > 0 then
local rest_lua = self:compile(SyntaxTree({
type = "Block",
source = body.source,
unpack(rest)
}))
local _list_0 = rest_lua:get_free_vars()
for _index_0 = 1, #_list_0 do
local v = _list_0[_index_0]
add_name(v)
end
rest_lua:remove_free_vars()
lua:add(rest_lua)
end
return lua, names
end
local compile_call
compile_call = function(self, _t, fn, ...)
local lua = LuaCode()
local fn_lua = self:compile(fn)
lua:add(fn_lua)
if not (fn_lua:text():match("^%(.*%)$") or fn_lua:text():match("^[_a-zA-Z][_a-zA-Z0-9.]*$")) then
lua:parenthesize()
end
lua:add("(")
for i = 1, select('#', ...) do
if i > 1 then
lua:add(", ")
end
lua:add(self:compile((select(i, ...))))
end
lua:add(")")
return lua
end
local compile_actions = {
[""] = compile_call,
["call"] = compile_call,
["Lua"] = function(self, _t, code)
if not code then
return LuaCode("LuaCode()")
end
if code.type ~= "Text" then
return LuaCode("LuaCode:from(", tostring(code.source):as_lua(), ", ", self:compile(code), ")")
end
local operate_on_text
operate_on_text = function(text)
local lua = LuaCode:from(text.source, "LuaCode:from(", tostring(text.source):as_lua())
for _index_0 = 1, #text do
local bit = text[_index_0]
local bit_lua
if type(bit) == "string" then
bit_lua = bit:as_lua()
elseif bit.type == "Text" then
bit_lua = operate_on_text(bit)
elseif bit.type == "Block" then
bit_lua = LuaCode:from(bit.source, "(function()", "\n local _lua = LuaCode:from(", tostring(bit.source):as_lua(), ")", "\n local function add(...) _lua:add(...) end", "\n local function join_with(glue)", "\n local old_bits = _lua.bits", "\n _lua = LuaCode:from(_lua.source)", "\n _lua:concat_add(old_bits, glue)", "\n end", "\n ", self:compile(bit), "\n return _lua", "\nend)()")
else
bit_lua = self:compile(bit)
end
local bit_leading_len = #(bit_lua:match("^[^\n]*"))
lua:add(lua:trailing_line_len() + bit_leading_len > MAX_LINE and ",\n " or ", ")
lua:add(bit_lua)
end
lua:add(")")
return lua
end
return operate_on_text(code)
end,
["lua >"] = function(self, _t, code)
if code.type ~= "Text" then
return code
end
local operate_on_text
operate_on_text = function(text)
local lua = LuaCode:from(text.source)
for _index_0 = 1, #text do
local bit = text[_index_0]
if type(bit) == "string" then
lua:add(bit)
elseif bit.type == "Text" then
lua:add(operate_on_text(bit))
else
lua:add(self:compile(bit))
end
end
return lua
end
return operate_on_text(code)
end,
["= lua"] = function(self, _t, code)
return self:compile(SyntaxTree({
type = "Action",
"lua",
">",
code
}))
end,
["1 as lua"] = function(self, _t, code)
return LuaCode("_ENV:compile(", self:compile(code), ")")
end,
["use"] = function(self, _t, path)
return LuaCode("_ENV:use(" .. tostring(self:compile(path)) .. ")")
end,
["export"] = function(self, _t, path)
return LuaCode("_ENV:export(" .. tostring(self:compile(path)) .. ")")
end,
["at compile time"] = function(self, _t, body)
local lua = self:compile(body)
lua:remove_free_vars()
return lua
end,
["local"] = function(self, _t, body)
local lua, names = compile_definitions(self, body)
lua.forced_locals = names
return lua
end,
["local 1 ="] = function(self, t, var, value)
return self:compile(SyntaxTree({
type = "Action",
source = t.source,
"local",
SyntaxTree({
type = "Action",
source = var.source,
var,
"=",
value
})
}))
end,
["public"] = function(self, _t, body)
local lua, names = compile_definitions(self, body)
lua.public_vars = names
if lua:text():match("COMPILE_RULES") then
lua:prepend("_ENV:begin_public_rules();\n")
lua:add("\n_ENV:end_public_rules();")
end
return lua
end,
["public 1 ="] = function(self, t, var, value)
return self:compile(SyntaxTree({
type = "Action",
source = t.source,
"public",
SyntaxTree({
type = "Action",
source = var.source,
var,
"=",
value
})
}))
end,
["run"] = function(self, _t, path)
return LuaCode("_ENV:run(" .. tostring(self:compile(path)) .. ")")
end,
["test"] = function(self, _t, body)
if not (body.type == 'Block') then
fail_at(body, "Compile error: This should be a Block")
end
if self.COMPILING_TESTS then
table.insert(self._tests_to_compile, body)
end
return LuaCode("")
end,
["is jit"] = function(self, _t, code)
return LuaCode("jit")
end,
["nomsu environment"] = function(self, _t)
return LuaCode("_ENV")
end,
["nomsu environment name"] = function(self, _t)
return LuaCode('"_ENV"')
end,
["Lua version"] = function(self, _t)
return LuaCode("LUA_API")
end,
["this file was run directly"] = function(self, _t)
return LuaCode('WAS_RUN_DIRECTLY')
end,
["the command line arguments"] = function(self, _t)
return LuaCode('COMMAND_LINE_ARGS')
end
}
return compile_actions
|