Learn: Extending min
min provides a fairly complete standard library with many useful modules. However, you may feel the need to extend min in order to perform more specialized tasks.
In such situations, you basically have the following options:
- Implementing new min modules using min itself
- Specifying your custom prelude program
- Embedding min in your Nim program
Implementing new min modules using min itself
When you just want to create more high-level min operator using functionalities that are already available in min, the easiest way is to create your own reusable min modules.
To create a new module, simply create a file containing your operator definitions implemented using either the operator operator or the lambda operator
Save your code to a file (e.g. quickpows.min) and you can use it in other Nim files using the require operator and the import (if you want to import the operators in the current scope):
Specifying your custom prelude program
By default, when min is started it loads the following prelude.min program:
Essentially, this causes min to import all the modules and unseals the prompt symbol so that it can be customized. If you want, you can provide your own prelude file to specify your custom behaviors, selectively import modules, and define your own symbols, like this:
$ min -i -p:myfile.min
Embedding min in your Nim program
If you’d like to use min as a scripting language within your own program, and maybe extend it by implementing additional operators, you can use min as a Nim library.
To do so:
- Install min sources using Nifty as explained in the Download section.
- Import it in your Nim file.
- Implement a new proc to define the module.
The following code is taken from HastySite and shows how to define a new hastysite module containing some symbols (preprocess, postprocess, process-rules, …):
Then you need to:
- Instantiate a new min interpreter using the newMinInterpreter proc.
- Run the proc used to define the module.
- Call the interpret method to interpret a min file or string:
Tip
For more information on how to create new modules with Nim, have a look in the lib folder of the min repository, which contains all the min modules included in the standard library.
→ Continue to min Module Management