diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2019-02-08 17:18:17 +0100 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2019-02-08 17:18:17 +0100 |
commit | 0841c64a3217594d0d260a0f24616b469447c1b9 (patch) | |
tree | d16995b64b4cd42b7e3d9adf94dd7f7ff57b600c /doc | |
parent | cb9110c43d4ae9c29a0a1e0d54f7735712d4ba62 (diff) | |
parent | 444f2231c9b48c34f9bec2ce6cfa3de5ae2560b1 (diff) | |
download | Nim-0841c64a3217594d0d260a0f24616b469447c1b9.tar.gz |
Merge branch 'devel' into araq-quirky-exceptions
Diffstat (limited to 'doc')
-rw-r--r-- | doc/advopt.txt | 35 | ||||
-rw-r--r-- | doc/basicopt.txt | 2 | ||||
-rw-r--r-- | doc/contributing.rst | 94 | ||||
-rw-r--r-- | doc/docgen.rst | 2 | ||||
-rw-r--r-- | doc/docs.rst | 3 | ||||
-rw-r--r-- | doc/docstyle.rst | 37 | ||||
-rw-r--r-- | doc/intern.rst | 4 | ||||
-rw-r--r-- | doc/lib.rst | 62 | ||||
-rw-r--r-- | doc/manual.rst | 379 | ||||
-rw-r--r-- | doc/nimc.rst | 6 | ||||
-rw-r--r-- | doc/nims.rst | 36 | ||||
-rw-r--r-- | doc/packaging.rst | 69 | ||||
-rw-r--r-- | doc/regexprs.txt | 12 | ||||
-rw-r--r-- | doc/tut3.rst | 16 |
14 files changed, 516 insertions, 241 deletions
diff --git a/doc/advopt.txt b/doc/advopt.txt index 7445068c1..58ed9edd0 100644 --- a/doc/advopt.txt +++ b/doc/advopt.txt @@ -18,9 +18,9 @@ Advanced commands: Advanced options: -o:FILE, --out:FILE set the output filename - --stdout output to stdout + --stdout:on|off output to stdout --colors:on|off turn compiler messages coloring on|off - --listFullPaths list full paths in messages + --listFullPaths:on|off list full paths in messages -w:on|off|list, --warnings:on|off|list turn all warnings on|off or list all available --warning[X]:on|off turn specific warning X on|off @@ -39,29 +39,28 @@ Advanced options: --nimcache:PATH set the path used for generated files --header:FILE the compiler should produce a .h file (FILE is optional) - -c, --compileOnly compile Nim files only; do not assemble or link - --noLinking compile Nim and generated files but do not link - --noMain do not generate a main procedure - --genScript generate a compile script (in the 'nimcache' + -c, --compileOnly:on|off compile Nim files only; do not assemble or link + --noLinking:on|off compile Nim and generated files but do not link + --noMain:on|off do not generate a main procedure + --genScript:on|off generate a compile script (in the 'nimcache' subdirectory named 'compile_$$project$$scriptext'), implies --compileOnly - --genDeps generate a '.deps' file containing the dependencies + --genDeps:on|off generate a '.deps' file containing the dependencies --os:SYMBOL set the target operating system (cross-compilation) --cpu:SYMBOL set the target processor (cross-compilation) - --debuginfo enables debug information + --debuginfo:on|off enables debug information -t, --passC:OPTION pass an option to the C compiler -l, --passL:OPTION pass an option to the linker --cincludes:DIR modify the C compiler header search path --clibdir:DIR modify the linker library search path --clib:LIBNAME link an additional C library (you should omit platform-specific extensions) - --genMapping generate a mapping file containing - (Nim, mangled) identifier pairs --project document the whole project (doc2) --docSeeSrcUrl:url activate 'see source' for doc and doc2 commands (see doc.item.seesrc in config/nimdoc.cfg) + --docInternal also generate documentation for non-exported symbols --lineDir:on|off generation of #line directive on|off - --embedsrc embeds the original source code as comments + --embedsrc:on|off embeds the original source code as comments in the generated output --threadanalysis:on|off turn thread analysis on|off --tlsEmulation:on|off turn thread local storage emulation on|off @@ -78,11 +77,12 @@ Advanced options: strings is allowed; only for backwards compatibility --nilseqs:on|off allow 'nil' for strings/seqs for backwards compatibility - --skipCfg do not read the general configuration file - --skipUserCfg do not read the user's configuration file - --skipParentCfg do not read the parent dirs' configuration files - --skipProjCfg do not read the project's configuration file - --gc:refc|v2|markAndSweep|boehm|go|none|regions + --oldast:on|off use old AST for backwards compatibility + --skipCfg:on|off do not read the nim installation's configuration file + --skipUserCfg:on|off do not read the user's configuration file + --skipParentCfg:on|off do not read the parent dirs' configuration files + --skipProjCfg:on|off do not read the project's configuration file + --gc:refc|markAndSweep|boehm|go|none|regions select the GC to use; default is 'refc' --index:on|off turn index file generation on|off --putenv:key=value set an environment variable @@ -98,7 +98,8 @@ Advanced options: symbol matching is fuzzy so that --dynlibOverride:lua matches dynlib: "liblua.so.3" - --dynlibOverrideAll makes the dynlib pragma have no effect + --dynlibOverrideAll + disables the effects of the dynlib pragma --listCmd list the commands used to execute external programs --parallelBuild:0|1|... perform a parallel build value = number of processors (0 for auto-detect) diff --git a/doc/basicopt.txt b/doc/basicopt.txt index a9166d36c..a65b2302e 100644 --- a/doc/basicopt.txt +++ b/doc/basicopt.txt @@ -15,7 +15,7 @@ Options: (Optionally: Define the value for that symbol, see: "compile time define pragmas") -u, --undef:SYMBOL undefine a conditional symbol - -f, --forceBuild force rebuilding of all modules + -f, --forceBuild:on|off force rebuilding of all modules --stackTrace:on|off turn stack tracing on|off --lineTrace:on|off turn line tracing on|off --threads:on|off turn support for multi-threading on|off diff --git a/doc/contributing.rst b/doc/contributing.rst index e3ab697d3..c240cb80e 100644 --- a/doc/contributing.rst +++ b/doc/contributing.rst @@ -1,6 +1,10 @@ +============ Contributing ============ +.. contents:: + + Contributing happens via "Pull requests" (PR) on github. Every PR needs to be reviewed before it can be merged and the Continuous Integration should be green. @@ -22,7 +26,7 @@ There are 3 types of tests: 2. tests in ``when isMainModule:`` block, ran by ``nim c mymod.nim`` ``nimble test`` also typially runs these in external nimble packages. -3. testament tests, eg: tests/stdlib/tos.nim (only used for Nim repo). +3. testament tests, eg: ``tests/stdlib/tos.nim`` (only used for Nim repo). Not all the tests follow the convention here, feel free to change the ones that don't. Always leave the code cleaner than you found it. @@ -49,8 +53,8 @@ Sample test: seq2D[0][1] = true doAssert seq2D == @[@[true, true], @[true, false], @[false, false], @[false, false]] - # doAssert with `not` can be done as follows: - doAssert: not 1 == 2 + # doAssert with `not` can now be done as follows: + doAssert not (1 == 2) Newer tests tend to be run via ``testament`` rather than via ``when isMainModule:``, eg ``tests/stdlib/tos.nim``; this allows additional features such as custom @@ -60,18 +64,19 @@ Compiler -------- The tests for the compiler use a testing tool called ``testament``. They are all -located in ``tests/`` (eg: ``tests/destructor/tdestructor3.nim``). +located in ``tests/`` (e.g.: ``tests/destructor/tdestructor3.nim``). Each test has its own file. All test files are prefixed with ``t``. If you want to create a file for import into another test only, use the prefix ``m``. At the beginning of every test is the expected behavior of the test. Possible keys are: -- output: The expected output, most likely via ``echo`` -- exitcode: Exit code of the test (via ``exit(number)``) -- errormsg: The expected error message -- file: The file the errormsg was produced at -- line: The line the errormsg was produced at +- ``cmd``: A compilation command template e.g. ``nim $target --threads:on $options $file`` +- ``output``: The expected output (stdout + stderr), most likely via ``echo`` +- ``exitcode``: Exit code of the test (via ``exit(number)``) +- ``errormsg``: The expected compiler error message +- ``file``: The file the errormsg was produced at +- ``line``: The line the errormsg was produced at For a full spec, see here: ``testament/specs.nim`` @@ -113,8 +118,15 @@ list of these, see ``testament/categories.nim``, at the bottom. :: - ./koch tests c lib + ./koch tests c lib # compiles/runs stdlib modules, including `isMainModule` tests + ./koch tests c megatest # runs a set of tests that can be combined into 1 + +To run a single test: + +:: + ./koch test run <category>/<name> # eg: tuples/ttuples_issues + ./koch test run tests/stdlib/tos.nim # can also provide relative path For reproducible tests (to reproduce an environment more similar to the one run by Continuous Integration on travis/appveyor), you may want to disable your @@ -165,12 +177,15 @@ the old name and introduce a new name: .. code-block:: nim # for routines (proc/template/macro/iterator) and types: - proc oldProc() {.deprecated: "use `newImpl: string -> int` instead".} = ... + proc oldProc() {.deprecated: "use `newImpl: string -> int` instead".} = discard - # for (const/var/let) the msg is not yet supported: + # for (const/var/let/fields) the msg is not yet supported: const Foo {.deprecated.} = 1 - # for enum types ``deprecated`` is not yet supported. + # for enum types, you can deprecate the type or some elements + # (likewise with object types and their fields): + type Bar {.deprecated.} = enum bar0, bar1 + type Barz = enum baz0, baz1 {.deprecated.}, baz2 See also `Deprecated <https://nim-lang.org/docs/manual.html#pragmas-deprecated-pragma>`_ @@ -181,21 +196,22 @@ Documentation ============= When contributing new procs, be sure to add documentation, especially if -the proc is public. Documentation begins on the line +the proc is public. Even private procs benefit from documentation and can be +viewed using ``nim doc --docInternal foo.nim``. +Documentation begins on the line following the ``proc`` definition, and is prefixed by ``##`` on each line. Runnable code examples are also encouraged, to show typical behavior with a few -test cases (typically 1 to 3 ``doAssert`` statements, depending on complexity). +test cases (typically 1 to 3 ``assert`` statements, depending on complexity). These ``runnableExamples`` are automatically run by ``nim doc mymodule.nim`` as well as ``testament`` and guarantee they stay in sync. .. code-block:: nim proc addBar*(a: string): string = - ## Adds "Bar" to ``a``. + ## Adds "Bar" to `a`. runnableExamples: - doAssert "baz".addBar == "bazBar" - - result = a & "Bar" + assert "baz".addBar == "bazBar" + result = a & "Bar" See `parentDir <https://nim-lang.github.io/Nim/os.html#parentDir%2Cstring>`_ example. @@ -209,14 +225,13 @@ not guaranteed to stay in sync, so ``runnableExamples`` is usually preferred: proc someproc*(): string = ## Return "something" ## - ## .. code-block:: nim - ## + ## .. code-block:: ## echo someproc() # "something" result = "something" # single-hash comments do not produce documentation The ``.. code-block:: nim`` followed by a newline and an indentation instructs the ``nim doc`` command to produce syntax-highlighted example code with the -documentation. +documentation (``.. code-block::`` is sufficient from inside a nim module). When forward declaration is used, the documentation should be included with the first appearance of the proc. @@ -236,14 +251,14 @@ the imperative (command) form. That is, between: .. code-block:: nim proc hello*(): string = - # Return "hello" + ## Return "hello" result = "hello" or .. code-block:: nim proc hello*(): string = - # says hello + ## says hello result = "hello" the first is preferred. @@ -282,12 +297,15 @@ rationale: https://forum.nim-lang.org/t/4089 doAssert() # preferred .. _tests_use_doAssert: -Use ``doAssert`` (or ``require``, etc), not ``assert`` in all tests. +Use ``doAssert`` (or ``require``, etc), not ``assert`` in all tests so they'll +be enabled even in release mode (except for tests in ``runnableExamples`` blocks +which for which ``nim doc`` ignores ``-d:release``). .. code-block:: nim - runnableExamples: assert foo() # bad - runnableExamples: doAssert foo() # preferred + when isMainModule: + assert foo() # bad + doAssert foo() # preferred .. _delegate_printing: Delegate printing to caller: return ``string`` instead of calling ``echo`` @@ -316,7 +334,7 @@ https://github.com/nim-lang/Nim/pull/9335 and https://forum.nim-lang.org/t/4089 .. code-block:: nim - echo foo() # adds a line in testament `discard` block. + echo foo() # adds a line for testament in `output:` block inside `discard`. doAssert foo() == [1, 2] # preferred, except when not possible to do so. The Git stuff @@ -398,6 +416,26 @@ Code reviews saves time explaining the change or applying it; see also https://forum.nim-lang.org/t/4317 +2. When reviewing large diffs that may involve code moving around, github's interface + doesn't help much as it doesn't highlight moves. Instead you can use something + like this, see visual results `here <https://github.com/nim-lang/Nim/pull/10431#issuecomment-456968196>`_: + + .. code-block:: sh + + git fetch origin pull/10431/head && git checkout FETCH_HEAD + git show --color-moved-ws=allow-indentation-change --color-moved=blocks HEAD^ + +3. In addition, you can view github-like diffs locally to identify what was changed + within a code block using `diff-highlight` or `diff-so-fancy`, eg: + + .. code-block:: sh + + # put this in ~/.gitconfig: + [core] + pager = "diff-so-fancy | less -R" # or: use: `diff-highlight` + + + .. include:: docstyle.rst diff --git a/doc/docgen.rst b/doc/docgen.rst index e6604d3bd..84969d8ee 100644 --- a/doc/docgen.rst +++ b/doc/docgen.rst @@ -283,7 +283,7 @@ symbols in the `system module <system.html>`_. * ``const NimVersion = "0.0.0"`` **=>** `#NimVersion <system.html#NimVersion>`_ * ``proc getTotalMem(): int {.rtl, raises: [], tags: [].}`` **=>** - `#getTotalMem, <system.html#getTotalMem,>`_ + `#getTotalMem, <system.html#getTotalMem>`_ * ``proc len[T](x: seq[T]): int {.magic: "LengthSeq", noSideEffect.}`` **=>** `#len,seq[T] <system.html#len,seq[T]>`_ * ``iterator pairs[T](a: seq[T]): tuple[key: int, val: T] {.inline.}`` **=>** diff --git a/doc/docs.rst b/doc/docs.rst index cd1a05853..4a69bd69a 100644 --- a/doc/docs.rst +++ b/doc/docs.rst @@ -6,6 +6,9 @@ The documentation consists of several documents: - | `Tutorial (part II) <tut2.html>`_ | The Nim tutorial part two deals with the advanced language constructs. +- | `Tutorial (part III) <tut3.html>`_ + | The Nim tutorial part three about Nim's macro system. + - | `Language Manual <manual.html>`_ | The Nim manual is a draft that will evolve into a proper specification. diff --git a/doc/docstyle.rst b/doc/docstyle.rst index d789b1df9..f0785bc0a 100644 --- a/doc/docstyle.rst +++ b/doc/docstyle.rst @@ -4,23 +4,31 @@ Documentation Style General Guidelines ------------------ -* Authors should document anything that is exported. +* Authors should document anything that is exported; documentation for private + procs can be useful too (visible via ``nim doc --docInternal foo.nim``). * Within documentation, a period (`.`) should follow each sentence (or sentence fragment) in a comment block. The documentation may be limited to one sentence fragment, but if multiple sentences are within the documentation, each sentence after the first should be complete and in present tense. -* Documentation is parsed as ReStructuredText (RST). -* Inline code should be surrounded by double tick marks ("``````"). If you would like a character to immediately follow inline code (e.g., "``int8``s are great!"), escape the following character with a backslash (``\``). The preceding is typed as ``` ``int8``\s are great!```. +* Documentation is parsed as a custom ReStructuredText (RST) with partial markdown support. + +.. code-block:: nim + + proc someproc*(s: string, foo: int) = + ## Use single backticks for inline code, eg: `s` or `someExpr(true)`. + ## Use a backlash to follow with alphanumeric char: `int8`\s are great. + Module-level documentation -------------------------- Documentation of a module is placed at the top of the module itself. Each line of documentation begins with double hashes (``##``). +Sometimes ``##[ multiline docs containing code ]##`` is preferable, see ``lib/pure/times.nim``. Code samples are encouraged, and should follow the general RST syntax: .. code-block:: Nim - ## The ``universe`` module computes the answer to life, the universe, and everything. + ## The `universe` module computes the answer to life, the universe, and everything. ## - ## .. code-block:: Nim - ## echo computeAnswerString() # "42" + ## .. code-block:: + ## doAssert computeAnswerString() == 42 Within this top-level comment, you can indicate the authorship and copyright of the code, which will be featured in the produced documentation. @@ -38,12 +46,12 @@ Leave a space between the last line of top-level documentation and the beginning Procs, Templates, Macros, Converters, and Iterators --------------------------------------------------- -The documentation of a procedure should begin with a capital letter and should be in present tense. Variables referenced in the documentation should be surrounded by double tick marks (``````). +The documentation of a procedure should begin with a capital letter and should be in present tense. Variables referenced in the documentation should be surrounded by single tick marks: .. code-block:: Nim proc example1*(x: int) = - ## Prints the value of ``x``. + ## Prints the value of `x`. echo x Whenever an example of usage would be helpful to the user, you should include one within the documentation in RST format as below. @@ -51,11 +59,16 @@ Whenever an example of usage would be helpful to the user, you should include on .. code-block:: Nim proc addThree*(x, y, z: int8): int = - ## Adds three ``int8`` values, treating them as unsigned and + ## Adds three `int8` values, treating them as unsigned and ## truncating the result. ## - ## .. code-block:: nim - ## echo addThree(3, 125, 6) # -122 + ## .. code-block:: + ## # things that aren't suitable for a `runnableExamples` go in code-block: + ## echo execCmdEx("git pull") + ## drawOnScreen() + runnableExamples: + # `runnableExamples` is usually preferred to `code-block`, when possible. + doAssert addThree(3, 125, 6) == -122 result = x +% y +% z The commands ``nim doc`` and ``nim doc2`` will then correctly syntax highlight the Nim code within the documentation. @@ -94,7 +107,7 @@ Make sure to place the documentation beside or within the object. .. code-block:: Nim type - ## This documentation disappears because it annotates the ``type`` keyword + ## Bad: this documentation disappears because it annotates the ``type`` keyword ## above, not ``NamedQueue``. NamedQueue*[T] = object name*: string ## This becomes the main documentation for the object, which diff --git a/doc/intern.rst b/doc/intern.rst index b71ad592f..8e0df8fd3 100644 --- a/doc/intern.rst +++ b/doc/intern.rst @@ -67,8 +67,8 @@ Coding Guidelines * Max line length is 80 characters. * Provide spaces around binary operators if that enhances readability. * Use a space after a colon, but not before it. -* Start types with a capital ``T``, unless they are pointers/references which - start with ``P``. +* [deprecated] Start types with a capital ``T``, unless they are + pointers/references which start with ``P``. See also the `API naming design <apis.html>`_ document. diff --git a/doc/lib.rst b/doc/lib.rst index 89e3cca40..d00591e10 100644 --- a/doc/lib.rst +++ b/doc/lib.rst @@ -67,6 +67,8 @@ Core * `lenientops <lenientops.html>`_ Provides binary operators for mixed integer/float expressions for convenience. +* `bitops <bitops.html>`_ + Provides a series of low level methods for bit manipulation. Collections and algorithms @@ -74,26 +76,38 @@ Collections and algorithms * `algorithm <algorithm.html>`_ Implements some common generic algorithms like sort or binary search. + * `tables <tables.html>`_ Nim hash table support. Contains tables, ordered tables and count tables. + * `sets <sets.html>`_ Nim hash and bit set support. + * `lists <lists.html>`_ Nim linked list support. Contains singly and doubly linked lists and circular lists ("rings"). + * `deques <deques.html>`_ Implementation of a double-ended queue. The underlying implementation uses a ``seq``. + +* `heapqueue <heapqueue.html>`_ + Implementation of a heap data structure that can be used as a priority queue. + * `intsets <intsets.html>`_ Efficient implementation of a set of ints as a sparse bit set. + * `critbits <critbits.html>`_ This module implements a *crit bit tree* which is an efficient container for a sorted set of strings, or for a sorted mapping of strings. + * `sequtils <sequtils.html>`_ This module implements operations for the built-in seq type which were inspired by functional programming languages. + * `sharedtables <sharedtables.html>`_ Nim shared hash table support. Contains shared tables. + * `sharedlist <sharedlist.html>`_ Nim shared linked list support. Contains shared singly linked list. @@ -107,8 +121,8 @@ String handling substrings, replacing substrings. * `strformat <strformat.html>`_ - Macro based standard string interpolation / formatting. Inpired by - Python's ```f``-strings. + Macro based standard string interpolation / formatting. Inspired by + Python's ``f``-strings. * `strmisc <strmisc.html>`_ This module contains uncommon string handling operations that do not @@ -129,6 +143,13 @@ String handling * `unicode <unicode.html>`_ This module provides support to handle the Unicode UTF-8 encoding. +* `unidecode <unidecode.html>`_ + It provides a single proc that does Unicode to ASCII transliterations. + Based on Python's Unidecode module. + +* `punycode <punycode.html>`_ + Implements a representation of Unicode with the limited ASCII character subset. + * `encodings <encodings.html>`_ Converts between different character encodings. On UNIX, this uses the ``iconv`` library, on Windows the Windows API. @@ -141,12 +162,6 @@ String handling Ropes can represent very long strings efficiently; especially concatenation is done in O(1) instead of O(n). -* `matchers <matchers.html>`_ - This module contains various string matchers for email addresses, etc. - -* `subexes <subexes.html>`_ - This module implements advanced string substitution operations. - * `std/editdistance <editdistance.html>`_ This module contains an algorithm to compute the edit distance between two Unicode strings. @@ -200,10 +215,16 @@ Generic Operating System Services ``asyncdispatch``. * `distros <distros.html>`_ - This module implements the basics for OS distribution ("distro") detection and the OS's native package manager. - Its primary purpose is to produce output for Nimble packages, but it also contains the widely used **Distribution** enum + This module implements the basics for OS distribution ("distro") detection + and the OS's native package manager. + Its primary purpose is to produce output for Nimble packages, + but it also contains the widely used **Distribution** enum that is useful for writing platform specific code. +* `volatile <volatile.html>`_ + This module contains code for generating volatile loads and stores, + which are useful in embedded and systems programming. + Math libraries -------------- @@ -236,9 +257,6 @@ Internet Protocols and Support * `cgi <cgi.html>`_ This module implements helpers for CGI applications. -* `scgi <scgi.html>`_ - This module implements helpers for SCGI applications. - * `browsers <browsers.html>`_ This module implements procs for opening URLs with the user's default browser. @@ -275,8 +293,8 @@ Internet Protocols and Support module. * `net <net.html>`_ - This module implements a high-level sockets API. It will replace the - ``sockets`` module in the future. + This module implements a high-level sockets API. It replaces the + ``sockets`` module. * `nativesockets <nativesockets.html>`_ This module implements a low-level sockets API. @@ -285,6 +303,7 @@ Internet Protocols and Support This module implements a selector API with backends specific to each OS. Currently epoll on Linux and select on other operating systems. + Parsers ------- @@ -411,6 +430,7 @@ Miscellaneous * `segfaults <segfaults.html>`_ Turns access violations or segfaults into a ``NilAccessError`` exception. + Modules for JS backend ---------------------- @@ -439,7 +459,6 @@ Regular expressions expressions. The current implementation uses PCRE. - Database support ---------------- @@ -456,20 +475,13 @@ Database support for other databases too. -Other ------ - -* `ssl <ssl.html>`_ - This module provides an easy to use sockets-style - Nim interface to the OpenSSL library. - - Wrappers ======== The generated HTML for some of these wrappers is so huge that it is not contained in the distribution. You can then find them on the website. + Windows specific ---------------- @@ -525,5 +537,5 @@ Nimble is a package manager for the Nim programming language. For instructions on how to install Nimble packages see `its README <https://github.com/nim-lang/nimble#readme>`_. -To see a list of Nimble's packages, check out `https://nimble.directory/ <https://nimble.directory/>`_ +To see a list of Nimble's packages, check out `<https://nimble.directory/>`_ or the `packages repo <https://github.com/nim-lang/packages>`_ on GitHub. diff --git a/doc/manual.rst b/doc/manual.rst index 4a1ad9d5e..7b0a30f6a 100644 --- a/doc/manual.rst +++ b/doc/manual.rst @@ -46,8 +46,8 @@ and ``a ^* b`` is short for ``(a (b a)*)?``. Example:: arrayConstructor = '[' expr ^* ',' ']' -Other parts of Nim - like scoping rules or runtime semantics are only -described in the, more easily comprehensible, informal manner for now. +Other parts of Nim, like scoping rules or runtime semantics, are +described informally. @@ -55,7 +55,7 @@ described in the, more easily comprehensible, informal manner for now. Definitions =========== -A Nim program specifies a computation that acts on a memory consisting of +Nim code specifies a computation that acts on a memory consisting of components called `locations`:idx:. A variable is basically a name for a location. Each variable and location is of a certain `type`:idx:. The variable's type is called `static type`:idx:, the location's type is called @@ -71,21 +71,40 @@ identifier is declared unless overloading resolution rules suggest otherwise. An expression specifies a computation that produces a value or location. Expressions that produce locations are called `l-values`:idx:. An l-value can denote either a location or the value the location contains, depending on -the context. Expressions whose values can be determined statically are called -`constant expressions`:idx:; they are never l-values. - -A `static error`:idx: is an error that the implementation detects before -program execution. Unless explicitly classified, an error is a static error. +the context. + +A Nim `program`:idx: consists of one or more text `source files`:idx: containing +Nim code. It is processed by a Nim `compiler`:idx: into an `executable`:idx:. +The nature of this executable depends on the compiler implementation; it may, +for example, be a native binary or JavaScript source code. + +In a typical Nim program, most of the code is compiled into the executable. +However, some of the code may be executed at +`compile time`:idx:. This can include constant expressions, macro definitions, +and Nim procedures used by macro definitions. Most of the Nim language is +supported at compile time, but there are some restrictions -- see `Restrictions +on Compile-Time Execution <#restrictions-on-compileminustime-execution>`_ for +details. We use the term `runtime`:idx: to cover both compile-time execution +and code execution in the executable. + +The compiler parses Nim source code into an internal data structure called the +`abstract syntax tree`:idx: (`AST`:idx:). Then, before executing the code or +compiling it into the executable, it transforms the AST through `semantic +analysis`:idx:. This adds semantic information such as expression types, +identifier meanings, and in some cases expression values. An error detected +during semantic analysis is called a `static error`:idx:. Errors described in +this manual are static errors when not otherwise specified. A `checked runtime error`:idx: is an error that the implementation detects -and reports at runtime. The method for reporting such errors is via *raising -exceptions* or *dying with a fatal error*. However, the implementation -provides a means to disable these runtime checks. See the section pragmas_ -for details. +and reports at runtime. The method for reporting such errors is via +*raising exceptions* or *dying with a fatal error*. However, the implementation +provides a means to disable these `runtime checks`:idx:. See the section +pragmas_ for details. -Whether a checked runtime error results in an exception or in a fatal error at -runtime is implementation specific. Thus the following program is always -invalid: +Whether a checked runtime error results in an exception or in a fatal error is +implementation specific. Thus the following program is invalid; even though the +code purports to catch the `IndexError` from an out-of-bounds array access, the +compiler may instead choose to allow the program to die with a fatal error. .. code-block:: nim var a: array[0..1, char] @@ -98,8 +117,23 @@ invalid: An `unchecked runtime error`:idx: is an error that is not guaranteed to be detected, and can cause the subsequent behavior of the computation to be arbitrary. Unchecked runtime errors cannot occur if only `safe`:idx: -language features are used. +language features are used and if no runtime checks are disabled. + +A `constant expression`:idx: is an expression whose value can be computed during +semantic analysis of the code in which it appears. It is never an l-value and +never has side effects. Constant expressions are not limited to the capabilities +of semantic analysis, such as constant folding; they can use all Nim language +features that are supported for compile-time execution. Since constant +expressions can be used as an input to semantic analysis (such as for defining +array bounds), this flexibility requires the compiler to interleave semantic +analysis and compile-time code execution. +It is mostly accurate to picture semantic analysis proceeding top to bottom and +left to right in the source code, with compile-time code execution interleaved +when necessary to compute values that are required for subsequent semantic +analysis. We will see much later in this document that macro invocation not only +requires this interleaving, but also creates a situation where semantic analyis +does not entirely proceed top to bottom and left to right. Lexical Analysis @@ -251,6 +285,10 @@ Another advantage is that it frees the programmer from remembering the exact spelling of an identifier. The exception with respect to the first letter allows common code like ``var foo: Foo`` to be parsed unambiguously. +Note that this rule also applies to keywords, meaning that ``notin`` is +the same as ``notIn`` and ``not_in`` (all-lowercase version (``notin``, ``isnot``) +is the preferred way of writing keywords). + Historically, Nim was a fully `style-insensitive`:idx: language. This meant that it was not case-sensitive and underscores were ignored and there was not even a distinction between ``foo`` and ``Foo``. @@ -480,7 +518,6 @@ The type suffixes are: ``'d`` float64 ``'f32`` float32 ``'f64`` float64 - ``'f128`` float128 ================= ========================= Floating point literals may also be in binary, octal or hexadecimal @@ -674,10 +711,87 @@ Rationale: Consistency with overloaded assignment or assignment-like operations, ``a = b`` can be read as ``performSomeCopy(a, b)``. +Constants and Constant Expressions +================================== + +A `constant`:idx: is a symbol that is bound to the value of a constant +expression. Constant expressions are restricted to depend only on the following +categories of values and operations, because these are either built into the +language or declared and evaluated before semantic analysis of the constant +expression: + +* literals +* built-in operators +* previously declared constants and compile-time variables +* previously declared macros and templates +* previously declared procedures that have no side effects beyond + possibly modifying compile-time variables + +A constant expression can contain code blocks that may internally use all Nim +features supported at compile time (as detailed in the next section below). +Within such a code block, it is possible to declare variables and then later +read and update them, or declare variables and pass them to procedures that +modify them. However, the code in such a block must still adhere to the +retrictions listed above for referencing values and operations outside the +block. + +The ability to access and modify compile-time variables adds flexibility to +constant expressions that may be surprising to those coming from other +statically typed languages. For example, the following code echoes the beginning +of the Fibonacci series **at compile time**. (This is a demonstration of +flexibility in defining constants, not a recommended style for solving this +problem!) + +.. code-block:: nim + :test: "nim c $1" + import strformat + + var fib_n {.compileTime.}: int + var fib_prev {.compileTime.}: int + var fib_prev_prev {.compileTime.}: int + + proc next_fib(): int = + result = if fib_n < 2: + fib_n + else: + fib_prev_prev + fib_prev + inc(fib_n) + fib_prev_prev = fib_prev + fib_prev = result + + const f0 = next_fib() + const f1 = next_fib() + + const display_fib = block: + const f2 = next_fib() + var result = fmt"Fibonacci sequence: {f0}, {f1}, {f2}" + for i in 3..12: + add(result, fmt", {next_fib()}") + result + + static: + echo display_fib + + +Restrictions on Compile-Time Execution +====================================== + +Nim code that will be executed at compile time cannot use the following +language features: + +* methods +* closure iterators +* the ``cast`` operator +* reference (pointer) types +* the FFI + +Some or all of these restrictions are likely to be lifted over time. + + Types ===== -All expressions have a type which is known at compile time. Nim +All expressions have a type which is known during semantic analysis. Nim is statically typed. One can declare new types, which is in essence defining an identifier that can be used to denote this custom type. @@ -798,8 +912,8 @@ For further details, see `Convertible relation Subrange types -------------- A subrange type is a range of values from an ordinal or floating point type (the base -type). To define a subrange type, one must specify it's limiting values: the -lowest and highest value of the type: +type). To define a subrange type, one must specify its limiting values -- the +lowest and highest value of the type. For example: .. code-block:: nim type @@ -811,8 +925,8 @@ lowest and highest value of the type: to 5. ``PositiveFloat`` defines a subrange of all positive floating point values. NaN does not belong to any subrange of floating point types. Assigning any other value to a variable of type ``Subrange`` is a -checked runtime error (or static error if it can be statically -determined). Assignments from the base type to one of its subrange types +checked runtime error (or static error if it can be determined during +semantic analysis). Assignments from the base type to one of its subrange types (and vice versa) are allowed. A subrange type has the same size as its base type (``int`` in the @@ -855,7 +969,7 @@ The IEEE standard defines five types of floating-point exceptions: * Inexact: operation produces a result that cannot be represented with infinite precision, for example, 2.0 / 3.0, log(1.1) and 0.1 in input. -The IEEE exceptions are either ignored at runtime or mapped to the +The IEEE exceptions are either ignored during execution or mapped to the Nim exceptions: `FloatInvalidOpError`:idx:, `FloatDivByZeroError`:idx:, `FloatOverflowError`:idx:, `FloatUnderflowError`:idx:, and `FloatInexactError`:idx:. @@ -882,8 +996,9 @@ The only operations that are affected by the ``floatChecks`` pragma are the ``+``, ``-``, ``*``, ``/`` operators for floating point types. An implementation should always use the maximum precision available to evaluate -floating pointer values at compile time; this means expressions like -``0.09'f32 + 0.01'f32 == 0.09'f64 + 0.01'f64`` are true. +floating pointer values during semantic analysis; this means expressions like +``0.09'f32 + 0.01'f32 == 0.09'f64 + 0.01'f64`` that are evaluating during +constant folding are true. Boolean type @@ -1110,9 +1225,9 @@ tuples, objects and sets belong to the structured types. Array and sequence types ------------------------ -Arrays are a homogeneous type, meaning that each element in the array -has the same type. Arrays always have a fixed length which is specified at -compile time (except for open arrays). They can be indexed by any ordinal type. +Arrays are a homogeneous type, meaning that each element in the array has the +same type. Arrays always have a fixed length specified as a constant expression +(except for open arrays). They can be indexed by any ordinal type. A parameter ``A`` may be an *open array*, in which case it is indexed by integers from 0 to ``len(A)-1``. An array expression may be constructed by the array constructor ``[]``. The element type of this array expression is @@ -1154,7 +1269,7 @@ operator, and remove (and get) the last element of a sequence with the The notation ``x[i]`` can be used to access the i-th element of ``x``. -Arrays are always bounds checked (at compile-time or at runtime). These +Arrays are always bounds checked (statically or at runtime). These checks can be disabled via pragmas or invoking the compiler with the ``--boundChecks:off`` command line switch. @@ -1317,10 +1432,10 @@ can also be defined with indentation instead of ``[]``: name: string # a person consists of a name age: natural # and an age -Objects provide many features that tuples do not. Object provide inheritance -and information hiding. Objects have access to their type at runtime, so that -the ``of`` operator can be used to determine the object's type. The ``of`` operator -is similar to the ``instanceof`` operator in Java. +Objects provide many features that tuples do not. Object provide inheritance and +information hiding. Objects have access to their type during at runtime, so that +the ``of`` operator can be used to determine the object's type. The ``of`` +operator is similar to the ``instanceof`` operator in Java. .. code-block:: nim type @@ -1415,8 +1530,8 @@ In the example the ``kind`` field is called the `discriminator`:idx:\: For safety its address cannot be taken and assignments to it are restricted: The new value must not lead to a change of the active object branch. For an object branch switch ``system.reset`` has to be used. Also, when the fields of a -particular branch are specified during object construction, the correct value -for the discriminator must be supplied at compile-time. +particular branch are specified during object construction, the corresponding +discriminator value must be specified as a constant expression. Package level objects --------------------- @@ -1430,8 +1545,8 @@ contexts (``var/ref/ptr IncompleteObject``) in general since the compiler does not yet know the size of the object. To complete an incomplete object the ``package`` pragma has to be used. ``package`` implies ``byref``. -As long as a type ``T`` is incomplete ``sizeof(T)`` or "runtime type -information" for ``T`` is not available. +As long as a type ``T`` is incomplete, neither ``sizeof(T)`` nor runtime +type information for ``T`` is available. Example: @@ -1587,7 +1702,7 @@ details like this when mixing garbage collected data with unmanaged memory. Not nil annotation ------------------ -All types for that ``nil`` is a valid value can be annotated to +All types for which ``nil`` is a valid value can be annotated to exclude ``nil`` as a valid value with the ``not nil`` annotation: .. code-block:: nim @@ -1664,12 +1779,12 @@ Nim supports these `calling conventions`:idx:\: and another one for the pointer to implicitly passed environment. `stdcall`:idx: - This the stdcall convention as specified by Microsoft. The generated C + This is the stdcall convention as specified by Microsoft. The generated C procedure is declared with the ``__stdcall`` keyword. `cdecl`:idx: The cdecl convention means that a procedure shall use the same convention - as the C compiler. Under windows the generated C procedure is declared with + as the C compiler. Under Windows the generated C procedure is declared with the ``__cdecl`` keyword. `safecall`:idx: @@ -1860,7 +1975,7 @@ that don't. Distinct types provide a means to introduce a new string type username: string db.query("SELECT FROM users WHERE name = '$1'" % username) - # Error at compile time: `query` expects an SQL string! + # Static error: `query` expects an SQL string! It is an essential property of abstract types that they **do not** imply a @@ -2247,7 +2362,8 @@ Overloading resolution ====================== In a call ``p(args)`` the routine ``p`` that matches best is selected. If -multiple routines match equally well, the ambiguity is reported at compiletime. +multiple routines match equally well, the ambiguity is reported during +semantic analysis. Every arg in args needs to match. There are multiple different categories how an argument can match. Let ``f`` be the formal parameter's type and ``a`` the type @@ -2628,53 +2744,36 @@ identifier ``_`` can be used to ignore some parts of the tuple: Const section ------------- -`Constants`:idx: are symbols which are bound to a value. The constant's value -cannot change. The compiler must be able to evaluate the expression in a -constant declaration at compile time. - -Nim contains a sophisticated compile-time evaluator, so procedures which -have no side-effect can be used in constant expressions too: +A const section declares constants whose values are constant expressions: -.. code-block:: nim +.. code-block:: import strutils const + roundPi = 3.1415 constEval = contains("abc", 'b') # computed at compile time! +Once declared, a constant's symbol can be used as a constant expression. -The rules for compile-time computability are: - -1. Literals are compile-time computable. -2. Type conversions are compile-time computable. -3. Procedure calls of the form ``p(X)`` are compile-time computable if - ``p`` is a proc without side-effects (see the `noSideEffect pragma - <#pragmas-nosideeffect-pragma>`_ for details) and if ``X`` is a - (possibly empty) list of compile-time computable arguments. - - -Constants cannot be of type ``ptr``, ``ref`` or ``var``, nor can -they contain such a type. - +See `Constants and Constant Expressions <#constants-and-constant-expressions>`_ +for details. Static statement/expression --------------------------- -A static statement/expression can be used to enforce compile -time evaluation explicitly. Enforced compile time evaluation can even evaluate -code that has side effects: +A static statement/expression explicitly requires compile-time execution. +Even some code that has side effects is permitted in a static block: .. code-block:: static: echo "echo at compile time" -It's a static error if the compiler cannot perform the evaluation at compile +There are limitations on what Nim code can be executed at compile time; +see `Restrictions on Compile-Time Execution +<#restrictions-on-compileminustime-execution>`_ for details. +It's a static error if the compiler cannot execute the block at compile time. -The current implementation poses some restrictions for compile time -evaluation: Code which contains ``cast`` or makes use of the foreign function -interface cannot be evaluated at compile time. Later versions of Nim will -support the FFI at compile time. - If statement ------------ @@ -2748,9 +2847,9 @@ empty ``discard`` statement should be used. For non ordinal types it is not possible to list every possible value and so these always require an ``else`` part. -As case statements perform compile-time exhaustiveness checks, the value in -every ``of`` branch must be known at compile time. This fact is also exploited -to generate more performant code. +Because case statements are checked for exhaustiveness during semantic analysis, +the value in every ``of`` branch must be a constant expression. +This restriction also allows the compiler to generate more performant code. As a special semantic extension, an expression in an ``of`` branch of a case statement may evaluate to a set or array constructor; the set or array is then @@ -2808,17 +2907,18 @@ When nimvm statement -------------------- ``nimvm`` is a special symbol, that may be used as expression of ``when nimvm`` -statement to differentiate execution path between runtime and compile time. +statement to differentiate execution path between compile time and the +executable. Example: .. code-block:: nim proc someProcThatMayRunInCompileTime(): bool = when nimvm: - # This code runs in compile time + # This branch is taken at compile time. result = true else: - # This code runs in runtime + # This branch is taken in the executable. result = false const ctValue = someProcThatMayRunInCompileTime() let rtValue = someProcThatMayRunInCompileTime() @@ -3558,7 +3658,7 @@ returned value is an l-value and can be modified by the caller: writeAccessToG() = 6 assert g == 6 -It is a compile time error if the implicitly introduced pointer could be +It is a static error if the implicitly introduced pointer could be used to access a location beyond its lifetime: .. code-block:: nim @@ -3674,7 +3774,7 @@ Invocation of a multi-method cannot be ambiguous: collide 2 is preferred over collide 1 because the resolution works from left to right. In the example ``Unit, Thing`` is preferred over ``Thing, Unit``. -**Note**: Compile time evaluation is not (yet) supported for methods. +**Note**: Compile-time execution is not (yet) supported for methods. Inhibit dynamic method resolution via procCall @@ -3770,7 +3870,14 @@ First class iterators There are 2 kinds of iterators in Nim: *inline* and *closure* iterators. An `inline iterator`:idx: is an iterator that's always inlined by the compiler leading to zero overhead for the abstraction, but may result in a heavy -increase in code size. Inline iterators are second class citizens; +increase in code size. + +Caution: the body of a for loop over an inline iterator is inlined into +each ``yield`` statement appearing in the iterator code, +so ideally the code should be refactored to contain a single yield when possible +to avoid code bloat. + +Inline iterators are second class citizens; They can be passed as parameters only to other inlining code facilities like templates, macros and other inline iterators. @@ -3795,7 +3902,7 @@ In contrast to that, a `closure iterator`:idx: can be passed around more freely: Closure iterators have other restrictions than inline iterators: 1. ``yield`` in a closure iterator can not occur in a ``try`` statement. -2. For now, a closure iterator cannot be evaluated at compile time. +2. For now, a closure iterator cannot be executed at compile time. 3. ``return`` is allowed in a closure iterator (but rarely useful) and ends iteration. 4. Neither inline nor closure iterators can be recursive. @@ -4114,8 +4221,8 @@ The exception tree is defined in the `system <system.html>`_ module. Every exception inherits from ``system.Exception``. Exceptions that indicate programming bugs inherit from ``system.Defect`` (which is a subtype of ``Exception``) and are stricly speaking not catchable as they can also be mapped to an operation -that terminates the whole process. Exceptions that indicate any other runtime error -that can be caught inherit from ``system.CatchableError`` +that terminates the whole process. Exceptions that indicate any other runtime +error that can be caught inherit from ``system.CatchableError`` (which is a subtype of ``Exception``). @@ -4352,8 +4459,9 @@ a `type variable`:idx:. Is operator ----------- -The ``is`` operator checks for type equivalence at compile time. It is -therefore very useful for type specialization within generic code: +The ``is`` operator is evaluated during semantic analysis to check for type +equivalence. It is therefore very useful for type specialization within generic +code: .. code-block:: nim type @@ -5349,14 +5457,22 @@ chance to convert it into a sequence. Macros ====== -A macro is a special function that is executed at compile-time. +A macro is a special function that is executed at compile time. Normally the input for a macro is an abstract syntax tree (AST) of the code that is passed to it. The macro can then do -transformations on it and return the transformed AST. The -transformed AST is then passed to the compiler as if the macro -invocation would have been replaced by its result in the source -code. This can be used to implement `domain specific -languages`:idx:. +transformations on it and return the transformed AST. This can be used to +add custom language features and implement `domain specific languages`:idx:. + +Macro invocation is a case where semantic analyis does **not** entirely proceed +top to bottom and left to right. Instead, semantic analysis happens at least +twice: + +* Semantic analysis recognizes and resolves the macro invocation. +* The compiler executes the macro body (which may invoke other procs). +* It replaces the AST of the macro invocation with the AST returned by the macro. +* It repeats semantic analysis of that region of the code. +* If the AST returned by the macro contains other macro invocations, + this process iterates. While macros enable advanced compile-time code transformations, they cannot change Nim's syntax. However, this is no real restriction because @@ -5640,7 +5756,7 @@ static[T] **Note**: static[T] is still in development. -As their name suggests, static parameters must be known at compile-time: +As their name suggests, static parameters must be constant expressions: .. code-block:: nim @@ -5652,7 +5768,7 @@ As their name suggests, static parameters must be known at compile-time: # regex, stored in a global variable precompiledRegex(paramStr(1)) # Error, command-line options - # are not known at compile-time + # are not constant expressions For the purposes of code generation, all static params are treated as @@ -5666,7 +5782,7 @@ Static params can also appear in the signatures of generic types: type Matrix[M,N: static int; T: Number] = array[0..(M*N - 1), T] # Note how `Number` is just a type constraint here, while - # `static int` requires us to supply a compile-time int value + # `static int` requires us to supply an int value AffineTransform2D[T] = Matrix[3, 3, T] AffineTransform3D[T] = Matrix[4, 4, T] @@ -5674,14 +5790,13 @@ Static params can also appear in the signatures of generic types: var m1: AffineTransform3D[float] # OK var m2: AffineTransform2D[string] # Error, `string` is not a `Number` -Please note that ``static T`` is just a syntactic convenience for the -underlying generic type ``static[T]``. The type param can be omitted -to obtain the type class of all values known at compile-time. A more -specific type class can be created by instantiating ``static`` with -another type class. +Please note that ``static T`` is just a syntactic convenience for the underlying +generic type ``static[T]``. The type param can be omitted to obtain the type +class of all constant expressions. A more specific type class can be created by +instantiating ``static`` with another type class. -You can force the evaluation of a certain expression at compile-time by -coercing it to a corresponding ``static`` type: +You can force an expression to be evaluated at compile time as a constant +expression by coercing it to a corresponding ``static`` type: .. code-block:: nim import math @@ -6538,6 +6653,13 @@ modules don't need to import a module's dependencies: When the exported symbol is another module, all of its definitions will be forwarded. You can use an ``except`` list to exclude some of the symbols. +Notice that when exporting, you need to specify only the module name: + +.. code-block:: nim + import foo/bar/baz + export baz + + Scope rules ----------- @@ -6767,11 +6889,11 @@ pragma block can be used: compileTime pragma ------------------ -The ``compileTime`` pragma is used to mark a proc or variable to be used at -compile time only. No code will be generated for it. Compile time procs are -useful as helpers for macros. Since version 0.12.0 of the language, a proc -that uses ``system.NimNode`` within its parameter types is implicitly declared -``compileTime``: +The ``compileTime`` pragma is used to mark a proc or variable to be used only +during compile-time execution. No code will be generated for it. Compile-time +procs are useful as helpers for macros. Since version 0.12.0 of the language, a +proc that uses ``system.NimNode`` within its parameter types is implicitly +declared ``compileTime``: .. code-block:: nim proc astHelper(n: NimNode): NimNode = @@ -6858,8 +6980,8 @@ structure: pure pragma ----------- -An object type can be marked with the ``pure`` pragma so that its type -field which is used for runtime type identification is omitted. This used to be +An object type can be marked with the ``pure`` pragma so that its type field +which is used for runtime type identification is omitted. This used to be necessary for binary compatibility with other compiled languages. An enum type can be marked as ``pure``. Then access of its fields always @@ -6885,7 +7007,7 @@ though. The ``error`` pragma can also be used to annotate a symbol (like an iterator or proc). The *usage* of the symbol then -triggers a compile-time error. This is especially useful to rule out that some +triggers a static error. This is especially useful to rule out that some operation is valid due to overloading and type conversions: .. code-block:: nim @@ -6923,7 +7045,7 @@ statement as seen in stack backtraces: template myassert*(cond: untyped, msg = "") = if not cond: # change run-time line information of the 'raise' statement: - {.line: InstantiationInfo().}: + {.line: instantiationInfo().}: raise newException(EAssertionFailed, msg) If the ``line`` pragma is used with a parameter, the parameter needs be a @@ -7007,7 +7129,7 @@ extension the pragma is simply ignored. unroll pragma ------------- The ``unroll`` pragma can be used to tell the compiler that it should unroll -a `for`:idx: or `while`:idx: loop for runtime efficiency: +a `for`:idx: or `while`:idx: loop for execution efficiency: .. code-block:: nim proc searchChar(s: string, c: char): int = @@ -7322,7 +7444,8 @@ compiler like you would using the commandline switch ``--passC``: {.passC: "-Wall -Werror".} Note that you can use ``gorge`` from the `system module <system.html>`_ to -embed parameters from an external command at compile time: +embed parameters from an external command that will be executed +during semantic analysis: .. code-block:: Nim {.passC: gorge("pkg-config --cflags sdl").} @@ -7336,7 +7459,8 @@ like you would using the commandline switch ``--passL``: {.passL: "-lSDLmain -lSDL".} Note that you can use ``gorge`` from the `system module <system.html>`_ to -embed parameters from an external command at compile time: +embed parameters from an external command that will be executed +during semantic analysis: .. code-block:: Nim {.passL: gorge("pkg-config --libs sdl").} @@ -7741,6 +7865,7 @@ pragma description ================= ============================================ `intdefine`:idx: Reads in a build-time define as an integer `strdefine`:idx: Reads in a build-time define as a string +`booldefine`:idx: Reads in a build-time define as a bool ================= ============================================ .. code-block:: nim @@ -7748,13 +7873,14 @@ pragma description echo FooBar :: - nim c -d:FooBar=42 foobar.c + nim c -d:FooBar=42 foobar.nim In the above example, providing the -d flag causes the symbol ``FooBar`` to be overwritten at compile time, printing out 42. If the ``-d:FooBar=42`` were to be omitted, the default value of 5 would be -used. +used. To see if a value was provided, `defined(FooBar)` can be used. +The syntax `-d:flag` is actually just a shortcut for `-d:flag=true`. Custom annotations ------------------ @@ -7947,7 +8073,7 @@ interoperability with C. Combining packed pragma with inheritance is not defined, and it should not be used with GC'ed memory (ref's). **Future directions**: Using GC'ed memory in packed pragma will result in -compile-time error. Usage with inheritance should be defined and documented. +a static error. Usage with inheritance should be defined and documented. Dynlib pragma for import @@ -8228,11 +8354,11 @@ Example: echo formatFloat(pi(5000)) -The parallel statement is the preferred mechanism to introduce parallelism -in a Nim program. A subset of the Nim language is valid within a -``parallel`` section. This subset is checked to be free of data races at -compile time. A sophisticated `disjoint checker`:idx: ensures that no data -races are possible even though shared memory is extensively supported! +The parallel statement is the preferred mechanism to introduce parallelism in a +Nim program. A subset of the Nim language is valid within a ``parallel`` +section. This subset is checked during semantic analysis to be free of data +races. A sophisticated `disjoint checker`:idx: ensures that no data races are +possible even though shared memory is extensively supported! The subset is in fact the full language with the following restrictions / changes: @@ -8264,8 +8390,8 @@ pragmas: 1) A `guard`:idx: annotation is introduced to prevent data races. 2) Every access of a guarded memory location needs to happen in an appropriate `locks`:idx: statement. -3) Locks and routines can be annotated with `lock levels`:idx: to prevent - deadlocks at compile time. +3) Locks and routines can be annotated with `lock levels`:idx: to allow + potential deadlocks to be detected during semantic analysis. Guards and the locks section @@ -8385,9 +8511,10 @@ This means the following compiles (for now) even though it really should not: Lock levels ----------- -Lock levels are used to enforce a global locking order in order to prevent -deadlocks at compile-time. A lock level is an constant integer in the range -0..1_000. Lock level 0 means that no lock is acquired at all. +Lock levels are used to enforce a global locking order in order to detect +potential deadlocks during semantic analysis. A lock level is an constant +integer in the range 0..1_000. Lock level 0 means that no lock is acquired at +all. If a section of code holds a lock of level ``M`` than it can also acquire any lock of level ``N < M``. Another lock of level ``M`` cannot be acquired. Locks diff --git a/doc/nimc.rst b/doc/nimc.rst index e1bf98ece..7c07e09d8 100644 --- a/doc/nimc.rst +++ b/doc/nimc.rst @@ -347,18 +347,22 @@ complete list. Define Effect ====================== ========================================================= ``release`` Turns off runtime checks and turns on the optimizer. + More aggressive optimizations are possible, eg: + ``--passC:-ffast-math`` (but see issue #10305) + ``--stacktrace:off`` ``useWinAnsi`` Modules like ``os`` and ``osproc`` use the Ansi versions of the Windows API. The default build uses the Unicode version. ``useFork`` Makes ``osproc`` use ``fork`` instead of ``posix_spawn``. ``useNimRtl`` Compile and link against ``nimrtl.dll``. ``useMalloc`` Makes Nim use C's `malloc`:idx: instead of Nim's - own memory manager, ableit prefixing each allocation with + own memory manager, albeit prefixing each allocation with its size to support clearing memory on reallocation. This only works with ``gc:none``. ``useRealtimeGC`` Enables support of Nim's GC for *soft* realtime systems. See the documentation of the `gc <gc.html>`_ for further information. +``logGC`` Enable GC logging to stdout. ``nodejs`` The JS target is actually ``node.js``. ``ssl`` Enables OpenSSL support for the sockets module. ``memProfiler`` Enables memory profiling for the native GC. diff --git a/doc/nims.rst b/doc/nims.rst index 034ad1fda..eaf20a6db 100644 --- a/doc/nims.rst +++ b/doc/nims.rst @@ -26,9 +26,28 @@ previous settings): ``$project.nim``. This file can be skipped with the same ``--skipProjCfg`` command line option. -The VM cannot deal with ``importc`` because the FFI is not -available. So the stdlib modules using ``importc`` cannot be used with -Nim's VM. However, at least the following modules are available: +Limitations +================================= + +NimScript is subject to some limitations caused by the implementation of the VM +(virtual machine): + +* Nim's FFI (foreign function interface) is not available in NimScript. This + means that any stdlib module which relies on ``importc`` can not be used in + the VM. + +* ``ptr`` operations are are hard to emulate with the symbolic representation + the VM uses. They are available and tested extensively but there are bugs left. + +* ``var T`` function arguments rely on ``ptr`` operations internally and might + also be problematic in some cases. + +* More than one level of `ref` is generally not supported (for example, the type + `ref ref int`). + +* multimethods are not available. + +Given the above restrictions, at least the following modules are available: * `macros <macros.html>`_ * `os <os.html>`_ @@ -98,17 +117,6 @@ Task Description ========= =================================================== -If the task runs an external command via ``exec`` it should afterwards call -``setCommand "nop"`` to tell the Nim compiler that nothing else needs to be -done: - -.. code-block:: nim - - task tests, "test regular expressions": - exec "nim c -r tests" - setCommand "nop" - - Look at the module `distros <distros.html>`_ for some support of the OS's native package managers. diff --git a/doc/packaging.rst b/doc/packaging.rst new file mode 100644 index 000000000..efbffeba5 --- /dev/null +++ b/doc/packaging.rst @@ -0,0 +1,69 @@ +============= +Packaging Nim +============= + + +Supported architectures +----------------------- + +Nim runs on a wide variety of platforms. Support on amd64 and i386 is tested regularly, while less popular platforms are tested by the community. + +- amd64 +- arm64 (aka aarch64) +- armel +- armhf +- i386 +- m68k +- mips64el +- mipsel +- powerpc +- ppc64 +- ppc64el (aka ppc64le) +- riscv64 + +The following platforms are seldomly tested: + +- alpha +- hppa +- ia64 +- mips +- s390x +- sparc64 + +Packaging for Linux +------------------- + +See https://github.com/nim-lang/Nim/labels/Installation for installation-related bugs. + +Build Nim from the released tarball at https://nim-lang.org/install_unix.html +It is different from the GitHub sources as it contains Nimble, C sources & other tools. + +The Debian package ships bash and ksh completion and manpages that can be reused. + +Hints on the build process: + +:: + + # build from C sources and then using koch + ./build.sh --os $os_type --cpu $cpu_arch + ./bin/nim c koch + ./koch boot -d:release + + # optionally generate docs into doc/html + ./koch docs + + ./koch tools -d:release + + # extract files to be really installed + ./install.sh <tempdir> + + # also include the tools + for fn in nimble nimsuggest nimgrep; do cp ./bin/$fn <tempdir>/nim/bin/; done + +What to install: + +- The expected stdlib location is /usr/lib/nim +- Global configuration files under /etc/nim +- Optionally: manpages, documentation, shell completion +- When installing documentation, .idx files are not required +- The "compiler" directory contains compiler sources and should not be part of the compiler binary package diff --git a/doc/regexprs.txt b/doc/regexprs.txt index 5c6d37e89..83dbd2eeb 100644 --- a/doc/regexprs.txt +++ b/doc/regexprs.txt @@ -80,13 +80,13 @@ meta character meaning ``|`` start of alternative branch ``(`` start subpattern ``)`` end subpattern -``?`` extends the meaning of ``(`` - also 0 or 1 quantifier - also quantifier minimizer -``*`` 0 or more quantifier -``+`` 1 or more quantifier - also "possessive quantifier" ``{`` start min/max quantifier +``?`` extends the meaning of ``(`` + | also 0 or 1 quantifier (equal to ``{0,1}``) + | also quantifier minimizer +``*`` 0 or more quantifier (equal to ``{0,}``) +``+`` 1 or more quantifier (equal to ``{1,}``) + | also "possessive quantifier" ============== ============================================================ diff --git a/doc/tut3.rst b/doc/tut3.rst index 5590db8fe..720b4fb37 100644 --- a/doc/tut3.rst +++ b/doc/tut3.rst @@ -19,16 +19,16 @@ a Nim syntax tree into a different tree. Examples of things that can be implemented in macros: - * An assert macro that prints both sides of a comparison operator, if -the assertion fails. ``myAssert(a == b)`` is converted to -``if a != b: quit($a " != " $b)`` +* An assert macro that prints both sides of a comparison operator, if + the assertion fails. ``myAssert(a == b)`` is converted to + ``if a != b: quit($a " != " $b)`` - * A debug macro that prints the value and the name of the symbol. -``myDebugEcho(a)`` is converted to ``echo "a: ", a`` +* A debug macro that prints the value and the name of the symbol. + ``myDebugEcho(a)`` is converted to ``echo "a: ", a`` - * Symbolic differentiation of an expression. -``diff(a*pow(x,3) + b*pow(x,2) + c*x + d, x)`` is converted to -``3*a*pow(x,2) + 2*a*x + c`` +* Symbolic differentiation of an expression. + ``diff(a*pow(x,3) + b*pow(x,2) + c*x + d, x)`` is converted to + ``3*a*pow(x,2) + 2*a*x + c`` Macro Arguments |