-
Notifications
You must be signed in to change notification settings - Fork 0
/
docs.html
647 lines (577 loc) · 30.7 KB
/
docs.html
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2022-09-07 Wed 21:19 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Dasy Docs</title>
<meta name="author" content="z80" />
<meta name="generator" content="Org Mode" />
<link rel="stylesheet" type="text/css" href="https://fniessen.github.io/org-html-themes/src/readtheorg_theme/css/htmlize.css"/>
<link rel="stylesheet" type="text/css" href="https://fniessen.github.io/org-html-themes/src/readtheorg_theme/css/readtheorg.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://fniessen.github.io/org-html-themes/src/lib/js/jquery.stickytableheaders.min.js"></script>
<script type="text/javascript" src="https://fniessen.github.io/org-html-themes/src/readtheorg_theme/js/readtheorg.js"></script>
</head>
<body>
<div id="content" class="content">
<h1 class="title">Dasy Docs</h1>
<div id="table-of-contents" role="doc-toc">
<h2>Table of Contents</h2>
<div id="text-table-of-contents" role="doc-toc">
<ul>
<li><a href="#org0853a27">1. Current Status</a></li>
<li><a href="#orge5890ec">2. Syntax</a>
<ul>
<li><a href="#orga7231c3">2.1. Tuples</a></li>
<li><a href="#org111318d">2.2. Arrays</a></li>
<li><a href="#orgcbed239">2.3. Types</a></li>
<li><a href="#org230f0f5">2.4. Operators</a>
<ul>
<li><a href="#org04792a6">2.4.1. Built-in chaining</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#org1837cfe">3. Core Forms</a>
<ul>
<li><a href="#orgd44a2b3">3.1. <code>defn</code></a></li>
<li><a href="#org50ab28c">3.2. <code>defvar</code></a></li>
<li><a href="#orgf1c61c7">3.3. <code>setv</code></a></li>
<li><a href="#org14956b4">3.4. <code>definterface</code></a></li>
<li><a href="#org6f76ddf">3.5. <code>defstruct</code></a></li>
<li><a href="#orgd41f180">3.6. <code>defevent</code></a></li>
<li><a href="#org1befa19">3.7. <code>defconst</code></a></li>
<li><a href="#org5eb4f48">3.8. <code>defmacro</code></a></li>
</ul>
</li>
<li><a href="#orgefe587b">4. Control Structures</a>
<ul>
<li><a href="#org745afb7">4.1. If</a>
<ul>
<li><a href="#orga46afdd">4.1.1. (return 1) (return 0))</a></li>
</ul>
</li>
<li><a href="#org4cb0e6f">4.2. Loops</a>
<ul>
<li><a href="#orga286475">4.2.1. For Loops</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#org6a232ad">5. Errors</a>
<ul>
<li><a href="#org687116a">5.1. <code>assert</code></a></li>
<li><a href="#org55308d5">5.2. <code>raise</code></a></li>
</ul>
</li>
<li><a href="#org3f59074">6. Built-in Macros</a>
<ul>
<li><a href="#org4922c80">6.1. <code>/</code></a></li>
<li><a href="#orge68a06f">6.2. <code>set-at</code></a></li>
<li><a href="#orge1cdd57">6.3. <code>set-in</code></a></li>
<li><a href="#org8c1b4a0">6.4. <code>doto</code></a></li>
</ul>
</li>
</ul>
</div>
</div>
<div id="outline-container-org0853a27" class="outline-2">
<h2 id="org0853a27"><span class="section-number-2">1.</span> Current Status</h2>
<div class="outline-text-2" id="text-1">
<p>
Dasy is currently in pre-alpha. The language’s core is still being designed and implemented.
</p>
</div>
</div>
<div id="outline-container-orge5890ec" class="outline-2">
<h2 id="orge5890ec"><span class="section-number-2">2.</span> Syntax</h2>
<div class="outline-text-2" id="text-2">
<p>
Dasy has a clojure-inspired lisp syntax with some influences from python. Some constructs are dasy-specific.
Most vyper code can be translated by wrapping in parentheses properly. For example, you can assume that for <code>arr.append(10)</code> in Vyper, the equivalent Dasy is <code>(.append arr 10)</code>
</p>
</div>
<div id="outline-container-orga7231c3" class="outline-3">
<h3 id="orga7231c3"><span class="section-number-3">2.1.</span> Tuples</h3>
<div class="outline-text-3" id="text-2-1">
<p>
Tuples are represented by a quoted list such as <code>'(1 2 3)</code>
</p>
<p>
The vyper equivalent is <code>(1, 2, 3)</code>
</p>
</div>
</div>
<div id="outline-container-org111318d" class="outline-3">
<h3 id="org111318d"><span class="section-number-3">2.2.</span> Arrays</h3>
<div class="outline-text-3" id="text-2-2">
<p>
Arrays are represented by a bracketed list, such as <code>[1 2 3]</code>
</p>
<p>
The vyper equivalent is <code>[1, 2, 3]</code>
</p>
</div>
</div>
<div id="outline-container-orgcbed239" class="outline-3">
<h3 id="orgcbed239"><span class="section-number-3">2.3.</span> Types</h3>
<div class="outline-text-3" id="text-2-3">
<p>
Dasy has all of Vyper’s types. Base types such as <code>uint256</code> are represented with a dasy ’keyword’, which uses a colon and an identifier. Complex types are represented with a function-call syntax. Arrays are created with <code>array</code>, or <code>dyn-array</code> for dynamic arrays.
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
<col class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Vyper</th>
<th scope="col" class="org-left">Dasy</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left"><code>uint256</code></td>
<td class="org-left"><code>:uint256</code></td>
</tr>
<tr>
<td class="org-left"><code>bool</code></td>
<td class="org-left"><code>:bool</code></td>
</tr>
<tr>
<td class="org-left"><code>bytes32</code></td>
<td class="org-left"><code>:bytes32</code></td>
</tr>
<tr>
<td class="org-left"><code>String[10]</code></td>
<td class="org-left"><code>(string 10)</code></td>
</tr>
<tr>
<td class="org-left"><code>uint256[10]</code></td>
<td class="org-left"><code>(array :uint256 10)</code></td>
</tr>
<tr>
<td class="org-left"><code>HashMap[uint256, bool]</code></td>
<td class="org-left"><code>(hash-map :uint256 :bool)</code></td>
</tr>
<tr>
<td class="org-left"><code>DynArray[uint256, 5]</code></td>
<td class="org-left"><code>(dyn-array :uint256 5)</code></td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="outline-container-org230f0f5" class="outline-3">
<h3 id="org230f0f5"><span class="section-number-3">2.4.</span> Operators</h3>
<div class="outline-text-3" id="text-2-4">
<p>
Dasy has mostly identical operators and builtins as Vyper. There are a few small differences.
</p>
</div>
<div id="outline-container-org04792a6" class="outline-4">
<h4 id="org04792a6"><span class="section-number-4">2.4.1.</span> Built-in chaining</h4>
<div class="outline-text-4" id="text-2-4-1">
<p>
Binary operations are chained by default in Dasy. This allows you to specify more than two arguments at at time.
</p>
<p>
Because of this, in Dasy, <code>+</code> functions like a <code>sum</code> operator.
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
<col class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Vyper</th>
<th scope="col" class="org-left">Dasy</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left"><code>2 + 3 + 4 + 5</code></td>
<td class="org-left"><code>(+ 2 3 4 5)</code></td>
</tr>
<tr>
<td class="org-left"><code>x < y and y < z and z < a</code></td>
<td class="org-left"><code>(< x y z a)</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<div id="outline-container-org1837cfe" class="outline-2">
<h2 id="org1837cfe"><span class="section-number-2">3.</span> Core Forms</h2>
<div class="outline-text-2" id="text-3">
</div>
<div id="outline-container-orgd44a2b3" class="outline-3">
<h3 id="orgd44a2b3"><span class="section-number-3">3.1.</span> <code>defn</code></h3>
<div class="outline-text-3" id="text-3-1">
<p>
<code>(defn fn-name args [return-type] visibility & body)</code>
</p>
<p>
This special form declares and defines a function within a smart contract.
</p>
<p>
The <code>args</code> list may be an empty list, but must be present. Returning multiple values requires declaring the return type as a tuple.
</p>
<p>
The <code>return-type</code> object is optional. If present, it may be a single keyword representing the return type, or it may be a tuple of keywords for returning multiple values.
</p>
<p>
The <code>visibility</code> object may also be a keyword or list of keywords. Valid values are:
</p>
<ul class="org-ul">
<li><code>:external</code></li>
<li><code>:internal</code></li>
<li><code>:payable</code></li>
<li><code>:view</code></li>
<li><code>:pure</code></li>
<li><code>(nonreentrant "lock-name")</code></li>
</ul>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #707183;">(</span><span style="color: #0000FF;">defn</span> <span style="color: #006699;">noArgs</span> <span style="color: #7388D6;">[]</span> <span style="color: #D0372D;">:external</span> <span style="color: #7388D6;">(</span>pass<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">defn</span> <span style="color: #006699;">setNum</span> <span style="color: #7388D6;">[</span><span style="color: #D0372D;">:uint256</span> x<span style="color: #7388D6;">]</span> <span style="color: #D0372D;">:external</span> <span style="color: #7388D6;">(</span>pass<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">defn</span> <span style="color: #006699;">addNums</span> <span style="color: #7388D6;">[</span><span style="color: #D0372D;">:uint256</span> x y<span style="color: #7388D6;">]</span> <span style="color: #D0372D;">:uint256</span> <span style="color: #7388D6;">[</span><span style="color: #D0372D;">:external</span> <span style="color: #D0372D;">:pure</span><span style="color: #7388D6;">]</span>
<span style="color: #7388D6;">(</span>+ x y<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">defn</span> <span style="color: #006699;">addAndSub</span> <span style="color: #7388D6;">[</span><span style="color: #D0372D;">:uint256</span> x y<span style="color: #7388D6;">]</span> '<span style="color: #7388D6;">(</span><span style="color: #D0372D;">:uint256</span> <span style="color: #D0372D;">:uint256</span><span style="color: #7388D6;">)</span> <span style="color: #7388D6;">[</span><span style="color: #D0372D;">:external</span> <span style="color: #D0372D;">:pure</span><span style="color: #7388D6;">]</span>
'<span style="color: #7388D6;">(</span><span style="color: #909183;">(</span>+ x y<span style="color: #909183;">)</span> <span style="color: #909183;">(</span>- x y<span style="color: #909183;">)</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org50ab28c" class="outline-3">
<h3 id="org50ab28c"><span class="section-number-3">3.2.</span> <code>defvar</code></h3>
<div class="outline-text-3" id="text-3-2">
<p>
<code>(defvar variable-name type [value])</code>
</p>
<p>
This special form declares and optionally assigns a value to a variable.
</p>
<p>
Outside of a <code>defn</code> form, variables are stored in <code>storage</code> and accessible via <code>self.variable-name</code>.
</p>
<p>
Inside a <code>defn</code> form, variables are stored in <code>memory</code> and accessible directly.
</p>
<p>
The <code>value</code> form is optional.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #707183;">(</span><span style="color: #0000FF;">defvar</span> <span style="color: #006699;">owner</span> <span style="color: #7388D6;">(</span>public <span style="color: #D0372D;">:address</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">defvar</span> <span style="color: #006699;">enabled</span> <span style="color: #D0372D;">:bool</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">defn</span> <span style="color: #006699;">foo</span> <span style="color: #7388D6;">[]</span> <span style="color: #D0372D;">:external</span>
<span style="color: #7388D6;">(</span><span style="color: #0000FF;">defvar</span> <span style="color: #006699;">owner_memory</span> <span style="color: #D0372D;">:address</span> <span style="color: #6434A3;">self</span>/owner<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span> <span style="color: #8D8D84;">;; </span><span style="color: #8D8D84; font-style: italic;">declare copy in memory</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orgf1c61c7" class="outline-3">
<h3 id="orgf1c61c7"><span class="section-number-3">3.3.</span> <code>setv</code></h3>
<div class="outline-text-3" id="text-3-3">
<p>
<code>(setv name value)</code>
</p>
<p>
This special form assigns a value to a name. It is roughly equivalent to the equal sign <code>=</code> in Vyper.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #8D8D84;">;; </span><span style="color: #8D8D84; font-style: italic;">Create a string variable that can store maximum 100 characters</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">defvar</span> <span style="color: #006699;">greet</span> <span style="color: #7388D6;">(</span>public <span style="color: #909183;">(</span>string <span style="color: #D0372D;">100</span><span style="color: #909183;">)</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">defn</span> <span style="color: #006699;">__init__</span> <span style="color: #7388D6;">[]</span> <span style="color: #D0372D;">:external</span>
<span style="color: #7388D6;">(</span>setv <span style="color: #6434A3;">self</span>/greet <span style="color: #008000;">"Hello World"</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span> <span style="color: #8D8D84;">;; </span><span style="color: #8D8D84; font-style: italic;">in vyper: self.greet = "Hello World"</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org14956b4" class="outline-3">
<h3 id="org14956b4"><span class="section-number-3">3.4.</span> <code>definterface</code></h3>
<div class="outline-text-3" id="text-3-4">
<p>
<code>(definterface name & fns)</code>
</p>
<p>
This special form declares an interface.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #707183;">(</span><span style="color: #0000FF;">definterface</span> <span style="color: #006699;">TestInterface</span>
<span style="color: #7388D6;">(</span><span style="color: #0000FF;">defn</span> <span style="color: #006699;">owner</span> <span style="color: #909183;">[]</span> <span style="color: #D0372D;">:address</span> <span style="color: #D0372D;">:view</span><span style="color: #7388D6;">)</span>
<span style="color: #7388D6;">(</span><span style="color: #0000FF;">defn</span> <span style="color: #006699;">setOwner</span> <span style="color: #909183;">[</span><span style="color: #D0372D;">:address</span> owner<span style="color: #909183;">]</span> <span style="color: #D0372D;">:nonpayable</span><span style="color: #7388D6;">)</span>
<span style="color: #7388D6;">(</span><span style="color: #0000FF;">defn</span> <span style="color: #006699;">sendEth</span> <span style="color: #909183;">[]</span> <span style="color: #D0372D;">:payable</span><span style="color: #7388D6;">)</span>
<span style="color: #7388D6;">(</span><span style="color: #0000FF;">defn</span> <span style="color: #006699;">setOwnerAndSendEth</span> <span style="color: #909183;">[</span><span style="color: #D0372D;">:address</span> owner<span style="color: #909183;">]</span> <span style="color: #D0372D;">:payable</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org6f76ddf" class="outline-3">
<h3 id="org6f76ddf"><span class="section-number-3">3.5.</span> <code>defstruct</code></h3>
<div class="outline-text-3" id="text-3-5">
<p>
<code>(defstruct name & variables)</code>
</p>
<p>
This special form declares a struct. Variables should be declared in pairs of <code>name</code> and <code>type</code>
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #707183;">(</span><span style="color: #0000FF;">defstruct</span> <span style="color: #6434A3;">Person</span>
name <span style="color: #7388D6;">(</span>string <span style="color: #D0372D;">100</span><span style="color: #7388D6;">)</span>
age <span style="color: #D0372D;">:uint256</span><span style="color: #707183;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orgd41f180" class="outline-3">
<h3 id="orgd41f180"><span class="section-number-3">3.6.</span> <code>defevent</code></h3>
<div class="outline-text-3" id="text-3-6">
<p>
<code>(defevent name & fields)</code>
</p>
<p>
This special form declares an event. Fields should be declared in pairs of <code>name</code> and <code>type</code>
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #707183;">(</span><span style="color: #0000FF;">defevent</span> <span style="color: #006699;">Transfer</span>
sender <span style="color: #7388D6;">(</span>indexed <span style="color: #D0372D;">:address</span><span style="color: #7388D6;">)</span>
receiver <span style="color: #7388D6;">(</span>indexed <span style="color: #D0372D;">:address</span><span style="color: #7388D6;">)</span>
amount <span style="color: #D0372D;">:uint256</span><span style="color: #707183;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org1befa19" class="outline-3">
<h3 id="org1befa19"><span class="section-number-3">3.7.</span> <code>defconst</code></h3>
<div class="outline-text-3" id="text-3-7">
<p>
<code>(defconst name value)</code>
</p>
<p>
This special form defines a constant. The value must be provided when defined. This value can never change.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #707183;">(</span><span style="color: #0000FF;">defconst</span> <span style="color: #006699;">MIN_AMT</span> <span style="color: #D0372D;">100</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">defconst</span> <span style="color: #006699;">GREETING</span> <span style="color: #008000;">"Hello"</span><span style="color: #707183;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org5eb4f48" class="outline-3">
<h3 id="org5eb4f48"><span class="section-number-3">3.8.</span> <code>defmacro</code></h3>
<div class="outline-text-3" id="text-3-8">
<p>
<code>(defmacro name args & body)</code>
</p>
<p>
This special form defines a macro. Macros are functions that run at compile time. Their inputs are code, and their outputs are code. They transform your code as it is built.
</p>
<p>
Macros can be used to implement convenient shorthand syntaxes. They can also be used to pull in information from the outside world into your contract at build time.
</p>
<p>
In the most simple terms, macros allow you to extend the Dasy compiler yourself in whichever way you see fit.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #8D8D84;">;; </span><span style="color: #8D8D84; font-style: italic;">(set-at myArr 0 100) -> (setv (subscript myArr 0) 100)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">defmacro</span> <span style="color: #006699;">set-at</span> <span style="color: #7388D6;">[</span>array index new-val<span style="color: #7388D6;">]</span> `<span style="color: #7388D6;">(</span>setv <span style="color: #909183;">(</span>subscript ~array ~index<span style="color: #909183;">)</span> ~new-val<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
<span style="color: #8D8D84;">;; </span><span style="color: #8D8D84; font-style: italic;">(doto obj (.append 10) (.append 20)) -> (do (.append obj 10) (.append obj 20))</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">defmacro</span> <span style="color: #006699;">doto</span> <span style="color: #7388D6;">[</span> obj #*cmds<span style="color: #7388D6;">]</span>
<span style="color: #7388D6;">(</span>lfor c cmds
`<span style="color: #909183;">(</span>~<span style="color: #709870;">(</span>get c <span style="color: #D0372D;">0</span><span style="color: #709870;">)</span> ~obj ~@<span style="color: #709870;">(</span>cut c <span style="color: #D0372D;">1</span> None<span style="color: #709870;">)</span><span style="color: #909183;">)</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-orgefe587b" class="outline-2">
<h2 id="orgefe587b"><span class="section-number-2">4.</span> Control Structures</h2>
<div class="outline-text-2" id="text-4">
</div>
<div id="outline-container-org745afb7" class="outline-3">
<h3 id="org745afb7"><span class="section-number-3">4.1.</span> If</h3>
<div class="outline-text-3" id="text-4-1">
<p>
<code>(if test body else-body)</code>
If executes a test, and depending on the result, either executes the <code>body</code> or the <code>else-body</code>.
</p>
<p>
#+begin<sub>src</sub> clojure
(if (x < 100)
</p>
</div>
<div id="outline-container-orga46afdd" class="outline-4">
<h4 id="orga46afdd"><span class="section-number-4">4.1.1.</span> (return 1) (return 0))</h4>
<div class="outline-text-4" id="text-4-1-1">
<p>
#+end<sub>src</sub>
</p>
</div>
</div>
</div>
<div id="outline-container-org4cb0e6f" class="outline-3">
<h3 id="org4cb0e6f"><span class="section-number-3">4.2.</span> Loops</h3>
<div class="outline-text-3" id="text-4-2">
</div>
<div id="outline-container-orga286475" class="outline-4">
<h4 id="orga286475"><span class="section-number-4">4.2.1.</span> For Loops</h4>
<div class="outline-text-4" id="text-4-2-1">
<p>
<code>for</code> loops can operate on arrays directly, or on a <code>range</code>
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #707183;">(</span><span style="color: #0000FF;">for</span> <span style="color: #7388D6;">[</span>i nums<span style="color: #7388D6;">]</span>
<span style="color: #7388D6;">(</span>+= sum i<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">for</span> <span style="color: #7388D6;">[</span>i <span style="color: #909183;">[</span><span style="color: #D0372D;">1</span> <span style="color: #D0372D;">2</span> <span style="color: #D0372D;">3</span> <span style="color: #D0372D;">4</span> <span style="color: #D0372D;">5</span><span style="color: #909183;">]</span><span style="color: #7388D6;">]</span>
<span style="color: #7388D6;">(</span>+= sum i<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">for</span> <span style="color: #7388D6;">[</span>i <span style="color: #909183;">(</span>range <span style="color: #D0372D;">10</span><span style="color: #909183;">)</span><span style="color: #7388D6;">]</span>
<span style="color: #7388D6;">(</span>+= sum i<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
<p>
In a <code>for</code> loop’s body, <code>continue</code> and <code>break</code> behave the same as they do in Vyper.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #707183;">(</span><span style="color: #0000FF;">for</span> <span style="color: #7388D6;">[</span>i <span style="color: #909183;">(</span>range <span style="color: #D0372D;">10</span><span style="color: #909183;">)</span><span style="color: #7388D6;">]</span>
<span style="color: #7388D6;">(</span><span style="color: #0000FF;">if</span> <span style="color: #909183;">(</span>== i <span style="color: #D0372D;">5</span><span style="color: #909183;">)</span>
<span style="color: #909183;">(</span>continue<span style="color: #909183;">)</span><span style="color: #7388D6;">)</span>
<span style="color: #7388D6;">(</span>+= sum i<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
</div>
</div>
</div>
</div>
<div id="outline-container-org6a232ad" class="outline-2">
<h2 id="org6a232ad"><span class="section-number-2">5.</span> Errors</h2>
<div class="outline-text-2" id="text-5">
</div>
<div id="outline-container-org687116a" class="outline-3">
<h3 id="org687116a"><span class="section-number-3">5.1.</span> <code>assert</code></h3>
<div class="outline-text-3" id="text-5-1">
<p>
<code>assert</code> behaves as it does in Vyper. It expects a test and an optional error message.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #707183;">(</span>assert <span style="color: #7388D6;">(</span>< x <span style="color: #D0372D;">100</span><span style="color: #7388D6;">)</span> <span style="color: #008000;">"x must be less than 100"</span><span style="color: #707183;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org55308d5" class="outline-3">
<h3 id="org55308d5"><span class="section-number-3">5.2.</span> <code>raise</code></h3>
<div class="outline-text-3" id="text-5-2">
<p>
<code>raise</code> behaves as it does in Vyper. It expects a message.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #707183;">(</span><span style="color: #0000FF;">if</span> <span style="color: #7388D6;">(</span>>= x <span style="color: #D0372D;">100</span><span style="color: #7388D6;">)</span>
<span style="color: #7388D6;">(</span>raise <span style="color: #008000;">"x must be less than 100"</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-org3f59074" class="outline-2">
<h2 id="org3f59074"><span class="section-number-2">6.</span> Built-in Macros</h2>
<div class="outline-text-2" id="text-6">
</div>
<div id="outline-container-org4922c80" class="outline-3">
<h3 id="org4922c80"><span class="section-number-3">6.1.</span> <code>/</code></h3>
<div class="outline-text-3" id="text-6-1">
<p>
<code>(setv self/foo bar)</code>
</p>
<p>
Access object attributes. <code>obj/name</code> is shorthand for <code>(. obj name)</code>
</p>
</div>
</div>
<div id="outline-container-orge68a06f" class="outline-3">
<h3 id="orge68a06f"><span class="section-number-3">6.2.</span> <code>set-at</code></h3>
<div class="outline-text-3" id="text-6-2">
<p>
<code>(set-at obj index val)</code>
</p>
<p>
Sets a value at an index within an object. This object can be an array, dynamic array, or hashmap.
</p>
<p>
This expands to <code>(setv (subscript array index) new-val)</code>
</p>
<p>
The vyper equivalent looks like <code>obj[index] = val</code>
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #707183;">(</span><span style="color: #0000FF;">defvar</span> <span style="color: #006699;">arr</span> <span style="color: #7388D6;">(</span>array <span style="color: #D0372D;">:uint256</span> <span style="color: #D0372D;">10</span><span style="color: #7388D6;">)</span>
myMap <span style="color: #7388D6;">(</span>hash-map <span style="color: #D0372D;">:addr</span> <span style="color: #D0372D;">:bool</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span>set-at arr <span style="color: #D0372D;">0</span> <span style="color: #D0372D;">100</span><span style="color: #707183;">)</span> <span style="color: #8D8D84;">;; </span><span style="color: #8D8D84; font-style: italic;">arr[0] = 100</span>
<span style="color: #707183;">(</span>set-at myMap <span style="color: #D0372D;">0x1234....</span> True<span style="color: #707183;">)</span> <span style="color: #8D8D84;">;; </span><span style="color: #8D8D84; font-style: italic;">myMap[0x1234....] = True</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orge1cdd57" class="outline-3">
<h3 id="orge1cdd57"><span class="section-number-3">6.3.</span> <code>set-in</code></h3>
<div class="outline-text-3" id="text-6-3">
<p>
<code>(set-in obj attr val)</code>
</p>
<p>
Sets the value of an attribute of an object. This object is usually a struct.
</p>
<p>
This expands to <code>(setv (. obj attr) val)</code>
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #707183;">(</span><span style="color: #0000FF;">defstruct</span> <span style="color: #6434A3;">Person</span>
name <span style="color: #7388D6;">(</span>string <span style="color: #D0372D;">10</span><span style="color: #7388D6;">)</span>
age <span style="color: #D0372D;">:uint256</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">defvar</span> <span style="color: #006699;">p</span> Person<span style="color: #707183;">)</span>
<span style="color: #707183;">(</span>set-in p age <span style="color: #D0372D;">40</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span>set-in p name <span style="color: #008000;">"Vitalik"</span><span style="color: #707183;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org8c1b4a0" class="outline-3">
<h3 id="org8c1b4a0"><span class="section-number-3">6.4.</span> <code>doto</code></h3>
<div class="outline-text-3" id="text-6-4">
<p>
<code>(doto obj & body)</code>
</p>
<p>
Call multiple functions on the same object. Allows for shorter code.
</p>
<p>
<code>(doto obj (.foo 10) (.bar 100))</code> expands to <code>(do (.foo obj 10) (.bar obj 100))</code>
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #8D8D84;">;; </span><span style="color: #8D8D84; font-style: italic;">above example rewritten with doto</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">defstruct</span> <span style="color: #6434A3;">Person</span>
name <span style="color: #7388D6;">(</span>string <span style="color: #D0372D;">10</span><span style="color: #7388D6;">)</span>
age <span style="color: #D0372D;">:uint256</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">doto</span> p
<span style="color: #7388D6;">(</span><span style="color: #0000FF;">defvar</span> <span style="color: #006699;">Person</span><span style="color: #7388D6;">)</span>
<span style="color: #7388D6;">(</span>set-in age <span style="color: #D0372D;">40</span><span style="color: #7388D6;">)</span>
<span style="color: #7388D6;">(</span>set-in name <span style="color: #008000;">"Vitalik"</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
</div>
</div>
</div>
</div>
<div id="postamble" class="status">
<p class="author">Author: z80</p>
<p class="date">Created: 2022-09-07 Wed 21:19</p>
</div>
</body>
</html>