-
Notifications
You must be signed in to change notification settings - Fork 0
Modules
No one wants to write code multiple times, right? Jask allows the definition of modules, a jask file containing code, which can be imported in other jask scripts:
; file math.jask
function power(base: number, exponent: number)
; implementation goes here...
endThis file can now be imported in another script:
; file main.jask
use "math" as math
print(math::power(2, 3)Note that you can choose the identifier for an imported module freely. It is recommended to use an identifier, which is recognizable and meaningful for your purposes. You can import a file with or without the .jask file ending.
When importing a module via use, the interpreter checks in the following order:
- Absolute paths to modules are always prioritized
- If it is a relative path, the interpreter checks if the file is next to the importing file
- If a file is not found under 2., the interpreter checks if the file is located relative to the starting process. Imagine the following directory:
/ (root)
├── usr/
│ └── local/
│ └── lib/
│ └── date.jask
└── home/
└── user/
└── jasksharp/ <-- (working directory for interpreter)
└── jcore/
├── date.jask
└── myJaskProject/
└── main.jask
The file main.jask can import the module date.jask via an absolute path or via the relative path jcore/date.jask.
The second case is relative to the interpreters working directory in jasksharp directory, so the file is found.
Jask consists not only of the interpreter, but also of a collection of helpful functions known as jcore. Jcore bundles a lot of common functionalities, like math operations or date handling, which are purely written in jask. It is meant as a demonstration of the language capabilities and features. If you have implemented a usefull feature, help expanding jcore via a pull request! Read more about available features here: standard library jcore
Only structs and functions are globally accessible from modules. Global variables are not accessible from scripts using the module. Other imported modules are too, not accessible from scripts using the module. Example: Module a using module b. A script using module a can access all its structs and functions but not implicitly anything from module b.