rst_examples.rst 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. ================
  2. Not a Nim Manual
  3. ================
  4. :Authors: Andreas Rumpf, Zahary Karadjov
  5. :Version: |nimversion|
  6. .. role:: nim(code)
  7. :language: nim
  8. .. default-role:: nim
  9. .. contents::
  10. "Complexity" seems to be a lot like "energy": you can transfer it from the
  11. end-user to one/some of the other players, but the total amount seems to remain
  12. pretty much constant for a given task. -- Ran
  13. About this document
  14. ===================
  15. **Note**: This document is a draft! Several of Nim's features may need more
  16. precise wording. This manual is constantly evolving into a proper specification.
  17. **Note**: The experimental features of Nim are
  18. covered `here <manual_experimental.html>`_.
  19. **Note**: Assignments, moves, and destruction are specified in
  20. the `destructors <destructors.html>`_ document.
  21. The language constructs are explained using an extended BNF, in which ``(a)*``
  22. means 0 or more ``a``'s, ``a+`` means 1 or more ``a``'s, and ``(a)?`` means an
  23. optional *a*. Parentheses may be used to group elements.
  24. ``&`` is the lookahead operator; ``&a`` means that an ``a`` is expected but
  25. not consumed. It will be consumed in the following rule.
  26. Non-terminals start with a lowercase letter, abstract terminal symbols are in
  27. UPPERCASE. Verbatim terminal symbols (including keywords) are quoted
  28. with ``'``. An example::
  29. ifStmt = 'if' expr ':' stmts ('elif' expr ':' stmts)* ('else' stmts)?
  30. In a typical Nim program, most of the code is compiled into the executable.
  31. However, some of the code may be executed at
  32. `compile-time`:idx:. This can include constant expressions, macro definitions,
  33. and Nim procedures used by macro definitions. Most of the Nim language is
  34. supported at compile-time, but there are some restrictions -- see `Restrictions
  35. on Compile-Time Execution <#restrictions-on-compileminustime-execution>`_ for
  36. details. We use the term `runtime`:idx: to cover both compile-time execution
  37. and code execution in the executable.
  38. .. code-block:: nim
  39. var a: array[0..1, char]
  40. let i = 5
  41. try:
  42. a[i] = 'N'
  43. except IndexDefect:
  44. echo "invalid index"
  45. Encoding
  46. --------
  47. All Nim source files are in the UTF-8 encoding (or its ASCII subset). Other
  48. encodings are not supported. Any of the standard platform line termination
  49. sequences can be used - the Unix form using ASCII LF (linefeed), the Windows
  50. form using the ASCII sequence CR LF (return followed by linefeed), or the old
  51. Macintosh form using the ASCII CR (return) character. All of these forms can be
  52. used equally, regardless of the platform.
  53. Indentation
  54. -----------
  55. Nim's standard grammar describes an `indentation sensitive`:idx: language.
  56. This means that all the control structures are recognized by indentation.
  57. Indentation consists only of spaces; tabulators are not allowed.
  58. With this notation we can now easily define the core of the grammar: A block of
  59. statements (simplified example)::
  60. ifStmt = 'if' expr ':' stmt
  61. (IND{=} 'elif' expr ':' stmt)*
  62. (IND{=} 'else' ':' stmt)?
  63. simpleStmt = ifStmt / ...
  64. stmt = IND{>} stmt ^+ IND{=} DED # list of statements
  65. / simpleStmt # or a simple statement
  66. String literals can be delimited by matching double quotes, and can
  67. contain the following `escape sequences`:idx:\ :
  68. ================== ===================================================
  69. Escape sequence Meaning
  70. ================== ===================================================
  71. ``\p`` platform specific newline: CRLF on Windows,
  72. LF on Unix
  73. ``\r``, ``\c`` `carriage return`:idx:
  74. ``\n``, ``\l`` `line feed`:idx: (often called `newline`:idx:)
  75. ``\f`` `form feed`:idx:
  76. ``\t`` `tabulator`:idx:
  77. ``\v`` `vertical tabulator`:idx:
  78. ``\\`` `backslash`:idx:
  79. ``\"`` `quotation mark`:idx:
  80. ``\'`` `apostrophe`:idx:
  81. ``\`` '0'..'9'+ `character with decimal value d`:idx:;
  82. all decimal digits directly
  83. following are used for the character
  84. ``\a`` `alert`:idx:
  85. ``\b`` `backspace`:idx:
  86. ``\e`` `escape`:idx: `[ESC]`:idx:
  87. ``\x`` HH `character with hex value HH`:idx:;
  88. exactly two hex digits are allowed
  89. ``\u`` HHHH `unicode codepoint with hex value HHHH`:idx:;
  90. exactly four hex digits are allowed
  91. ``\u`` {H+} `unicode codepoint`:idx:;
  92. all hex digits enclosed in ``{}`` are used for
  93. the codepoint
  94. ================== ===================================================
  95. .. code-block:: nim
  96. """"long string within quotes""""
  97. Produces::
  98. "long string within quotes"
  99. Operators
  100. ---------
  101. Nim allows user defined operators. An operator is any combination of the
  102. following characters::
  103. = + - * / < >
  104. @ $ ~ & % |
  105. ! ? ^ . : \
  106. (The grammar uses the terminal OPR to refer to operator symbols as
  107. defined here.)
  108. The following strings denote other tokens::
  109. ` ( ) { } [ ] , ; [. .] {. .} (. .) [:
  110. Otherwise, precedence is determined by the first character.
  111. ================ ======================================================= ================== ===============
  112. Precedence level Operators First character Terminal symbol
  113. ================ ======================================================= ================== ===============
  114. 10 (highest) ``$ ^`` OP10
  115. 9 ``* / div mod shl shr %`` ``* % \ /`` OP9
  116. 8 ``+ -`` ``+ - ~ |`` OP8
  117. 7 ``&`` ``&`` OP7
  118. 6 ``..`` ``.`` OP6
  119. 5 ``== <= < >= > != in notin is isnot not of as from`` ``= < > !`` OP5
  120. 4 ``and`` OP4
  121. 3 ``or xor`` OP3
  122. 2 ``@ : ?`` OP2
  123. 1 *assignment operator* (like ``+=``, ``*=``) OP1
  124. 0 (lowest) *arrow like operator* (like ``->``, ``=>``) OP0
  125. ================ ======================================================= ================== ===============
  126. Constants and Constant Expressions
  127. ==================================
  128. A `constant`:idx: is a symbol that is bound to the value of a constant
  129. expression. Constant expressions are restricted to depend only on the following
  130. categories of values and operations, because these are either built into the
  131. language or declared and evaluated before semantic analysis of the constant
  132. expression:
  133. * literals
  134. * built-in operators
  135. * previously declared constants and compile-time variables
  136. * previously declared macros and templates
  137. * previously declared procedures that have no side effects beyond
  138. possibly modifying compile-time variables
  139. These integer types are pre-defined:
  140. ``int``
  141. the generic signed integer type; its size is platform-dependent and has the
  142. same size as a pointer. This type should be used in general. An integer
  143. literal that has no type suffix is of this type if it is in the range
  144. ``low(int32)..high(int32)`` otherwise the literal's type is ``int64``.
  145. intXX
  146. additional signed integer types of XX bits use this naming scheme
  147. (example: int16 is a 16-bit wide integer).
  148. The current implementation supports ``int8``, ``int16``, ``int32``, ``int64``.
  149. Literals of these types have the suffix 'iXX.
  150. ``uint``
  151. the generic `unsigned integer`:idx: type; its size is platform-dependent and has the same size as a pointer. An integer literal with the type suffix ``'u`` is of this type.
  152. Let ``T``'s be ``p``'s return type. NRVO applies for ``T``
  153. if ``sizeof(T) >= N`` (where ``N`` is implementation dependent),
  154. in other words, it applies for "big" structures.
  155. Apart from built-in operations like array indexing, memory allocation, etc.
  156. the ``raise`` statement is the only way to raise an exception.
  157. .. XXX document this better!
  158. `typedesc` used as a parameter type also introduces an implicit
  159. generic. `typedesc` has its own set of rules:
  160. The ``!=``, ``>``, ``>=``, ``in``, ``notin``, ``isnot`` operators are in fact
  161. templates:
  162. | ``a > b`` is transformed into ``b < a``.
  163. | ``a in b`` is transformed into ``contains(b, a)``.
  164. | ``notin`` and ``isnot`` have the obvious meanings.
  165. A template where every parameter is ``untyped`` is called an `immediate`:idx:
  166. template. For historical reasons templates can be explicitly annotated with
  167. an ``immediate`` pragma and then these templates do not take part in
  168. overloading resolution and the parameters' types are *ignored* by the
  169. compiler. Explicit immediate templates are now deprecated.
  170. Symbol lookup in generics
  171. -------------------------
  172. Open and Closed symbols
  173. ~~~~~~~~~~~~~~~~~~~~~~~
  174. The symbol binding rules in generics are slightly subtle: There are "open" and
  175. "closed" symbols. A "closed" symbol cannot be re-bound in the instantiation
  176. context, an "open" symbol can. Per default overloaded symbols are open
  177. and every other symbol is closed.
  178. In templates identifiers can be constructed with the backticks notation:
  179. .. code-block:: nim
  180. :test: "nim c $1"
  181. template typedef(name: untyped, typ: typedesc) =
  182. type
  183. `T name`* {.inject.} = typ
  184. `P name`* {.inject.} = ref `T name`
  185. typedef(myint, int)
  186. var x: PMyInt
  187. In the example ``name`` is instantiated with ``myint``, so \`T name\` becomes
  188. ``Tmyint``.
  189. Only top-level symbols that are marked with an asterisk (``*``) are
  190. exported.
  191. The algorithm for compiling modules is:
  192. - compile the whole module as usual, following import statements recursively
  193. - if there is a cycle only import the already parsed symbols (that are
  194. exported); if an unknown identifier occurs then abort
  195. Collective imports from a directory
  196. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  197. The syntax ``import dir / [moduleA, moduleB]`` can be used to import multiple modules
  198. from the same directory.
  199. Pragmas
  200. =======
  201. Pragmas are Nim's method to give the compiler additional information /
  202. commands without introducing a massive number of new keywords. Pragmas are
  203. processed on the fly during semantic checking. Pragmas are enclosed in the
  204. special ``{.`` and ``.}`` curly brackets. Pragmas are also often used as a
  205. first implementation to play with a language feature before a nicer syntax
  206. to access the feature becomes available.
  207. deprecated pragma
  208. -----------------
  209. The deprecated pragma is used to mark a symbol as deprecated:
  210. **Note**: `c2nim <https://github.com/nim-lang/c2nim/blob/master/doc/c2nim.rst>`_ can parse a large subset of C++ and knows
  211. about the ``importcpp`` pragma pattern language. It is not necessary
  212. to know all the details described here.
  213. Pure libraries do not depend on any external ``*.dll`` or ``lib*.so`` binary
  214. while impure libraries do. A wrapper is an impure library that is a very
  215. low-level interface to a C library.
  216. Pure libraries
  217. ==============
  218. Automatic imports
  219. -----------------
  220. * `system <system.html>`_
  221. Basic procs and operators that every program needs. It also provides IO
  222. facilities for reading and writing text and binary files. It is imported
  223. implicitly by the compiler. Do not import it directly. It relies on compiler
  224. magic to work.
  225. * `threads <threads.html>`_
  226. Basic Nim thread support. **Note**: This is part of the system module. Do not
  227. import it explicitly. Enabled with ``--threads:on``.
  228. Code reordering
  229. ===============
  230. The code reordering feature can implicitly rearrange procedure, template, and
  231. macro definitions along with variable declarations and initializations at the top
  232. level scope so that, to a large extent, a programmer should not have to worry
  233. about ordering definitions correctly or be forced to use forward declarations to
  234. preface definitions inside a module.
  235. ..
  236. NOTE: The following was documentation for the code reordering precursor,
  237. which was {.noForward.}.
  238. In this mode, procedure definitions may appear out of order and the compiler
  239. will postpone their semantic analysis and compilation until it actually needs
  240. to generate code using the definitions. In this regard, this mode is similar
  241. to the modus operandi of dynamic scripting languages, where the function
  242. calls are not resolved until the code is executed. Here is the detailed
  243. algorithm taken by the compiler:
  244. 1. When a callable symbol is first encountered, the compiler will only note
  245. the symbol callable name and it will add it to the appropriate overload set
  246. in the current scope. At this step, it won't try to resolve any of the type
  247. expressions used in the signature of the symbol (so they can refer to other
  248. not yet defined symbols).
  249. 2. When a top level call is encountered (usually at the very end of the
  250. module), the compiler will try to determine the actual types of all of the
  251. symbols in the matching overload set. This is a potentially recursive process
  252. as the signatures of the symbols may include other call expressions, whose
  253. types will be resolved at this point too.
  254. 3. Finally, after the best overload is picked, the compiler will start
  255. compiling the body of the respective symbol. This in turn will lead the
  256. compiler to discover more call expressions that need to be resolved and steps
  257. 2 and 3 will be repeated as necessary.
  258. Please note that if a callable symbol is never used in this scenario, its
  259. body will never be compiled. This is the default behavior leading to best
  260. compilation times, but if exhaustive compilation of all definitions is
  261. required, using ``nim check`` provides this option as well.
  262. Example:
  263. .. code-block:: nim
  264. {.experimental: "codeReordering".}
  265. proc foo(x: int) =
  266. bar(x)
  267. proc bar(x: int) =
  268. echo(x)
  269. foo(10)
  270. ..
  271. TODO: Let's table this for now. This is an *experimental feature* and so the
  272. specific manner in which ``declared`` operates with it can be decided in
  273. eventuality, because right now it works a bit weirdly.
  274. The values of expressions involving ``declared`` are decided *before* the
  275. code reordering process, and not after. As an example, the output of this
  276. code is the same as it would be with code reordering disabled.
  277. .. code-block:: nim
  278. {.experimental: "codeReordering".}
  279. proc x() =
  280. echo(declared(foo))
  281. var foo = 4
  282. x() # "false"
  283. It is important to note that reordering *only* works for symbols at top level
  284. scope. Therefore, the following will *fail to compile:*
  285. Parameter constraints
  286. ---------------------
  287. The `parameter constraint`:idx: expression can use the operators ``|`` (or),
  288. ``&`` (and) and ``~`` (not) and the following predicates:
  289. The ``~`` operator
  290. ~~~~~~~~~~~~~~~~~~
  291. The ``~`` operator is the **not** operator in patterns:
  292. The ``**`` operator
  293. ~~~~~~~~~~~~~~~~~~~
  294. The ``**`` is much like the ``*`` operator, except that it gathers not only
  295. all the arguments, but also the matched operators in reverse polish notation:
  296. Nim significantly improves on the safety of these features via additional
  297. pragmas:
  298. 1) A `guard`:idx: annotation is introduced to prevent data races.
  299. 2) Every access of a guarded memory location needs to happen in an
  300. appropriate `locks`:idx: statement.
  301. 3) Locks and routines can be annotated with `lock levels`:idx: to allow
  302. potential deadlocks to be detected during semantic analysis.
  303. 1. Two output parameters should never be aliased.
  304. 2. An input and an output parameter should not be aliased.
  305. 3. An output parameter should never be aliased with a global or thread local
  306. variable referenced by the called proc.
  307. 4. An input parameter should not be aliased with a global or thread local
  308. variable updated by the called proc.
  309. One problem with rules 3 and 4 is that they affect specific global or thread
  310. local variables, but Nim's effect tracking only tracks "uses no global variable"
  311. via ``.noSideEffect``. The rules 3 and 4 can also be approximated by a different rule:
  312. 5. A global or thread local variable (or a location derived from such a location)
  313. can only passed to a parameter of a ``.noSideEffect`` proc.
  314. These two procs are the two modus operandi of the real-time garbage collector:
  315. (1) GC_SetMaxPause Mode
  316. You can call ``GC_SetMaxPause`` at program startup and then each triggered
  317. garbage collector run tries to not take longer than ``maxPause`` time. However, it is
  318. possible (and common) that the work is nevertheless not evenly distributed
  319. as each call to ``new`` can trigger the garbage collector and thus take ``maxPause``
  320. time.
  321. (2) GC_step Mode
  322. This allows the garbage collector to perform some work for up to ``us`` time.
  323. This is useful to call in the main loop to ensure the garbage collector can do its work.
  324. To bind all garbage collector activity to a ``GC_step`` call,
  325. deactivate the garbage collector with ``GC_disable`` at program startup.
  326. If ``strongAdvice`` is set to ``true``,
  327. then the garbage collector will be forced to perform the collection cycle.
  328. Otherwise, the garbage collector may decide not to do anything,
  329. if there is not much garbage to collect.
  330. You may also specify the current stack size via ``stackSize`` parameter.
  331. It can improve performance when you know that there are no unique Nim references
  332. below a certain point on the stack. Make sure the size you specify is greater
  333. than the potential worst-case size.
  334. It can improve performance when you know that there are no unique Nim
  335. references below a certain point on the stack. Make sure the size you specify
  336. is greater than the potential worst-case size.
  337. These procs provide a "best effort" real-time guarantee; in particular the
  338. cycle collector is not aware of deadlines. Deactivate it to get more
  339. predictable real-time behaviour. Tests show that a 1ms max pause
  340. time will be met in almost all cases on modern CPUs (with the cycle collector
  341. disabled).
  342. Time measurement with garbage collectors
  343. ----------------------------------------
  344. The garbage collectors' way of measuring time uses
  345. (see ``lib/system/timers.nim`` for the implementation):
  346. 1) ``QueryPerformanceCounter`` and ``QueryPerformanceFrequency`` on Windows.
  347. 2) ``mach_absolute_time`` on Mac OS X.
  348. 3) ``gettimeofday`` on Posix systems.
  349. As such it supports a resolution of nanoseconds internally; however, the API
  350. uses microseconds for convenience.
  351. Introduction
  352. ============
  353. .. raw:: html
  354. <blockquote><p>
  355. "Der Mensch ist doch ein Augentier -- sch&ouml;ne Dinge w&uuml;nsch ich mir."
  356. </p></blockquote>
  357. This document is a tutorial for the programming language *Nim*.
  358. This tutorial assumes that you are familiar with basic programming concepts
  359. like variables, types, or statements but is kept very basic. The `manual
  360. <manual.html>`_ contains many more examples of the advanced language features.
  361. All code examples in this tutorial, as well as the ones found in the rest of
  362. Nim's documentation, follow the `Nim style guide <nep1.html>`_.
  363. However, this does not work. The problem is that the procedure should not
  364. only ``return``, but return and **continue** after an iteration has
  365. finished. This *return and continue* is called a `yield` statement. Now
  366. the only thing left to do is to replace the ``proc`` keyword by ``iterator``
  367. and here it is - our first iterator:
  368. | A1 header | A2 \| not fooled
  369. | :--- | ----: |
  370. | C1 | C2 **bold** | ignored |
  371. | D1 `code \|` | D2 | also ignored
  372. | E1 \| text |
  373. | | F2 without pipe
  374. not in table