summary refs log tree commit diff stats
path: root/doc/astspec.txt
blob: 6b6242614ecfaee0197a728d56f73232bf81980c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
The AST in Nimrod
=================
This section describes how the AST is modelled with Nimrod's type system.
The AST consists of nodes (``PNimrodNode``) with a variable number of
children. Each node has a field named ``kind`` which describes what the node
contains:

.. code-block:: nimrod

  type
    TNimrodNodeKind = enum ## kind of a node; only explanatory
      nnkNone,             ## invalid node kind
      nnkEmpty,            ## empty node
      nnkIdent,            ## node contains an identifier
      nnkIntLit,           ## node contains an int literal (example: 10)
      nnkStrLit,           ## node contains a string literal (example: "abc")
      nnkNilLit,           ## node contains a nil literal (example: nil)
      nnkCaseStmt,         ## node represents a case statement
      ...                  ## many more

    PNimrodNode = ref TNimrodNode
    TNimrodNode {.final.} = object
      case kind: TNimrodNodeKind       ## the node's kind
      of nnkNone, nnkEmpty, nnkNilLit:
        nil                            ## node contains no additional fields
      of nnkCharLit..nnkInt64Lit:
        intVal: biggestInt             ## the int literal
      of nnkFloatLit..nnkFloat64Lit:
        floatVal: biggestFloat         ## the float literal
      of nnkStrLit..nnkTripleStrLit:
        strVal: string                 ## the string literal
      of nnkIdent:
        ident: TNimrodIdent            ## the identifier
      of nnkSym:
        symbol: PNimrodSymbol          ## the symbol (after symbol lookup phase)
      else:
        sons: seq[PNimrodNode]         ## the node's sons (or children)

For the ``PNimrodNode`` type, the ``[]`` operator has been overloaded:
``n[i]`` is ``n``'s ``i``-th child.

To specify the AST for the different Nimrod constructs, the notation
``nodekind(son1, son2, ...)`` or ``nodekind(value)`` or 
``nodekind(field=value)`` is used.

Some child may be missing. A missing child is a node of kind ``nnkEmpty``; 
a child can never be nil.


Leaf nodes/Atoms
================
A leaf of the AST often corresponds to a terminal symbol in the concrete 
syntax.

-----------------                ---------------------------------------------
Nimrod expression                corresponding AST
-----------------                ---------------------------------------------
``42``                           ``nnkIntLit(intVal = 42)``
``42'i8``                        ``nnkInt8Lit(intVal = 42)``
``42'i16``                       ``nnkInt16Lit(intVal = 42)``
``42'i32``                       ``nnkInt32Lit(intVal = 42)``
``42'i64``                       ``nnkInt64Lit(intVal = 42)``
``42.0``                         ``nnkFloatLit(floatVal = 42.0)``
``42.0'f32``                     ``nnkFloat32Lit(floatVal = 42.0)``
``42.0'f64``                     ``nnkFloat64Lit(floatVal = 42.0)``
``"abc"``                        ``nnkStrLit(strVal = "abc")``
``r"abc"``                       ``nnkRStrLit(strVal = "abc")``
``"""abc"""``                    ``nnkTripleStrLit(strVal = "abc")``
``' '``                          ``nnkCharLit(intVal = 32)``
``nil``                          ``nnkNilLit()``
``myIdentifier``                 ``nnkIdent(ident = !"myIdentifier")``
``myIdentifier``                 after lookup pass: ``nnkSym(symbol = ...)``
-----------------                ---------------------------------------------

Identifiers are ``nnkIdent`` nodes. After the name lookup pass these nodes
get transferred into ``nnkSym`` nodes. However, a macro receives an AST that
has not been checked for semantics and thus the identifiers have not been
looked up. Macros should deal with ``nnkIdent`` nodes and do not need to deal
with ``nnkSym`` nodes.


Calls/expressions
=================

Command call
------------

Concrete syntax:

.. code-block:: nimrod
  echo "abc", "xyz"

AST:

.. code-block:: nimrod
  nnkCommand(nnkIdent(!"echo"), nnkStrLit("abc"), nnkStrLit("xyz"))


Call with ``()``
----------------

Concrete syntax:

.. code-block:: nimrod
  echo("abc", "xyz")

AST:

.. code-block:: nimrod
  nnkCall(nnkIdent(!"echo"), nnkStrLit("abc"), nnkStrLit("xyz"))


Infix operator call
-------------------

Concrete syntax:

.. code-block:: nimrod
  "abc" & "xyz"

AST:

.. code-block:: nimrod
  nnkInfix(nnkIdent(!"&"), nnkStrLit("abc"), nnkStrLit("xyz"))


Prefix operator call
--------------------

Concrete syntax:

.. code-block:: nimrod
  ? "xyz"

AST:

.. code-block:: nimrod
  nnkPrefix(nnkIdent(!"?"), nnkStrLit("abc"))


Postfix operator call
---------------------

**Note:** There are no postfix operators in Nimrod. However, the 
``nnkPostfix`` node is used for the *asterisk export marker* ``*``:

Concrete syntax:

.. code-block:: nimrod
  identifier*

AST:

.. code-block:: nimrod
  nnkPostfix(nnkIdent(!"*"), nnkIdent(!"identifier"))


Call with named arguments
-------------------------

Concrete syntax:

.. code-block:: nimrod
  writeln(file=stdout, "hallo")

AST:

.. code-block:: nimrod
  nnkCall(nnkIdent(!"writeln"), 
          nnkExprEqExpr(nnkIdent(!"file"), nnkIdent(!"stdout")), 
          nnkStrLit("hallo"))


Dereference operator ``^``
--------------------------

Concrete syntax:

.. code-block:: nimrod
  x^

AST:

.. code-block:: nimrod
  nnkDerefExpr(nnkIdent(!"x"))


Addr operator
-------------

Concrete syntax:

.. code-block:: nimrod
  addr(x)

AST:

.. code-block:: nimrod
  nnkAddr(nnkIdent(!"x"))


Cast operator
-------------

Concrete syntax:

.. code-block:: nimrod
  cast[T](x)

AST:

.. code-block:: nimrod
  nnkCast(nnkIdent(!"T"), nnkIdent(!"x"))


Object access operator ``.``
----------------------------

Concrete syntax:

.. code-block:: nimrod
  x.y

AST:

.. code-block:: nimrod
  nnkDotExpr(nnkIdent(!"x"), nnkIdent(!"y"))


Array access operator ``[]``
----------------------------

Concrete syntax:

.. code-block:: nimrod
  x[y]

AST:

.. code-block:: nimrod
  nnkBracketExpr(nnkIdent(!"x"), nnkIdent(!"y"))


Parentheses
-----------

Parentheses for affecting operator precedence or tuple construction 
are built with the ``nnkPar`` node.

Concrete syntax:

.. code-block:: nimrod
  (1, 2, (3))

AST:

.. code-block:: nimrod
  nnkPar(nnkIntLit(1), nnkIntLit(2), nnkPar(nnkIntLit(3)))
  
  
Curly braces
------------

Curly braces are used as the set constructor. 

Concrete syntax:

.. code-block:: nimrod
  {1, 2, 3}

AST:

.. code-block:: nimrod
  nnkCurly(nnkIntLit(1), nnkIntLit(2), nnkIntLit(3))


Brackets
--------

Brackets are used as the array constructor.  

Concrete syntax:

.. code-block:: nimrod
  [1, 2, 3]

AST:

.. code-block:: nimrod
  nnkBracket(nnkIntLit(1), nnkIntLit(2), nnkIntLit(3))


Ranges
------

Ranges occur in set constructors, case statement branches or array slices.

Concrete syntax:

.. code-block:: nimrod
  1..3

AST:

.. code-block:: nimrod
  nnkRange(nnkIntLit(1), nnkIntLit(3))


If expression
-------------

The representation of the if expression is subtle, but easy to traverse.

Concrete syntax:

.. code-block:: nimrod
  if cond1: expr1 elif cond2: expr2 else: expr3

AST:

.. code-block:: nimrod
  nnkIfExpr(
    nnkElifExpr(cond1, expr1),
    nnkElifExpr(cond2, expr2),
    nnkElseExpr(expr3)
  )


Statements
==========

If statement
------------

The representation of the if statement is subtle, but easy to traverse. If
there is no ``else`` branch, no ``nnkElse`` child exists.

Concrete syntax:

.. code-block:: nimrod
  if cond1: 
    stmt1
  elif cond2:
    stmt2
  elif cond3:
    stmt3
  else:
    stmt4

AST:

.. code-block:: nimrod
  nnkIfStmt(
    nnkElifBranch(cond1, stmt1),
    nnkElifBranch(cond2, stmt2),
    nnkElifBranch(cond3, stmt3),
    nnkElse(stmt4)
  )


When statement
--------------

Like the ``if`` statement, but the root has the kind ``nnkWhenStmt``.


Assignment
----------

Concrete syntax:

.. code-block:: nimrod
  x = 42

AST:

.. code-block:: nimrod
  nnkAsgn(nnkIdent(!"x"), nnkIntLit(42))


Statement list
--------------

Concrete syntax:

.. code-block:: nimrod
  stmt1
  stmt2
  stmt3

AST:

.. code-block:: nimrod
  nnkStmtList(stmt1, stmt2, stmt3)

  
Case statement
--------------

Concrete syntax:

.. code-block:: nimrod
  case expr1
  of expr2, expr3..expr4: 
    stmt1
  of expr5:
    stmt2
  elif cond1:
    stmt3
  else:
    stmt4

AST:

.. code-block:: nimrod
  nnkCaseStmt(
    expr1,
    nnkOfBranch(expr2, nnkRange(expr3, expr4), stmt1),
    nnkOfBranch(expr5, stmt2),
    nnkElifBranch(cond1, stmt3),
    nnkElse(stmt4)
  )

The ``nnkElifBranch`` and ``nnkElse`` parts may be missing.


While statement
---------------

Concrete syntax:

.. code-block:: nimrod
  while expr1:
    stmt1

AST:

.. code-block:: nimrod
  nnkWhileStmt(expr1, stmt1)


For statement
-------------

Concrete syntax:

.. code-block:: nimrod
  for ident1, ident2 in expr1:
    stmt1

AST:

.. code-block:: nimrod
  nnkForStmt(ident1, ident2, expr1, stmt1)


Try statement
-------------

Concrete syntax:

.. code-block:: nimrod
  try:
    stmt1
  except e1, e2: 
    stmt2
  except e3:
    stmt3
  except: 
    stmt4
  finally:
    stmt5

AST:

.. code-block:: nimrod
  nnkTryStmt(
    stmt1, 
    nnkExceptBranch(e1, e2, stmt2), 
    nnkExceptBranch(e3, stmt3), 
    nnkExceptBranch(stmt4),
    nnkFinally(stmt5)
  )


Return statement
----------------

Concrete syntax:

.. code-block:: nimrod
  return expr1

AST:

.. code-block:: nimrod
  nnkReturnStmt(expr1)


Yield statement
---------------

Like ``return``, but with ``nnkYieldStmt`` kind.


Discard statement
-----------------

Like ``return``, but with ``nnkDiscardStmt`` kind.


Continue statement
------------------

Concrete syntax:

.. code-block:: nimrod
  continue

AST:

.. code-block:: nimrod
  nnkContinueStmt()

Var section
-----------

To be written.


Const section
-------------

To be written.


Type section
------------

To be written.


Procedure declaration
---------------------

To be written.


Iterator declaration
--------------------

To be written.


Template declaration
--------------------

To be written.


Macro declaration
-----------------

To be written.


Special node kinds
==================

There are several node kinds that are used for semantic checking or code 
generation. These are accessible from this module, but should not be used.
Other node kinds are especially designed to make AST manipulations easier.
These are explained here. 

To be written.