nims.rst 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. .. default-role:: code
  2. ================================
  3. NimScript
  4. ================================
  5. Strictly speaking, `NimScript` is the subset of Nim that can be evaluated
  6. by Nim's builtin virtual machine (VM). This VM is used for Nim's compiletime
  7. function evaluation features.
  8. The `nim` executable processes the `.nims` configuration files in
  9. the following directories (in this order; later files overwrite
  10. previous settings):
  11. 1) If environment variable `XDG_CONFIG_HOME` is defined,
  12. `$XDG_CONFIG_HOME/nim/config.nims` or
  13. `~/.config/nim/config.nims` (POSIX) or
  14. `%APPDATA%/nim/config.nims` (Windows). This file can be skipped
  15. with the `--skipUserCfg` command line option.
  16. 2) `$parentDir/config.nims` where `$parentDir` stands for any
  17. parent directory of the project file's path. These files can be
  18. skipped with the `--skipParentCfg` command line option.
  19. 3) `$projectDir/config.nims` where `$projectDir` stands for the
  20. project's path. This file can be skipped with the `--skipProjCfg`
  21. command line option.
  22. 4) A project can also have a project specific configuration file named
  23. `$project.nims` that resides in the same directory as
  24. `$project.nim`. This file can be skipped with the same
  25. `--skipProjCfg` command line option.
  26. For available procs and implementation details see `nimscript <nimscript.html>`_.
  27. Limitations
  28. ===========
  29. NimScript is subject to some limitations caused by the implementation of the VM
  30. (virtual machine):
  31. * Nim's FFI (foreign function interface) is not available in NimScript. This
  32. means that any stdlib module which relies on `importc` can not be used in
  33. the VM.
  34. * `ptr` operations are are hard to emulate with the symbolic representation
  35. the VM uses. They are available and tested extensively but there are bugs left.
  36. * `var T` function arguments rely on `ptr` operations internally and might
  37. also be problematic in some cases.
  38. * More than one level of `ref` is generally not supported (for example, the type
  39. `ref ref int`).
  40. * Multimethods are not available.
  41. * `random.randomize()` requires an `int64` explicitly passed as argument, you *must* pass a Seed integer.
  42. Standard library modules
  43. ========================
  44. At least the following standard library modules are available:
  45. * `macros <macros.html>`_
  46. * `os <os.html>`_
  47. * `strutils <strutils.html>`_
  48. * `math <math.html>`_
  49. * `distros <distros.html>`_
  50. * `sugar <sugar.html>`_
  51. * `algorithm <algorithm.html>`_
  52. * `base64 <base64.html>`_
  53. * `bitops <bitops.html>`_
  54. * `chains <chains.html>`_
  55. * `colors <colors.html>`_
  56. * `complex <complex.html>`_
  57. * `htmlgen <htmlgen.html>`_
  58. * `httpcore <httpcore.html>`_
  59. * `lenientops <lenientops.html>`_
  60. * `mersenne <mersenne.html>`_
  61. * `options <options.html>`_
  62. * `parseutils <parseutils.html>`_
  63. * `punycode <punycode.html>`_
  64. * `random <punycode.html>`_
  65. * `stats <stats.html>`_
  66. * `strformat <strformat.html>`_
  67. * `strmisc <strmisc.html>`_
  68. * `strscans <strscans.html>`_
  69. * `unicode <unicode.html>`_
  70. * `uri <uri.html>`_
  71. * `std/editdistance <editdistance.html>`_
  72. * `std/wordwrap <wordwrap.html>`_
  73. * `std/sums <sums.html>`_
  74. * `parsecsv <parsecsv.html>`_
  75. * `parsecfg <parsecfg.html>`_
  76. * `parsesql <parsesql.html>`_
  77. * `xmlparser <xmlparser.html>`_
  78. * `htmlparser <htmlparser.html>`_
  79. * `ropes <ropes.html>`_
  80. * `json <json.html>`_
  81. * `parsejson <parsejson.html>`_
  82. * `strtabs <strtabs.html>`_
  83. * `unidecode <unidecode.html>`_
  84. In addition to the standard Nim syntax (`system <system.html>`_ module),
  85. NimScripts support the procs and templates defined in the
  86. `nimscript <nimscript.html>`_ module too.
  87. See also:
  88. * `Check the tests for more information about modules compatible with NimScript. <https://github.com/nim-lang/Nim/blob/devel/tests/test_nimscript.nims>`_
  89. NimScript as a configuration file
  90. =================================
  91. A command-line switch `--FOO` is written as `switch("FOO")` in
  92. NimScript. Similarly, command-line `--FOO:VAL` translates to
  93. `switch("FOO", "VAL")`.
  94. Here are few examples of using the `switch` proc:
  95. .. code-block:: nim
  96. # command-line: --opt:size
  97. switch("opt", "size")
  98. # command-line: --define:foo or -d:foo
  99. switch("define", "foo")
  100. # command-line: --forceBuild
  101. switch("forceBuild")
  102. NimScripts also support `--` templates for convenience, which look
  103. like command-line switches written as-is in the NimScript file. So the
  104. above example can be rewritten as:
  105. .. code-block:: nim
  106. --opt:size
  107. --define:foo
  108. --forceBuild
  109. **Note**: In general, the *define* switches can also be set in
  110. NimScripts using `switch` or `--`, as shown in above
  111. examples. Only the `release` define (`-d:release`) cannot be set
  112. in NimScripts.
  113. NimScript as a build tool
  114. =========================
  115. The `task` template that the `system` module defines allows a NimScript
  116. file to be used as a build tool. The following example defines a
  117. task `build` that is an alias for the `c` command:
  118. .. code-block:: nim
  119. task build, "builds an example":
  120. setCommand "c"
  121. In fact, as a convention the following tasks should be available:
  122. ========= ===================================================
  123. Task Description
  124. ========= ===================================================
  125. `help` List all the available NimScript tasks along with their docstrings.
  126. `build` Build the project with the required
  127. backend (`c`, `cpp` or `js`).
  128. `tests` Runs the tests belonging to the project.
  129. `bench` Runs benchmarks belonging to the project.
  130. ========= ===================================================
  131. Look at the module `distros <distros.html>`_ for some support of the
  132. OS's native package managers.
  133. Nimble integration
  134. ==================
  135. See the `Nimble readme <https://github.com/nim-lang/nimble#readme>`_
  136. for more information.
  137. Standalone NimScript
  138. ====================
  139. NimScript can also be used directly as a portable replacement for Bash and
  140. Batch files. Use `nim myscript.nims` to run `myscript.nims`. For example,
  141. installation of Nimble could be accomplished with this simple script:
  142. .. code-block:: nim
  143. mode = ScriptMode.Verbose
  144. var id = 0
  145. while dirExists("nimble" & $id):
  146. inc id
  147. exec "git clone https://github.com/nim-lang/nimble.git nimble" & $id
  148. withDir "nimble" & $id & "/src":
  149. exec "nim c nimble"
  150. mvFile "nimble" & $id & "/src/nimble".toExe, "bin/nimble".toExe
  151. On Unix, you can also use the shebang `#!/usr/bin/env nim`, as long as your filename
  152. ends with `.nims`:
  153. .. code-block:: nim
  154. #!/usr/bin/env nim
  155. mode = ScriptMode.Silent
  156. echo "hello world"
  157. Use `#!/usr/bin/env -S nim --hints:off` to disable hints.
  158. Benefits
  159. ========
  160. Cross-Platform
  161. --------------
  162. It is a cross-platform scripting language that can run where Nim can run,
  163. e.g. you can not run Batch or PowerShell on Linux or Mac,
  164. the Bash for Linux might not run on Mac,
  165. there are no unit tests tools for Batch, etc.
  166. NimScript can detect on which platform, operating system,
  167. architecture, and even which Linux distribution is running on,
  168. allowing the same script to support a lot of systems.
  169. See the following (incomplete) example:
  170. .. code-block:: nim
  171. import std/distros
  172. # Architectures.
  173. if defined(amd64):
  174. echo "Architecture is x86 64Bits"
  175. elif defined(i386):
  176. echo "Architecture is x86 32Bits"
  177. elif defined(arm):
  178. echo "Architecture is ARM"
  179. # Operating Systems.
  180. if defined(linux):
  181. echo "Operating System is GNU Linux"
  182. elif defined(windows):
  183. echo "Operating System is Microsoft Windows"
  184. elif defined(macosx):
  185. echo "Operating System is Apple OS X"
  186. # Distros.
  187. if detectOs(Ubuntu):
  188. echo "Distro is Ubuntu"
  189. elif detectOs(ArchLinux):
  190. echo "Distro is ArchLinux"
  191. elif detectOs(Debian):
  192. echo "Distro is Debian"
  193. Uniform Syntax
  194. --------------
  195. The syntax, style, and rest of the ecosystem is the same as for compiled Nim,
  196. that means there is nothing new to learn, no context switch for developers.
  197. Powerful Metaprogramming
  198. ------------------------
  199. NimScript can use Nim's templates, macros, types, concepts, effect tracking system, and more,
  200. you can create modules that work on compiled Nim and also on interpreted NimScript.
  201. `func` will still check for side effects, `debugEcho` also works as expected,
  202. making it ideal for functional scripting metaprogramming.
  203. This is an example of a third party module that uses macros and templates to
  204. translate text strings on unmodified NimScript:
  205. .. code-block:: nim
  206. import nimterlingua
  207. nimterlingua("translations.cfg")
  208. echo "cat" # Run with -d:RU becomes "kot", -d:ES becomes "gato", ...
  209. translations.cfg
  210. .. code-block:: none
  211. [cat]
  212. ES = gato
  213. IT = gatto
  214. RU = kot
  215. FR = chat
  216. * `Nimterlingua <https://nimble.directory/pkg/nimterlingua>`_
  217. Graceful Fallback
  218. -----------------
  219. Some features of compiled Nim may not work on NimScript,
  220. but often a graceful and seamless fallback degradation is used.
  221. See the following NimScript:
  222. .. code-block:: nim
  223. if likely(true):
  224. discard
  225. elif unlikely(false):
  226. discard
  227. proc foo() {.compiletime.} = echo NimVersion
  228. static:
  229. echo CompileDate
  230. `likely()`, `unlikely()`, `static:` and `{.compiletime.}`
  231. will produce no code at all when run on NimScript,
  232. but still no error nor warning is produced and the code just works.
  233. Evolving Scripting language
  234. ---------------------------
  235. NimScript evolves together with Nim,
  236. `occasionally new features might become available on NimScript <https://github.com/nim-lang/Nim/pulls?utf8=%E2%9C%93&q=nimscript>`_ ,
  237. adapted from compiled Nim or added as new features on both.
  238. Scripting Language with a Package Manager
  239. -----------------------------------------
  240. You can create your own modules to be compatible with NimScript,
  241. and check `Nimble <https://nimble.directory>`_
  242. to search for third party modules that may work on NimScript.
  243. DevOps Scripting
  244. ----------------
  245. You can use NimScript to deploy to production, run tests, build projects, do benchmarks,
  246. generate documentation, and all kinds of DevOps/SysAdmin specific tasks.
  247. * `An example of a third party NimScript that can be used as a project-agnostic tool. <https://github.com/kaushalmodi/nim_config#list-available-tasks>`_