nims.rst 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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, but also replaces Nim's existing configuration
  7. system.
  8. So instead of a ``myproject.nim.cfg`` configuration file, you can use
  9. a ``myproject.nims`` file that simply contains Nim code controlling the
  10. compilation process. For a directory wide configuration, use ``config.nims``
  11. instead of ``nim.cfg``.
  12. The VM cannot deal with ``importc``, the FFI is not available, so there are not
  13. many stdlib modules that you can use with Nim's VM. However, at least the
  14. following modules are available:
  15. * `strutils <strutils.html>`_
  16. * `ospaths <ospaths.html>`_
  17. * `math <math.html>`_
  18. * `distros <distros.html>`_
  19. The `system <system.html>`_ module in NimScript mode additionally supports
  20. these operations: `nimscript <nimscript.html>`_.
  21. NimScript as a configuration file
  22. =================================
  23. What is ``x.y.key = "value"`` in the configuration file
  24. becomes ``switch("x.y.key", "value")``. ``--option`` is ``switch("option")``.
  25. The ``system`` module also exports 2 ``--`` templates for convenience:
  26. .. code-block:: nim
  27. --forceBuild
  28. # is the same as:
  29. switch("forceBuild")
  30. NimScript as a build tool
  31. =========================
  32. The ``task`` template that the ``system`` module defines allows a NimScript
  33. file to be used as a build tool. The following example defines a
  34. task ``build`` that is an alias for the ``c`` command:
  35. .. code-block:: nim
  36. task build, "builds an example":
  37. setCommand "c"
  38. In fact, as a convention the following tasks should be available:
  39. ========= ===================================================
  40. Task Description
  41. ========= ===================================================
  42. ``build`` Build the project with the required
  43. backend (``c``, ``cpp`` or ``js``).
  44. ``tests`` Runs the tests belonging to the project.
  45. ``bench`` Runs benchmarks belonging to the project.
  46. ========= ===================================================
  47. If the task runs an external command via ``exec`` it should afterwards call
  48. ``setCommand "nop"`` to tell the Nim compiler that nothing else needs to be
  49. done:
  50. .. code-block:: nim
  51. task tests, "test regular expressions":
  52. exec "nim c -r tests"
  53. setCommand "nop"
  54. Look at the module `distros <distros.html>`_ for some support of the
  55. OS's native package managers.
  56. Nimble integration
  57. ==================
  58. See the `Nimble readme <https://github.com/nim-lang/nimble#readme>`_
  59. for more information.
  60. Standalone NimScript
  61. ====================
  62. NimScript can also be used directly as a portable replacement for Bash and
  63. Batch files. Use ``nim e myscript.nims`` to run ``myscript.nims``. For example,
  64. installation of Nimble is done with this simple script:
  65. .. code-block:: nim
  66. mode = ScriptMode.Verbose
  67. var id = 0
  68. while dirExists("nimble" & $id):
  69. inc id
  70. exec "git clone https://github.com/nim-lang/nimble.git nimble" & $id
  71. withDir "nimble" & $id & "/src":
  72. exec "nim c nimble"
  73. mvFile "nimble" & $id & "/src/nimble".toExe, "bin/nimble".toExe
  74. You can also use the shebang ``#!/usr/bin/env nim``, as long as your filename
  75. ends with ``.nims``:
  76. .. code-block:: nim
  77. #!/usr/bin/env nim
  78. mode = ScriptMode.Silent
  79. echo "hello world"