nims.rst 10.0 KB

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