1 local match, sub, gsub, format, byte, find
4 match, sub, gsub, format, byte, find = _obj_0.match, _obj_0.sub, _obj_0.gsub, _obj_0.format, _obj_0.byte, _obj_0.find
8 local _obj_0 = require("code_obj")
9 LuaCode, Source = _obj_0.LuaCode, _obj_0.Source
11 local SyntaxTree = require("syntax_tree")
12 local Files = require("files")
14 fail_at = require('nomsu_compiler').fail_at
16 local compile_actions = {
17 [""] = function(self, _t, fn, ...)
19 local fn_lua = self:compile(fn)
21 if not (fn_lua:text():match("^%(.*%)$") or fn_lua:text():match("^[_a-zA-Z][_a-zA-Z0-9.]*$")) then
25 for i = 1, select('#', ...) do
29 lua:add(self:compile((select(i, ...))))
34 ["Lua"] = function(self, _t, code)
36 return LuaCode("LuaCode()")
38 if code.type ~= "Text" then
39 return LuaCode("LuaCode:from(", tostring(code.source):as_lua(), ", ", self:compile(code), ")")
42 operate_on_text = function(text)
43 local lua = LuaCode:from(text.source, "LuaCode:from(", tostring(text.source):as_lua())
44 for _index_0 = 1, #text do
45 local bit = text[_index_0]
47 if type(bit) == "string" then
48 bit_lua = bit:as_lua()
49 elseif bit.type == "Text" then
50 bit_lua = operate_on_text(bit)
51 elseif bit.type == "Block" then
52 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)()")
54 bit_lua = self:compile(bit)
56 local bit_leading_len = #(bit_lua:match("^[^\n]*"))
57 lua:add(lua:trailing_line_len() + bit_leading_len > MAX_LINE and ",\n " or ", ")
63 return operate_on_text(code)
65 ["lua >"] = function(self, _t, code)
66 if code.type ~= "Text" then
70 operate_on_text = function(text)
71 local lua = LuaCode:from(text.source)
72 for _index_0 = 1, #text do
73 local bit = text[_index_0]
74 if type(bit) == "string" then
76 elseif bit.type == "Text" then
77 lua:add(operate_on_text(bit))
79 lua:add(self:compile(bit))
84 return operate_on_text(code)
86 ["= lua"] = function(self, _t, code)
87 return self:compile(SyntaxTree({
94 ["1 as lua"] = function(self, _t, code)
95 return LuaCode("_ENV:compile(", self:compile(code), ")")
97 ["use"] = function(self, _t, path)
98 return LuaCode("_ENV:use(" .. tostring(self:compile(path)) .. ")")
100 ["export"] = function(self, _t, path)
101 return LuaCode("_ENV:export(" .. tostring(self:compile(path)) .. ")")
103 ["run"] = function(self, _t, path)
104 return LuaCode("_ENV:run(" .. tostring(self:compile(path)) .. ")")
106 ["test"] = function(self, _t, body)
107 if not (body.type == 'Block') then
108 fail_at(body, "Compile error: This should be a Block")
110 local test_nomsu = body:get_source_code():match(":[ ]*(.*)")
112 local indent = test_nomsu:match("\n([ ]*)")
114 test_nomsu = test_nomsu:gsub("\n" .. indent, "\n")
117 local test_text = self:compile(SyntaxTree({
119 source = body.source,
122 return LuaCode("TESTS[" .. tostring(tostring(body.source):as_lua()) .. "] = ", test_text)
124 ["is jit"] = function(self, _t, code)
125 return LuaCode("jit")
127 ["nomsu environment"] = function(self, _t)
128 return LuaCode("_ENV")
130 ["nomsu environment name"] = function(self, _t)
131 return LuaCode('"_ENV"')
133 ["Lua version"] = function(self, _t)
134 return LuaCode("LUA_API")
136 ["this file was run directly"] = function(self, _t)
137 return LuaCode('WAS_RUN_DIRECTLY')
139 ["the command line arguments"] = function(self, _t)
140 return LuaCode('COMMAND_LINE_ARGS')
143 return compile_actions