1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
if NOMSU_VERSION and NOMSU_PREFIX then
local ver_bits
do
local _accum_0 = { }
local _len_0 = 1
for ver_bit in NOMSU_VERSION:gmatch("[0-9]+") do
_accum_0[_len_0] = ver_bit
_len_0 = _len_0 + 1
end
ver_bits = _accum_0
end
local partial_vers
do
local _accum_0 = { }
local _len_0 = 1
for i = #ver_bits, 1, -1 do
_accum_0[_len_0] = table.concat(ver_bits, '.', 1, i)
_len_0 = _len_0 + 1
end
partial_vers = _accum_0
end
package.path = table.concat((function()
local _accum_0 = { }
local _len_0 = 1
for _index_0 = 1, #partial_vers do
local v = partial_vers[_index_0]
_accum_0[_len_0] = tostring(NOMSU_PREFIX) .. "/share/nomsu/" .. tostring(v) .. "/?.lua"
_len_0 = _len_0 + 1
end
return _accum_0
end)(), ";") .. ";" .. package.path
package.cpath = table.concat((function()
local _accum_0 = { }
local _len_0 = 1
for _index_0 = 1, #partial_vers do
local v = partial_vers[_index_0]
_accum_0[_len_0] = tostring(NOMSU_PREFIX) .. "/lib/nomsu/" .. tostring(v) .. "/?.so"
_len_0 = _len_0 + 1
end
return _accum_0
end)(), ";") .. ";" .. package.cpath
package.nomsupath = table.concat((function()
local _accum_0 = { }
local _len_0 = 1
for _index_0 = 1, #partial_vers do
local v = partial_vers[_index_0]
_accum_0[_len_0] = tostring(NOMSU_PREFIX) .. "/share/nomsu/" .. tostring(v)
_len_0 = _len_0 + 1
end
return _accum_0
end)(), ";")
end
local EXIT_SUCCESS, EXIT_FAILURE = 0, 1
local usage = [=[Nomsu Compiler
Usage: (lua nomsu.lua | moon nomsu.moon) [-V version] [-i] [-O] [-v] [-c] [-f] [-s] [--help] [--version] [-p print_file] file1 file2... [-- nomsu args...]
OPTIONS
-i Run the compiler in interactive mode (REPL)
-O Run the compiler in optimized mode (use precompiled .lua versions of Nomsu files, when available)
-v Verbose: print compiled lua code
-c Compile .nom files into .lua files
-f Auto-format the given Nomsu file and print the result.
-s Check the program for syntax errors.
-h/--help Print this message.
--version Print the version number and exit.
-V specify which Nomsu version is desired
-p <file> Print to the specified file instead of stdout.
<input> Input file can be "-" to use stdin.
]=]
local ok, _ = pcall(function()
lpeg = require('lpeg')
re = require('re')
end)
if not ok then
print("Error: unable to find the 'lpeg' Lua module. Please install LPEG either from http://www.inf.puc-rio.br/~roberto/lpeg/re.html or, if you use luarocks: `luarocks install lpeg`")
os.exit(EXIT_FAILURE)
end
local Errhand = require("error_handling")
local NomsuCompiler = require("nomsu_compiler")
local NomsuCode, LuaCode, Source
do
local _obj_0 = require("code_obj")
NomsuCode, LuaCode, Source = _obj_0.NomsuCode, _obj_0.LuaCode, _obj_0.Source
end
local repr
repr = require("utils").repr
if not arg or debug.getinfo(2).func == require then
return NomsuCompiler
end
local parser = re.compile([[ args <- {| (flag ";")* {:inputs: {| ({file} ";")* |} :} {:nomsu_args: {| ("--;" ({[^;]*} ";")*)? |} :} ";"? |} !.
flag <-
{:interactive: ("-i" -> true) :}
/ {:optimized: ("-O" -> true) :}
/ {:format: ("-f" -> true) :}
/ {:syntax: ("-s" -> true) :}
/ {:print_file: "-p" ";" {file} :}
/ {:compile: ("-c" -> true) :}
/ {:verbose: ("-v" -> true) :}
/ {:help: (("-h" / "--help") -> true) :}
/ {:version: ("--version" -> true) :}
/ {:requested_version: "-V" ((";")? {([0-9.])+})? :}
file <- "-" / [^;]+
]], {
["true"] = function()
return true
end
})
local arg_string = table.concat(arg, ";") .. ";"
local args = parser:match(arg_string)
if not args or args.help then
print(usage)
os.exit(EXIT_FAILURE)
end
local files = require("files")
local nomsu = NomsuCompiler
nomsu.arg = args.nomsu_args
if args.version then
nomsu:run([[use "core"
say (Nomsu version)]])
os.exit(EXIT_SUCCESS)
end
FILE_CACHE = setmetatable({ }, {
__index = function(self, filename)
local file = io.open(filename)
if not (file) then
return nil
end
local contents = file:read("*a")
file:close()
self[filename] = contents
return contents
end
})
local run
run = function()
for i, input in ipairs(args.inputs) do
if input == "-" then
args.inputs[i] = 'stdin'
end
end
if #args.inputs == 0 and not args.interactive then
args.inputs = {
"core"
}
args.interactive = true
end
local print_file
if args.print_file == "-" then
print_file = io.stdout
elseif args.print_file then
print_file = io.open(args.print_file, 'w')
else
print_file = io.stdout
end
if print_file == nil then
nomsu.print = function() end
elseif print_file ~= io.stdout then
nomsu.print = function(...)
local N = select("#", ...)
if N > 0 then
print_file:write(tostring(select(1, ...)))
for i = 2, N do
print_file:write('\t', tostring(select(1, ...)))
end
end
print_file:write('\n')
return print_file:flush()
end
end
local input_files = { }
local to_run = { }
local _list_0 = args.inputs
for _index_0 = 1, #_list_0 do
local _continue_0 = false
repeat
local input = _list_0[_index_0]
if input == 'stdin' then
input_files[#input_files + 1] = 'stdin'
to_run['stdin'] = true
_continue_0 = true
break
end
local found = false
for f in files.walk(input) do
input_files[#input_files + 1] = f
to_run[f] = true
found = true
end
if not found then
error("Could not find: " .. tostring(input))
end
_continue_0 = true
until true
if not _continue_0 then
break
end
end
nomsu.can_optimize = function(f)
if not (args.optimized) then
return false
end
if to_run[f] then
return false
end
return true
end
local parse_errs = { }
local _list_1 = args.inputs
for _index_0 = 1, #_list_1 do
local arg = _list_1[_index_0]
for filename in files.walk(arg) do
local _continue_0 = false
repeat
local file, source
if filename == 'stdin' then
file = io.read("*a")
files.spoof('stdin', file)
source = Source('stdin', 1, #file)
elseif filename:match("%.nom$") then
file = files.read(filename)
if not file then
error("File does not exist: " .. tostring(filename), 0)
end
source = Source(filename, 1, #file)
else
_continue_0 = true
break
end
source = Source(filename, 1, #file)
local output
if args.compile then
output = io.open(filename:gsub("%.nom$", ".lua"), "w")
else
output = nil
end
if args.syntax then
local err
ok, err = pcall(nomsu.parse, nomsu, file, source)
if not ok then
table.insert(parse_errs, err)
elseif print_file then
print_file:write("Parse succeeded: " .. tostring(filename) .. "\n")
print_file:flush()
end
_continue_0 = true
break
end
local tree = nomsu:parse(file, source)
if args.format then
local formatted = tree and tostring(nomsu:tree_to_nomsu(tree)) or ""
if print_file then
print_file:write(formatted, "\n")
print_file:flush()
end
_continue_0 = true
break
end
if tree then
if tree.type == "FileChunks" then
for _index_1 = 1, #tree do
local chunk = tree[_index_1]
local lua = nomsu:compile(chunk):as_statements("return ")
lua:declare_locals()
lua:prepend("-- File: " .. tostring(source.filename:gsub("\n.*", "...")) .. "\n")
if args.compile then
output:write(tostring(lua), "\n")
end
if args.verbose then
print(tostring(lua))
end
nomsu:run_lua(lua)
end
else
local lua = nomsu:compile(tree):as_statements("return ")
lua:declare_locals()
lua:prepend("-- File: " .. tostring(source.filename:gsub("\n.*", "...")) .. "\n")
if args.compile then
output:write(tostring(lua), "\n")
end
if args.verbose then
print(tostring(lua))
end
nomsu:run_lua(lua)
end
end
if args.compile then
print(("Compiled %-25s -> %s"):format(filename, filename:gsub("%.nom$", ".lua")))
output:close()
end
_continue_0 = true
until true
if not _continue_0 then
break
end
end
end
if #parse_errs > 0 then
io.stderr:write(table.concat(parse_errs, "\n\n"))
io.stderr:flush()
os.exit(EXIT_FAILURE)
elseif args.syntax then
os.exit(EXIT_SUCCESS)
end
if args.interactive then
nomsu:run([[use "core"
use "lib/consolecolor.nom"
action [quit, exit]: lua> "os.exit(0)"
action [help]
say ".."
This is the Nomsu v\(Nomsu version) interactive console.
You can type in Nomsu code here and hit 'enter' twice to run it.
To exit, type 'exit' or 'quit' and hit enter twice.
say ".."
\(bright)\(underscore)Welcome to the Nomsu v\(Nomsu version) interactive console!\(reset color)
press 'enter' twice to run a command
\("")]])
for repl_line = 1, math.huge do
io.write(colored.bright(colored.yellow(">> ")))
local buff = { }
while true do
io.write(colors.bright)
local line = io.read("*L")
io.write(colors.reset)
if line == "\n" or not line then
if #buff > 0 then
io.write("\027[1A\027[2K")
end
break
end
line = line:gsub("\t", " ")
table.insert(buff, line)
io.write(colored.dim(colored.yellow(".. ")))
end
if #buff == 0 then
break
end
buff = table.concat(buff)
local pseudo_filename = "user input #" .. repl_line
files.spoof(pseudo_filename, buff)
local err_hand
err_hand = function(error_message)
return Errhand.print_error(error_message)
end
local ret
ok, ret = xpcall(nomsu.run, err_hand, nomsu, buff, Source(pseudo_filename, 1, #buff))
if ok and ret ~= nil then
print("= " .. repr(ret))
elseif not ok then
Errhand.print_error(ret)
end
end
end
end
local has_ldt, ldt = pcall(require, 'ldt')
if has_ldt then
return ldt.guard(run)
else
return Errhand.run_safely(run)
end
|