Debian Codegraph @

examples/prism-nim.html

raw symbols SCIP
1 <h2>Comments</h2>
2 <pre><code># This is a comment</code></pre>
3
4 <h2>Strings</h2>
5 <pre><code>"This is a string."
6 "This is a string with \"quotes\" in it."
7 """This is
8 a "multi-line"
9 string."""
10 """"A long string within quotes.""""
11 R"This is a raw string."
12 r"Some ""quotes"" inside a raw string."
13 r"""Raw strings
14 can also be multi-line."""
15 foo"This is a generalized raw string literal."
16 bar"""This is also
17 a generalized raw string literal."""</code></pre>
18
19 <h2>Characters</h2>
20 <pre><code>'a'
21 '\''
22 '\t'
23 '\15'
24 '\xFC'</code></pre>
25
26 <h2>Numbers</h2>
27 <pre><code>42
28 0xaf
29 0xf_2_c
30 0o07
31 0b1111_0000
32 0B0_10001110100_0000101001000111101011101111111011000101001101001001'f64
33 9_000'u
34 32.
35 32.1f32
36 32.e-5
37 32.2e+2
38 2'i16
39 2i16
40 0xfe'f32</code></pre>
41
42 <h2>Full example</h2>
43 <pre><code># Example from http://nim-by-example.github.io/oop_macro/
44 import macros
45
46 macro class*(head: expr, body: stmt): stmt {.immediate.} =
47 # The macro is immediate so that it doesn't
48 # resolve identifiers passed to it
49
50 var typeName, baseName: NimNode
51
52 if head.kind == nnkIdent:
53 # `head` is expression `typeName`
54 # echo head.treeRepr
55 # --------------------
56 # Ident !"Animal"
57 typeName = head
58
59 elif head.kind == nnkInfix and $head[0] == "of":
60 # `head` is expression `typeName of baseClass`
61 # echo head.treeRepr
62 # --------------------
63 # Infix
64 # Ident !"of"
65 # Ident !"Animal"
66 # Ident !"RootObj"
67 typeName = head[1]
68 baseName = head[2]
69
70 else:
71 quit "Invalid node: " & head.lispRepr
72
73 # echo treeRepr(body)
74 # --------------------
75 # StmtList
76 # VarSection
77 # IdentDefs
78 # Ident !"name"
79 # Ident !"string"
80 # Empty
81 # IdentDefs
82 # Ident !"age"
83 # Ident !"int"
84 # Empty
85 # MethodDef
86 # Ident !"vocalize"
87 # Empty
88 # Empty
89 # FormalParams
90 # Ident !"string"
91 # Empty
92 # Empty
93 # StmtList
94 # StrLit ...
95 # MethodDef
96 # Ident !"age_human_yrs"
97 # Empty
98 # Empty
99 # FormalParams
100 # Ident !"int"
101 # Empty
102 # Empty
103 # StmtList
104 # DotExpr
105 # Ident !"this"
106 # Ident !"age"
107
108 # create a new stmtList for the result
109 result = newStmtList()
110
111 # var declarations will be turned into object fields
112 var recList = newNimNode(nnkRecList)
113
114 # Iterate over the statements, adding `this: T`
115 # to the parameters of functions
116 for node in body.children:
117 case node.kind:
118
119 of nnkMethodDef, nnkProcDef:
120 # inject `this: T` into the arguments
121 let p = copyNimTree(node.params)
122 p.insert(1, newIdentDefs(ident"this", typeName))
123 node.params = p
124 result.add(node)
125
126 of nnkVarSection:
127 # variables get turned into fields of the type.
128 for n in node.children:
129 recList.add(n)
130
131 else:
132 result.add(node)
133
134 # The following prints out the AST structure:
135 #
136 # import macros
137 # dumptree:
138 # type X = ref object of Y
139 # z: int
140 # --------------------
141 # TypeSection
142 # TypeDef
143 # Ident !"X"
144 # Empty
145 # RefTy
146 # ObjectTy
147 # Empty
148 # OfInherit
149 # Ident !"Y"
150 # RecList
151 # IdentDefs
152 # Ident !"z"
153 # Ident !"int"
154 # Empty
155
156 result.insert(0,
157 if baseName == nil:
158 quote do:
159 type `typeName` = ref object of RootObj
160 else:
161 quote do:
162 type `typeName` = ref object of `baseName`
163 )
164 # Inspect the tree structure:
165 #
166 # echo result.treeRepr
167 # --------------------
168 # StmtList
169 # StmtList
170 # TypeSection
171 # TypeDef
172 # Ident !"Animal"
173 # Empty
174 # RefTy
175 # ObjectTy
176 # Empty
177 # OfInherit
178 # Ident !"RootObj"
179 # Empty &lt;= We want to replace this
180 # MethodDef
181 # ...
182
183 result[0][0][0][2][0][2] = recList
184
185 # Lets inspect the human-readable version of the output
186 # echo repr(result)
187 # Output:
188 # type
189 # Animal = ref object of RootObj
190 # name: string
191 # age: int
192 #
193 # method vocalize(this: Animal): string =
194 # "..."
195 #
196 # method age_human_yrs(this: Animal): int =
197 # this.age
198
199 # ---
200
201 class Animal of RootObj:
202 var name: string
203 var age: int
204 method vocalize: string = "..."
205 method age_human_yrs: int = this.age # `this` is injected
206
207 class Dog of Animal:
208 method vocalize: string = "woof"
209 method age_human_yrs: int = this.age * 7
210
211 class Cat of Animal:
212 method vocalize: string = "meow"
213
214 # ---
215
216 var animals: seq[Animal] = @[]
217 animals.add(Dog(name: "Sparky", age: 10))
218 animals.add(Cat(name: "Mitten", age: 10))
219
220 for a in animals:
221 echo a.vocalize()
222 echo a.age_human_yrs()</code></pre>