code / nomsu

Lines6.6K Lua5.1K PEG1.3K make117
2 others 83
Markdown60 Bourne Again Shell23
(143 lines)
1 local match, sub, gsub, format, byte, find
2 do
3 local _obj_0 = string
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
5 end
6 local LuaCode, Source
7 do
8 local _obj_0 = require("code_obj")
9 LuaCode, Source = _obj_0.LuaCode, _obj_0.Source
10 end
11 local SyntaxTree = require("syntax_tree")
12 local Files = require("files")
13 local fail_at
14 fail_at = require('nomsu_compiler').fail_at
15 local MAX_LINE = 80
16 local compile_actions = {
17 [""] = function(self, _t, fn, ...)
18 local lua = LuaCode()
19 local fn_lua = self:compile(fn)
20 lua:add(fn_lua)
21 if not (fn_lua:text():match("^%(.*%)$") or fn_lua:text():match("^[_a-zA-Z][_a-zA-Z0-9.]*$")) then
22 lua:parenthesize()
23 end
24 lua:add("(")
25 for i = 1, select('#', ...) do
26 if i > 1 then
27 lua:add(", ")
28 end
29 lua:add(self:compile((select(i, ...))))
30 end
31 lua:add(")")
32 return lua
33 end,
34 ["Lua"] = function(self, _t, code)
35 if not code then
36 return LuaCode("LuaCode()")
37 end
38 if code.type ~= "Text" then
39 return LuaCode("LuaCode:from(", tostring(code.source):as_lua(), ", ", self:compile(code), ")")
40 end
41 local operate_on_text
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]
46 local bit_lua
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)()")
53 else
54 bit_lua = self:compile(bit)
55 end
56 local bit_leading_len = #(bit_lua:match("^[^\n]*"))
57 lua:add(lua:trailing_line_len() + bit_leading_len > MAX_LINE and ",\n " or ", ")
58 lua:add(bit_lua)
59 end
60 lua:add(")")
61 return lua
62 end
63 return operate_on_text(code)
64 end,
65 ["lua >"] = function(self, _t, code)
66 if code.type ~= "Text" then
67 return code
68 end
69 local operate_on_text
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
75 lua:add(bit)
76 elseif bit.type == "Text" then
77 lua:add(operate_on_text(bit))
78 else
79 lua:add(self:compile(bit))
80 end
81 end
82 return lua
83 end
84 return operate_on_text(code)
85 end,
86 ["= lua"] = function(self, _t, code)
87 return self:compile(SyntaxTree({
88 type = "Action",
89 "lua",
90 ">",
91 code
92 }))
93 end,
94 ["1 as lua"] = function(self, _t, code)
95 return LuaCode("_ENV:compile(", self:compile(code), ")")
96 end,
97 ["use"] = function(self, _t, path)
98 return LuaCode("_ENV:use(" .. tostring(self:compile(path)) .. ")")
99 end,
100 ["export"] = function(self, _t, path)
101 return LuaCode("_ENV:export(" .. tostring(self:compile(path)) .. ")")
102 end,
103 ["run"] = function(self, _t, path)
104 return LuaCode("_ENV:run(" .. tostring(self:compile(path)) .. ")")
105 end,
106 ["test"] = function(self, _t, body)
107 if not (body.type == 'Block') then
108 fail_at(body, "Compile error: This should be a Block")
109 end
110 local test_nomsu = body:get_source_code():match(":[ ]*(.*)")
112 local indent = test_nomsu:match("\n([ ]*)")
113 if indent then
114 test_nomsu = test_nomsu:gsub("\n" .. indent, "\n")
115 end
116 end
117 local test_text = self:compile(SyntaxTree({
118 type = "Text",
119 source = body.source,
120 test_nomsu
121 }))
122 return LuaCode("TESTS[" .. tostring(tostring(body.source):as_lua()) .. "] = ", test_text)
123 end,
124 ["is jit"] = function(self, _t, code)
125 return LuaCode("jit")
126 end,
127 ["nomsu environment"] = function(self, _t)
128 return LuaCode("_ENV")
129 end,
130 ["nomsu environment name"] = function(self, _t)
131 return LuaCode('"_ENV"')
132 end,
133 ["Lua version"] = function(self, _t)
134 return LuaCode("LUA_API")
135 end,
136 ["this file was run directly"] = function(self, _t)
137 return LuaCode('WAS_RUN_DIRECTLY')
138 end,
139 ["the command line arguments"] = function(self, _t)
140 return LuaCode('COMMAND_LINE_ARGS')
141 end
143 return compile_actions