Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import statement and -- @module directive #5

Open
1 of 2 tasks
RyanSquared opened this issue Jun 29, 2017 · 0 comments
Open
1 of 2 tasks

import statement and -- @module directive #5

RyanSquared opened this issue Jun 29, 2017 · 0 comments

Comments

@RyanSquared
Copy link
Owner

RyanSquared commented Jun 29, 2017

  • @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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant