Skip to content

Commit 1eb3f84

Browse files
committed
compilers/source: Add inject_extensions() and readjust extension acquiring
1 parent 036e009 commit 1eb3f84

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

fusion/core/compilers/source.lua

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,26 @@ local _tablegen_level = 0
6161
handlers['nil'] = function() return 'nil' end
6262
handlers['vararg'] = function() return '...' end
6363

64-
local dirs = {
65-
class = 'local class = require("fusion.stdlib.class")';
66-
fnl = 'local fnl = require("fusion.stdlib.functional")';
67-
itr = 'local itr = require("fusion.stdlib.iterable")';
68-
re = 'local re = require("re")';
69-
ternary = 'local ternary = require("fusion.stdlib.ternary")';
64+
compiler.extensions = {
65+
class = "fusion.stdlib.class";
66+
fnl = "fusion.stdlib.functional";
67+
itr = "fusion.stdlib.iterable";
68+
re = "re";
69+
ternary = "fusion.stdlib.ternary";
7070
}
71+
local ext_pat = "local %s = require(%q)"
7172

7273
handlers['using'] = function(self, node) -- TODO: no repeat?
7374
local output = {}
7475
if node[1] == "*" then
75-
for _, directive in pairs(dirs) do
76-
output[#output + 1] = directive
76+
for name, module in pairs(self.extensions) do
77+
output[#output + 1] = ext_pat:format(name, module)
7778
end
7879
table.sort(output) -- consistency, helps w/ tests
7980
else
80-
for _, directive in ipairs(node) do
81-
output[#output + 1] = dirs[directive]
81+
for _, extension in ipairs(node) do
82+
output[#output + 1] = ext_pat:format(extension,
83+
self.extensions[extension])
8284
end
8385
end
8486
return table.concat(output, self:l"\n")
@@ -705,4 +707,12 @@ if not package.fusepath then -- luacheck: ignore 143
705707
package.fusepath_t = paths -- luacheck: ignore 142
706708
end
707709

710+
--- Inject some global variables into the runtime. The `fusion-source` program
711+
-- does this already.
712+
function compiler.inject_extensions()
713+
for name, module in pairs(compiler.extensions) do
714+
_G[name] = require(module)
715+
end
716+
end
717+
708718
return compiler

0 commit comments

Comments
 (0)