Skip to content

10. Modules

Tom Dodd edited this page Dec 8, 2023 · 3 revisions

The import keyword can be used to dynamically run other scripts. It requires two arguments: a label as the first and the script's path relative to the interpreter's working directory as the second. The script will be executed with its own root scope, and once finished, its hierarchy will become a sub-hierarchy of the current scope, accessible using the name of the label and member identifiers. This behaviour is similar to namespacing in other languages. For example, if an imported script defines a variable x, it can be accessed in the following way:

/Other "relative/path/to/other.dssl" import

/Other .x 10 *=   # Modify Other .x
Other .x println  # Print Other .x

The . prefix is used for the member access of a sub-hierarchy, in this case Other.

More variables can also be added to the sub-hierarchy from outside the imported script:

/Other .y 0.577 def  # Define Other .y
Other .y println     # Print Other .y

An important note is that similar to labels, definitions of variables within sub-hierarchies are not scoped by where the def keyword acts. For example, it is not possible to make a new shadowing definition of a pre-existing Other .x within some inner scope, because the member access .x acting on the label /Other still produces a label corresponding to a variable with its scope being the root of the Other sub-hierarchy.

Built-in modules can be imported instead by using a built-in module identifier in place of a path string. Furthermore, an alternative to the import keyword is the include keyword, which uses a relative path or built-in module identifier as its only argument. The contents of the hierarchy of the other script or built-in module will be added directly to the current scope, inspired by the #include preprocessor directive in C. For example, including the built-in module $math allows its macros to be directly accessed:

$math include
100.0 log10 println  # Prints 2.0

All built-in modules are listed here.

Clone this wiki locally