-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
2306 lines (2284 loc) · 123 KB
/
index.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 name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="apple-touch-icon" sizes="57x57" href="/img/apple-icon-57x57.png">
<link rel="apple-touch-icon" sizes="60x60" href="/img/apple-icon-60x60.png">
<link rel="apple-touch-icon" sizes="72x72" href="/img/apple-icon-72x72.png">
<link rel="apple-touch-icon" sizes="76x76" href="/img/apple-icon-76x76.png">
<link rel="apple-touch-icon" sizes="114x114" href="/img/apple-icon-114x114.png">
<link rel="apple-touch-icon" sizes="120x120" href="/img/apple-icon-120x120.png">
<link rel="apple-touch-icon" sizes="144x144" href="/img/apple-icon-144x144.png">
<link rel="apple-touch-icon" sizes="152x152" href="/img/apple-icon-152x152.png">
<link rel="apple-touch-icon" sizes="180x180" href="/img/apple-icon-180x180.png">
<link rel="icon" type="image/png" sizes="192x192" href="/img/android-icon-192x192.png">
<link rel="icon" type="image/png" sizes="32x32" href="/img/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="96x96" href="/img/favicon-96x96.png">
<link rel="icon" type="image/png" sizes="16x16" href="/img/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<meta name="msapplication-TileColor" content="#ffffff">
<meta name="msapplication-TileImage" content="/img/ms-icon-144x144.png">
<meta name="theme-color" content="#ffffff">
<title>Z</title>
<link href="https://fonts.googleapis.com/css?family=Crimson+Text&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link href="/assets/css/prism.css" rel="stylesheet" />
<link rel="stylesheet" href="/assets/css/styles.css">
</head>
<body>
<div class="w3-top" id="#navbar" style="z-index:4;">
<div class="w3-bar w3-border darker-gray">
<a href="#" class="w3-bar-item w3-button"><img src="zlogo.png"></a>
<a href="#intro" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round" style="margin-top:8px;">Docs</a>
<a href="#exs" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round" style="margin-top:8px;">Examples</a>
<a href="#stdlib" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round" style="margin-top:8px;">Standard Library</a>
<a href="#contribute" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round" style="margin-top:8px">Contribute</a>
<a href="https://github.com/zlanguage/zcomp" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round" style="margin-top:8px" target="_blank">GitHub</a>
<a href="https://www.reddit.com/r/zlanguage/" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round" style="margin-top:8px" target="_blank">Reddit</a>
<button class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round" style="margin-top:8px" id="menu"><i class="fa fa-bars"></i></button>
</div>
</div>
<br>
<br>
<div style="height:14px;"></div>
<div class="w3-row">
<div class="w3-third w3-container" style="padding:0px;" id="nav">
<div class="w3-sidebar w3-light-gray w3-bar-block" style="overflow-y:scroll; z-index: 4;">
<a href="#intro" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">Introduction</a>
<a href="#getstarted" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">Get Started</a>
<a href="#bexpr" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">Basic Expressions and Math</a>
<a href="#mexpr" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">More Expressions</a>
<a href="#files" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">Your First File</a>
<a href="#vars" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">Variables</a>
<a href="#objs" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">Invocations, Arrays, and Objects</a>
<a href="#ctrlf" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">Control Flow</a>
<a href="#fintro" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">Intro to Functions</a>
<a href="#except" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">Exceptions</a>
<a href="#mds" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">Modules</a>
<a href="#pmatch" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">Pattern Matching</a>
<a href="#rtypes" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">Runtime Types</a>
<a href="#loopexpr" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">Loop Expressions</a>
<a href="#ops" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">Operators</a>
<a href="#macros" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">Macros</a>
<a href="#enums" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">Enums</a>
<a href="#acmds" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">Advanced Compiler Commands</a>
<a href="#rdoc" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">Runtime Overview</a>
<a href="#stdlib" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">Standard Library</a>
<a href="#template" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">Template</a>
<a href="#tuple" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">Tuple</a>
<a href="#uni" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">Unicode Support</a>
<a href="#fp" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">Functional Programming</a>
<a href="#con" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">Concurrency</a>
<a href="#nums" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">More Numbers</a>
<a href="#scrape" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">Web Scraping</a>
<a href="#edu" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">EDU Modules</a>
<a href="#imp" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">Imperative Macros</a>
<a href="#exs" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">Examples</a>
<a href="#why" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">The Why of Z</a>
<a href="#contribute" class="w3-bar-item w3-button w3-hover-blue w3-btn w3-round">Contribute</a>
<br>
<br>
<br>
</div>
</div>
<div class="w3-rest w3-container w3-padding" style="margin-left:200px;" id="content">
<div class="w3-overlay" id="overlay" style="cursor:pointer"></div>
<div id="intro">
<img src="zlogodark.png" style="margin-left: calc(50% - 102px);">
<h3 style="text-align:center"><em>Where your <a href="https://github.com/zlanguage/zcomp">fantaZies</a> come true</em></h3>
<p>
Why create a new programming language? <br> If you want to know why Z is designed the way it is, go <a href="#why">here</a>. But if you want want to know why Z was built in the first place, keep reading. Transpile to JS is pretty popular
these days. JS has become the de facto assembely of the web, because virtually no one codes in "Vanilla JS" these days. A plethora of languages (CoffeeScript, LiveScript, Elm, ClojureScript, TypeScript) have all emerged on the sole
basis of compiling to JS. However, we can seperate the languages that transpile to JS into two categories:
<h3>1. Syntatic Sugar</h3>
<p>Languages that add new compile-time features to JavaScript, but no knew runtime features. These languages interop with JavaScript nearly seemlessly, but they fail to deliever certain features that require runtime additions, including
operator overloading, pattern matching, better concatenation rules, etc.
</p>
<h4>Examples:</h4>
<ul>
<li>CoffeeScript</li>
<li>LiveScript</li>
<li>ESNext + Babel</li>
<li>TypeScript</li>
</ul>
<h3>2. JVM Languages (JavaScript Virtual Machine)</h3>
<p>These languages add new compile-time and run-time features to JS, and use JavaScript more like assembely code than a target language. While these languages can add any features they want, they often have limited and clunky interop
with JS, and are all are limited to the frontend, whereas Syntatic Sugar languages can be used anywhere.
</p>
<h4>Examples:</h4>
<ul>
<li>Elm</li>
<li>Haxe</li>
<li>ClojureScript</li>
</ul>
Z mixes these two sides of transpiled languages. It has it's own runtime, with custom operators, a standard library, goroutines, and other cool out-of-the-box features. However, it also can call any JavaScript method, uses the same APIs as JavaScript,
and has a generally similar syntax, making it easy for JavaScript users to pick up. Z is also one of the only langauges that transpiles to JS, comes with a custom runtime, and functions on the backend!<br> So what
are you waiting for? Are you ready to transform your backend experience with amazing features <em>and</em> a cool runtime and standard library? Jump in and learn some Z!
</p>
<p class="footnote">*Z is still in rapid development. There may be bugs that pop up in the Z Compiler as you develop your application. Report them <a href="https://github.com/zlanguage/zcomp/issues">here</a>.</p>
<p class="footnote">If you still have any problems with Z, or want any new features added, email <a href="mailto:[email protected]">[email protected]</a>.</p>
</div>
<div id="getstarted">
<h1>Getting Started</h1>
<p class="footnote">This tutorial assumes that you have both node and npm installed. If you don't, you can install node <a href="https://nodejs.org/en/download/" target="_blank">here</a> and npm comes with node. If you prefer Yarn (the faster, prettier alternative,
you can download it <a href="https://yarnpkg.com">here</a>)</p>
<p>To start, enter a terminal session. Every Z package on npm is namespaced under <code>@zlanguage</code>. Install the Z compiler as so (make sure to install version 0.4.5, as it is the last stable release):</p>
<pre>$ npm install -g @zlanguage/[email protected]</pre> Now, in order for the compiler to function, you must also install <code>globby</code>:
<pre>$ npm install -g globby</pre>
<p>Or, with Yarn:</p>
<pre>$ yarn global add @zlanguage/[email protected] globby</pre>
<p>That wasn't too hard! Now, to experiment with Z, let's launch a REPL.</p>
<p>You can launch a Z REPL with:</p>
<pre>$ zcomp repl</pre>
<p>If all goes well, you should see the following:</p>
<pre>zrepl></pre>
</div>
<div id="bexpr">
<h1>Basic Expressions and Math</h1>
<p class="footnote">All expressions defined here are meant to be executed from a REPL.</p>
<p>Let's start by creating a simple math expression:</p>
<pre lang="z">3 + 2</pre>
<p>The REPL will print back 5.</p>
<p>Order of operations works:</p>
<pre lang="z">3 + 2 * 7</pre>
<p>The REPL gives you back 17, not 35.</p>
<p>The following mathematical operators are supported at the following orders of precedence:</p>
<p><code>^ pow</code></p>
<p><code>* / %</code></p>
<p><code>+ -</code></p>
<p class="footnote"><code>pow</code> is the same as <code>^</code> except that <code>^</code> is left-associative while <code>pow</code> is right-associative.</p>
<h3>Number Extensions</h3>
<p>Besides typical JavaScript number literals, Z supports the following extensions to them: A number can have:</p>
<ul class="w3-large">
<li><code>1_000</code> Underscores</li>
<li><code>0x100</code> Hexadecimal, Binary, and Octal prefixes!</li>
<li><code>10bytes</code> Trailing characters</li>
<li><code>1.somePropertyHere</code> Trailing refinement</li>
</ul>
</div>
<div id="mexpr">
<h1>More Expressions</h1>
<p>To start, you may have noticed that inputting a raw number without any math is considered an error in the Z REPL. While this may seem peculiar, this is to avoid "useless expressions", like randomly putting a string or a number on some
code.
</p>
<p>Single line Comments in Z are denoted with <code>#</code>:</p>
<pre lang="z"># I'm a single line comment!</pre>
<p>Block comments are denoted with <code>/*</code> and <code>*/</code></p>
<p>Strings in Z are easy enough to reason with:</p>
<pre lang="z">"Hello" ++ " World" # ===> "Hello World"</pre>
<p>Z performs coercion, but its coercion rules make more sense than JavaScript's:</p>
<pre lang="z">
"9" + 2 # ==> 11
"9" ++ 2 # ==> "92"</pre>
<p>Booleans also make sense:</p>
<pre lang="z">
true and false # ==> false
false or true # ==> true
</pre>
<p>Symbol literals are denoted with @, so <code>@x</code> is the same as <code>Symbol.for("x")</code>, and <code>@@iterator</code> is the same as <code>Symbol.iterator</code>.</p>
<p>Regexps literals are like JavaScript, but they are denoted with <code>`</code> rather than <code>/</code>. So <code>`x`g</code> is the same as <code>/x/g</code></p>
<p>Now that you have touched the surface of Z, it's time to take it up a notch and start writing files in Z.</p>
</div>
<div id="files">
<h1>Your First File</h1>
<p>Now that you've tested Z out, create a directory, call it ztest:</p>
<pre>$ mkdir ztest</pre>
<p>For each directory you create that will use Z, you must install the Z Standard Library:</p>
<pre>$ npm install @zlanguage/zstdlib --save</pre>
<p>That really wasn't that much setup.</p>
<p>Now, create a new file, call it <code>hello-world.zlang</code>:</p>
<pre>$ touch hello-world.zlang</pre>
<p>Launch your favorite IDE, open helloworld.zlang, and type:</p>
<pre lang="z">log("Hello World")</pre>
<p>Execute the file with:</p>
<pre>$ zcomp run helloworld.zlang</pre>
<p>It should print out <code>Hello World</code> to the terminal.</p>
<p>Files can hold more advanced expressions than the REPL, and have statements in them two. From now on, all examples assume that they are being typed <em>in a file</em>. Some examples will contain features that don't work in the REPL.
</p>
</div>
<div id="vars">
<h1>Variables</h1>
<p>Variables in Z can hold any value, they are not constrained to one type of value.</p>
<p>Reassignable variables can be declared with <code>let</code>:</p>
<p>Note that <code>:</code> is the assignment operator.</p>
<pre lang="z">
let x: 5 # ==> x is now 5
x: "Hola Mundo" # ==> x has changed types
let y: undefined # ==> You must assign a variable when you initialize it.
y: Math.random() # ==> Put something in y
</pre>
<p>Constant variables are declared with <code>def</code>. Constant variables cannot be reassigned, but their internal value can still change:</p>
<pre lang="z">
def x: 5 # ==> This looks familiar.
x: "Hola Mundo" # ==> Runtime error.
</pre>
<p>Finally, hoisted variables (akin to variables declared with <code>var</code> in JavaScript) are declared with <code>hoist</code>:</p>
<pre lang="z">
log(x) # I can see x from all the way up here!
hoist x: 5
</pre>
<p>So to map Z's assignment statements to their equivalents in JS:</p>
<ul>
<li><code>let</code> - <code>let</code></li>
<li><code>def</code> - <code>const</code></li>
<li><code>hoist</code> - <code>var</code></li>
</ul>
</div>
<div id="objs">
<h1>Invocations, Arrays, and Objects</h1>
<p>You've already seen some invocations in Z (of <code>log</code> and <code>Math.random</code>) </p>
<p>As will all built-in data types in Z, Z functions map right to their JavaScript equivalents. Which means calling a function in Z with <code>()</code> transpiles to calling a function in JavaScript with <code>()</code>:</p>
<pre lang="z">
log("Hola mundo!") # Log acts like console.log.
console.log("Hola mundo!") # Does the same thing.
Math.random() # Let's call another JS function
Date.now() # They all work fine!</pre>
<h3>Collections in Z</h3>
<p>Z supports numerous flexible collection literals to represent objects and arrays, the simplest being brackets:</p>
<pre lang="z">
[1, 2, 3] # Arrays literals are just like JavaScript
let x: 5
[
"hola": "mundo", # Objects are a bit different, object properties in Z are computed, so quotes are required.
x # Expands to "x": 5 if there are other properties in the object
]
[] # Empty Array
[:] # Empty Object</pre>
<p>Parentheses can also be used to denote arrays, and brackets can denote arrays or objects. Arrays constructed from parentheses and objects constructed from brackets can be used in destructuring (which will be covered later):</p>
<pre lang="z">
(1, 2, 3) # Array Literal
{1, 2, 3} # Array Literal
{
"x": 3
} # Object Literal
() # Empty Array Literal
{} # Empty Array Literal
</pre>
<p>Range literals correspond to arrays. They can be written in several different fashions:</p>
<pre lang="z">
1 to 5 # [1, 2, 3, 4, 5]
1...5 # [1, 2, 3, 4, 5]
1 til 5 # [1, 2, 3, 4]
1 to 5 by 2 # [1, 3, 5]
1 til 5 by 2 # [1, 3]
</pre>
<pre lang="z"></pre>
<p>When creating range literals, you can </p>
<p>When invoking a function, you can emulate named parameters with an implicit object (like in ruby):</p>
<pre lang="z">
Point(x: 3, y: 4) # Is like...
Point([
"x": 3,
"y": 4
])</pre>
<p>Property Access is akin to JavaScript:</p>
<pre lang="z">
x.y # Alphanumeric property access
x["y"] # Computed property access (any expression can go in the brackets)
x..y # Conditional property access (only works if x isn't falsy, akin to x && x.y)</pre>
</div>
<div id="ctrlf">
<h1>Control Flow</h1>
<p>Z supports very simple control flow. It may seem primitive, but for most imperative constructs, it's all you need. When coding Z procedurally (rather than functionally) these control flow statements will be your best friends.
</p>
<p>Z supports typical <code>if</code>, <code>else if</code>, and <code>else</code> statements:</p>
<pre lang="z">
let x: 3
if x = 3 { # Check for equality
log("Three times is the charm!")
} else if x > 0 { # Check for positivity
log("X is positive.")
} else {
log("X is feeling negative today.")
}</pre>
<p>Z also has the ternary operator, though its syntax is more readable than most programming languages:</p>
<pre lang="z">
# An easier way to write the above example
let x: 3
log(
if (x = 3) "Three times is the charm!"
else if (x > 0) "X is positive."
else "X is feeling negative today."
)</pre>
<p>You can simplify one line if statements by putting the <code>if</code> at the end of the line (Ruby-style):</p>
<pre lang="z">
log("I'm feeling lucky!") if Math.random() > 0.5
</pre>
<p>In the looping department, Z supports <code>loop</code>, which is the equivalent of a <code>while(true)</code> loop. You exit a loop with <code>break</code>:</p>
<pre lang="z">
let i: 0
loop {
if i > 9 {
break
}
log(i)
i: i + 1
}</pre>
</div>
<p>That's it. No fancy potpourri. Just one conditional structure and one type of loop. However, this section only covered Z's <em>imperative</em> control flow structures. You'll see more <em>functional</em> ones soon.</p>
<div id="fintro">
<h1>Intro to Functions</h1>
<p>Functions in Z are created with the <code>func</code> keyword. Z supports anonymous functions only, like CoffeeScript. You can name functions by binding a function to a constant variable. Otherwise, parameters and return statements are
rather similar:</p>
<pre lang="z">
def add: func (x, y) {
return x + y
} # Declare a simple function
setTimeout(func () {
log("Around one second has passed!")
}, 1000) # Passing a function as a parameter
def something: func () {
# Return something awesome
}() # IIFE
def boundFunction: func () {
}.bind(someThisValue) # Functions can have properties accessed on them because they are just objects will an internal [[Call]] property
</pre>
<p>Default parameters are created with the <code>:</code> operator, and rest parameters are created with the <code>...</code> operator.</p>
<pre lang="z">
def add: func (x: 0, y: 0) { # Defaults
return x + y
}
def sum: func (...xs) {
return xs.reduce(func (t, v) { # We'll make this example more concise later.
return t + v
})
}</pre>
<p>If a function only consists of one return statement, the curly brackets and <code>return</code> may be omitted:</p>
<pre lang="z">def sum: func (...xs) xs.reduce(func (t, v) t + v)</pre>
<p>You can mark variables declared within a one-line function (a function with an implicit return statement) to be inferred, by ending them with an exclamation point:</p>
<pre lang="z">def sum: func (...xs) xs.reduce(func t! + v!) # We'll see how to make this even more concise later.</pre>
<p>You can pipe a value through multiple functions via <code>|></code>:</p>
<pre lang="z">3 |> +(1) |> *(2) |> log # Logs 8</pre>
<p>You can use <code>>></code> and <code><<</code> for function composition (as in Elm).</p>
<p>You can partially apply functions with <code>@</code></p>
<pre lang="z">
def square: Math.pow(@, 2)
def logSquare: log >> square
logSquare(10) # Logs 100</pre>
<p>A standalone <code>.</code> creates an implied function for property access and method invocations. Currently, implied functions and partial application via <code>@</code> cannot be mixed. For example:</p>
<pre lang="z">
users.map(.name) # Get the name property of users
[1, 2, 3, 4, 5].map(.toString(2)) # Get the binary representation of these numbers (in string form).
</pre>
<p>That's pretty much all there is to know about basic functions in Z.</p>
</div>
<div id="except">
<h1>Exceptions</h1>
<p>The first thing Z clarifies is that <em>Exceptions are not baseballs</em>. For some reason, rather than "raising" an issue, you would "throw" it. That makes no sense at all. And then, to resolve the issue, someone would not "settle" it,
but "catch" it. You can't play a friendly game of catch with exceptions. Z's choice of keywords is more intuitive than <code>throw</code> and <code>catch</code>:</p>
<pre lang="z">
try { # Attempt to do something
raise "Something contrived went wrong" # String coerced into an error object
} on err { # When something bad happens
settle err # Explicitly tell Z that the error has been settled/resolved.
}</pre>
<p>At this point you are probably asking: why explicitly settle an error? The reason is, explicitly settling an error allows you to put time and thought into how to settle it, and what countermeasures to take. If you forget to settle an error,
Z will raise a runtime error. This helps with making Plan Bs when something goes wrong.
</p>
<p>A try can only have one <code>on</code> clause. Handle type checking of the exception in the <code>on</code> clause.</p>
<p class="footnote">Z has exception handling for JavaScript interop, but please don't overuse it. Failing to parse an integer should not cause an exception.
</p>
</div>
<div id="mds">
<h1>Modules</h1>
<p>Z's module system is closely related to JavaScript's. A simple example will demonstrate this. Create a file called <code>exporter.zlang</code> in your test directory, and another file called <code>importer.zlang</code> in that same directory.
Now, in <code>exporter.zlang</code>, type:
</p>
<pre lang="z">export 3</pre>
<p>In <code>importer.zlang</code>, type:</p>
<pre lang="z">
import num: "./exporter"
log(num)</pre>
<p>Now, to transpile <code>exporter.zlang</code>, and not immediately run it via the compiler, use the command:</p>
<pre>$ zcomp transpile exporter.zlang</pre>
<p>And:</p>
<pre>$ zcomp transpile importer.zlang</pre>
<p>To run the code:</p>
<pre>$ node importer.zlang</pre>
<p>You should see a 3 printed out.</p>
<p>To further elaborate, each module in Z can export one thing, which is implicitly stoned (Z's version of <code>Object.freeze</code>) when exported.
</p>
<p>Imports in Z are similar to JavaScript ones, except that <code>from</code> is replaced with <code>:</code>:</p>
<pre lang="z">
import something: "./somewhere"
import fs # This shorthand becomes:
import fs: "fs"
import ramda.src.identity # Becomes:
import identity: "rambda/src/identity"</pre>
<p>In order to export multiple things, you can just export an object:</p>
<pre lang="z">
export [
"something": cool,
"very": cool,
cool,
"some": other.thing
]</pre>
<p>As you can see, Z modules are (pretty) easy to work with. We'll see a cool way to import multiple things from a module that exports an object in the next section.</p>
</div>
<div id="pmatch">
<h1>Pattern Matching</h1>
<p>Z comes with a default ternary operator:</p>
<pre lang="z">
let happy = true
let mood = if (happy) "good" else "bad" # if (cond) result2 else result2
let moodMessage =
if (mood = "good") "My mood is good."
else if (mood = "bad") "I'm not feeling good today."
else "Unknown mood." # Chaining ternary operators.</pre>
<p>However, for advanced conditional checks, this fails to be sufficient. That's where Z's pattern matching comes into play. The <code>match</code> expression at its simplest can match simplest can match simple values:
</p>
<pre lang="z">
let moodMessage = match mood {
"good" => "My mood is good",
"bad" => "My mood is bad",
_ => "Unknown mood" # _ is a catch-all
}</pre>
<p>Patten matching is more powerful than this though. It's not limited to matching primitives. You can also match exact values that are arrays and objects:</p>
<pre lang="z">
let whatItIs: match thing {
[1, 2, 3] => "An array of [1, 2, 3]",
["x": 3] => "An object with an x value of 3",
_ => "I don't know what thing is."
}</pre>
<p>You can also match types with pattern matching:</p>
<pre lang="z">
let contrived: match someExample {
number! => "A number.",
string! => "A string.",
array! => "An array",
_ => "Something else."
}</pre>
<p>If you want to capture the value of a certain type, use <code>!</code> like an infix operator:</p>
<pre lang="z">
let times2: match thing {
number!n => n * 2,
string!s => s ++ s,
_ => [_, _]
}</pre>
<p>Now, to capture elements of arrays, use <code>(</code> and <code>)</code>:</p>
<pre lang="z">
def arrResult: match arr {
(number!) => "An array that starts with a number.",
(string!s, string2!s2) => s ++ s2,
(x, ...xs) => xs, # xs represents the rest of the array, which excludes the first element in the array
_ => []
}</pre>
<p>Objects can be matched with the <code>{</code> and <code>}</code> characters:</p>
<pre lang="z">
def objResult: match obj {
{ x: number!, y: number! } => "A point-like object.", # Match an objects with x and y properties
{
name: string!name,
age: number!age,
car: {
cost: number!,
brand: string!brand
}
} => "A person named " ++ name ++ " that is " ++ age ++ " years old. He/She owns a " ++ brand ++ " type of car.",
_ => "Some other thing"
}</pre>
<p>To match a number in between other numbers, use range literals:</p>
<pre lang="z">
def typeOfSquare: match square {
{ size: 1...10 } => "A small square.",
{ size: 11...20 } => "A medium square.",
{ size: number! } => "A big square.",
_ => "Something else."
}</pre>
<p>The object and array delimiters in pattern matching work as destructuring too: </p>
<pre lang="z">
def (x, y): [3, 4] # x is 3, y is 4
def {x, y}: [
"x": 3,
"y": 4
] # x is 3, y is 4
</pre>
<p>You can define <code>blocks</code> to be associated with different patterns, for example:</p>
<pre lang="z">
match num {
1 => {
log("Num is 1.")
log ("I love the number 1.") # You can put multiple lines in a "block"
return "YAY!" # Blocks are wrapped into functions, so you can return from them.
},
_ => "Not 1 :("
}
</pre>
<p>You can define your own custom pattern matching conditions with <code>predicates</code>. To start, define some functions that return a booleans:</p>
<pre lang="z">
def even: func x! % 2 = 0
def odd: func x! % 2 = 1</pre>
<p>Then, use the <code>?</code> at the end of the function name inside a <code>match</code> body to use the predicate:</p>
<pre lang="z">
match num {
even? => "An even number.",
odd? => "An odd number.",
number! => "Some other number.",
_ => "Something else."
}
</pre>
<p>The most advanced form of custom pattern matching is the <code>extractor</code>. It allows you to not only perform a conditional check on data, but to perform custom matching on it.</p>
<p>Let's start by defining a simple email function:</p>
<pre lang="z">
def Email: func user! ++ "@" ++ domain!
</pre>
<p>Then, we can defined a <code>extract</code> method on <code>email</code>. This <code>extract</code> method should return an array if there is a pattern to be matched, or <code>undefined</code>, if there is no match:</p>
<pre lang="z">
Email.extract: func (str) if (str.includes("@")) str.split("@") else undefined
</pre>
<pre lang="z">
def myEmail: "[email protected]"
match myEmail {
Email(user, domain) => log(user, domain), # Logs programmer, cloud.com
_ => log("Invalid email.")
}
</pre>
<p>As you can see <code>extractors</code> and <code>predicates</code> add greater flexibility and power to pattern matching.</p>
</div>
<div id="rtypes">
<h1>Runtime Types</h1>
<p>Z supports numerous ways to create runtime type checks. Each object in Z can specify its "type" by having a function called <code>type</code>:</p>
<pre lang="z">
[
"x": 5,
"y": 5,
"type": func "Point"
]</pre>
<p>You can find out something's type using the built-in <code>typeOf</code> function:</p>
<pre lang="z">
typeOf(3) # ==> "number"
typeOf([1, 2, 3]) # ==> "array"
typeOf([
"x": 5,
"y": 5,
"type": func "Point"
]) # ==> "Point"</pre>
<p>You can check that a parameter passed to a function is of a certain type at runtime (checking is done behind the scenes with <code>typeOf</code>):</p>
<pre lang="z">
def add: func (x number!, y number!) { # Note that you can't mix type annotations with default values and rest/spread
def res: x + y
return res
}</pre>
<p><code>!</code> isn't actually part of the type. It just denotes that a type is present.</p>
<p>You can also add return type annotations:</p>
<pre lang="z">
def add: func (x number!, y number!) number! {
def res: x + y
return res
}</pre>
<p>You can also validate that the right-hand side of an assignment is of a certain type:</p>
<pre lang="z">
def add: func (x number!, y number!) number! {
def res number!: x + y
return res
}</pre>
<p>This works great for simple functions, however you may need to implement more complex ones. This is made possible by the <code>enter</code> and <code>exit</code> statements:</p>
<pre lang="z">
def readPrefs: func (prefs string!) {
enter {
prefs.length < 25
}
def fileHandler: file.getHandler(prefs) # Some imaginary file system.
# Do some stuff
return something
exit {
fileHandler.close() # Clean up the file handler, exit is like finally and must be the last statement in a function.
}
}</pre>
<p><code>enter</code> is a block of code that contains comma-separated conditions, all of which must be true when the function starts:</p>
<pre lang="z">
def readBytes(bytestream Bytestream!, amount number!) { # fictional type Bytestream
enter {
bytestream.size < amount,
amount < 100,
amount > 0,
}
# Do stuff...
}</pre>
<p><code>exit</code> pretty much the same as enter, except it is executed at the end of the function, to see if certain conditions have been met. <code>exit</code> must be the last statement in a function.</p>
<p>A function may only have one
<code>enter</code> statement and one
<code>exit</code> statement.</p>
</div>
<div id="loopexpr">
<h1><code>loop</code> Expressions</h1>
<p><code>loop</code> expressions are directly inspired by Scala. They are based on Scala's <code>for</code> expressions, and they may resemble list comprehensions in some languages.</p>
<p>To start, use the operator <code><-</code> to map over a list:</p>
<pre lang="z">
def xs: [1, 2, 3]
def result: loop (x <- xs) x * 2 # Result is [2, 4, 6]</pre>
<p>You can add predicates using a form of <code>if</code>:</p>
<pre lang="z">
def xs: [1, 2, 3]
def result: loop (x <- xs, if x % 2 = 0) x * 2 # Result is [2, 6]</pre>
<p>You can iterate over multiple lists by inserting multiple <code><-</code>s:</p>
<pre lang="z">
# Range literals: 1...5 is [1, 2, 3, 4, 5]
def result: loop (x <- 1...10, y <- 1...10) [x, y] # Matrix of numbers 1 to 10
</pre>
<p>Using all of this, you could define a <code>flatMap</code> function:</p>
<pre lang="z">
def flatMap: func (f, xs) {
return loop (
x <- xs,
y <- f(x)
) y
}</pre>
<p>Note that you cannot start a line with a <code>loop</code> expression, as it will be confused with the imperative <code>loop</code> statement.</p>
<p>The final ability of the <code>loop</code> expression is that you can place assignments in it. For example:</p>
<pre lang="z">
def strs: ["Hello", "World"]
def res: loop (s <- strs, l: s.length) l * 2 # res is [10, 10]</pre>
</div>
<div id="ops">
<h1>Operators</h1>
<p>You've already seen use of plenty of operators in Z. You've seen addition, subtraction, comparison, equality, and more. But for complete reference, below is a list of operators that come with the Z runtime, and their precedence:
</p>
<p>The Left Overload is a method you can define on an object to overload the operator on the left-hand side:
<pre lang="z">x + y</pre> becomes <pre lang="z">x.+(y)</pre> if <code>x</code> defines a <code>+</code> method.
</p>
<p>The Right Overload is a method you can define on an object to overload the operator on the right-hand side:
<pre lang="z">x + y</pre> becomes <pre lang="z">y.r+(x)</pre> if <code>y</code> defines a <code>r+</code> method.
</p>
<table class="w3-table">
<tr>
<th>Operator</th>
<th>Associativity</th>
<th>Precedence</th>
<th>Function</th>
<th>Left Overload</th>
<th>Right Overload</th>
</tr>
<tr>
<td>pow</td>
<td>Right</td>
<td>Infinity</td>
<td>Performs exponentiation</td>
<td>NA (overload * instead)</td>
<td>NA (overload r* instead)</td>
</tr>
<tr>
<td>til</td>
<td>Left</td>
<td>555</td>
<td>Exclusive range</td>
<td>prev & succ & <</td>
<td>NA</td>
</tr>
<tr>
<td>to</td>
<td>Left</td>
<td>555</td>
<td>Inclusive range</td>
<td>prev & succ & <</td>
<td>NA</td>
</tr>
<tr>
<td>by</td>
<td>Left</td>
<td>444</td>
<td>Used to specify the step of ranges</td>
<td>NA</td>
<td>NA</td>
</tr>
<tr>
<td>^</td>
<td>Left</td>
<td>333</td>
<td>Performs exponentiation</td>
<td>NA (overload * instead)</td>
<td>NA (overload r* instead)</td>
</tr>
<tr>
<td>%</td>
<td>Left</td>
<td>222</td>
<td>Performs modulus</td>
<td>%</td>
<td>r%</td>
</tr>
<tr>
<td>/</td>
<td>Left</td>
<td>222</td>
<td>Performs division</td>
<td>/</td>
<td>r/</td>
</tr>
<tr>
<td>*</td>
<td>Left</td>
<td>222</td>
<td>Performs multiplication</td>
<td>*</td>
<td>r*</td>
</tr>
<tr>
<td>+</td>
<td>Left</td>
<td>111</td>
<td>Performs addition</td>
<td>+</td>
<td>r+</td>
</tr>
<tr>
<td>-</td>
<td>Left</td>
<td>111</td>
<td>Performs subtraction</td>
<td>-</td>
<td>r-</td>
</tr>
<tr>
<td>++</td>
<td>Left</td>
<td>111</td>
<td>Performs concatenation</td>
<td>concat</td>
<td>NA</td>
</tr>
<tr>
<td>>></td>
<td>Left</td>
<td>1</td>
<td>Left-to-right composition</td>
<td>NA</td>
<td>NA</td>
</tr>
<tr>
<td><<</td>
<td>Left</td>
<td>1</td>
<td>Right-to-left composition</td>
<td>NA</td>
<td>NA</td>
</tr>
<tr>
<td>|></td>
<td>Left</td>
<td>1</td>
<td>Pipe</td>
<td>NA</td>
<td>NA</td>
</tr>
<tr>
<td><</td>
<td>Left</td>
<td>-111</td>
<td>Less-than</td>
<td><</td>
<td>r<</td>
</tr>
<tr>
<td><=</td>
<td>Left</td>
<td>-111</td>
<td>Less-than or Equal-to</td>
<td>NA (Define < instead)</td>
<td>NA (Define r< instead)</td>
</tr>
<tr>
<td>></td>
<td>Left</td>
<td>-111</td>
<td>Greater-than</td>
<td>NA (Define < instead)</td>
<td>NA (Define r< instead)</td>
</tr>
<tr>
<td>>=</td>
<td>Left</td>
<td>-111</td>
<td>Greater-than or Equal-to</td>
<td>NA (Define < instead)</td>
<td>NA (Define r< instead)</td>
</tr>
<tr>
<td>=</td>
<td>Left</td>
<td>-222</td>
<td>Compares Structural Equality</td>
<td>=</td>
<td>r=</td>
</tr>
<tr>
<td>and</td>
<td>Left</td>
<td>-333</td>
<td>And boolean comparison</td>
<td>NA</td>
<td>NA</td>
</tr>
<tr>
<td>or</td>
<td>Left</td>
<td>-333</td>
<td>Or boolean comparison</td>
<td>NA</td>
<td>NA</td>
</tr>
</table>
<p>The negative precedence and non-consecutive precedence numbers will be explained soon.</p>
<h3>First Class Operators</h3>
<p>Z has first-class operators, meaning the operators aren't special. They can be created, stored in variables, and in fact, are just ordinary functions.</p>
<p><code>+</code> is just defined as an ordinary function! Functions (like <code>+</code>) can then be called with infix syntax (Note that in Z 0.4.0+, operators MUST HAVE ALL SYMBOL NAMES):</p>
<p>However, operators are left associative and can have custom precedence:</p>
<pre lang="z">
def +': func x! + y!
3 +' 4 * 2 # ==> 11 +' has no precedence, defaults to 1, evaluates after multiplication
</pre>
<p>You can define a custom precedence for your operators:</p>
<pre lang="z">
# Continuing from the last example:
operator +': 1000 # Give it a high Precedence
3 +' 4 * 2 # ==> 14
</pre>
<p>Now, all the large precedence numbers should make sense. Operators having large gaps in precedence allows for insertion of operators in between precedence levels.</p>
<p>Since operators are functions, they can be curried. All the built-in operators actually are:</p>
<pre lang="z">3 |> *(2) |> +(1) |> to(1) # [1, 2, 3, 4, 5, 6, 7]</pre>
<p>The following symbol characters are allowed in identifiers: <code>+</code>, <code>-</code>, <code>*</code>, <code>/</code>, <code>^</code>, <code>?</code>, <code><</code>, <code>></code>, <code>=</code>, <code>!</code>, <code>\</code>,
<code>&</code>, <code>|</code>, <code>%</code>, <code>'</code></p>
<p>Since operators are just functions, you can use them like ordinary functions:</p>
<pre lang="z">
# Add function from before:
def add: +
# Sum an array
[1, 2, 3].reduce(+)
</pre>
</div>
<div id="macros">
<h1>Macros</h1>
<p class="footnote"><em>Note that dollar directives have been removed. The new macro system is much more capable.</em></p>
<p>Macros are compile-time "functions" that operate on AST nodes. Let's start by looking at a very simple macro:</p>
<pre lang="z">
macro $hello () {
return ~{
"Hello World"
}~
}
</pre>
<p>There's a lot going on here. First, we define a macro called <code>$hello</code> with the <code>macro</code> keyword. All macros in Z start with the <code>$</code> symbol, because this makes them "pop out" - so you know what's a macro
and what isn't. Then, we return a template, denoted by <code>~{</code> and <code>}~</code> which contains some Z code. This template in this case just contains the string <code>"Hello World"</code>, meaning that any call to this macro
is immediately replaced with <code>"Hello World"</code> at compile time. For example:</p>
<pre lang="z">
log($hello)
</pre>
<p>With the macro above, it will now print "Hello World". Now, macros can also take parameters. This simple <code>$id</code> macro takes one expression and returns it:</p>
<pre lang="z">
macro $id (~x:expr) {
return ~{
{{~x}}
}~
}
</pre>
<p>You can see what happens. The tilde denotes a macro parameter, which in this case is of type <code>expr</code>. You call this parameter <code>x</code>. Then, when you return a template, you use double braces and the tilde operator again,
to "spread" the value of <code>x</code> into the template. For example:</p>
<pre lang="z">
log($id [1, 2, 3])
</pre>
<p>It just logs what you passed to the macro. However, parameters passed to any macro are also available in AST form. For example:</p>
<pre lang="z">
macro $addFour (~arr:expr) {
arr.push(4)
return ~{
{{~arr}}
}~
}
log($addFour [1, 2, 3])
</pre>
<p>It will log <code>[1, 2, 3, 4]</code>. However, because AST nodes are passed to macro by reference, you're not allowed to reassign them without loosing the reference. So <code>arr: arr.concat(4)</code> would make not change the value of
the AST node.</p>
<p>You can also pass blocks to macros - this makes for a natural looking syntax:</p>
<pre lang="z">
macro $while (~cond:expr, ~body:block) {
return ~{
loop {
if not({{~cond}}) {
break
}
{{~body}}
}
}~
}
$while true {
log("MACROS ARE AWESOME!!!!")
}
</pre>
<p>As one can see, blocks spread into their execution context, so the block passed to <code>$while</code> becomes part of the <code>loop</code> block.</p>
<p>Let's say we wanted to make a for-loop macro. It would iterate over a collection, like JavaScript's for-of loop. The parameter list looks like this:</p>
<pre lang="z">
macro $for (~l:expr, of, ~r:expr, ~body:block) {
}
</pre>
<p></p>
<p>The standalone <code>of</code> in the parameter list defines a conextual keyword. So <code>of</code> can act like a keyword in the context of a <code>$for</code> macro. Now, since we want to iterate over all iterators, we are going to
use Z's loop expression (from the last section): <code><-</code>. We are going to define a callback, and for each element of the iterator we're going to call the function for it. Since Z's macros are are non-hygenic, and since we
don't want to corrupt the local environment, we are going to use psuedohygiene for a variable names. This means Z will randomly generate ids for a variable names that keep them readable while leaving only a one in one million chance
of a name conflict with another generated identifier. You accomplish this by using double brackets without the tilde:</p>
<pre lang="z">
macro $for (~l:expr, of, ~r:expr, ~body:block) {
return ~{
def {{callback}}: func ({{~l}}) {
{{~body}}
}
{} ++ loop ({{i}} <- {{~r}}) {{callback}}({{i}})
}~
}
</pre>
<p>You can use it like this:</p>
<pre lang="z">
$for x of [1, 2, 3] {
log(x)
}
</pre>
<p>Now, macros can also take keywods as arguments via id parameters:</p>
<pre lang="z">
macro $asgn(~type:id, ~lvalue:expr, =>, ~rvalue:expr)
{
return ~{
{{~type}} {{~lvalue}}: {{~rvalue}}
}~
}
$asgn def x => 3
$asgn let y => 3
</pre>
<p>Finally: marco varargs capture groups within a certain pattern. You can then use <code>...(){}</code> to generate statements for each captured argument:</p>
<pre lang="z">
macro $logEach (...(~toLog:expr)){
return ~{
...(){
log({{~toLog}})
}
}~
}
$logEach (1 2 3 4 5)
</pre>
<p>This logs each number. A trailing comma can be added to indicate comma seperated values:</p>
<pre lang="z">
macro $logEach (...(~toLog:expr,)){
return ~{
...(){
log({{~toLog}})
}
}~
}
$logEach (1, 2, 3, 4, 5)
</pre>
<p>Using all of this, you can define a <code>$switch</code> macro:</p>
<pre lang="z">
macro $switch (~val:expr, ...{case, ~test:expr, ~body:block}){
return ~{
...(){
if {{~test}} = {{~val}} {
{{~body}}
}
}
}~
}
</pre>
<p>Then you can use it like this:</p>
<pre lang="z">
$switch 1 {
case 1 {
log("1")
}
case 2 {
log("2")
}
case 3 {
log("3")
}
}
</pre>