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
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) 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.
Example project structure:
In main.cksp (imports from a subdirectory):
In component.cksp (importing utility functions)
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.
-
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.