-
Notifications
You must be signed in to change notification settings - Fork 3
/
nlconfig.lua
266 lines (228 loc) · 6.88 KB
/
nlconfig.lua
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
local analyzer_config = {
working_directory = input_dir,
inline_require = true,
}
local config = {}
config["build:vscode"] = function()
os.execute(
"cd vscode_extension && yarn && yarn build && code --install-extension nattlua-0.0.1.vsix"
)
end
config.install = function()
-- only linux for now
os.execute("mkdir -p ~/.local/bin")
os.execute("cp build_output.lua ~/.local/bin/nattlua")
os.execute("chmod +x ~/.local/bin/nattlua")
end
config.format = function()
require("format")
end
config.test = function(path)
assert(loadfile("test/run.lua"))(path)
end
config["build-for-ai"] = function(mode)
-- this is just for something like a single file you can paste into gemini 1.5 or chatgpt. gemini's ai studio interface kind of doesn't work with many files, so this is easier.
local paths = {}
for path in (
io.popen("git ls-tree --full-tree --name-only -r HEAD"):read("*a")
):gmatch("(.-)\n") do
if path:find("%.lua") or path:find("%.nlua") then
table.insert(paths, path)
end
end
if mode == "all" then
elseif mode == "core" or mode == "minimal" then
local new_paths = {}
for _, path in ipairs(paths) do
if path:sub(1, #"nattlua/") == "nattlua/" then
table.insert(new_paths, path)
end
end
paths = new_paths
elseif mode == "tests" then
local new_paths = {}
for _, path in ipairs(paths) do
if path:sub(1, #"test/tests/nattlua/analyzer/") == "test/tests/nattlua/analyzer/" then
table.insert(new_paths, path)
end
end
paths = new_paths
end
local summarize = {
["other/fs.lua"] = "a file system library, very similar to lua's io library",
["other/utf8.lua"] = "a polyfill utf8 library",
["other/bit32.lua"] = "a polyfill bit library",
["other/bit.lua"] = "a polyfill bit library",
["nattlua/parser/teal.lua"] = "parser code for dealing with teal syntax, not very relevant",
["nattlua/definitions/glua.nlua"] = "garry's mode lua type defintions, not very relevant",
["nattlua/other/table_clear.lua"] = "polyfill for table.clear",
["nattlua/other/table_new.lua"] = "polyfill for table.new",
["nattlua/other/shallow_copy.lua"] = "helper function to shalow copy a table",
["nattlua/other/table_pool.lua"] = "unused",
["nattlua/cli.lua"] = "code for handling command line arguments, not very relevant",
["nattlua/lexer.lua"] = "tokenizes the code into a table",
["nattlua/emitter.lua"] = "emits a root node back to lua code",
["nattlua/parser/"] = "parses lua code into an AST",
["nattlua/parser.lua"] = "parses lua code into an AST",
}
local tokens = {}
local f = io.open("nattlua_for_ai.lua", "w")
for _, path in ipairs(paths) do
local str
if mode == "minimal" then
for k, v in pairs(summarize) do
if path:find(k, nil, true) then
str = v
break
end
end
end
if not str then
str = io.open(path):read("*a")
str = str:gsub("\n+", "\n")
str = str:gsub("\t", " ")
end
local ext = path:match(".+%.(.+)")
str = "### " .. path .. " ###\n" .. "```" .. ext .. "\n" .. str .. "\n```\n"
f:write(str)
table.insert(tokens, {path = path, count = #str / 2.5})
end
f:close()
table.sort(tokens, function(a, b)
return a.count < b.count
end)
local total_tokens = 0
for path, info in ipairs(tokens) do
print("added " .. info.path .. ": " .. info.count .. " tokens")
total_tokens = total_tokens + info.count
end
print("roughly " .. math.floor(total_tokens) .. "k tokens")
end
config.build = function(mode)
local nl = require("nattlua.init")
local entry = "./nattlua.lua"
io.write("parsing " .. entry)
local c = assert(
nl.Compiler(
[[
_G.ARGS = {...}
if _G.IMPORTS then
for k, v in pairs(_G.IMPORTS) do
if not k:find("/") then package.preload[k] = v end
end
package.preload.nattlua = package.preload["nattlua.init"]
end
require("nattlua.c_declarations.lexer")
require("nattlua.c_declarations.parser")
require("nattlua.c_declarations.emitter")
require("nattlua.c_declarations.analyzer")
require("nattlua.c_declarations.main")
return require("nattlua")
]],
"nattlua",
{
type_annotations = false,
inline_require = true,
emit_environment = true,
}
)
)
local lua_code = c:Emit(
{
preserve_whitespace = false,
string_quote = "\"",
no_semicolon = true,
omit_invalid_code = true,
comment_type_annotations = true,
type_annotations = true,
force_parenthesis = true,
module_encapsulation_method = "loadstring",
extra_indent = {
Start = {to = "Stop"},
Toggle = "toggle",
},
}
)
lua_code = "_G.BUNDLE = true\n" .. lua_code
lua_code = lua_code:gsub("%#%!%/usr%/local%/bin%/luajit\n", "\n")
io.write(" - OK\n")
io.write("output is " .. #lua_code .. " bytes\n")
-- double check that the lua_code is valid
io.write("checking if lua_code is loadable")
local func, err = loadstring(lua_code)
if not func then
io.write(" - FAILED\n")
io.write(err .. "\n")
local f = io.open("temp_build_output.lua", "w")
f:write(lua_code)
f:close()
nl.File("temp_build_output.lua"):Parse()
return
end
io.write(" - OK\n")
if mode ~= "fast" then
-- run tests before we write the file
local f = io.open("temp_build_output.lua", "w")
f:write(lua_code)
f:close()
io.write("running tests with temp_build_output.lua ")
io.flush()
local exit_code = os.execute("luajit -e 'require(\"temp_build_output\") assert(loadfile(\"test/run.lua\"))()'")
if exit_code ~= 0 then
io.write(" - FAIL\n")
return
end
io.write(" - OK\n")
io.write("checking if file can be required outside of the working directory")
io.flush()
local exit_code = os.execute("cd .github && luajit -e 'local nl = loadfile(\"../temp_build_output.lua\")'")
if exit_code ~= 0 then
io.write(" - FAIL\n")
return
end
io.write(" - OK\n")
end
io.write("writing build_output.lua")
local f = assert(io.open("build_output.lua", "w"))
local shebang = "#!/usr/local/bin/luajit\n"
f:write(shebang .. lua_code)
f:close()
os.execute("chmod +x ./build_output.lua")
io.write(" - OK\n")
os.remove("temp_build_output.lua")
end
config["get-analyzer-config"] = function()
local analyzer_config = {}
return analyzer_config
end
config.check = function()
require("test.helpers.profiler").Start()
local nl = require("nattlua.init")
local compiler = assert(
nl.Compiler(
[[return import("]] .. "./nattlua.lua" .. [[")]],
"./nattlua.lua",
analyzer_config
)
)
print("parsing")
compiler:Parse()
for k, v in pairs(compiler.SyntaxTree.imported) do
print(k)
end
print("analyzing")
compiler:Analyze()
for node, res in pairs(compiler.analyzer.analyzed_root_statements) do
local found = false
for path, node2 in pairs(compiler.SyntaxTree.imported) do
if node == node2 then
print("analyzed " .. path)
found = true
break
end
end
if not found then print("cannot find ", node.path) end
end
require("test.helpers.profiler").Stop()
end
return config