From 0f0fb2256a46a8808794d7d4746d112278da3730 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Thu, 10 Jan 2019 16:33:37 -0800 Subject: Major overhaul of how modules and environments work, along with some steamlining and tweaks to the makefile. Version bump: 6.14.13.8 --- bootstrap.lua | 163 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 bootstrap.lua (limited to 'bootstrap.lua') diff --git a/bootstrap.lua b/bootstrap.lua new file mode 100644 index 0000000..844bcca --- /dev/null +++ b/bootstrap.lua @@ -0,0 +1,163 @@ +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 pretty_error = require("pretty_errors") +local compile_error +compile_error = function(source, err_msg, hint) + if hint == nil then + hint = nil + end + local file + if SyntaxTree:is_instance(source) then + file = source:get_source_file() + source = source.source + elseif type(source) == 'string' then + source = Source:from_string(source) + end + if source and not file then + file = Files.read(source.filename) + end + local err_str = pretty_error({ + title = "Compile error", + error = err_msg, + hint = hint, + source = file, + start = source.start, + stop = source.stop, + filename = source.filename + }) + return error(err_str, 0) +end +local MAX_LINE = 80 +local ENVNAME = jit and "_G" or "_ENV" +local compile_actions = { + [""] = function(self, 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, + ["Lua"] = function(self, 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, 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, code) + return self:compile(SyntaxTree({ + type = "Action", + "lua", + ">", + code + })) + end, + ["1 as lua"] = function(self, code) + return LuaCode(tostring(ENVNAME) .. ":compile(", self:compile(code), ")") + end, + ["use"] = function(self, path) + return LuaCode(tostring(ENVNAME) .. ":use(" .. tostring(self:compile(path)) .. ")") + end, + ["export"] = function(self, path) + return LuaCode(tostring(ENVNAME) .. ":export(" .. tostring(self:compile(path)) .. ")") + end, + ["run"] = function(self, path) + return LuaCode(tostring(ENVNAME) .. ":run(" .. tostring(self:compile(path)) .. ")") + end, + ["test"] = function(self, body) + if not (body.type == 'Block') then + compile_error(body, "This should be a Block") + end + local test_nomsu = body:get_source_code():match(":[ ]*(.*)") + do + local indent = test_nomsu:match("\n([ ]*)") + if indent then + test_nomsu = test_nomsu:gsub("\n" .. indent, "\n") + end + end + local test_text = self:compile(SyntaxTree({ + type = "Text", + source = body.source, + test_nomsu + })) + return LuaCode("TESTS[" .. tostring(tostring(body.source):as_lua()) .. "] = ", test_text) + end, + ["is jit"] = function(self, code) + return LuaCode("jit") + end, + ["Lua version"] = function(self, code) + return LuaCode("_VERSION") + end, + ["nomsu environment"] = function(self) + return LuaCode(ENVNAME) + end, + ["nomsu environment name"] = function(self) + return LuaCode(ENVNAME:as_lua()) + end +} +return compile_actions -- cgit v1.2.3