aboutsummaryrefslogtreecommitdiff
path: root/bootstrap.moon
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2019-01-10 16:33:37 -0800
committerBruce Hill <bruce@bruce-hill.com>2019-01-10 16:35:08 -0800
commit0f0fb2256a46a8808794d7d4746d112278da3730 (patch)
tree7cfb6b255beeb49705044876913e0332376b66d9 /bootstrap.moon
parentdb552f56dc1f2c6ea19a7d39d38ac66e52ed156e (diff)
Major overhaul of how modules and environments work, along with some
steamlining and tweaks to the makefile. Version bump: 6.14.13.8
Diffstat (limited to 'bootstrap.moon')
-rw-r--r--bootstrap.moon125
1 files changed, 125 insertions, 0 deletions
diff --git a/bootstrap.moon b/bootstrap.moon
new file mode 100644
index 0000000..66c8553
--- /dev/null
+++ b/bootstrap.moon
@@ -0,0 +1,125 @@
+-- This file contains a set of compile actions needed for bootstrapping
+{:match, :sub, :gsub, :format, :byte, :find} = string
+{:LuaCode, :Source} = require "code_obj"
+SyntaxTree = require "syntax_tree"
+Files = require "files"
+
+-- TODO: de-duplicate this
+pretty_error = require("pretty_errors")
+compile_error = (source, err_msg, hint=nil)->
+ local file
+ if SyntaxTree\is_instance(source)
+ file = source\get_source_file!
+ source = source.source
+ elseif type(source) == 'string'
+ source = Source\from_string(source)
+ if source and not file
+ file = Files.read(source.filename)
+
+ err_str = pretty_error{
+ title: "Compile error"
+ error:err_msg, hint:hint, source:file
+ start:source.start, stop:source.stop, filename:source.filename
+ }
+ error(err_str, 0)
+
+MAX_LINE = 80 -- For beautification purposes, try not to make lines much longer than this value
+--ENVNAME = jit and "getfenv(1)" or "_ENV"
+ENVNAME = jit and "_G" or "_ENV"
+compile_actions = {
+ [""]: (fn, ...)=>
+ lua = LuaCode!
+ fn_lua = @compile(fn)
+ lua\add fn_lua
+ unless fn_lua\text!\match("^%(.*%)$") or fn_lua\text!\match("^[_a-zA-Z][_a-zA-Z0-9.]*$")
+ lua\parenthesize!
+ lua\add "("
+ for i=1,select('#',...)
+ lua\add(", ") if i > 1
+ lua\add @compile((select(i, ...)))
+ lua\add ")"
+ return lua
+
+ ["Lua"]: (code)=>
+ if not code
+ return LuaCode("LuaCode()")
+ if code.type != "Text"
+ return LuaCode("LuaCode:from(", tostring(code.source)\as_lua!, ", ", @compile(code), ")")
+
+ operate_on_text = (text)->
+ lua = LuaCode\from(text.source, "LuaCode:from(", tostring(text.source)\as_lua!)
+ for bit in *text
+ local bit_lua
+ if type(bit) == "string"
+ bit_lua = bit\as_lua!
+ elseif bit.type == "Text"
+ bit_lua = operate_on_text(bit)
+ elseif bit.type == "Block"
+ 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 ", @compile(bit),
+ "\n return _lua",
+ "\nend)()"
+ else
+ bit_lua = @compile(bit)
+
+ 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)
+ lua\add ")"
+ return lua
+
+ return operate_on_text code
+
+ ["lua >"]: (code)=>
+ if code.type != "Text"
+ return code
+ operate_on_text = (text)->
+ lua = LuaCode\from(text.source)
+ for bit in *text
+ if type(bit) == "string"
+ lua\add bit
+ elseif bit.type == "Text"
+ lua\add(operate_on_text(bit))
+ else
+ lua\add @compile(bit)
+ return lua
+ return operate_on_text code
+
+ ["= lua"]: (code)=>
+ @compile(SyntaxTree{type:"Action", "lua", ">", code})
+
+ ["1 as lua"]: (code)=>
+ LuaCode("#{ENVNAME}:compile(", @compile(code), ")")
+
+ ["use"]: (path)=>
+ LuaCode("#{ENVNAME}:use(#{@compile(path)})")
+
+ ["export"]: (path)=>
+ LuaCode("#{ENVNAME}:export(#{@compile(path)})")
+
+ ["run"]: (path)=>
+ LuaCode("#{ENVNAME}:run(#{@compile(path)})")
+
+ ["test"]: (body)=>
+ unless body.type == 'Block'
+ compile_error(body, "This should be a Block")
+ test_nomsu = body\get_source_code!\match(":[ ]*(.*)")
+ if indent = test_nomsu\match("\n([ ]*)")
+ test_nomsu = test_nomsu\gsub("\n"..indent, "\n")
+ test_text = @compile(SyntaxTree{type:"Text", source:body.source, test_nomsu})
+ return LuaCode "TESTS[#{tostring(body.source)\as_lua!}] = ", test_text
+
+ ["is jit"]: (code)=> LuaCode("jit")
+ ["Lua version"]: (code)=> LuaCode("_VERSION")
+ ["nomsu environment"]: ()=> LuaCode(ENVNAME)
+ ["nomsu environment name"]: ()=> LuaCode(ENVNAME\as_lua!)
+}
+
+return compile_actions