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

Include/require #2

Closed
lfkeitel opened this issue Nov 17, 2017 · 0 comments
Closed

Include/require #2

lfkeitel opened this issue Nov 17, 2017 · 0 comments

Comments

@lfkeitel
Copy link
Collaborator

lfkeitel commented Nov 17, 2017

Add language constructs to include source code from other Nitrogen files. I like PHP here where they have include and required and then an _once variant of each. I think that allows flexibility on how strict or loose code needs to be.

Implementation details: Can be implemented as a collection of functions or as keywords and constructs directly in the language. Either method would use the same underlying process. A map would be used to store pathnames of source code as it's included. Each path will map to an *ast.Program struct. For the _once variants, nil can be used instead as the parsed tree will never be used again. If a file couldn't be included and it's not required, an Error would be generated and returned. If the script is required, an Exception is returned causing execution to halt.

Implementing as a language keyword and direct AST node would probably be more efficient as an evaluated function call wouldn't need to get setup and invoked. Even builtins need to do a bit of type casting and argument checking. Using an AST node, the evaluator can be guaranteed the arguments and options are correct and use the node directly without a lot of extra checking. The evalInclude function would check if the path has already been included. If yes and _once, do nothing. Otherwise, lex and parse the file. Store the resulting *ast.Program in a map and execute it. If _once, store nil in the map instead as the tree is never used again.

This map is shared across the entire execution. The map will need to be prepopulated with the main script so it can't cause circular imports.

If a file has the extension .so load it as a plugin module. These can only be loaded once. Perhaps use the module keyword instead? or maybe use?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant