Import Statements
The CKSP compiler supports modular programming by allowing code from other *.cksp, *.ksp or *.txt files to be included using the import statement.
The statements can be placed in any scope and specify the path to the file to be imported.
Import statements are resolved at the start of the preprocessing phase, before defines and macros are processed. This means that using defines or macro substitutions in import paths is not supported; the paths must be specified as string literals.
Syntax
The second form gives the imported file an alias, through which everything it declares is then reached.
Import Path Resolution
Import paths always use forward slashes (/), regardless of the operating system. To move up a directory, use ../. To specify a path relative to the project root, use ./.
Import paths can be specified in two ways:
-
Relative to the current file The path is resolved based on the location of the file containing the
importstatement. This is especially useful when using independent modules or libraries that might be shared across different projects. -
Relative to the project root The project root is always the file initially passed to the compiler. By prefixing the path with
./, you can reference paths relative to this root, regardless of the location of the importing file. However, (to maintain compatibility withSublimeKSP, which reads every import path this way) you can also use paths relative to the project root without the./prefix. In this case, you would simply specify the path as if it were imported from the root. A project ported fromSublimeKSPtherefore compiles without rewriting a single import.
Which one wins
A path without a ./ prefix is looked for next to the importing file first, and only then at the project root. If a file of that name exists in both places, the one beside the importing file is the one that is imported. The ./ prefix is the way to say the project root and nothing else.
Example project structure:
In main.cksp (imports from a subdirectory):
In component.cksp (importing utility functions)
Importing with an Alias
An import can give the imported file a name of its own:
| main.cksp | |
|---|---|
Everything the file declares is then reached through that name, and only through it:
This is not a scope of its own: the compiler wraps the imported file in a namespace named after the alias. Everything that holds for a namespace therefore holds here as well — qualified access, nesting, and the fully qualified names in the generated KSP, where math.Point becomes math__Point.
An alias is the tool for the two problems a growing project runs into: two modules that both want to declare a reset function, and a reader who cannot tell from apply_curve(x) which file that came from.
What an Aliased File May Contain
Because the file becomes a namespace, it may contain exactly what a namespace may contain: declare statements (including declare const), function definitions and struct definitions.
A const block, a list block, a family or a callback is rejected — the compiler reports that a namespace can only hold declarations and function definitions. Such a file has to be imported without an alias.
Defines and Macros Stay Global
define and macro are resolved by the preprocessor before namespaces exist, so they are lifted out and stay global. They are used unqualified, exactly as in an import without an alias:
| lib/math.cksp | |
|---|---|
| main.cksp | |
|---|---|
Nesting
Aliases nest the way namespaces do. An aliased import inside an aliased file is reached through both names:
| main.cksp | |
|---|---|
A namespace written out inside the imported file nests just as well, so a file imported as lib that declares namespace inner is reached as lib.inner.member. An import without an alias inside an aliased file is folded into the alias instead: its declarations are reachable under the outer alias.
Several Files Under One Alias
Two imports may share an alias. Their contents end up in the same namespace, which is the way to assemble one module from several files:
A File Is Imported Only Once
The compiler imports each file the first time it is named and skips every later import of the same file, which is what keeps circular imports from looping. One file therefore cannot be given two names, and asking for it is an error:
The same applies when one import carries an alias and the other does not. Two imports spelled identically are fine — the second asks for nothing the first did not already do — which is what lets two modules depend on the same file without coordinating.
Restrictions
- The alias must be a name. A reserved keyword such as
ifis rejected. - Top level only. A namespace can only be declared globally, so an aliased import cannot sit inside a callback, a function or any other construct. Imports inside constructs still work — just without an alias.
Import Statements inside Constructs
Other than being placed at the top level of a file, import statements can also be used inside constructs such as macros, functions, loops, or conditional statements. This allows for more dynamic and modular code organization, where imports can be conditionally included based on runtime logic or specific scopes.
This way, imported files do not necessarily need to be syntactically correct on their own, as they will only be processed when the import statement is executed. Importing the contents of a list block, for example, allows you to define a comma-separated list in a separate file and import it directly into the block where it is needed.
Key Features of Import Paths
-
Flexible Resolution Paths can be based on the current file location or fixed to the project root. Without a
./prefix the current file wins, with the project root as fallback. -
Navigation with
../Use../to move up one directory; can be chained for multiple levels. -
Root-based Imports with
./Start a path with./to resolve from the project root, which is always the file first passed to the compiler. -
Imports in Any Scope
importstatements can be placed in any scope, allowing for modular code organization. -
Aliases with
asimport "..." as nameputs the imported file into a namespace of that name, so its members are reached asname.member.