nims.rst 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. * ``unicode`` can be imported, but not ``unidecode``.
  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. In addition to the standard Nim syntax (`system <system.html>`_ module),
  75. NimScripts support the procs and templates defined in the
  76. `nimscript <nimscript.html>`_ module too.
  77. NimScript as a configuration file
  78. =================================
  79. A command-line switch ``--FOO`` is written as ``switch("FOO")`` in
  80. NimScript. Similarly, command-line ``--FOO:VAL`` translates to
  81. ``switch("FOO", "VAL")``.
  82. Here are few examples of using the ``switch`` proc:
  83. .. code-block:: nim
  84. # command-line: --opt:size
  85. switch("opt", "size")
  86. # command-line: --define:foo or -d:foo
  87. switch("define", "foo")
  88. # command-line: --forceBuild
  89. switch("forceBuild")
  90. NimScripts also support ``--`` templates for convenience, which look
  91. like command-line switches written as-is in the NimScript file. So the
  92. above example can be rewritten as:
  93. .. code-block:: nim
  94. --opt:size
  95. --define:foo
  96. --forceBuild
  97. **Note**: In general, the *define* switches can also be set in
  98. NimScripts using ``switch`` or ``--``, as shown in above
  99. examples. Only the ``release`` define (``-d:release``) cannot be set
  100. in NimScripts.
  101. NimScript as a build tool
  102. =========================
  103. The ``task`` template that the ``system`` module defines allows a NimScript
  104. file to be used as a build tool. The following example defines a
  105. task ``build`` that is an alias for the ``c`` command:
  106. .. code-block:: nim
  107. task build, "builds an example":
  108. setCommand "c"
  109. In fact, as a convention the following tasks should be available:
  110. ========= ===================================================
  111. Task Description
  112. ========= ===================================================
  113. ``help`` List all the available NimScript tasks along with their docstrings.
  114. ``build`` Build the project with the required
  115. backend (``c``, ``cpp`` or ``js``).
  116. ``tests`` Runs the tests belonging to the project.
  117. ``bench`` Runs benchmarks belonging to the project.
  118. ========= ===================================================
  119. Look at the module `distros <distros.html>`_ for some support of the
  120. OS's native package managers.
  121. Nimble integration
  122. ==================
  123. See the `Nimble readme <https://github.com/nim-lang/nimble#readme>`_
  124. for more information.
  125. Standalone NimScript
  126. ====================
  127. NimScript can also be used directly as a portable replacement for Bash and
  128. Batch files. Use ``nim myscript.nims`` to run ``myscript.nims``. For example,
  129. installation of Nimble could be accomplished with this simple script:
  130. .. code-block:: nim
  131. mode = ScriptMode.Verbose
  132. var id = 0
  133. while dirExists("nimble" & $id):
  134. inc id
  135. exec "git clone https://github.com/nim-lang/nimble.git nimble" & $id
  136. withDir "nimble" & $id & "/src":
  137. exec "nim c nimble"
  138. mvFile "nimble" & $id & "/src/nimble".toExe, "bin/nimble".toExe
  139. On Unix, you can also use the shebang ``#!/usr/bin/env nim``, as long as your filename
  140. ends with ``.nims``:
  141. .. code-block:: nim
  142. #!/usr/bin/env nim
  143. mode = ScriptMode.Silent
  144. echo "hello world"
  145. Use ``#!/usr/bin/env -S nim --hints:off`` to disable hints.
  146. Benefits
  147. ========
  148. Cross-Platform
  149. --------------
  150. It is a cross-platform scripting language that can run where Nim can run,
  151. e.g. you can not run Batch or PowerShell on Linux or Mac,
  152. the Bash for Linux might not run on Mac,
  153. there are no unit tests tools for Batch, etc.
  154. NimScript can detect on which platform, operating system,
  155. architecture, and even which Linux distribution is running on,
  156. allowing the same script to support a lot of systems.
  157. See the following (incomplete) example:
  158. .. code-block:: nim
  159. import distros
  160. # Architectures.
  161. if defined(amd64):
  162. echo "Architecture is x86 64Bits"
  163. elif defined(i386):
  164. echo "Architecture is x86 32Bits"
  165. elif defined(arm):
  166. echo "Architecture is ARM"
  167. # Operating Systems.
  168. if defined(linux):
  169. echo "Operating System is GNU Linux"
  170. elif defined(windows):
  171. echo "Operating System is Microsoft Windows"
  172. elif defined(macosx):
  173. echo "Operating System is Apple OS X"
  174. # Distros.
  175. if detectOs(Ubuntu):
  176. echo "Distro is Ubuntu"
  177. elif detectOs(ArchLinux):
  178. echo "Distro is ArchLinux"
  179. elif detectOs(Debian):
  180. echo "Distro is Debian"
  181. Uniform Syntax
  182. --------------
  183. The syntax, style, and rest of the ecosystem is the same as for compiled Nim,
  184. that means there is nothing new to learn, no context switch for developers.
  185. Powerful Metaprogramming
  186. ------------------------
  187. NimScript can use Nim's templates, macros, types, concepts, effect tracking system, and more,
  188. you can create modules that work on compiled Nim and also on interpreted NimScript.
  189. ``func`` will still check for side effects, ``debugEcho`` also works as expected,
  190. making it ideal for functional scripting metaprogramming.
  191. This is an example of a third party module that uses macros and templates to
  192. translate text strings on unmodified NimScript:
  193. .. code-block:: nim
  194. import nimterlingua
  195. nimterlingua("translations.cfg")
  196. echo "cat" # Run with -d:RU becomes "kot", -d:ES becomes "gato", ...
  197. translations.cfg
  198. .. code-block:: none
  199. [cat]
  200. ES = gato
  201. PT = minino
  202. RU = kot
  203. FR = chat
  204. * `Nimterlingua <https://nimble.directory/pkg/nimterlingua>`_
  205. Graceful Fallback
  206. -----------------
  207. Some features of compiled Nim may not work on NimScript,
  208. but often a graceful and seamless fallback degradation is used.
  209. See the following NimScript:
  210. .. code-block:: nim
  211. if likely(true):
  212. discard
  213. elif unlikely(false):
  214. discard
  215. proc foo() {.compiletime.} = echo NimVersion
  216. static:
  217. echo CompileDate
  218. ``likely()``, ``unlikely()``, ``static:`` and ``{.compiletime.}``
  219. will produce no code at all when run on NimScript,
  220. but still no error nor warning is produced and the code just works.
  221. Evolving Scripting language
  222. ---------------------------
  223. NimScript evolves together with Nim,
  224. `occasionally new features might become available on NimScript <https://github.com/nim-lang/Nim/pulls?utf8=%E2%9C%93&q=nimscript>`_ ,
  225. adapted from compiled Nim or added as new features on both.
  226. Scripting Language with a Package Manager
  227. -----------------------------------------
  228. You can create your own modules to be compatible with NimScript,
  229. and check `Nimble <https://nimble.directory>`_
  230. to search for third party modules that may work on NimScript.
  231. DevOps Scripting
  232. ----------------
  233. You can use NimScript to deploy to production, run tests, build projects, do benchmarks,
  234. generate documentation, and all kinds of DevOps/SysAdmin specific tasks.
  235. * `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>`_