-
Notifications
You must be signed in to change notification settings - Fork 0
/
lang.html
1539 lines (1513 loc) · 123 KB
/
lang.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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 2.0.12">
<meta name="author" content="v0.1">
<title>The Agate Language</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700">
<link rel="stylesheet" href="./asciidoctor.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="./rouge-old_github.css">
<meta name="keywords" content="Agate, programming language, scripting, gamedev" />
<meta name="description" content="Agate, a simple and embeddable programming language" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/site.webmanifest">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="theme-color" content="#ffffff">
</head>
<body class="article toc2 toc-right">
<div id="header">
<h1>The Agate Language</h1>
<div class="details">
<span id="author" class="author">v0.1</span><br>
</div>
<div id="toc" class="toc2">
<div id="toctitle">Table of Contents</div>
<ul class="sectlevel1">
<li><a href="#_syntax">Syntax</a>
<ul class="sectlevel2">
<li><a href="#_comments">Comments</a></li>
<li><a href="#_keywords">Keywords</a></li>
<li><a href="#_identifiers">Identifiers</a></li>
<li><a href="#_newlines">Newlines</a></li>
<li><a href="#_blocks">Blocks</a></li>
</ul>
</li>
<li><a href="#_values">Values</a>
<ul class="sectlevel2">
<li><a href="#_nil">Nil</a></li>
<li><a href="#_boolean">Boolean</a></li>
<li><a href="#_integers">Integers</a></li>
<li><a href="#_floats">Floats</a></li>
<li><a href="#_characters">Characters</a></li>
<li><a href="#_strings">Strings</a></li>
<li><a href="#_ranges">Ranges</a></li>
</ul>
</li>
<li><a href="#_expressions">Expressions</a>
<ul class="sectlevel2">
<li><a href="#_array_literals">Array Literals</a></li>
<li><a href="#_map_literals">Map Literals</a></li>
<li><a href="#_tuple_literals">Tuple Literals</a></li>
<li><a href="#_operators">Operators</a></li>
<li><a href="#_the_truth_is_out_there">The truth is out there</a></li>
<li><a href="#_logical_operators">Logical Operators</a></li>
</ul>
</li>
<li><a href="#_control_flow">Control Flow</a>
<ul class="sectlevel2">
<li><a href="#_if_statements">If Statements</a></li>
<li><a href="#_once_statements">Once Statements</a></li>
<li><a href="#_loop_statements">Loop Statements</a></li>
<li><a href="#_while_statements">While Statements</a></li>
<li><a href="#_for_statements">For Statements</a></li>
<li><a href="#_iterator_protocol">Iterator Protocol</a></li>
<li><a href="#_interrupting_a_loop">Interrupting a Loop</a></li>
<li><a href="#_assert_statements">Assert Statements</a></li>
</ul>
</li>
<li><a href="#_variables">Variables</a>
<ul class="sectlevel2">
<li><a href="#_definition">Definition</a></li>
<li><a href="#_assignment">Assignment</a></li>
<li><a href="#_scope">Scope</a></li>
</ul>
</li>
<li><a href="#_classes">Classes</a>
<ul class="sectlevel2">
<li><a href="#_methods">Methods</a></li>
<li><a href="#_constructor">Constructor</a></li>
<li><a href="#_static_methods">Static methods</a></li>
<li><a href="#_fields">Fields</a></li>
<li><a href="#_this"><code>this</code></a></li>
<li><a href="#_inheritance">Inheritance</a></li>
<li><a href="#_mixin">Mixin</a></li>
</ul>
</li>
<li><a href="#_functions">Functions</a>
<ul class="sectlevel2">
<li><a href="#_definition_parameters_and_return_value">Definition, parameters and return value</a></li>
<li><a href="#_call">Call</a></li>
<li><a href="#_block_argument">Block Argument</a></li>
<li><a href="#_closure">Closure</a></li>
<li><a href="#_callable">Callable</a></li>
</ul>
</li>
<li><a href="#_units">Units</a>
<ul class="sectlevel2">
<li><a href="#_import">Import</a></li>
<li><a href="#_process">Process</a></li>
<li><a href="#_location">Location</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p>The Agate language is a scripting language.</p>
</div>
<div class="paragraph">
<p><a href="index.html">< Back to main page</a></p>
</div>
<div class="paragraph">
<p>An Agate file is a UTF-8 plain text file with a <code>.agate</code> extension.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_syntax"><a class="anchor" href="#_syntax"></a>Syntax</h2>
<div class="sectionbody">
<div class="paragraph">
<p>The syntax of Agate is inspired by C, but also takes ideas from Ruby and other languages.</p>
</div>
<div class="sect2">
<h3 id="_comments"><a class="anchor" href="#_comments"></a>Comments</h3>
<div class="paragraph">
<p>Comments starts with <code>#</code> and run until the end of the line.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="c1"># comments are useful for</span><span class="err">
</span><span class="c1"># programmers, do comment your</span><span class="err">
</span><span class="c1"># source code!</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>In particular, it’s possible to put a <a href="https://en.wikipedia.org/wiki/Shebang_(Unix)">shebang</a> at the beginning of the file.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="c1">#!/usr/local/bin/agate-cli</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>There is no multi-line comments in Agate.</p>
</div>
</div>
<div class="sect2">
<h3 id="_keywords"><a class="anchor" href="#_keywords"></a>Keywords</h3>
<div class="paragraph">
<p>Agate has <strong>25</strong> keywords: <code>as</code>, <code>assert</code>, <code>break</code>, <code>class</code>, <code>construct</code>, <code>continue</code>, <code>def</code>, <code>else</code>, <code>false</code>, <code>for</code>, <code>foreign</code>, <code>if</code>, <code>import</code>, <code>in</code>, <code>is</code>, <code>loop</code>, <code>mixin</code>, <code>nil</code>, <code>once</code>, <code>return</code>, <code>static</code>, <code>super</code>, <code>this</code>, <code>true</code>, <code>while</code>.</p>
</div>
</div>
<div class="sect2">
<h3 id="_identifiers"><a class="anchor" href="#_identifiers"></a>Identifiers</h3>
<div class="paragraph">
<p>Agate identifiers follow the same rules as the C identifiers. An idenfitier must start with a letter or <code>_</code>, and then may contain letters, digits or <code>_</code>. Identifiers are case-sensitive. Identifiers must not be keywords.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="n">variable_name</span><span class="err">
</span><span class="n">ClassName</span><span class="err">
</span><span class="n">__private_name</span><span class="err">
</span><span class="n">CONSTANT_NAME</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_newlines"><a class="anchor" href="#_newlines"></a>Newlines</h3>
<div class="paragraph">
<p>Newlines are meaningfull in Agate. They mark the end of a statement. Each statement or expression must be on a single line. If the statement seems too long to be on a single line, it’s probably because you should refactor your code in order to make it more readable.</p>
</div>
</div>
<div class="sect2">
<h3 id="_blocks"><a class="anchor" href="#_blocks"></a>Blocks</h3>
<div class="paragraph">
<p>Blocks are surrounded by <code>{</code> and <code>}</code>. Blocks are used in <a href="#_control_flow">Control Flow</a> and <a href="#_functions">Functions</a>. They are two kinds of blocks: expression blocks and statement blocks.</p>
</div>
<div class="paragraph">
<p>An expression block is a block with no newline after the opening <code>{</code> and consisting in a single expression, followed by the closing <code>}</code> on the same line. An expression block evaluates to the expression that is inside the block.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="p">{</span><span class="w"> </span><span class="s">"expression"</span><span class="w"> </span><span class="p">}</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>It’s useful in the case of simple functions or methods that can be written as a single expression. In this case, it’s equivalent to:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="s">"expression"</span><span class="err">
</span><span class="p">}</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>A statement block is a block with a newline after the opening <code>{</code> followed by statements, and a closing <code>}</code> on its own line.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"The cake"</span><span class="p">)</span><span class="err">
</span><span class="w"> </span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"is"</span><span class="p">)</span><span class="err">
</span><span class="w"> </span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"a lie"</span><span class="p">)</span><span class="err">
</span><span class="p">}</span></code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_values"><a class="anchor" href="#_values"></a>Values</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Values are immutable built-in objects of the language.</p>
</div>
<div class="sect2">
<h3 id="_nil"><a class="anchor" href="#_nil"></a>Nil</h3>
<div class="paragraph">
<p><code>nil</code> represents the absence of value, it is the only instance of the <a href="Nil.html"><code>Nil</code></a> class. It is similar to <code>nullptr</code> in C++.</p>
</div>
</div>
<div class="sect2">
<h3 id="_boolean"><a class="anchor" href="#_boolean"></a>Boolean</h3>
<div class="paragraph">
<p>A boolean value represents whether something is true or false. The literals <code>true</code> and <code>false</code> can be used to create the two possible boolean values. Their class is <a href="Bool.html"><code>Bool</code></a>.</p>
</div>
</div>
<div class="sect2">
<h3 id="_integers"><a class="anchor" href="#_integers"></a>Integers</h3>
<div class="paragraph">
<p>An <a href="https://en.wikipedia.org/wiki/Integer_(computer_science)">integer</a> is a 64-bit signed number.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="mi">12345</span><span class="w"> </span><span class="c1"># decimal form</span><span class="err">
</span><span class="mh">0xDeadBeef</span><span class="w"> </span><span class="c1"># hexadecimal form</span><span class="err">
</span><span class="mo">0o755</span><span class="w"> </span><span class="c1"># octal form</span><span class="err">
</span><span class="mb">0b101010</span><span class="w"> </span><span class="c1"># binary form</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>Integers are instances of the <a href="Int.html"><code>Int</code></a> class.</p>
</div>
</div>
<div class="sect2">
<h3 id="_floats"><a class="anchor" href="#_floats"></a>Floats</h3>
<div class="paragraph">
<p>A <a href="https://en.wikipedia.org/wiki/Double-precision_floating-point_format">float</a> is a double precision floating-point number. In order to be recognized as floats, float literals must have a decimal part starting with a point <code>.</code> and at least one digit after the point, or an exponent part starting with <code>e</code>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="mi">3</span><span class="p">.</span><span class="mi">141592</span><span class="err">
</span><span class="mi">42</span><span class="p">.</span><span class="mi">0</span><span class="err">
</span><span class="mi">1</span><span class="n">e10</span><span class="err">
</span><span class="mi">1</span><span class="p">.</span><span class="mi">0</span><span class="n">e</span><span class="o">-</span><span class="mi">5</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>Floats are instances of the <a href="Float.html"><code>Float</code></a> class.</p>
</div>
</div>
<div class="sect2">
<h3 id="_characters"><a class="anchor" href="#_characters"></a>Characters</h3>
<div class="paragraph">
<p>A character is a single UTF-32 encoded codepoint. A character is surrounded by single quotes <code>'</code>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="sc">'a'</span><span class="err">
</span><span class="sc">'木'</span><span class="err">
</span><span class="sc">'🎮'</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>Characters are instances of the <a href="Char.html"><code>Char</code></a> class.</p>
</div>
<div class="sect3">
<h4 id="_escape_sequences"><a class="anchor" href="#_escape_sequences"></a>Escape Sequences</h4>
<div class="paragraph">
<p>Usual escape sequences are available.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="sc">'\0'</span><span class="w"> </span><span class="c1"># U+0000 NULL</span><span class="err">
</span><span class="sc">'\a'</span><span class="w"> </span><span class="c1"># U+0007 BELL</span><span class="err">
</span><span class="sc">'\b'</span><span class="w"> </span><span class="c1"># U+0008 BACKSPACE</span><span class="err">
</span><span class="sc">'\t'</span><span class="w"> </span><span class="c1"># U+0009 CHARACTER TABULATION (horizontal tabulation)</span><span class="err">
</span><span class="sc">'\n'</span><span class="w"> </span><span class="c1"># U+000A LINE FEED (new line)</span><span class="err">
</span><span class="sc">'\v'</span><span class="w"> </span><span class="c1"># U+000B LINE TABULATION (vertical tabulation)</span><span class="err">
</span><span class="sc">'\f'</span><span class="w"> </span><span class="c1"># U+000C FORM FEED</span><span class="err">
</span><span class="sc">'\r'</span><span class="w"> </span><span class="c1"># U+000D CARRIAGE RETURN</span><span class="err">
</span><span class="sc">'\e'</span><span class="w"> </span><span class="c1"># U+001B ESCAPE</span><span class="err">
</span><span class="sc">'\"'</span><span class="w"> </span><span class="c1"># U+0022 QUOTATION MARK (double quote)</span><span class="err">
</span><span class="sc">'\%'</span><span class="w"> </span><span class="c1"># U+0025 PERCENT SIGN</span><span class="err">
</span><span class="sc">'\''</span><span class="w"> </span><span class="c1"># U+0027 APOSTROPHE (single quote)</span><span class="err">
</span><span class="sc">'\\'</span><span class="w"> </span><span class="c1"># U+005C REVERSE SOLIDUS (backslash)</span><span class="err">
</span><span class="sc">'\u6728'</span><span class="w"> </span><span class="c1"># '木'</span><span class="err">
</span><span class="sc">'\U0001F3AE'</span><span class="w"> </span><span class="c1"># '🎮'</span></code></pre>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_strings"><a class="anchor" href="#_strings"></a>Strings</h3>
<div class="paragraph">
<p>A string is a sequence of Unicode codepoints. They are stored using UTF-8 encoding. A string literal is surrounded by double quotes <code>"</code>. Escape sequences can also be used in strings.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="s">""</span><span class="w"> </span><span class="c1"># empty string</span><span class="err">
</span><span class="s">"The cake is a lie"</span><span class="err">
</span><span class="s">"I love </span><span class="se">\U</span><span class="s">0001F3AE"</span><span class="w"> </span><span class="c1"># I love 🎮</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>Strings are instances of the <a href="String.html"><code>String</code></a> class.</p>
</div>
<div class="sect3">
<h4 id="_interpolation"><a class="anchor" href="#_interpolation"></a>Interpolation</h4>
<div class="paragraph">
<p>Interpolation in strings is introduced by <code>%</code> followed by a parenthesized expression. The expression is evaluated and the <code>.to_s</code> method is called on the result and inserted in the string. Interpolations can be nested.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"The answer is </span><span class="si">%(</span><span class="mi">6</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mi">7</span><span class="si">)</span><span class="s">"</span><span class="p">)</span><span class="w"> </span><span class="c1">#> The answer is 42</span><span class="err">
</span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"The answer is </span><span class="si">%(</span><span class="p">(</span><span class="mi">2</span><span class="p">..</span><span class="mi">1</span><span class="p">).</span><span class="n">map</span><span class="w"> </span><span class="p">{</span><span class="o">|</span><span class="n">n</span><span class="o">|</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">n</span><span class="p">}.</span><span class="n">join</span><span class="p">()</span><span class="si">)</span><span class="s">"</span><span class="p">)</span><span class="w"> </span><span class="c1">#> The answer is 42</span></code></pre>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_ranges"><a class="anchor" href="#_ranges"></a>Ranges</h3>
<div class="paragraph">
<p>A range is a value that represents an interval of integers. A range can be constructed either with the <code>..</code> operator for inclusive range, or with the <code>...</code> operator for exclusive range. Ranges are used for <a href="#_for_statements">iterating</a> over a sequence of numbers, or for <a href="Array.html#_subscript_getter">array indexing</a>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="mi">2</span><span class="p">..</span><span class="mi">4</span><span class="w"> </span><span class="c1"># 2 to 4 (included)</span><span class="err">
</span><span class="mi">6</span><span class="p">...</span><span class="mi">9</span><span class="w"> </span><span class="c1"># 6 to 9 (excluded)</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>Ranges are instances of the <a href="Range.html"><code>Range</code></a> class.</p>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_expressions"><a class="anchor" href="#_expressions"></a>Expressions</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_array_literals"><a class="anchor" href="#_array_literals"></a>Array Literals</h3>
<div class="paragraph">
<p>An array is a growable sequence of contiguous elements, also called <a href="https://en.wikipedia.org/wiki/Dynamic_array">dynamic array</a>. Arrays are instances of the <a href="Array.html"><code>Array</code></a> class. An array can contain any number of elements of any type. In Agate, array indexing is zero-based.</p>
</div>
<div class="paragraph">
<p>An array literal can be created with elements between <code>[</code> and <code>]</code>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="p">[</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="sc">'a'</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">.</span><span class="mi">14</span><span class="p">,</span><span class="w"> </span><span class="s">"good"</span><span class="p">,</span><span class="w"> </span><span class="k">true</span><span class="w"> </span><span class="p">]</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_map_literals"><a class="anchor" href="#_map_literals"></a>Map Literals</h3>
<div class="paragraph">
<p>A map is a sequence of key-value pairs, also called <a href="https://en.wikipedia.org/wiki/Associative_array">associative array</a>. Maps are instances of the <a href="Map.html"><code>Map</code></a> class. A map can contain any number of key-value pairs. Keys must implement a <code>.hash</code> method and preferably are not mutable.</p>
</div>
<div class="paragraph">
<p>A map literal can be created with key-value pairs between <code>{</code> and <code>}</code>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="p">{</span><span class="w"> </span><span class="mi">1</span><span class="o">:</span><span class="w"> </span><span class="sc">'a'</span><span class="p">,</span><span class="w"> </span><span class="k">true</span><span class="o">:</span><span class="w"> </span><span class="mi">3</span><span class="p">.</span><span class="mi">14</span><span class="p">,</span><span class="w"> </span><span class="s">"good"</span><span class="o">:</span><span class="w"> </span><span class="k">nil</span><span class="w"> </span><span class="p">}</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_tuple_literals"><a class="anchor" href="#_tuple_literals"></a>Tuple Literals</h3>
<div class="paragraph">
<p>A tuple is an immutable sequence of contiguous components, also called <a href="https://en.wikipedia.org/wiki/Product_type">product type</a>. Tuples are instances of the <a href="Tuple.html"><code>Tuple</code></a> class. A tuple can contain two or more components of any type. Like array, tuple indexing is zero-based.</p>
</div>
<div class="paragraph">
<p>A tuple literal can be created with components between <code>(</code> and <code>)</code>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="sc">'a'</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">.</span><span class="mi">14</span><span class="p">,</span><span class="w"> </span><span class="s">"good"</span><span class="p">,</span><span class="w"> </span><span class="k">true</span><span class="p">)</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_operators"><a class="anchor" href="#_operators"></a>Operators</h3>
<div class="paragraph">
<p>The following table shows the precedence and associativity of the different operators in Agate. Note that, compared to C, the bitwise operators do not have the same precedence.</p>
</div>
<table class="tableblock frame-all grid-all stretch">
<caption class="title">Table 1. Operators precedence and associativity</caption>
<colgroup>
<col style="width: 14.2857%;">
<col style="width: 14.2857%;">
<col style="width: 57.1428%;">
<col style="width: 14.2858%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">Precedence</th>
<th class="tableblock halign-left valign-top">Operators</th>
<th class="tableblock halign-left valign-top">Description</th>
<th class="tableblock halign-left valign-top">Associativity</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">1</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>()</code> <code>[]</code> <code>.</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Call, subscript, method</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Left</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">2</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>+</code>, <code>-</code>, <code>!</code>, <code>~</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Unary plus and minus, logical NOT, bitwise NOT</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Right</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">3</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>*</code>, <code>/</code>, <code>%</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Multiplication, division, and remainder</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Left</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">4</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>+</code>, <code>-</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Addition and subtraction</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Left</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">5</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>..</code>, <code>...</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Inclusive and exclusive range</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Left</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">6</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code><<</code>, <code>>></code>, <code>>>></code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Arithmetic left shift, arithmetic right shift, logical right shift</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Left</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">7</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>&</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Bitwise AND</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Left</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">8</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>^</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Bitwise XOR</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Left</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">9</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>|</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Bitwise OR</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Left</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">10</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code><</code>, <code>></code>, <code>>=</code>, <code><=</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Comparison operators</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Left</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">11</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>is</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Type test</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Left</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">12</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>==</code>, <code>!=</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Equality, inequality</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Left</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">13</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>&&</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Logical AND</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Left</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">14</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>||</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Logical OR</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Left</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">15</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>?:</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Conditional</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Right</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">16</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>=</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Assignment</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Right</p></td>
</tr>
</tbody>
</table>
</div>
<div class="sect2">
<h3 id="_the_truth_is_out_there"><a class="anchor" href="#_the_truth_is_out_there"></a>The truth is out there</h3>
<div class="paragraph">
<p>In the context of control flow, any expression can be evaluated as a condition. Thus, it is necessary to split the expressions between those that are true and those that are false:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>false</code> and <code>nil</code> are false</p>
</li>
<li>
<p>everything else is true</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>In particular, <code>0</code> is true whereas it is false in C/C++.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="k">false</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"This is true"</span><span class="p">)</span><span class="w"> </span><span class="c1">#> This is true</span><span class="err">
</span><span class="p">}</span><span class="err">
</span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"This is also true"</span><span class="p">)</span><span class="w"> </span><span class="c1">#> This is also true</span><span class="err">
</span><span class="p">}</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_logical_operators"><a class="anchor" href="#_logical_operators"></a>Logical Operators</h3>
<div class="paragraph">
<p>Logical operators <code>&&</code> and <code>||</code> do <a href="https://en.wikipedia.org/wiki/Short-circuit_evaluation">short-circuit evaluation</a>: the second argument is evaluated only if the first argument is not enough to determine the value of the expression.</p>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_control_flow"><a class="anchor" href="#_control_flow"></a>Control Flow</h2>
<div class="sectionbody">
<div class="paragraph">
<p>All control flow statements in Agate follow the same principles:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>the condition is between brackets i.e. <code>(</code> and <code>)</code> are mandatory</p>
</li>
<li>
<p>the statements are statement blocks i.e. <code>{</code> and <code>}</code> are mandatory</p>
</li>
</ul>
</div>
<div class="sect2">
<h3 id="_if_statements"><a class="anchor" href="#_if_statements"></a>If Statements</h3>
<div class="paragraph">
<p>The <code>if</code> statement is a <a href="https://en.wikipedia.org/wiki/Conditional_(computer_programming)">conditional statement</a>. The <code>else</code> branch is optional. An <code>else</code> branch may be either a statement block or another <code>if</code> statement.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="k">def</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">42</span><span class="err">
</span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"</span><span class="si">%(</span><span class="n">x</span><span class="si">)</span><span class="s"> is even"</span><span class="p">)</span><span class="w"> </span><span class="c1">#> 42 is even</span><span class="err">
</span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"</span><span class="si">%(</span><span class="n">x</span><span class="si">)</span><span class="s"> is odd"</span><span class="p">)</span><span class="err">
</span><span class="p">}</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_once_statements"><a class="anchor" href="#_once_statements"></a>Once Statements</h3>
<div class="paragraph">
<p>The <code>once</code> statement is a degenerate loop statement where the loop is executed exactly once. It is an idiomatic way to create a new scope, with the additional property that it is a loop like any other, so it can be interrupted.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="k">once</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"Hello"</span><span class="p">)</span><span class="w"> </span><span class="c1">#> Hello</span><span class="err">
</span><span class="w"> </span><span class="k">break</span><span class="err">
</span><span class="w"> </span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"World"</span><span class="p">)</span><span class="err">
</span><span class="p">}</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>See also: <a href="#_interrupting_a_loop">Interrupting a Loop</a></p>
</div>
</div>
<div class="sect2">
<h3 id="_loop_statements"><a class="anchor" href="#_loop_statements"></a>Loop Statements</h3>
<div class="paragraph">
<p>The <code>loop</code> statement creates an <a href="https://en.wikipedia.org/wiki/Infinite_loop">infinite loop</a>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="k">def</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="err">
</span><span class="k">loop</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="err">
</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="mi">2</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="k">break</span><span class="err">
</span><span class="w"> </span><span class="p">}</span><span class="err">
</span><span class="w"> </span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="err">
</span><span class="p">}</span><span class="err">
</span><span class="c1">#> 1</span><span class="err">
</span><span class="c1">#> 2</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>See also: <a href="#_interrupting_a_loop">Interrupting a Loop</a></p>
</div>
</div>
<div class="sect2">
<h3 id="_while_statements"><a class="anchor" href="#_while_statements"></a>While Statements</h3>
<div class="paragraph">
<p>The <code>while</code> statement is a <a href="https://en.wikipedia.org/wiki/While_loop">loop statement</a> that allows to loop over statements while a condition is true.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="k">def</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">27</span><span class="err">
</span><span class="k">def</span><span class="w"> </span><span class="n">steps</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="err">
</span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mi">2</span><span class="err">
</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="err">
</span><span class="w"> </span><span class="p">}</span><span class="err">
</span><span class="w"> </span><span class="n">steps</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">steps</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="err">
</span><span class="p">}</span><span class="err">
</span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="n">steps</span><span class="p">)</span><span class="w"> </span><span class="c1">#> 111</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>See also: <a href="#_interrupting_a_loop">Interrupting a Loop</a></p>
</div>
</div>
<div class="sect2">
<h3 id="_for_statements"><a class="anchor" href="#_for_statements"></a>For Statements</h3>
<div class="paragraph">
<p>The <code>for</code> statement is a <a href="https://en.wikipedia.org/wiki/Foreach_loop">loop statement</a> that allows to loop over a sequence. It has three components:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>a variable name to bind, a new variable with that name with the loop scope is created</p>
</li>
<li>
<p>a sequence to iterate that is evaluated once before the loop starts</p>
</li>
<li>
<p>a body that is a statement block</p>
</li>
</ul>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">direction</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="p">[</span><span class="w"> </span><span class="s">"north"</span><span class="p">,</span><span class="w"> </span><span class="s">"east"</span><span class="p">,</span><span class="w"> </span><span class="s">"south"</span><span class="p">,</span><span class="w"> </span><span class="s">"west"</span><span class="w"> </span><span class="p">])</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="n">direction</span><span class="p">)</span><span class="err">
</span><span class="p">}</span><span class="err">
</span><span class="c1">#> north</span><span class="err">
</span><span class="c1">#> east</span><span class="err">
</span><span class="c1">#> south</span><span class="err">
</span><span class="c1">#> west</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>The sequence can be an <a href="Array.html"><code>Array</code></a>, a <a href="Range.html"><code>Range</code></a>, a <a href="Map.html"><code>Map</code></a>, a <a href="String.html"><code>String</code></a> or any other object that implements the iterator protocol.</p>
</div>
<div class="paragraph">
<p>See also: <a href="#_interrupting_a_loop">Interrupting a Loop</a></p>
</div>
</div>
<div class="sect2">
<h3 id="_iterator_protocol"><a class="anchor" href="#_iterator_protocol"></a>Iterator Protocol</h3>
<div class="paragraph">
<p>The iterator protocol is the protocol used by <code>for</code> statements. A sequence used in a for statement must implement two functions:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>iterate(iterator)</code>: it takes an iterator or <code>nil</code> at the first iteration and returns an iterator to the next item of the sequence or <code>nil</code> if the sequence is finished.</p>
</li>
<li>
<p><code>iterator_value(iterator)</code>: it takes an iterator and returns the value of the item in the sequence that is pointed by the iterator.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The iterator can be any type.</p>
</div>
<div class="paragraph">
<p>Then the following statement:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">variable</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">sequence</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="n">statements</span><span class="err">
</span><span class="p">}</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>is translated to the following:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="k">def</span><span class="w"> </span><span class="vm">$sequence</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">sequence</span><span class="err">
</span><span class="k">def</span><span class="w"> </span><span class="vm">$iterator</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">nil</span><span class="err">
</span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="vm">$iterator</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vm">$sequence</span><span class="p">.</span><span class="n">iterate</span><span class="p">(</span><span class="vm">$iterator</span><span class="p">))</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="k">def</span><span class="w"> </span><span class="n">variable</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vm">$sequence</span><span class="p">.</span><span class="n">iterator_value</span><span class="p">(</span><span class="vm">$iterator</span><span class="p">)</span><span class="err">
</span><span class="w"> </span><span class="n">statements</span><span class="err">
</span><span class="p">}</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>where <code>$sequence</code> and <code>$iterator</code> are internal variables that you cannot use directly.</p>
</div>
</div>
<div class="sect2">
<h3 id="_interrupting_a_loop"><a class="anchor" href="#_interrupting_a_loop"></a>Interrupting a Loop</h3>
<div class="paragraph">
<p>There are two statements to interrupt a loop.</p>
</div>
<div class="paragraph">
<p>The <code>break</code> statement exits the loop early.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="p">[</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="w"> </span><span class="p">])</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="c1">#> 1</span><span class="err">
</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">3</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">#> 2</span><span class="err">
</span><span class="w"> </span><span class="k">break</span><span class="w"> </span><span class="c1">#> 3</span><span class="err">
</span><span class="w"> </span><span class="p">}</span><span class="err">
</span><span class="p">}</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>The <code>continue</code> statement jumps to the next iteration of the loop.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="p">[</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="w"> </span><span class="p">])</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">2</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">#> 1</span><span class="err">
</span><span class="w"> </span><span class="k">continue</span><span class="w"> </span><span class="c1">#> 3</span><span class="err">
</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="c1">#> 4</span><span class="err">
</span><span class="w"> </span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="err">
</span><span class="p">}</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_assert_statements"><a class="anchor" href="#_assert_statements"></a>Assert Statements</h3>
<div class="paragraph">
<p>The <code>assert</code> statement is useful for defensive programming. <a href="https://en.wikipedia.org/wiki/Assertion_(software_development)">Assertions</a> can prevent programming errors. In Agate, there are composed of a conditional expression that must be satisfied, and a string explaining the error in case the expression is false. The expression is verified and if false, the message is printed and the script stops with a runtime error.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="k">def</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">3</span><span class="err">
</span><span class="k">assert</span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="nc">Int</span><span class="p">,</span><span class="w"> </span><span class="s">"</span><span class="si">%(</span><span class="n">n</span><span class="si">)</span><span class="s"> should be an Int"</span><span class="p">)</span></code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_variables"><a class="anchor" href="#_variables"></a>Variables</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_definition"><a class="anchor" href="#_definition"></a>Definition</h3>
<div class="paragraph">
<p>New variables are created using the <code>def</code> keyword followed by the name of the variable. By default, they are initialized to <code>nil</code> unless they are explicitely initialized with <code>=</code> followed by an expression.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="k">def</span><span class="w"> </span><span class="n">anakin</span><span class="err">
</span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="n">anakin</span><span class="p">)</span><span class="w"> </span><span class="c1">#> nil</span><span class="err">
</span><span class="k">def</span><span class="w"> </span><span class="n">luke</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">5</span><span class="p">.</span><span class="mi">4</span><span class="err">
</span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="n">luke</span><span class="p">)</span><span class="w"> </span><span class="c1">#> 5.4</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_assignment"><a class="anchor" href="#_assignment"></a>Assignment</h3>
<div class="paragraph">
<p>Variables can be assigned a new value with the <code>=</code> operator. The <code>=</code> operator is right associative. An assignment is an expression whose value is the result of the right side of <code>=</code>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="k">def</span><span class="w"> </span><span class="n">a</span><span class="err">
</span><span class="k">def</span><span class="w"> </span><span class="n">b</span><span class="err">
</span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">42</span><span class="err">
</span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="n">a</span><span class="p">)</span><span class="w"> </span><span class="c1">#> 42</span><span class="err">
</span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">69</span><span class="err">
</span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="n">a</span><span class="p">)</span><span class="w"> </span><span class="c1">#> 69</span><span class="err">
</span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="n">b</span><span class="p">)</span><span class="w"> </span><span class="c1">#> 69</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_scope"><a class="anchor" href="#_scope"></a>Scope</h3>
<div class="paragraph">
<p>A variable life starts at its definition and ends when exiting the scope where it is defined. It is not possible to use a variable outside its lifetime. It is an error to define a variable with the same name in the same scope. It is possible to define a variable in an inner scope, this is called <a href="https://en.wikipedia.org/wiki/Variable_shadowing">variable shadowing</a> and it is strongly discouraged.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="k">def</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">42</span><span class="err">
</span><span class="k">once</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="k">def</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">69</span><span class="err">
</span><span class="w"> </span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="n">a</span><span class="p">)</span><span class="w"> </span><span class="c1">#> 69</span><span class="err">
</span><span class="p">}</span><span class="err">
</span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="n">a</span><span class="p">)</span><span class="w"> </span><span class="c1">#> 42</span></code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_classes"><a class="anchor" href="#_classes"></a>Classes</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Every value is an object, and every object is an instance of a class. For example, <code>true</code> and <code>false</code> are instances of the <a href="Bool.html"><code>Bool</code></a> class.</p>
</div>
<div class="paragraph">
<p>New classes can be created with the <code>class</code> keyword.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="k">class</span><span class="w"> </span><span class="n">Entity</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="p">}</span></code></pre>
</div>
</div>
<div class="sect2">
<h3 id="_methods"><a class="anchor" href="#_methods"></a>Methods</h3>
<div class="paragraph">
<p><a href="https://en.wikipedia.org/wiki/Method_(computer_programming)">Methods</a> describe the behaviour of the objects of a class. Methods are defined inside the class scope, they may have zero, one or more arguments.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="k">class</span><span class="w"> </span><span class="n">Entity</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="n">update</span><span class="p">(</span><span class="n">dt</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"Update the state of the entity"</span><span class="p">)</span><span class="err">
</span><span class="w"> </span><span class="p">}</span><span class="err">
</span><span class="w"> </span><span class="n">render</span><span class="p">(</span><span class="n">target</span><span class="p">,</span><span class="w"> </span><span class="n">state</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"Render to target using state"</span><span class="p">)</span><span class="err">
</span><span class="w"> </span><span class="p">}</span><span class="err">
</span><span class="p">}</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>Several methods can have the same name as long as they do not have the same number of parameters. This means Agate support <a href="https://en.wikipedia.org/wiki/Function_overloading">overload</a> by arity. In fact, two different methods must have different <em>signatures</em>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="k">class</span><span class="w"> </span><span class="n">Entity</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="n">render</span><span class="p">(</span><span class="n">target</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"Render to target using default state"</span><span class="p">)</span><span class="err">
</span><span class="w"> </span><span class="p">}</span><span class="err">
</span><span class="w"> </span><span class="n">render</span><span class="p">(</span><span class="n">target</span><span class="p">,</span><span class="w"> </span><span class="n">state</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"Render to target using state"</span><span class="p">)</span><span class="err">
</span><span class="w"> </span><span class="p">}</span><span class="err">
</span><span class="p">}</span></code></pre>
</div>
</div>
<div class="sect3">
<h4 id="_signature"><a class="anchor" href="#_signature"></a>Signature</h4>
<div class="paragraph">
<p>The signature of a method is composed of the name of the method with its parameters where the names of the parameters are replaced with <code>_</code>. For example, the signature of the <code>update</code> method in the example above is <code>update(_)</code>. The signatures of the <code>render</code> methods are <code>render(_)</code> and <code>render(_,_)</code>.</p>
</div>
<div class="paragraph">
<p>Signatures are at the heart of Agate method call, so it’s important to understand how they work.</p>
</div>
<table class="tableblock frame-all grid-all stretch">
<caption class="title">Table 2. Signatures of methods</caption>
<colgroup>
<col style="width: 50%;">
<col style="width: 50%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">Method</th>
<th class="tableblock halign-left valign-top">Signatures</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">Simple method</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>name()</code>, <code>name(_)</code>, <code>name(_,_)</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">Getter</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>name</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">Setter</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>name=(_)</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">Prefix operator</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">Infix operator</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@(_)</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">Subscript getter</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>[_]</code>, <code>[_,_]</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">Subscript Setter</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>[_]=(_)</code>, <code>[_,_]=(_)</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">Call operator</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>()</code>, <code>(_)</code>, <code>(_,_)</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">Missing method operator</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>?(_,_)</code></p></td>
</tr>
</tbody>
</table>
</div>
<div class="sect3">
<h4 id="_getter"><a class="anchor" href="#_getter"></a>Getter</h4>
<div class="paragraph">
<p>A getter is a method without any parameters, and no parentheses. Generally, they are used for getting properties of the object without modify the object, and are named with an adjective, contrary to zero-parameter methods that are verbs (and have parentheses) and modify the object.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="k">class</span><span class="w"> </span><span class="n">Entity</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="n">alive</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">true</span><span class="w"> </span><span class="p">}</span><span class="err">
</span><span class="p">}</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>The signature of a getter is just the name of the method. For example, <code>alive</code> is the signature of the method in the previous example.</p>
</div>
</div>
<div class="sect3">
<h4 id="_setter"><a class="anchor" href="#_setter"></a>Setter</h4>
<div class="paragraph">
<p>A setter is a method with a <code>=</code> after its name and exactly one parameter. They are used for setting properties of the object. They are called whenever there is an assignment.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="k">class</span><span class="w"> </span><span class="n">Entity</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="k">construct</span><span class="w"> </span><span class="n">new</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">}</span><span class="err">
</span><span class="w"> </span><span class="n">alive</span><span class="o">=</span><span class="p">(</span><span class="n">value</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"setter: </span><span class="si">%(</span><span class="n">value</span><span class="si">)</span><span class="s">"</span><span class="p">)</span><span class="err">
</span><span class="w"> </span><span class="p">}</span><span class="err">
</span><span class="p">}</span><span class="err">
</span><span class="k">def</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Entity</span><span class="p">.</span><span class="n">new</span><span class="p">()</span><span class="err">
</span><span class="n">a</span><span class="p">.</span><span class="n">alive</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">false</span><span class="w"> </span><span class="c1">#> setter: false</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>The signature of a setter is the name of the method including the <code>=</code> followed by one parameter. For example, <code>alive=(_)</code> is the signature of the method in the previous example.</p>
</div>
</div>
<div class="sect3">
<h4 id="_operators_2"><a class="anchor" href="#_operators_2"></a>Operators</h4>
<div class="paragraph">
<p>Agate supports <a href="https://en.wikipedia.org/wiki/Operator_overloading">operator overloading</a> through operator methods. In fact, expressions with an operator are simply calls to operator methods, even for <a href="Int.html"><code>Int</code></a> or <a href="Float.html"><code>Float</code></a>!</p>
</div>
<div class="paragraph">
<p>A prefix operator method can be defined with the operator name as the method name, like a getter method. The list of the <strong>4</strong> overloadable prefix operators is: <code>+</code>, <code>-</code>, <code>!</code> and <code>~</code>. The signature of a prefix operator method is the name of the operator, e.g. <code>-</code>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="k">class</span><span class="w"> </span><span class="n">Vec</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="k">construct</span><span class="w"> </span><span class="n">new</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">}</span><span class="err">
</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s">"Negate a vector"</span><span class="p">)</span><span class="err">
</span><span class="w"> </span><span class="p">}</span><span class="err">
</span><span class="p">}</span><span class="err">
</span><span class="k">def</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Vec</span><span class="p">.</span><span class="n">new</span><span class="p">()</span><span class="err">
</span><span class="k">def</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">-</span><span class="n">a</span><span class="w"> </span><span class="c1">#> Negate a vector</span></code></pre>