`import` statement and `-- @module` directive
RyanSquared opened this issue · 0 comments
-
@module
directive -
import
import
To help with using #4 across multiple files, the compiler should be aware of enums that are used cross-process. The import
statement can be used to direct the compiler to both scan the module for enumerations, as well as load values from the module:
--- FusionScript
import stdlib.error {
BaseError;
};
--- Lua
local _des_0 = require("stdlib.error")
local BaseError = _des_0.BaseError
All values can be extracted from a module, but many linters would not be aware of this. The syntax will also only work if the module has a module
directive at the top of the file.
--- FusionScript
import stdlib.error *;
--- Lua
local _des_0 = require("stdlib.error")
local BaseError, Error, assert = _des_0.BaseError, _des_0.Error, _des_0.assert
Enumerations can also be included but will not show up in the compiled output:
--- FusionScript - example.fuse
import connections {
ConnectionType;
};
print(ConnectionType.INET);
--- FusionScript - connections.fuse
-- @module connections
enum ConnectionType {
INET;
INET6;
UNIX;
};
--- Lua - example.lua
print(1);
--- Lua - connections.lua
_ENV = setmetatable({}, {__index = _G})
return _ENV
An entire module can be loaded into a table, instead of having values destructured into the local scope:
--- FusionScript
import stdlib.error => error;
--- Lua
local error = require("stdlib.error");
-- @module
directive
A comment that matches the pattern -- @<directive> [parameter]
is considered a directive. When parsing a file, the compiler will take advantage of these comments to produce output that better matches the type of file. The -- @module
directive (with a single required parameter, the name of the module) instructs the compiler that the following file should be treated as a module, which will set the environment to a contained table (to prevent leaking of values assigned globally) and return the table at the end of the file:
--- FusionScript - example.fuse
-- @module example
printf(fmt, ...)=>
print(string.format(fmt, ...));
_ENV = setmetatable({}, {__index = _G})
function printf(fmt, ...)
print(string.format(fmt, ...))
end
return _ENV