nims.rst 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. The VM cannot deal with ``importc`` because the FFI is not
  26. available. So the stdlib modules using ``importc`` cannot be used with
  27. Nim's VM. However, at least the following modules are available:
  28. * `macros <macros.html>`_
  29. * `os <os.html>`_
  30. * `strutils <strutils.html>`_
  31. * `math <math.html>`_
  32. * `distros <distros.html>`_
  33. In addition to the standard Nim syntax (`system <system.html>`_
  34. module), NimScripts support the procs and templates defined in the
  35. `nimscript <nimscript.html>`_ module too.
  36. NimScript as a configuration file
  37. =================================
  38. A command-line switch ``--FOO`` is written as ``switch("FOO")`` in
  39. NimScript. Similarly, command-line ``--FOO:VAL`` translates to
  40. ``switch("FOO", "VAL")``.
  41. Here are few examples of using the ``switch`` proc:
  42. .. code-block:: nim
  43. # command-line: --opt:size
  44. switch("opt", "size")
  45. # command-line: --define:foo or -d:foo
  46. switch("define", "foo")
  47. # command-line: --forceBuild
  48. switch("forceBuild")
  49. NimScripts also support ``--`` templates for convenience, which look
  50. like command-line switches written as-is in the NimScript file. So the
  51. above example can be rewritten as:
  52. .. code-block:: nim
  53. --opt:size
  54. --define:foo
  55. --forceBuild
  56. **Note**: In general, the *define* switches can also be set in
  57. NimScripts using ``switch`` or ``--``, as shown in above
  58. examples. Only the ``release`` define (``-d:release``) cannot be set
  59. in NimScripts.
  60. NimScript as a build tool
  61. =========================
  62. The ``task`` template that the ``system`` module defines allows a NimScript
  63. file to be used as a build tool. The following example defines a
  64. task ``build`` that is an alias for the ``c`` command:
  65. .. code-block:: nim
  66. task build, "builds an example":
  67. setCommand "c"
  68. In fact, as a convention the following tasks should be available:
  69. ========= ===================================================
  70. Task Description
  71. ========= ===================================================
  72. ``help`` List all the available NimScript tasks along with their docstrings.
  73. ``build`` Build the project with the required
  74. backend (``c``, ``cpp`` or ``js``).
  75. ``tests`` Runs the tests belonging to the project.
  76. ``bench`` Runs benchmarks belonging to the project.
  77. ========= ===================================================
  78. If the task runs an external command via ``exec`` it should afterwards call
  79. ``setCommand "nop"`` to tell the Nim compiler that nothing else needs to be
  80. done:
  81. .. code-block:: nim
  82. task tests, "test regular expressions":
  83. exec "nim c -r tests"
  84. setCommand "nop"
  85. Look at the module `distros <distros.html>`_ for some support of the
  86. OS's native package managers.
  87. Nimble integration
  88. ==================
  89. See the `Nimble readme <https://github.com/nim-lang/nimble#readme>`_
  90. for more information.
  91. Standalone NimScript
  92. ====================
  93. NimScript can also be used directly as a portable replacement for Bash and
  94. Batch files. Use ``nim e myscript.nims`` to run ``myscript.nims``. For example,
  95. installation of Nimble could be accomplished with this simple script:
  96. .. code-block:: nim
  97. mode = ScriptMode.Verbose
  98. var id = 0
  99. while dirExists("nimble" & $id):
  100. inc id
  101. exec "git clone https://github.com/nim-lang/nimble.git nimble" & $id
  102. withDir "nimble" & $id & "/src":
  103. exec "nim c nimble"
  104. mvFile "nimble" & $id & "/src/nimble".toExe, "bin/nimble".toExe
  105. You can also use the shebang ``#!/usr/bin/env nim``, as long as your filename
  106. ends with ``.nims``:
  107. .. code-block:: nim
  108. #!/usr/bin/env nim
  109. mode = ScriptMode.Silent
  110. echo "hello world"