summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorDominik Picheta <dominikpicheta@googlemail.com>2015-06-18 12:10:31 +0100
committerDominik Picheta <dominikpicheta@googlemail.com>2015-06-18 12:10:31 +0100
commit6f1cc914c5a81f3f75556f67af497a373c143fe9 (patch)
tree81ff4b422ef79ed50a05d83ea6e4fae281ee13c9
parent1949eb0f920259cbcaecc999f0134db491498d74 (diff)
parent49465c470c348ef1e8fc8fd546666b1de66c1e7c (diff)
downloadNim-6f1cc914c5a81f3f75556f67af497a373c143fe9.tar.gz
Merge pull request #2948 from apense/patch-4
More documentation of AST
-rw-r--r--doc/astspec.txt96
1 files changed, 95 insertions, 1 deletions
diff --git a/doc/astspec.txt b/doc/astspec.txt
index 596d07bab..ebc39322f 100644
--- a/doc/astspec.txt
+++ b/doc/astspec.txt
@@ -824,10 +824,23 @@ AST:
 .. code-block:: nim
   nnkExportStmt(nnkIdent(!"unsigned"))
 
+Similar to the ``import`` statement, the AST is different for 
+``export ... except``.
+
+Concrete syntax:
+
+.. code-block:: nim
+  export math except pow # we're going to implement our own exponentiation
+
+AST:
+
+.. code-block:: nim
+  nnkExportExceptStmt(nnkIdent(!"math"),nnkIdent(!"pow"))
+
 Include statement
 -----------------
 
-Like a plain ``import`` statement.
+Like a plain ``import`` statement but with ``nnkIncludeStmt``.
 
 Concrete syntax:
 
@@ -872,6 +885,20 @@ Let section
 This is equivalent to ``var``, but with ``nnkLetSection`` rather than
 ``nnkVarSection``.
 
+Concrete syntax:
+
+.. code-block:: nim
+  let v = 3
+
+AST:
+
+.. code-block:: nim
+  nnkLetSection(
+    nnkIdentDefs(!"v"),
+    nnkEmpty(), # for the type
+    nnkIntLit(3)
+  )
+
 Const section
 -------------
 
@@ -983,6 +1010,73 @@ AST:
     )
   )
 
+Nim's object syntax is rich. Let's take a look at an involved example in
+its entirety to see some of the complexities.
+
+Concrete syntax:
+
+.. code-block:: nim
+  type Obj[T] = object {.inheritable.}
+    name: string
+    case isFat: bool
+    of true:
+      m: array[100_000, T]
+    of false:
+      m: array[10, T]
+
+AST:
+
+.. code-block:: nim
+  # ...
+  nnkObjectTy(
+    nnkPragma(
+      nnkIdent(!"inheritable")
+    ),
+    nnkEmpty(),
+    nnkRecList( # list of object parameters
+      nnkIdentDefs(
+            nnkIdent(!"name"),
+            nnkIdent(!"string"),
+            nnkEmpty()
+      ),
+      nnkRecCase( # case statement within object (not nnkCaseStmt)
+        nnkIdentDefs(
+              nnkIdent(!"isFat"),
+              nnkIdent(!"bool"),
+              nnkEmpty()
+        ),
+        nnkOfBranch(
+              nnkIdent(!"true"),
+              nnkRecList( # again, a list of object parameters
+                nnkIdentDefs(
+                  nnkIdent(!"m"),
+                  nnkBracketExpr(
+                    nnkIdent(!"array"),
+                    nnkIntLit(100000),
+                    nnkIdent(!"T")
+                  ),
+                  nnkEmpty()
+              )
+        ),
+        nnkOfBranch(
+              nnkIdent(!"false"),
+              nnkRecList(
+                nnkIdentDefs(
+                  nnkIdent(!"m"),
+                  nnkBracketExpr(
+                    nnkIdent(!"array"),
+                    nnkIntLit(10),
+                    nnkIdent(!"T")
+                  ),
+                  nnkEmpty()
+                )
+              )
+        )
+      )
+    )
+  )
+
+
 Using an ``enum`` is similar to using an ``object``.
 
 Concrete syntax: