astspec.txt 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626
  1. The AST in Nim
  2. ==============
  3. This section describes how the AST is modelled with Nim's type system.
  4. The AST consists of nodes (``NimNode``) with a variable number of
  5. children. Each node has a field named ``kind`` which describes what the node
  6. contains:
  7. ```nim
  8. type
  9. NimNodeKind = enum ## kind of a node; only explanatory
  10. nnkNone, ## invalid node kind
  11. nnkEmpty, ## empty node
  12. nnkIdent, ## node contains an identifier
  13. nnkIntLit, ## node contains an int literal (example: 10)
  14. nnkStrLit, ## node contains a string literal (example: "abc")
  15. nnkNilLit, ## node contains a nil literal (example: nil)
  16. nnkCaseStmt, ## node represents a case statement
  17. ... ## many more
  18. NimNode = ref NimNodeObj
  19. NimNodeObj = object
  20. case kind: NimNodeKind ## the node's kind
  21. of nnkNone, nnkEmpty, nnkNilLit:
  22. discard ## node contains no additional fields
  23. of nnkCharLit..nnkUInt64Lit:
  24. intVal: BiggestInt ## the int literal
  25. of nnkFloatLit..nnkFloat64Lit:
  26. floatVal: BiggestFloat ## the float literal
  27. of nnkStrLit..nnkTripleStrLit, nnkCommentStmt, nnkIdent, nnkSym:
  28. strVal: string ## the string literal
  29. else:
  30. sons: seq[NimNode] ## the node's sons (or children)
  31. ```
  32. For the ``NimNode`` type, the ``[]`` operator has been overloaded:
  33. ``n[i]`` is ``n``'s ``i``-th child.
  34. To specify the AST for the different Nim constructs, the notation
  35. ``nodekind(son1, son2, ...)`` or ``nodekind(value)`` or
  36. ``nodekind(field=value)`` is used.
  37. Some child may be missing. A missing child is a node of kind ``nnkEmpty``;
  38. a child can never be nil.
  39. Leaf nodes/Atoms
  40. ================
  41. A leaf of the AST often corresponds to a terminal symbol in the concrete
  42. syntax. Note that the default ``float`` in Nim maps to ``float64`` such that
  43. the default AST for a float is ``nnkFloat64Lit`` as below.
  44. ================= =============================================
  45. Nim expression Corresponding AST
  46. ================= =============================================
  47. ``42`` ``nnkIntLit(intVal = 42)``
  48. ``42'i8`` ``nnkInt8Lit(intVal = 42)``
  49. ``42'i16`` ``nnkInt16Lit(intVal = 42)``
  50. ``42'i32`` ``nnkInt32Lit(intVal = 42)``
  51. ``42'i64`` ``nnkInt64Lit(intVal = 42)``
  52. ``42'u8`` ``nnkUInt8Lit(intVal = 42)``
  53. ``42'u16`` ``nnkUInt16Lit(intVal = 42)``
  54. ``42'u32`` ``nnkUInt32Lit(intVal = 42)``
  55. ``42'u64`` ``nnkUInt64Lit(intVal = 42)``
  56. ``42.0`` ``nnkFloat64Lit(floatVal = 42.0)``
  57. ``42.0'f32`` ``nnkFloat32Lit(floatVal = 42.0)``
  58. ``42.0'f64`` ``nnkFloat64Lit(floatVal = 42.0)``
  59. ``"abc"`` ``nnkStrLit(strVal = "abc")``
  60. ``r"abc"`` ``nnkRStrLit(strVal = "abc")``
  61. ``"""abc"""`` ``nnkTripleStrLit(strVal = "abc")``
  62. ``' '`` ``nnkCharLit(intVal = 32)``
  63. ``nil`` ``nnkNilLit()``
  64. ``myIdentifier`` ``nnkIdent(strVal = "myIdentifier")``
  65. ``myIdentifier`` after lookup pass: ``nnkSym(strVal = "myIdentifier", ...)``
  66. ================= =============================================
  67. Identifiers are ``nnkIdent`` nodes. After the name lookup pass these nodes
  68. get transferred into ``nnkSym`` nodes.
  69. Calls/expressions
  70. =================
  71. Command call
  72. ------------
  73. Concrete syntax:
  74. ```nim
  75. echo "abc", "xyz"
  76. ```
  77. AST:
  78. ```nim
  79. nnkCommand(
  80. nnkIdent("echo"),
  81. nnkStrLit("abc"),
  82. nnkStrLit("xyz")
  83. )
  84. ```
  85. Call with ``()``
  86. ----------------
  87. Concrete syntax:
  88. ```nim
  89. echo("abc", "xyz")
  90. ```
  91. AST:
  92. ```nim
  93. nnkCall(
  94. nnkIdent("echo"),
  95. nnkStrLit("abc"),
  96. nnkStrLit("xyz")
  97. )
  98. ```
  99. Infix operator call
  100. -------------------
  101. Concrete syntax:
  102. ```nim
  103. "abc" & "xyz"
  104. ```
  105. AST:
  106. ```nim
  107. nnkInfix(
  108. nnkIdent("&"),
  109. nnkStrLit("abc"),
  110. nnkStrLit("xyz")
  111. )
  112. ```
  113. Note that with multiple infix operators, the command is parsed by operator
  114. precedence.
  115. Concrete syntax:
  116. ```nim
  117. 5 + 3 * 4
  118. ```
  119. AST:
  120. ```nim
  121. nnkInfix(
  122. nnkIdent("+"),
  123. nnkIntLit(5),
  124. nnkInfix(
  125. nnkIdent("*"),
  126. nnkIntLit(3),
  127. nnkIntLit(4)
  128. )
  129. )
  130. ```
  131. As a side note, if you choose to use infix operators in a prefix form, the AST
  132. behaves as a
  133. [parenthetical function call](#callsslashexpressions-call-with) with
  134. ``nnkAccQuoted``, as follows:
  135. Concrete syntax:
  136. ```nim
  137. `+`(3, 4)
  138. ```
  139. AST:
  140. ```nim
  141. nnkCall(
  142. nnkAccQuoted(
  143. nnkIdent("+")
  144. ),
  145. nnkIntLit(3),
  146. nnkIntLit(4)
  147. )
  148. ```
  149. Prefix operator call
  150. --------------------
  151. Concrete syntax:
  152. ```nim
  153. ? "xyz"
  154. ```
  155. AST:
  156. ```nim
  157. nnkPrefix(
  158. nnkIdent("?"),
  159. nnkStrLit("abc")
  160. )
  161. ```
  162. Postfix operator call
  163. ---------------------
  164. **Note:** There are no postfix operators in Nim. However, the
  165. ``nnkPostfix`` node is used for the *asterisk export marker* ``*``:
  166. Concrete syntax:
  167. ```nim
  168. identifier*
  169. ```
  170. AST:
  171. ```nim
  172. nnkPostfix(
  173. nnkIdent("*"),
  174. nnkIdent("identifier")
  175. )
  176. ```
  177. Call with named arguments
  178. -------------------------
  179. Concrete syntax:
  180. ```nim
  181. writeLine(file=stdout, "hallo")
  182. ```
  183. AST:
  184. ```nim
  185. nnkCall(
  186. nnkIdent("writeLine"),
  187. nnkExprEqExpr(
  188. nnkIdent("file"),
  189. nnkIdent("stdout")
  190. ),
  191. nnkStrLit("hallo")
  192. )
  193. ```
  194. Call with raw string literal
  195. ----------------------------
  196. This is used, for example, in the ``bindSym`` examples
  197. [here](manual.html#macros-bindsym) and with
  198. ``re"some regexp"`` in the regular expression module.
  199. Concrete syntax:
  200. ```nim
  201. echo"abc"
  202. ```
  203. AST:
  204. ```nim
  205. nnkCallStrLit(
  206. nnkIdent("echo"),
  207. nnkRStrLit("hello")
  208. )
  209. ```
  210. Dereference operator ``[]``
  211. ---------------------------
  212. Concrete syntax:
  213. ```nim
  214. x[]
  215. ```
  216. AST:
  217. ```nim
  218. nnkDerefExpr(nnkIdent("x"))
  219. ```
  220. Addr operator
  221. -------------
  222. Concrete syntax:
  223. ```nim
  224. addr(x)
  225. ```
  226. AST:
  227. ```nim
  228. nnkAddr(nnkIdent("x"))
  229. ```
  230. Cast operator
  231. -------------
  232. Concrete syntax:
  233. ```nim
  234. cast[T](x)
  235. ```
  236. AST:
  237. ```nim
  238. nnkCast(nnkIdent("T"), nnkIdent("x"))
  239. ```
  240. Object access operator ``.``
  241. ----------------------------
  242. Concrete syntax:
  243. ```nim
  244. x.y
  245. ```
  246. AST:
  247. ```nim
  248. nnkDotExpr(nnkIdent("x"), nnkIdent("y"))
  249. ```
  250. If you use Nim's flexible calling syntax (as in ``x.len()``), the result is the
  251. same as above but wrapped in an ``nnkCall``.
  252. Array access operator ``[]``
  253. ----------------------------
  254. Concrete syntax:
  255. ```nim
  256. x[y]
  257. ```
  258. AST:
  259. ```nim
  260. nnkBracketExpr(nnkIdent("x"), nnkIdent("y"))
  261. ```
  262. Parentheses
  263. -----------
  264. Parentheses for affecting operator precedence use the ``nnkPar`` node.
  265. Concrete syntax:
  266. ```nim
  267. (a + b) * c
  268. ```
  269. AST:
  270. ```nim
  271. nnkInfix(nnkIdent("*"),
  272. nnkPar(
  273. nnkInfix(nnkIdent("+"), nnkIdent("a"), nnkIdent("b"))),
  274. nnkIdent("c"))
  275. ```
  276. Tuple Constructors
  277. ------------------
  278. Nodes for tuple construction are built with the ``nnkTupleConstr`` node.
  279. Concrete syntax:
  280. ```nim
  281. (1, 2, 3)
  282. (a: 1, b: 2, c: 3)
  283. ()
  284. ```
  285. AST:
  286. ```nim
  287. nnkTupleConstr(nnkIntLit(1), nnkIntLit(2), nnkIntLit(3))
  288. nnkTupleConstr(
  289. nnkExprColonExpr(nnkIdent("a"), nnkIntLit(1)),
  290. nnkExprColonExpr(nnkIdent("b"), nnkIntLit(2)),
  291. nnkExprColonExpr(nnkIdent("c"), nnkIntLit(3)))
  292. nnkTupleConstr()
  293. ```
  294. Since the one tuple would be syntactically identical to parentheses
  295. with an expression in them, the parser expects a trailing comma for
  296. them. For tuple constructors with field names, this is not necessary.
  297. ```nim
  298. (1,)
  299. (a: 1)
  300. ```
  301. AST:
  302. ```nim
  303. nnkTupleConstr(nnkIntLit(1))
  304. nnkTupleConstr(
  305. nnkExprColonExpr(nnkIdent("a"), nnkIntLit(1)))
  306. ```
  307. Curly braces
  308. ------------
  309. Curly braces are used as the set constructor.
  310. Concrete syntax:
  311. ```nim
  312. {1, 2, 3}
  313. ```
  314. AST:
  315. ```nim
  316. nnkCurly(nnkIntLit(1), nnkIntLit(2), nnkIntLit(3))
  317. ```
  318. When used as a table constructor, the syntax is different.
  319. Concrete syntax:
  320. ```nim
  321. {a: 3, b: 5}
  322. ```
  323. AST:
  324. ```nim
  325. nnkTableConstr(
  326. nnkExprColonExpr(nnkIdent("a"), nnkIntLit(3)),
  327. nnkExprColonExpr(nnkIdent("b"), nnkIntLit(5))
  328. )
  329. ```
  330. Brackets
  331. --------
  332. Brackets are used as the array constructor.
  333. Concrete syntax:
  334. ```nim
  335. [1, 2, 3]
  336. ```
  337. AST:
  338. ```nim
  339. nnkBracket(nnkIntLit(1), nnkIntLit(2), nnkIntLit(3))
  340. ```
  341. Ranges
  342. ------
  343. Ranges occur in set constructors, case statement branches, or array slices.
  344. Internally, the node kind ``nnkRange`` is used, but when constructing the
  345. AST, construction with ``..`` as an infix operator should be used instead.
  346. Concrete syntax:
  347. ```nim
  348. 1..3
  349. ```
  350. AST:
  351. ```nim
  352. nnkInfix(
  353. nnkIdent(".."),
  354. nnkIntLit(1),
  355. nnkIntLit(3)
  356. )
  357. ```
  358. Example code:
  359. ```nim
  360. macro genRepeatEcho() =
  361. result = newNimNode(nnkStmtList)
  362. var forStmt = newNimNode(nnkForStmt) # generate a for statement
  363. forStmt.add(ident("i")) # use the variable `i` for iteration
  364. var rangeDef = newNimNode(nnkInfix).add(
  365. ident("..")).add(
  366. newIntLitNode(3),newIntLitNode(5)) # iterate over the range 3..5
  367. forStmt.add(rangeDef)
  368. forStmt.add(newCall(ident("echo"), newIntLitNode(3))) # meat of the loop
  369. result.add(forStmt)
  370. genRepeatEcho() # gives:
  371. # 3
  372. # 3
  373. # 3
  374. ```
  375. If expression
  376. -------------
  377. The representation of the ``if`` expression is subtle, but easy to traverse.
  378. Concrete syntax:
  379. ```nim
  380. if cond1: expr1 elif cond2: expr2 else: expr3
  381. ```
  382. AST:
  383. ```nim
  384. nnkIfExpr(
  385. nnkElifExpr(cond1, expr1),
  386. nnkElifExpr(cond2, expr2),
  387. nnkElseExpr(expr3)
  388. )
  389. ```
  390. Documentation Comments
  391. ----------------------
  392. Double-hash (``##``) comments in the code actually have their own format,
  393. using ``strVal`` to get and set the comment text. Single-hash (``#``)
  394. comments are ignored.
  395. Concrete syntax:
  396. ```nim
  397. ## This is a comment
  398. ## This is part of the first comment
  399. stmt1
  400. ## Yet another
  401. ```
  402. AST:
  403. ```nim
  404. nnkCommentStmt() # only appears once for the first two lines!
  405. stmt1
  406. nnkCommentStmt() # another nnkCommentStmt because there is another comment
  407. # (separate from the first)
  408. ```
  409. Pragmas
  410. -------
  411. One of Nim's cool features is pragmas, which allow fine-tuning of various
  412. aspects of the language. They come in all types, such as adorning procs and
  413. objects, but the standalone ``emit`` pragma shows the basics with the AST.
  414. Concrete syntax:
  415. ```nim
  416. {.emit: "#include <stdio.h>".}
  417. ```
  418. AST:
  419. ```nim
  420. nnkPragma(
  421. nnkExprColonExpr(
  422. nnkIdent("emit"),
  423. nnkStrLit("#include <stdio.h>") # the "argument"
  424. )
  425. )
  426. ```
  427. As many ``nnkIdent`` appear as there are pragmas between ``{..}``. Note that
  428. the declaration of new pragmas is essentially the same:
  429. Concrete syntax:
  430. ```nim
  431. {.pragma: cdeclRename, cdecl.}
  432. ```
  433. AST:
  434. ```nim
  435. nnkPragma(
  436. nnkExprColonExpr(
  437. nnkIdent("pragma"), # this is always first when declaring a new pragma
  438. nnkIdent("cdeclRename") # the name of the pragma
  439. ),
  440. nnkIdent("cdecl")
  441. )
  442. ```
  443. Statements
  444. ==========
  445. If statement
  446. ------------
  447. The representation of the if statement is subtle, but easy to traverse. If
  448. there is no ``else`` branch, no ``nnkElse`` child exists.
  449. Concrete syntax:
  450. ```nim
  451. if cond1:
  452. stmt1
  453. elif cond2:
  454. stmt2
  455. elif cond3:
  456. stmt3
  457. else:
  458. stmt4
  459. ```
  460. AST:
  461. ```nim
  462. nnkIfStmt(
  463. nnkElifBranch(cond1, stmt1),
  464. nnkElifBranch(cond2, stmt2),
  465. nnkElifBranch(cond3, stmt3),
  466. nnkElse(stmt4)
  467. )
  468. ```
  469. When statement
  470. --------------
  471. Like the ``if`` statement, but the root has the kind ``nnkWhenStmt``.
  472. Assignment
  473. ----------
  474. Concrete syntax:
  475. ```nim
  476. x = 42
  477. ```
  478. AST:
  479. ```nim
  480. nnkAsgn(nnkIdent("x"), nnkIntLit(42))
  481. ```
  482. This is not the syntax for assignment when combined with ``var``, ``let``,
  483. or ``const``.
  484. Statement list
  485. --------------
  486. Concrete syntax:
  487. ```nim
  488. stmt1
  489. stmt2
  490. stmt3
  491. ```
  492. AST:
  493. ```nim
  494. nnkStmtList(stmt1, stmt2, stmt3)
  495. ```
  496. Case statement
  497. --------------
  498. Concrete syntax:
  499. ```nim
  500. case expr1
  501. of expr2, expr3..expr4:
  502. stmt1
  503. of expr5:
  504. stmt2
  505. elif cond1:
  506. stmt3
  507. else:
  508. stmt4
  509. ```
  510. AST:
  511. ```nim
  512. nnkCaseStmt(
  513. expr1,
  514. nnkOfBranch(expr2, nnkRange(expr3, expr4), stmt1),
  515. nnkOfBranch(expr5, stmt2),
  516. nnkElifBranch(cond1, stmt3),
  517. nnkElse(stmt4)
  518. )
  519. ```
  520. The ``nnkElifBranch`` and ``nnkElse`` parts may be missing.
  521. While statement
  522. ---------------
  523. Concrete syntax:
  524. ```nim
  525. while expr1:
  526. stmt1
  527. ```
  528. AST:
  529. ```nim
  530. nnkWhileStmt(expr1, stmt1)
  531. ```
  532. For statement
  533. -------------
  534. Concrete syntax:
  535. ```nim
  536. for ident1, ident2 in expr1:
  537. stmt1
  538. ```
  539. AST:
  540. ```nim
  541. nnkForStmt(ident1, ident2, expr1, stmt1)
  542. ```
  543. Try statement
  544. -------------
  545. Concrete syntax:
  546. ```nim
  547. try:
  548. stmt1
  549. except e1, e2:
  550. stmt2
  551. except e3:
  552. stmt3
  553. except:
  554. stmt4
  555. finally:
  556. stmt5
  557. ```
  558. AST:
  559. ```nim
  560. nnkTryStmt(
  561. stmt1,
  562. nnkExceptBranch(e1, e2, stmt2),
  563. nnkExceptBranch(e3, stmt3),
  564. nnkExceptBranch(stmt4),
  565. nnkFinally(stmt5)
  566. )
  567. ```
  568. Return statement
  569. ----------------
  570. Concrete syntax:
  571. ```nim
  572. return expr1
  573. ```
  574. AST:
  575. ```nim
  576. nnkReturnStmt(expr1)
  577. ```
  578. Yield statement
  579. ---------------
  580. Like ``return``, but with ``nnkYieldStmt`` kind.
  581. ```nim
  582. nnkYieldStmt(expr1)
  583. ```
  584. Discard statement
  585. -----------------
  586. Like ``return``, but with ``nnkDiscardStmt`` kind.
  587. ```nim
  588. nnkDiscardStmt(expr1)
  589. ```
  590. Continue statement
  591. ------------------
  592. Concrete syntax:
  593. ```nim
  594. continue
  595. ```
  596. AST:
  597. ```nim
  598. nnkContinueStmt()
  599. ```
  600. Break statement
  601. ---------------
  602. Concrete syntax:
  603. ```nim
  604. break otherLocation
  605. ```
  606. AST:
  607. ```nim
  608. nnkBreakStmt(nnkIdent("otherLocation"))
  609. ```
  610. If ``break`` is used without a jump-to location, ``nnkEmpty`` replaces ``nnkIdent``.
  611. Block statement
  612. ---------------
  613. Concrete syntax:
  614. ```nim
  615. block name:
  616. ```
  617. AST:
  618. ```nim
  619. nnkBlockStmt(nnkIdent("name"), nnkStmtList(...))
  620. ```
  621. A ``block`` doesn't need an name, in which case ``nnkEmpty`` is used.
  622. Asm statement
  623. -------------
  624. Concrete syntax:
  625. ```nim
  626. asm """
  627. some asm
  628. """
  629. ```
  630. AST:
  631. ```nim
  632. nnkAsmStmt(
  633. nnkEmpty(), # for pragmas
  634. nnkTripleStrLit("some asm"),
  635. )
  636. ```
  637. Import section
  638. --------------
  639. Nim's ``import`` statement actually takes different variations depending
  640. on what keywords are present. Let's start with the simplest form.
  641. Concrete syntax:
  642. ```nim
  643. import math
  644. ```
  645. AST:
  646. ```nim
  647. nnkImportStmt(nnkIdent("math"))
  648. ```
  649. With ``except``, we get ``nnkImportExceptStmt``.
  650. Concrete syntax:
  651. ```nim
  652. import math except pow
  653. ```
  654. AST:
  655. ```nim
  656. nnkImportExceptStmt(nnkIdent("math"),nnkIdent("pow"))
  657. ```
  658. Note that ``import math as m`` does not use a different node; rather,
  659. we use ``nnkImportStmt`` with ``as`` as an infix operator.
  660. Concrete syntax:
  661. ```nim
  662. import strutils as su
  663. ```
  664. AST:
  665. ```nim
  666. nnkImportStmt(
  667. nnkInfix(
  668. nnkIdent("as"),
  669. nnkIdent("strutils"),
  670. nnkIdent("su")
  671. )
  672. )
  673. ```
  674. From statement
  675. --------------
  676. If we use ``from ... import``, the result is different, too.
  677. Concrete syntax:
  678. ```nim
  679. from math import pow
  680. ```
  681. AST:
  682. ```nim
  683. nnkFromStmt(nnkIdent("math"), nnkIdent("pow"))
  684. ```
  685. Using ``from math as m import pow`` works identically to the ``as`` modifier
  686. with the ``import`` statement, but wrapped in ``nnkFromStmt``.
  687. Export statement
  688. ----------------
  689. When you are making an imported module accessible by modules that import yours,
  690. the ``export`` syntax is pretty straightforward.
  691. Concrete syntax:
  692. ```nim
  693. export unsigned
  694. ```
  695. AST:
  696. ```nim
  697. nnkExportStmt(nnkIdent("unsigned"))
  698. ```
  699. Similar to the ``import`` statement, the AST is different for
  700. ``export ... except``.
  701. Concrete syntax:
  702. ```nim
  703. export math except pow # we're going to implement our own exponentiation
  704. ```
  705. AST:
  706. ```nim
  707. nnkExportExceptStmt(nnkIdent("math"),nnkIdent("pow"))
  708. ```
  709. Include statement
  710. -----------------
  711. Like a plain ``import`` statement but with ``nnkIncludeStmt``.
  712. Concrete syntax:
  713. ```nim
  714. include blocks
  715. ```
  716. AST:
  717. ```nim
  718. nnkIncludeStmt(nnkIdent("blocks"))
  719. ```
  720. Var section
  721. -----------
  722. Concrete syntax:
  723. ```nim
  724. var a = 3
  725. ```
  726. AST:
  727. ```nim
  728. nnkVarSection(
  729. nnkIdentDefs(
  730. nnkIdent("a"),
  731. nnkEmpty(), # or nnkIdent(...) if the variable declares the type
  732. nnkIntLit(3),
  733. )
  734. )
  735. ```
  736. Note that either the second or third (or both) parameters above must exist,
  737. as the compiler needs to know the type somehow (which it can infer from
  738. the given assignment).
  739. This is not the same AST for all uses of ``var``. See
  740. [Procedure declaration](macros.html#statements-procedure-declaration)
  741. for details.
  742. Let section
  743. -----------
  744. This is equivalent to ``var``, but with ``nnkLetSection`` rather than
  745. ``nnkVarSection``.
  746. Concrete syntax:
  747. ```nim
  748. let a = 3
  749. ```
  750. AST:
  751. ```nim
  752. nnkLetSection(
  753. nnkIdentDefs(
  754. nnkIdent("a"),
  755. nnkEmpty(), # or nnkIdent(...) for the type
  756. nnkIntLit(3),
  757. )
  758. )
  759. ```
  760. Const section
  761. -------------
  762. Concrete syntax:
  763. ```nim
  764. const a = 3
  765. ```
  766. AST:
  767. ```nim
  768. nnkConstSection(
  769. nnkConstDef( # not nnkConstDefs!
  770. nnkIdent("a"),
  771. nnkEmpty(), # or nnkIdent(...) if the variable declares the type
  772. nnkIntLit(3), # required in a const declaration!
  773. )
  774. )
  775. ```
  776. Type section
  777. ------------
  778. Starting with the simplest case, a ``type`` section appears much like ``var``
  779. and ``const``.
  780. Concrete syntax:
  781. ```nim
  782. type A = int
  783. ```
  784. AST:
  785. ```nim
  786. nnkTypeSection(
  787. nnkTypeDef(
  788. nnkIdent("A"),
  789. nnkEmpty(),
  790. nnkIdent("int")
  791. )
  792. )
  793. ```
  794. Declaring ``distinct`` types is similar, with the last ``nnkIdent`` wrapped
  795. in ``nnkDistinctTy``.
  796. Concrete syntax:
  797. ```nim
  798. type MyInt = distinct int
  799. ```
  800. AST:
  801. ```nim
  802. # ...
  803. nnkTypeDef(
  804. nnkIdent("MyInt"),
  805. nnkEmpty(),
  806. nnkDistinctTy(
  807. nnkIdent("int")
  808. )
  809. )
  810. ```
  811. If a type section uses generic parameters, they are treated here:
  812. Concrete syntax:
  813. ```nim
  814. type A[T] = expr1
  815. ```
  816. AST:
  817. ```nim
  818. nnkTypeSection(
  819. nnkTypeDef(
  820. nnkIdent("A"),
  821. nnkGenericParams(
  822. nnkIdentDefs(
  823. nnkIdent("T"),
  824. nnkEmpty(), # if the type is declared with options, like
  825. # ``[T: SomeInteger]``, they are given here
  826. nnkEmpty(),
  827. )
  828. )
  829. expr1,
  830. )
  831. )
  832. ```
  833. Note that not all ``nnkTypeDef`` utilize ``nnkIdent`` as their
  834. parameter. One of the most common uses of type declarations
  835. is to work with objects.
  836. Concrete syntax:
  837. ```nim
  838. type IO = object of RootObj
  839. ```
  840. AST:
  841. ```nim
  842. # ...
  843. nnkTypeDef(
  844. nnkIdent("IO"),
  845. nnkEmpty(),
  846. nnkObjectTy(
  847. nnkEmpty(), # no pragmas here
  848. nnkOfInherit(
  849. nnkIdent("RootObj") # inherits from RootObj
  850. ),
  851. nnkEmpty()
  852. )
  853. )
  854. ```
  855. Nim's object syntax is rich. Let's take a look at an involved example in
  856. its entirety to see some of the complexities.
  857. Concrete syntax:
  858. ```nim
  859. type Obj[T] {.inheritable.} = object
  860. name: string
  861. case isFat: bool
  862. of true:
  863. m: array[100_000, T]
  864. of false:
  865. m: array[10, T]
  866. ```
  867. AST:
  868. ```nim
  869. # ...
  870. nnkPragmaExpr(
  871. nnkIdent("Obj"),
  872. nnkPragma(nnkIdent("inheritable"))
  873. ),
  874. nnkGenericParams(
  875. nnkIdentDefs(
  876. nnkIdent("T"),
  877. nnkEmpty(),
  878. nnkEmpty())
  879. ),
  880. nnkObjectTy(
  881. nnkEmpty(),
  882. nnkEmpty(),
  883. nnkRecList( # list of object parameters
  884. nnkIdentDefs(
  885. nnkIdent("name"),
  886. nnkIdent("string"),
  887. nnkEmpty()
  888. ),
  889. nnkRecCase( # case statement within object (not nnkCaseStmt)
  890. nnkIdentDefs(
  891. nnkIdent("isFat"),
  892. nnkIdent("bool"),
  893. nnkEmpty()
  894. ),
  895. nnkOfBranch(
  896. nnkIdent("true"),
  897. nnkRecList( # again, a list of object parameters
  898. nnkIdentDefs(
  899. nnkIdent("m"),
  900. nnkBracketExpr(
  901. nnkIdent("array"),
  902. nnkIntLit(100000),
  903. nnkIdent("T")
  904. ),
  905. nnkEmpty()
  906. )
  907. ),
  908. nnkOfBranch(
  909. nnkIdent("false"),
  910. nnkRecList(
  911. nnkIdentDefs(
  912. nnkIdent("m"),
  913. nnkBracketExpr(
  914. nnkIdent("array"),
  915. nnkIntLit(10),
  916. nnkIdent("T")
  917. ),
  918. nnkEmpty()
  919. )
  920. )
  921. )
  922. )
  923. )
  924. )
  925. ```
  926. Using an ``enum`` is similar to using an ``object``.
  927. Concrete syntax:
  928. ```nim
  929. type X = enum
  930. First
  931. ```
  932. AST:
  933. ```nim
  934. # ...
  935. nnkEnumTy(
  936. nnkEmpty(),
  937. nnkIdent("First") # you need at least one nnkIdent or the compiler complains
  938. )
  939. ```
  940. The usage of ``concept`` (experimental) is similar to objects.
  941. Concrete syntax:
  942. ```nim
  943. type Con = concept x,y,z
  944. (x & y & z) is string
  945. ```
  946. AST:
  947. ```nim
  948. # ...
  949. nnkTypeClassTy( # note this isn't nnkConceptTy!
  950. nnkArgList(
  951. # ... idents for x, y, z
  952. )
  953. # ...
  954. )
  955. ```
  956. Static types, like ``static[int]``, use ``nnkIdent`` wrapped in
  957. ``nnkStaticTy``.
  958. Concrete syntax:
  959. ```nim
  960. type A[T: static[int]] = object
  961. ```
  962. AST:
  963. ```nim
  964. # ... within nnkGenericParams
  965. nnkIdentDefs(
  966. nnkIdent("T"),
  967. nnkStaticTy(
  968. nnkIdent("int")
  969. ),
  970. nnkEmpty()
  971. )
  972. # ...
  973. ```
  974. In general, declaring types mirrors this syntax (i.e., ``nnkStaticTy`` for
  975. ``static``, etc.). Examples follow (exceptions marked by ``*``):
  976. ============= =============================================
  977. Nim type Corresponding AST
  978. ============= =============================================
  979. ``static`` ``nnkStaticTy``
  980. ``tuple`` ``nnkTupleTy``
  981. ``var`` ``nnkVarTy``
  982. ``ptr`` ``nnkPtrTy``
  983. ``ref`` ``nnkRefTy``
  984. ``distinct`` ``nnkDistinctTy``
  985. ``enum`` ``nnkEnumTy``
  986. ``concept`` ``nnkTypeClassTy``\*
  987. ``array`` ``nnkBracketExpr(nnkIdent("array"),...``\*
  988. ``proc`` ``nnkProcTy``
  989. ``iterator`` ``nnkIteratorTy``
  990. ``object`` ``nnkObjectTy``
  991. ============= =============================================
  992. Take special care when declaring types as ``proc``. The behavior is similar
  993. to ``Procedure declaration``, below, but does not treat ``nnkGenericParams``.
  994. Generic parameters are treated in the type, not the ``proc`` itself.
  995. Concrete syntax:
  996. ```nim
  997. type MyProc[T] = proc(x: T) {.nimcall.}
  998. ```
  999. AST:
  1000. ```nim
  1001. # ...
  1002. nnkTypeDef(
  1003. nnkIdent("MyProc"),
  1004. nnkGenericParams( # here, not with the proc
  1005. # ...
  1006. )
  1007. nnkProcTy( # behaves like a procedure declaration from here on
  1008. nnkFormalParams(
  1009. # ...
  1010. ),
  1011. nnkPragma(nnkIdent("nimcall"))
  1012. )
  1013. )
  1014. ```
  1015. The same syntax applies to ``iterator`` (with ``nnkIteratorTy``), but
  1016. *does not* apply to ``converter`` or ``template``.
  1017. Type class versions of these nodes generally share the same node kind but
  1018. without any child nodes. The ``tuple`` type class is represented by
  1019. ``nnkTupleClassTy``, while a ``proc`` or ``iterator`` type class with pragmas
  1020. has an ``nnkEmpty`` node in place of the ``nnkFormalParams`` node of a
  1021. concrete ``proc`` or ``iterator`` type node.
  1022. ```nim
  1023. type TypeClass = proc {.nimcall.} | ref | tuple
  1024. ```
  1025. AST:
  1026. ```nim
  1027. nnkTypeDef(
  1028. nnkIdent("TypeClass"),
  1029. nnkEmpty(),
  1030. nnkInfix(
  1031. nnkIdent("|"),
  1032. nnkProcTy(
  1033. nnkEmpty(),
  1034. nnkPragma(nnkIdent("nimcall"))
  1035. ),
  1036. nnkInfix(
  1037. nnkIdent("|"),
  1038. nnkRefTy(),
  1039. nnkTupleClassTy()
  1040. )
  1041. )
  1042. )
  1043. ```
  1044. Mixin statement
  1045. ---------------
  1046. Concrete syntax:
  1047. ```nim
  1048. mixin x
  1049. ```
  1050. AST:
  1051. ```nim
  1052. nnkMixinStmt(nnkIdent("x"))
  1053. ```
  1054. Bind statement
  1055. --------------
  1056. Concrete syntax:
  1057. ```nim
  1058. bind x
  1059. ```
  1060. AST:
  1061. ```nim
  1062. nnkBindStmt(nnkIdent("x"))
  1063. ```
  1064. Procedure declaration
  1065. ---------------------
  1066. Let's take a look at a procedure with a lot of interesting aspects to get
  1067. a feel for how procedure calls are broken down.
  1068. Concrete syntax:
  1069. ```nim
  1070. proc hello*[T: SomeInteger](x: int = 3, y: float32): int {.inline.} = discard
  1071. ```
  1072. AST:
  1073. ```nim
  1074. nnkProcDef(
  1075. nnkPostfix(nnkIdent("*"), nnkIdent("hello")), # the exported proc name
  1076. nnkEmpty(), # patterns for term rewriting in templates and macros (not procs)
  1077. nnkGenericParams( # generic type parameters, like with type declaration
  1078. nnkIdentDefs(
  1079. nnkIdent("T"),
  1080. nnkIdent("SomeInteger"),
  1081. nnkEmpty()
  1082. )
  1083. ),
  1084. nnkFormalParams(
  1085. nnkIdent("int"), # the first FormalParam is the return type. nnkEmpty() if there is none
  1086. nnkIdentDefs(
  1087. nnkIdent("x"),
  1088. nnkIdent("int"), # type type (required for procs, not for templates)
  1089. nnkIntLit(3) # a default value
  1090. ),
  1091. nnkIdentDefs(
  1092. nnkIdent("y"),
  1093. nnkIdent("float32"),
  1094. nnkEmpty()
  1095. )
  1096. ),
  1097. nnkPragma(nnkIdent("inline")),
  1098. nnkEmpty(), # reserved slot for future use
  1099. nnkStmtList(nnkDiscardStmt(nnkEmpty())) # the meat of the proc
  1100. )
  1101. ```
  1102. There is another consideration. Nim has flexible type identification for
  1103. its procs. Even though ``proc(a: int, b: int)`` and ``proc(a, b: int)``
  1104. are equivalent in the code, the AST is a little different for the latter.
  1105. Concrete syntax:
  1106. ```nim
  1107. proc(a, b: int)
  1108. ```
  1109. AST:
  1110. ```nim
  1111. # ...AST as above...
  1112. nnkFormalParams(
  1113. nnkEmpty(), # no return here
  1114. nnkIdentDefs(
  1115. nnkIdent("a"), # the first parameter
  1116. nnkIdent("b"), # directly to the second parameter
  1117. nnkIdent("int"), # their shared type identifier
  1118. nnkEmpty(), # default value would go here
  1119. )
  1120. ),
  1121. # ...
  1122. ```
  1123. When a procedure uses the special ``var`` type return variable, the result
  1124. is different from that of a var section.
  1125. Concrete syntax:
  1126. ```nim
  1127. proc hello(): var int
  1128. ```
  1129. AST:
  1130. ```nim
  1131. # ...
  1132. nnkFormalParams(
  1133. nnkVarTy(
  1134. nnkIdent("int")
  1135. )
  1136. )
  1137. ```
  1138. Iterator declaration
  1139. --------------------
  1140. The syntax for iterators is similar to procs, but with ``nnkIteratorDef``
  1141. replacing ``nnkProcDef``.
  1142. Concrete syntax:
  1143. ```nim
  1144. iterator nonsense[T](x: seq[T]): float {.closure.} = ...
  1145. ```
  1146. AST:
  1147. ```nim
  1148. nnkIteratorDef(
  1149. nnkIdent("nonsense"),
  1150. nnkEmpty(),
  1151. ...
  1152. )
  1153. ```
  1154. Converter declaration
  1155. ---------------------
  1156. A converter is similar to a proc.
  1157. Concrete syntax:
  1158. ```nim
  1159. converter toBool(x: float): bool
  1160. ```
  1161. AST:
  1162. ```nim
  1163. nnkConverterDef(
  1164. nnkIdent("toBool"),
  1165. # ...
  1166. )
  1167. ```
  1168. Template declaration
  1169. --------------------
  1170. Templates (as well as macros, as we'll see) have a slightly expanded AST when
  1171. compared to procs and iterators. The reason for this is [term-rewriting
  1172. macros](manual.html#term-rewriting-macros). Notice
  1173. the ``nnkEmpty()`` as the second argument to ``nnkProcDef`` and
  1174. ``nnkIteratorDef`` above? That's where the term-rewriting macros go.
  1175. Concrete syntax:
  1176. ```nim
  1177. template optOpt{expr1}(a: int): int
  1178. ```
  1179. AST:
  1180. ```nim
  1181. nnkTemplateDef(
  1182. nnkIdent("optOpt"),
  1183. nnkStmtList( # instead of nnkEmpty()
  1184. expr1
  1185. ),
  1186. # follows like a proc or iterator
  1187. )
  1188. ```
  1189. If the template does not have types for its parameters, the type identifiers
  1190. inside ``nnkFormalParams`` just becomes ``nnkEmpty``.
  1191. Macro declaration
  1192. -----------------
  1193. Macros behave like templates, but ``nnkTemplateDef`` is replaced with
  1194. ``nnkMacroDef``.
  1195. Hidden Standard Conversion
  1196. --------------------------
  1197. ```nim
  1198. var f: float = 1
  1199. ```
  1200. The type of "f" is ``float`` but the type of "1" is actually ``int``. Inserting
  1201. ``int`` into a ``float`` is a type error. Nim inserts the ``nnkHiddenStdConv``
  1202. node around the ``nnkIntLit`` node so that the new node has the correct type of
  1203. ``float``. This works for any auto converted nodes and makes the conversion
  1204. explicit.
  1205. Special node kinds
  1206. ==================
  1207. There are several node kinds that are used for semantic checking or code
  1208. generation. These are accessible from this module, but should not be used.
  1209. Other node kinds are especially designed to make AST manipulations easier.
  1210. These are explained here.
  1211. To be written.