-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
3743 lines (3565 loc) · 151 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
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0">
<title>Red/System语言规范</title>
<link media="screen" href="light.css" type="text/css" rel="stylesheet">
</head>
<body bgcolor="white">
<table width="800" cellpadding="4" cellspacing="0" border="0">
<tbody><tr height="10"><td align="right">
<a href="http://static.red-lang.org/red-system-specs-light.html" target="_blank">原文</a>
</td></tr>
<tr><td class="cnt"><h1>Red/System语言规范</h1>
<blockquote><b>
作者: Nenad Rakocevic<br>日期: 01/05/2017<br>修订号: 52<br>类型: 参考文档<br>主页: <a href="http://www.red-lang.org/">red-lang.org</a>
</b></blockquote>
<h2>目录:</h2>
<a class="toc1" href="#section-1">1. 概要</a><br>
<a class="toc1" href="#section-2">2. 语法</a><br>
<a class="toc2" href="#section-2.1">2.1 分隔符</a><br>
<a class="toc2" href="#section-2.2">2.2 范式无关语法</a><br>
<a class="toc2" href="#section-2.3">2.3 注释</a><br>
<a class="toc1" href="#section-3">3. 变量</a><br>
<a class="toc2" href="#section-3.1">3.1 设置值</a><br>
<a class="toc2" href="#section-3.2">3.2 获取值</a><br>
<a class="toc2" href="#section-3.3">3.3 类型声明</a><br>
<a class="toc1" href="#section-4">4. 数据类型</a><br>
<a class="toc2" href="#section-4.1">4.1 整数</a><br>
<a class="toc2" href="#section-4.2">4.2 字节</a><br>
<a class="toc2" href="#section-4.3">4.3 浮点数</a><br>
<a class="toc2" href="#section-4.4">4.4 32位浮点数</a><br>
<a class="toc2" href="#section-4.5">4.5 逻辑值</a><br>
<a class="toc2" href="#section-4.6">4.6 C语言字符串</a><br>
<a class="toc2" href="#section-4.7">4.7 结构体</a><br>
<a class="toc2" href="#section-4.8">4.8 指针</a><br>
<a class="toc2" href="#section-4.9">4.9 类型转换</a><br>
<a class="toc2" href="#section-4.10">4.10 大小</a><br>
<a class="toc1" href="#section-5">5. 表达式</a><br>
<a class="toc2" href="#section-5.1">5.1 一般性规则</a><br>
<a class="toc2" href="#section-5.2">5.2 执行顺序规则</a><br>
<a class="toc1" href="#section-6">6. 函数</a><br>
<a class="toc2" href="#section-6.1">6.1 声明</a><br>
<a class="toc2" href="#section-6.2">6.2 返回值</a><br>
<a class="toc2" href="#section-6.3">6.3 属性</a><br>
<a class="toc2" href="#section-6.4">6.4 类型推断</a><br>
<a class="toc2" href="#section-6.5">6.5 函数调用</a><br>
<a class="toc2" href="#section-6.6">6.6 函数指针</a><br>
<a class="toc2" href="#section-6.7">6.7 提前退出</a><br>
<a class="toc1" href="#section-7">7. 作用域</a><br>
<a class="toc2" href="#section-7.1">7.1 全局上下文</a><br>
<a class="toc2" href="#section-7.2">7.2 函数上下文</a><br>
<a class="toc2" href="#section-7.3">7.3 命名空间</a><br>
<a class="toc1" href="#section-8">8. 中缀操作符</a><br>
<a class="toc2" href="#section-8.1">8.1 数学操作符</a><br>
<a class="toc2" href="#section-8.2">8.2 移位操作符</a><br>
<a class="toc2" href="#section-8.3">8.3 位操作符</a><br>
<a class="toc2" href="#section-8.4">8.4 比较操作符</a><br>
<a class="toc1" href="#section-9">9. 控制流函数</a><br>
<a class="toc2" href="#section-9.1">9.1 if</a><br>
<a class="toc2" href="#section-9.2">9.2 either</a><br>
<a class="toc2" href="#section-9.3">9.3 loop</a><br>
<a class="toc2" href="#section-9.4">9.4 until</a><br>
<a class="toc2" href="#section-9.5">9.5 while</a><br>
<a class="toc2" href="#section-9.6">9.6 break</a><br>
<a class="toc2" href="#section-9.7">9.7 continue</a><br>
<a class="toc2" href="#section-9.8">9.8 any</a><br>
<a class="toc2" href="#section-9.9">9.9 all</a><br>
<a class="toc2" href="#section-9.10">9.10 case</a><br>
<a class="toc2" href="#section-9.11">9.11 switch</a><br>
<a class="toc1" href="#section-10">10. 异常</a><br>
<a class="toc2" href="#section-10.1">10.1 throw</a><br>
<a class="toc2" href="#section-10.2">10.2 catch</a><br>
<a class="toc2" href="#section-10.3">10.3 异常值</a><br>
<a class="toc1" href="#section-11">11. 栈函数</a><br>
<a class="toc2" href="#section-11.1">11.1 push</a><br>
<a class="toc2" href="#section-11.2">11.2 pop</a><br>
<a class="toc1" href="#section-12">12. 调试函数</a><br>
<a class="toc2" href="#section-12.1">12.1 assert</a><br>
<a class="toc1" href="#section-13">13. 系统结构体</a><br>
<a class="toc2" href="#section-13.1">13.1 args-count</a><br>
<a class="toc2" href="#section-13.2">13.2 args-list</a><br>
<a class="toc2" href="#section-13.3">13.3 env-vars</a><br>
<a class="toc2" href="#section-13.4">13.4 stack/top</a><br>
<a class="toc2" href="#section-13.5">13.5 stack/frame</a><br>
<a class="toc2" href="#section-13.6">13.6 stack/align</a><br>
<a class="toc2" href="#section-13.7">13.7 stack/allocate</a><br>
<a class="toc2" href="#section-13.8">13.8 stack/free</a><br>
<a class="toc2" href="#section-13.9">13.9 pc</a><br>
<a class="toc2" href="#section-13.10">13.10 cpu</a><br>
<a class="toc2" href="#section-13.11">13.11 cpu/overflow?</a><br>
<a class="toc2" href="#section-13.12">13.12 fpu/type</a><br>
<a class="toc2" href="#section-13.13">13.13 fpu/option</a><br>
<a class="toc2" href="#section-13.14">13.14 fpu/mask</a><br>
<a class="toc2" href="#section-13.15">13.15 fpu/control-word</a><br>
<a class="toc2" href="#section-13.16">13.16 fpu/epsilon</a><br>
<a class="toc2" href="#section-13.17">13.17 fpu/update</a><br>
<a class="toc2" href="#section-13.18">13.18 fpu/init</a><br>
<a class="toc2" href="#section-13.19">13.19 alias</a><br>
<a class="toc1" href="#section-14">14. 编译器指令</a><br>
<a class="toc1" href="#section-15">15. 导入外部库</a><br>
<a class="toc2" href="#section-15.1">15.1 #import</a><br>
<a class="toc2" href="#section-15.2">15.2 #syscall</a><br>
<a class="toc1" href="#section-16">16. 预处理指令</a><br>
<a class="toc2" href="#section-16.1">16.1 #define</a><br>
<a class="toc2" href="#section-16.2">16.2 #enum</a><br>
<a class="toc2" href="#section-16.3">16.3 #include</a><br>
<a class="toc2" href="#section-16.4">16.4 #if</a><br>
<a class="toc2" href="#section-16.5">16.5 #either</a><br>
<a class="toc2" href="#section-16.6">16.6 #switch</a><br>
<a class="toc2" href="#section-16.7">16.7 #verbose</a><br>
<a class="toc2" href="#section-16.8">16.8 #call</a><br>
<a class="toc2" href="#section-16.9">16.9 #export</a><br>
<a class="toc2" href="#section-16.10">16.10 #u16</a><br>
<a class="toc1" href="#section-17">17. 代码组织</a><br>
<a class="toc2" href="#section-17.1">17.1 源文件后缀</a><br>
<a class="toc2" href="#section-17.2">17.2 文件头</a><br>
<a class="toc2" href="#section-17.3">17.3 代码布局</a><br>
<a class="toc2" href="#section-17.4">17.4 编码指南</a><br>
<a class="toc2" href="#section-17.5">17.5 共享库程序</a><br>
<a class="toc2" href="#section-17.6">17.6 驱动程序</a><br>
<a class="toc1" href="#section-18">18. 保留关键字</a><br>
<a class="toc1" href="#section-19">19. 未来的改进</a><br>
<a class="toc2" href="#section-19.1">19.1 变量</a><br>
<a class="toc2" href="#section-19.2">19.2 指针</a><br>
<a class="toc2" href="#section-19.3">19.3 结构体</a><br>
<a class="toc2" href="#section-19.4">19.4 C语言字符串</a><br>
<a class="toc2" href="#section-19.5">19.5 逻辑值</a><br>
<a class="toc2" href="#section-19.6">19.6 整数</a><br>
<a class="toc2" href="#section-19.7">19.7 函数</a><br>
<a class="toc2" href="#section-19.8">19.8 新的数据类型</a><br>
<a class="toc2" href="#section-19.9">19.9 新的函数</a><br>
<a class="toc2" href="#section-19.10">19.10 其它</a><br>
<a class="toc1" href="#section-20">20. 文档历史记录</a><br>
<h2 id="section-1">1. 概要</h2>
<blockquote>
<p>Red/System是Red语言的一种方言(<a href="http://en.wikipedia.org/wiki/Domain-specific_language" target="_new">DSL</a>)。它旨在提供以下特性:</p>
<ul>
<li>底层系统编程能力</li>
<li>用作构建Red运行时底层库的工具</li>
<li>用作链接代码并产生可执行文件的工具</li>
</ul>
<p>Red/System可以看作是拥有内存指针和一些基本且有限的数据类型的和C语言同一级别的语言。</p>
<p><u>注意</u>:Red/System当前提供了一套完整的从源码构建出可执行文件的工具链。这是Red/System存在于Red中的临时状态,因此它也被嵌入在Red脚本中。</p>
</blockquote>
<h2 id="section-2">2. 语法</h2>
<blockquote>
<p>Red的语法和REBOL语言的语法基本上相同,因为用在启动阶段的词法分析器(<a href="http://www.rebol.com/docs/words/wload.html" target="_new">LOAD</a>)当前是就是用REBOL语言写的。REBOL语言的语法既没有正式的规范也没有完整的文档,仅仅是一个粗略的描述。不过这都不是事儿,拿来开发项目完全足够了。详情请参考:</p>
<ul>
<li><a href="http://www.rebol.com/docs/core23/rebolcore-3.html" target="_new">http://www.rebol.com/docs/core23/rebolcore-3.html</a> (这个页面的表格中有一处错误,所有字面量的第一个引号后面都缺少(^)符号)</li>
<li><a href="http://www.rebol.com/r3/docs/guide/code-syntax.html" target="_new">http://www.rebol.com/r3/docs/guide/code-syntax.html</a></li>
</ul>
<p>在实现Red的语言层面之后,我们将会提供一份完整的Red和Red/System的语法规范。</p>
<p>目前,Red/System使用的是8-bit字符编码(ASCII)。一旦在Red的语言层面实现了对Unicode的支持,Red/System也会切换到UTF-8编码。</p>
<p>以下是一些语言语法的具体部分:</p>
</blockquote>
<h3 id="section-2.1">2.1 分隔符</h3>
<blockquote>
<p>字符串分隔符:双引号</p>
<pre>"this is a string"
{This is
a multiline
string.
}
</pre>
<p>代码块分隔符:方括号</p>
<pre>if a > 0 [print "TRUE"]
either a > 0 [print "TRUE"][print "FALSE"]
while [a > 0][print "loop" a: a - 1]
</pre>
<p>路径分隔符:斜线(表示层级关系)</p>
<pre>s: declare struct! [i [integer!] b [byte!]]
s/i: 123
s/b: #"A"
</pre>
</blockquote>
<h3 id="section-2.2">2.2 范式无关语法</h3>
<blockquote>
<p>Red/System(以及Red)继承了REBOL语言的范式无关语法。只有句法约束是放在以空格分隔的记号和一对分隔符之间。</p>
<p>有效的例子代码:</p>
<pre>while [a > 0][print "loop" a: a - 1]
while [a > 0]
[print "loop" a: a - 1]
while [
a > 0
][
print "loop"
a: a - 1
]
</pre>
<p>现在代码风格还不确定,我们目前是遵循标准的REBOL风格。</p>
</blockquote>
<h3 id="section-2.3">2.3 注释</h3>
<blockquote>
<p>单行注释:</p>
<pre>;this is a commented line
print "hello world" ; this is another comment
</pre>
<p>多行注释:</p>
<pre>comment {
This is a
multiline
comment
}
</pre>
<p>使用规定:</p>
<ul>
<li>单行注释允许出现在代码中的任何地方</li>
<li>多行注释也可以出现在代码中的任何地方,但是不允许出现在表达式中间。例如:</li>
</ul>
<pre>a: 1 + comment {5} 4 ; this will produce a compilation error
</pre>
</blockquote>
<h2 id="section-3">3. 变量</h2>
<blockquote>
<p>变量是用于表示内存位置的一个标签。标签(现在叫做标识符(<b>identifiers</b>))是由一系列不包含任何空白(空格、换行或制表符)的可打印字符组成的。可打印字符是除了以下(用作分隔符或一些数据类型字面量)这些之外任何编码范围在20h-7Eh的单字节范围内的字符:</p>
<pre>[ ] { } " ( ) / \ @ # $ % ^ , : ; < >
</pre>
<p>第一个字符有严格的限制,以下是严禁出现在第一个字符,但是允许出现在其它位置的:</p>
<pre>0 1 2 3 4 5 6 7 8 9 '
</pre>
<p>另外还有个为了避免编译器误认为十六进制是一个变量名的限制:以2个A-F字母开头,加上4或8个A-F和0-9字符,然后以h结尾的变量名,都是不允许的。</p>
<p>所有的标识符(变量和函数名)都是<u>大小写不敏感</u>的。</p>
<p></p><fieldset class="fset"><legend>Unicode支持</legend>
<p>如语法部分所言,Unicode支持目前在启动阶段无效,它在Red层才有效,所以一旦Red/System用Red重写,Red/System会继承此特性。</p>
</fieldset>
</blockquote>
<h3 id="section-3.1">3.1 设置值</h3>
<blockquote>
<p>变量可以保存任何有效的数据类型值。它可以是一个实数(比如integer!或pointer!)或一个到真实值的一个引用。为了给变量赋值,在标识符后面写一个分号即可。</p>
<pre>foo: 123
bar: "hello"
</pre>
<p></p><fieldset class="fset"><legend>多次赋值</legend>
<p>多次赋值,例如 a: b: 123,当前在Red/System中还没有支持。这种特性将会在将来的某个时间点添加,也许在用Red重写Red/System的时候。</p>
</fieldset>
</blockquote>
<h3 id="section-3.2">3.2 获取值</h3>
<blockquote>
<p>直接用变量名,不用任何其它的装饰就能得到它的值,或者将它作为函数参数:</p>
<pre>bar: "hello"
print bar
</pre>
<p>将会输出:</p>
<pre>hello
</pre>
</blockquote>
<h3 id="section-3.3">3.3 类型声明</h3>
<blockquote>
<p>变量全都拥有类型。它不需要在使用前声明类型,但是初始化的时候需要。函数局部变量也要求声明类型,但是如果变量已经被相应地初始化了,那么类型声明部分可以跳过。例如这些:</p>
<pre>foo: 123
bar: "hello"
size: length? bar
id: GetProcessID ;-- 'GetProcessID would return an integer!
compute: func [
a [integer!]
return: [integer!]
/local c ;-- 'c is declared without a type
][
c: 1 ;-- inferred type is integer!
a + c
]
</pre>
<p>都是有效的用法。</p>
<p>初始化必须在代码的最顶层完成,尝试在一个代码块中初始化将会造成编译时错误。</p>
<pre>foo: 123 ;-- valid initialization
if a < b [foo: 123] ;-- invalid initialization
</pre>
<p><u>注意</u>:函数的body block也被看作是最顶层。</p>
<p>允许的变量类型为以下:</p>
<ul>
<li>integer!</li>
<li>byte!</li>
<li>float!</li>
<li>float32!</li>
<li>logic!</li>
<li>c-string!</li>
<li>struct!</li>
<li>pointer!</li>
</ul>
</blockquote>
<h2 id="section-4">4. Datatypes</h2>
<blockquote>
<p>All the following datatypes are <a href="http://en.wikipedia.org/wiki/First-class_citizen">first-class citizens</a> of Red/System language.</p>
</blockquote>
<h3 id="section-4.1">4.1 Integer!</h3>
<blockquote>
<h4 id="section-4.1.1">4.1.1 Literal format</h4>
<pre>decimal form : 1234
decimal negative form : -1234
hexadecimal form : 04D2h
</pre>
<p>The integer! datatype represents natural and negative natural numbers. The memory size of an integer is 32 bits, so the range of supported numbers is :</p>
<pre>-2147483648 to 2147483647
</pre>
<p><b>Hexadecimal format</b></p>
<p>Hexadecimal integer representation is mostly used to represent memory addresses or binary data for bitwise operations. As for character, all hexadecimal literals found in sources are converted to their integer decimal value during lexical analysis. Allowed range is:</p>
<pre>00000000h to FFFFFFFFh
</pre>
<p>Hex letters have to be written in <b>uppercase</b> and only 2, 4 and 8 characters are allowed (prefixing with leading zeros is allowed).</p>
<p></p><fieldset class="fset"><legend>Hex literal form design decision</legend>
<p>The 0x prefix is often used to mark a literal hexadecimal value. It could have been used in Red/System too if the <number>x<number> literal form wasn't reserved in Red for the pair! datatype. As Red/System is a dialect of Red, it has to use the same representation for hex values, so <b><hexa>h</b> was chosen instead.</p>
</fieldset>
</blockquote>
<h3 id="section-4.2">4.2 Byte!</h3>
<blockquote>
<p>The byte! datatype's purpose is to represent unsigned integers in the 0-255 range.</p>
<h4 id="section-4.2.1">4.2.1 Syntax</h4>
<pre>#"<character>"
#"^<character>"
#"^(hexadecimal)"
#"^(name)"
</pre>
<p>Examples:</p>
<pre>#"a"
#"A"
#"5"
#"^A"
#"^(1A)"
#"^(back)"
</pre>
<p>See <a href="http://www.rebol.com/docs/core23/rebolcore-16.html#section-3.1" target="_new">http://www.rebol.com/docs/core23/rebolcore-16.html#section-3.1</a> for a more complete description of this format.</p>
<h4 id="section-4.2.2">4.2.2 Casting</h4>
<p>Casting is allowed to some extent (see section "4.9 Type Casting").</p>
<pre>foo: as integer! #"a" ;-- foo holds 97
bar: as byte! foo ;-- bar holds #"a"
</pre>
<p><u>Note</u>: trying to cast an integer value greater than 255 to a byte! will result in a data loss or data corruption. <i>(The handling of this case might be changed in future revisions)</i></p>
</blockquote>
<h3 id="section-4.3">4.3 Float!</h3>
<blockquote>
<p>The float! datatype represents an IEEE-754 double precision floating point number. Float! memory size is 64-bit.</p>
<p><u>Note</u>: <b>float64!</b> can be used as an alias to <b>float!</b>.</p>
<h4 id="section-4.3.1">4.3.1 Syntax</h4>
<pre><sign><digits>.<digits>
</pre>
<p>or using scientific notation:</p>
<pre><sign><digits>E<exponent>
<sign><digits>.<digits>E<exponent>
</pre>
<p>where:</p>
<pre><sign> : an optional + or - symbol
<digits> : one or more digits
<exponent> : a positive or negative integer
</pre>
<p>Examples:</p>
<pre>0.0
1.0
-12345.6789
3.14159265358979
-1E3
+1.23456E-265
</pre>
<p>A maximum of 16 digits are accepted for literal float! values. If more are specified, they will be dropped.</p>
<p><i>For more information on double precision floating point numbers, see <a href="http://en.wikipedia.org/wiki/Double-precision_floating-point_format">Wikipedia</a></i>.</p>
<h4 id="section-4.3.2">4.3.2 Casting</h4>
<p>It is allowed to apply a type casting transformation on a float! value to convert it to a float32! value.</p>
<p>Examples:</p>
<pre>pi: 3.14159265358979
pi-32: as float32! pi
print pi-32
</pre>
<p>will output</p>
<pre>3.1415927
</pre>
<h4 id="section-4.3.3">4.3.3 Math operations</h4>
<p>All Red/System math operators (+, -, *, /, //, %) are supported. The default rounding method on results is "rounding to nearest". Both operands need to be of float! types (no implicit casting).</p>
<p>The modulo (//) and remainder (%) operators give the same results when used on float! values.</p>
</blockquote>
<h3 id="section-4.4">4.4 Float32!</h3>
<blockquote>
<p>The float32! datatype represents an IEEE-754 single precision floating point number. Float32! memory size is 32-bit.</p>
<p></p><fieldset class="fset"><legend>Float32! purpose</legend>
<p>The reason for having a single precision floating point type is for making the interfacing with popular libraries straightforward. For pure Red/System programs, <b>float!</b> should be the default choice.</p>
</fieldset>
<h4 id="section-4.4.1">4.4.1 Syntax</h4>
<p>There is no literal form for float32! datatype values. To load a float32! constant, the method consists of providing a float! literal and prefixing it with a type casting to float32!.</p>
<p>Example:</p>
<pre>pi32: as float32! 3.1415927
</pre>
<p><i>For more information on single precision floating point numbers, see <a href="http://en.wikipedia.org/wiki/Single-precision_floating-point_format">Wikipedia</a></i>.</p>
<h4 id="section-4.4.2">4.4.2 Casting</h4>
<p>It is allowed to apply a type casting transformation on a float32! value to convert it to a float! value. Type casting to integer! is also allowed mainly for bits manipulation purpose (this is not a float32! number to integer! number conversion).</p>
<p>Examples:</p>
<pre>s: as float32! 3.1415927
print [
as float! s lf
as integer! s
]
</pre>
<p>will output</p>
<pre>3.14159270000000
1518260631
</pre>
<h4 id="section-4.4.3">4.4.3 Math operations</h4>
<p>All Red/System math operators (+, -, *, /, //, %) are supported. The default rounding method on results is "rounding to nearest". Both operands need to be of float32! types (no implicit casting).</p>
<p>The modulo (//) and remainder (%) operators give the same results when used on float32! values.</p>
</blockquote>
<h3 id="section-4.5">4.5 Logic!</h3>
<blockquote>
<p>The logic! datatype represents boolean values: <b>TRUE</b> and <b>FALSE</b>. Logic variables are initialized using a literal logic value or the result of a conditional expression.</p>
<p>As a first class datatype, you can pass logic values and variables as function arguments or use them as function's return value.</p>
<h4 id="section-4.5.1">4.5.1 Literal format</h4>
<pre>true
false
</pre>
<p>Using a literal to initialize a logic variable:</p>
<pre>foo: true
either foo [print "true"][print "false"]
</pre>
<p>will output:</p>
<pre>true
</pre>
<p>Using a conditional expression to initialize a logic variable:</p>
<pre>bar: 2 > 5
either bar [print "true"][print "false"]
</pre>
<p>will output:</p>
<pre>false
</pre>
</blockquote>
<h3 id="section-4.6">4.6 C-string!</h3>
<blockquote>
<p>A c-string! value is a sequence of non-null bytes terminated by a null byte. A c-string variable holds the memory address of the first byte of the c-string, so it can be viewed as an implicit pointer to a variable of the byte! datatype. A c-string having a null character as first byte is an empty c-string.</p>
<h4 id="section-4.6.1">4.6.1 Syntax</h4>
<p>Literal c-strings are defined using double quotes delimiters or a pair of matching curly braces:</p>
<pre>foo: "I am a c-string"
bar: {I am
a multiline
c-string
}
</pre>
<p></p><fieldset class="fset"><legend>C-string null byte ending</legend>
<p>You don't have to add a null byte to literal c-strings. It is added automatically during compilation.</p>
</fieldset>
<h4 id="section-4.6.2">4.6.2 C-string length</h4>
<p>It is possible to retrieve the number of bytes (<b>excluding the null</b> end marker) in a c-string at runtime using the LENGTH? function:</p>
<pre>a: length? "Hello" ;-- here length? will return 5
</pre>
<p></p><fieldset class="fset"><legend>Length? vs Size?</legend>
<p>Do not confuse the <b>length?</b> function with the <b>size?</b> function. <b>Size?</b> function will return the number of bytes in the c-string, including the ending null byte.</p>
</fieldset>
<h4 id="section-4.6.3">4.6.3 C-string arithmetic</h4>
<p>It is possible to apply some simple math operations on c-string variables like additions and subtractions. C-string address would be increased or decreased by the integer argument.</p>
<p>Syntax</p>
<pre><c-string> + <n>
<c-string> - <n>
<c-string> : c-string variable
<n> : expression resulting in an integer! value
</pre>
<p>Example:</p>
<pre>s: "hello" ;-- let's suppose s points to address 40000000h
s: s + 1 ;-- now s points to address 40000001h
print s ;-- "ello" would be printed
s: s + 1 ;-- now s points to address 40000002h
print s ;-- "llo" would be printed
</pre>
<h4 id="section-4.6.4">4.6.4 Accessing bytes</h4>
<p>It is possible to access individual bytes in a c-string using path notation:</p>
<pre><c-string>/integer! ;-- literal integer index provided
<c-string>/<index> ;-- index provided by a variable
<c-string> : a c-string variable
<index> : an integer variable
</pre>
<p>The returned value will be of the type <b>byte!</b>.</p>
<p>Examples:</p>
<pre>foo: "I am a c-string"
foo/1 => #"I" ;-- byte! value (73)
foo/2 => #" " ;-- byte! value (32)
...
foo/15 => #"g" ;-- byte! value (103)
foo/16 => #"^(00)" ;-- byte! value (0) (end marker)
</pre>
<p></p><fieldset class="fset"><legend>Important notes</legend>
<ul>
<li>In contrast to the C language, indexes in Red/System are <b>one-based</b>.</li>
<li>The behaviour of path with an index out of bounds is not yet defined. It is better to avoid it.</li>
</ul>
</fieldset>
<p>Example of a variable used as index:</p>
<pre>c: 4
foo/c => #"m" ;-- byte! value (109)
</pre>
<p>A simple way to traverse a c-string would be:</p>
<pre>foo: "I am a c-string"
bar: foo
until [
print bar/1
bar: bar + 1
bar/1 = null-byte
]
</pre>
<p>will output:</p>
<pre>I am a c-string
</pre>
<p>Similarly, it is also possible to modify the c-string's bytes using path notation with an ending colon:</p>
<pre><c-string>/integer!: <value> ;-- literal integer index provided
<c-string>/<index>: <value> ;-- index provided by a variable
<c-string> : a c-string variable
<index> : an integer variable
<value> : a byte! value
</pre>
<p>For example:</p>
<pre>foo: "I am a c-string"
foo/3: #"-"
c: 4
foo/c: #"-"
print foo
</pre>
<p>will output</p>
<pre>I -- a c-string
</pre>
</blockquote>
<h3 id="section-4.7">4.7 Struct!</h3>
<blockquote>
<p>Struct! datatype is roughly equivalent to C struct type. It is a record of one or several values, each value having its own datatype. A struct variable holds the memory address of a struct value.</p>
<p><u>Implementation note</u>: Struct! values members are <u>padded</u> in memory in order to preserve optimal alignment for each target (for example, it is aligned to 4 bytes for IA32 target). <b>Size?</b> function will return the size of the struct! value in memory including the padding bytes.</p>
<h4 id="section-4.7.1">4.7.1 Declaration</h4>
<p>Declaring a struct! value is achieved by using the DECLARE STRUCT! sequence followed by a specification block. That block defines struct! value members using pairs of name and datatype definition.</p>
<pre>declare struct! [
<member> [<datatype>]
...
]
<member> : a valid identifier
<datatype> : integer! | byte! | pointer! [integer! | byte!] | logic! |
float! | float32! | c-string! | struct! [<members>] |
struct! [<members>] value | function! [<spec>]
</pre>
<p>The returned value of DECLARE STRUCT! is the memory address of the newly created struct! value.</p>
<p><u>Note</u>: Struct members are all initialized to 0 when a new literal struct! is declared.</p>
<h4 id="section-4.7.2">4.7.2 Usage</h4>
<pre>s: declare struct! [
a [integer!]
b [c-string!]
c [struct! [d [integer!] e [float!]]]
]
</pre>
<p>In this example, the struct value has 3 members: a, b, c, each with a different datatype. The c member is a struct! value <b>pointer</b>, it needs to be assigned to a struct value to be used. So a correct initialization for the c member would be:</p>
<pre>s/c: declare struct! [d [integer!]]
</pre>
<p>It is possible to nest struct values (and not just pointers to struct values) by adding the <b>value</b> keyword at the tail of a nested struct type specification:</p>
<pre>s2: declare struct! [
a [integer!]
b [c-string!]
c [struct! [d [integer!] e [float!]] value]
]
</pre>
<p>In this case, the nested struct c storage space is reserved when allocating the space for the parent struct s2. The size of struct s2 is 20 bytes, while the size of s is 12 bytes.</p>
<p>Struct pointers and struct values can be arbitrarily nested and mixed together.</p>
<h4 id="section-4.7.3">4.7.3 Accessing members</h4>
<p>Member access is achieved using path notation. Syntax is:</p>
<pre><struct>/<member> ;-- read access
<struct>/<member>: <value> ;-- write access
<struct> : a valid struct variable
<member> : a valid member identifier in <struct>
<value> : a value of same datatype as <member>
</pre>
<p>From last example, that would give:</p>
<pre>foo: s/a ;-- reading member 'a in struct 's
s/a: 123 ;-- writing 123 in member 'a in struct 's
s/b: "hello"
bar: s/c/d ;-- deep read/write access is also possible
</pre>
<p><u>Note</u>: Accessing a function! pointer member will result in dereferencing the pointer.</p>
<p>It is also possible to acquire a pointer on a struct member using the get-path notation:</p>
<pre>:<struct>/<member>
<struct> : a valid struct variable
<member> : a valid member identifier in <struct>
</pre>
<p>The returned type is always `pointer! [integer!]` in such case. It can be freely type-casted to other pointer types.</p>
<pre>p: :s/a
p/value ;-- returns the value of s/a
p/value: 456 ;-- sets a new value in s/a
</pre>
<h4 id="section-4.7.4">4.7.4 Struct arithmetic</h4>
<p>It is possible to apply some simple math operations on struct variables, like additions and subtractions. Struct address would be increased or decreased by the size of the pointed struct value multiplied by the integer argument.</p>
<p><b>Syntax</b></p>
<pre><struct> + <n>
<struct> - <n>
<struct> : struct variable
<n> : integer value
</pre>
<p><b>Examples</b></p>
<pre>p: declare struct! [ ;-- let suppose p = 40000000h
a [integer!]
b [pointer! [integer!]]
] ;-- struct memory size would be 8 bytes
p: p + 1 ;-- now p = 40000008h
</pre>
<p><u>Note</u>: The struct value size is target and alignment dependent. In the above example, it is supposed to run on a 32-bit system with a struct alignment to 4 bytes.</p>
<h4 id="section-4.7.5">4.7.5 Aliases</h4>
<p>Struct! values definitions tend to be quite long, so in some cases, when struct! definitions are required to be inserted in other struct! definitions or in functions specification block, it is possible to use an alias name to reference a struct! definition through the source code. It also allows the self-referencing case to be quite simply solved.</p>
<p><u>Notes</u>:</p>
<ul>
<li>An alias is not a value, it doesn't take any space in memory, it can be seen as a <i>virtual datatype</i>. So, by convention, alias names should end with an exclamation mark, in order to distinguish them more easily from variables in the source code.</li>
<li>Aliased names live in their own namespace, so they cannot interfere with variable names.</li>
</ul>
<p>Aliasing syntax:</p>
<pre><name>: alias struct! [
<member> [<datatype>]
...
]
<name> : the name to use as alias
<member> : a valid identifier
<datatype> : integer! | byte! | pointer! [integer! | byte!] | logic! |
float! | float32! | c-string! | struct! [<members>] |
struct! [<members>] value | function! [<spec>]
</pre>
<p>Struct value declaration using an aliased definition:</p>
<pre><variable>: declare <alias>
<variable> : a struct variable
<alias> : a previously declared alias name
</pre>
<p>Struct usage example:</p>
<pre>book!: alias struct! [ ;-- defines a new aliased type
title [c-string!]
author [c-string!]
year [integer!]
last-book [book!] ;-- self-referenced definition
]
gift: declare struct! [
first [book!] ;-- reference to a book! struct
second [book!] ;-- reference to a book! struct
]
gift/first: declare book! ;-- book! struct allocation
gift/first/title: "Contact"
gift/first/author: "Carl Sagan"
gift/first/year: 1985
gift2: declare struct! [
first [book! value] ;-- inlined book! struct value
second [book! value] ;-- inlined book! struct value
]
</pre>
</blockquote>
<h3 id="section-4.8">4.8 Pointer!</h3>
<blockquote>
<p>The purpose of the pointer datatype is to hold the memory address of another value. A pointer value is defined by the pointed value address and datatype. As c-string! and struct! are already implicit pointers, the only pointed datatypes allowed are integer!, float!, float32! and byte! (logic! pointer is not needed).</p>
<p>Byte! pointers are equivalent to c-string! references, the difference lies only in the interpretation of the pointed values. Byte! pointer is meant to point to a stream of byte without a specified bound, while c-string! references an array of bytes terminated by a null byte.</p>
<p><u>Implementation note</u>: The memory size of a pointer is 4 bytes on 32-bit systems (and 8 bytes on 64-bit systems).</p>
<h4 id="section-4.8.1">4.8.1 Literal format</h4>
<p>New pointers value can be created using the following syntax:</p>
<pre>declare pointer! [<datatype>]
<datatype>: integer! | byte! | float! | float32!
</pre>
<p></p><fieldset class="fset"><legend>Possible syntactic sugar</legend>
<p>The & symbol used in previous revisions of this document has been removed due to the new limited pointer! datatype usage. It could be reintroduced again in the future if required.</p>
</fieldset>
<p>Examples:</p>
<pre>foo: declare pointer! [integer!] ;-- equivalent to C's: int *foo;
bar: declare pointer! [byte!] ;-- equivalent to C's: char *bar;
baz: declare pointer! [float!] ;-- equivalent to C's: double *baz;
</pre>
<p></p><fieldset class="fset"><legend>Pointer value initialization</legend>
<p>Do not assume any default value for a pointer value until it is initialized properly. In the current implementation, global pointer variables are set to <b>null</b> by default while local pointer variables default value is undefined. This might change in the future to adopt a default value more suitable for debugging (like 0BADBAD0h or similar hex trick).</p>
</fieldset>
<h4 id="section-4.8.2">4.8.2 Declaration</h4>
<p>Pointer declaration is only required for arguments in functions' specification block. For local pointer variables, the datatype declaration can be omitted and left to the inferencer to guess. (See "Type inference" section)</p>
<pre>pointer! [<datatype>]
<datatype>: integer! | byte! | float! | float32!
</pre>
<p>Global variables declaration examples (with C equivalents):</p>
<pre>p: declare pointer! [integer!] ;-- int *p;
p: declare pointer! [byte!] ;-- char *p;
p: declare pointer! [float!] ;-- double *p;
</pre>
<p>Same with local variables declaration examples (with C equivalents):</p>
<pre>func [/local p [pointer! [integer!]] ;-- int *p;
func [/local p [pointer! [byte!]] ;-- char *p;
func [/local p [pointer! [float!]] ;-- double *p;
</pre>
<p>Example of inferred pointer variable type:</p>
<pre>foo: func [
a [struct! [count [integer!]]]
/local
p [pointer! [integer!]] ;-- explicit declaration
][
foobar p ;-- foobar modifies p
a/count: p/value
]
bar: func [
a [struct! [count [integer!]]]
/local p ;-- p datatype inferred
][
p: declare pointer! [integer!] ;-- p initialized (implicit declaration)
foobar p
a/count: p/value
]
bar2: func [
a [struct! [count [integer!]]]
/local p ;-- p datatype inferred
][
p: GetPointer a ;-- datatype is guessed from return value
foobar p
a/count: p/value
]
</pre>
<h4 id="section-4.8.3">4.8.3 Dereferencing</h4>
<p>Dereferencing a pointer is the operation allowing access to the pointed value. In Red/System, it is achieved by adding a <b>/value</b> refinement to the pointer variable (called more generally "path notation"):</p>
<pre><pointer>/value ;-- read access
<pointer>/value: <value> ;-- write access
<pointer> : pointer variable
<value> : a value of same type as in pointer's definition
</pre>
<p>Usage example</p>
<pre>p: declare pointer! [integer!] ;-- declare a pointer on an integer
bar: declare pointer! [integer!] ;-- declare another pointer on an integer
p: as [pointer! [integer!]] 40000000h ;-- type cast an integer! to a pointer!
p/value: 1234 ;-- write 1234 at address 40000000h
foo: p/value ;-- read pointed value back
bar: p ;-- assign pointer address to 'bar
</pre>
<p><u>Note</u>: Remember that a pointed value is undefined (can contain an arbitrary value) until you define it explicitly</p>
<h4 id="section-4.8.4">4.8.4 Pointer arithmetic</h4>
<p>It is possible to apply some simple math operations on pointers, like additions and subtractions (as in C). A pointer address will be increased or decreased by the memory size of the pointed value multiplied by the amount to respectively add or subtract.</p>
<pre>p: declare pointer! [integer!] ;-- pointed value memory size is 4 bytes
p: as [pointer! [integer!]] 40000000h
p: p + 1 ;-- p points now to 40000004h
p: p + 1 ;-- p points now to 40000008h
q: declare pointer! [byte!] ;-- pointed value memory size is 1 byte
q: as [pointer! [byte!]] 40000000h
q: q + 1 ;-- p points now to 40000001h
q: q + 1 ;-- p points now to 40000002h
</pre>
<p>Also, additions and subtractions between pointer addresses are allowed. The result value type is, as usual, the type of left operand.</p>
<pre>offset: p - q ;-- would store 6 in offset
;-- type of offset is pointer! [integer!]
</pre>
<h4 id="section-4.8.5">4.8.5 Pointer path notation</h4>
<p>It is possible to use path notation to simulate an array with indexed access. Both reading and writing are possible. Indexes are <b>one-based</b>.</p>
<p><b>Syntax</b></p>
<pre><pointer>/<integer> ;-- literal integer index provided
<pointer>/<index> ;-- index provided by a variable
<pointer> : a pointer variable
<integer> : an integer literal value
<index> : an integer variable
</pre>
<p>Examples:</p>
<pre>p: declare pointer! [integer!]
p: as [pointer! [integer!]] 40000000h
a: p/1 ;-- reads an integer! from 40000000h
p/2: a ;-- writes the integer! to 40000004h
</pre>
<p>Integer variable can also be used as index:</p>
<pre>p: declare pointer! [integer!]
p: as [pointer! [integer!]] 40000000h
c: 2
p/c: 1234 ;-- writes 1234 (4 bytes) at 40000004h
</pre>
<p><u>Note</u>: Pointer's <b>/value</b> notation is strictly equivalent to <b>/1</b>. The <b>/value</b> notation can be considered as syntactic sugar.</p>
<h4 id="section-4.8.6">4.8.6 Literal arrays</h4>
<p>A pointer can also point to a one-dimensional array of values literally specified.</p>
<p><b>Syntax</b></p>
<pre><variable>: [<items>]
<variable> : a pointer of same type as the array items.
<items> : is a non-empty list of integer!, byte!, float! literal values.
</pre>
<p>The array is statically allocated and can be accessed using pointer path notation or pointer arithmetic. The size of the array (in number of elements) is stored in a 32-bit word just preceding the beginning of the array.</p>
<p>Examples:</p>
<pre>list: [123 456 789]
probe list/0 ;-- outputs 3
probe list/2 ;-- outputs 456
s: [#"h" #"e" #"l" #"l" #"o"]
sz: as int-ptr! s
probe sz/0 ;-- outputs 5
probe s/5 ;-- outputs o
</pre>
<p><u>Note</u>: In this last example, this literal array is not equivalent to its c-string! counterpart "hello", as the literal array does not add a NUL value at tail of the sequence.</p>
<p></p><fieldset class="fset"><legend>Write access to arrays</legend>
<p>Currently literal arrays allow write access, but there is no bound checking as it is planned to be a feature of a future array! first-class datatype.</p>
</fieldset>
<h4 id="section-4.8.7">4.8.7 Null value</h4>
<p>A special <b>null</b> value is available to use for pointer! and other pointer-like (pass-by-reference) types (struct!, c-string!) and pseudo-type function!. <b>Null</b> does not have a specific type, but can be used to replace any other pointer-like value. So, <b>null</b> cannot be used as initializing value for a global variable or a local variable that does not have an explicit type specification.</p>
<p><b>Null</b> is a first class value, so it can be assigned to a variable, passed as argument to a function or returned by a function.</p>
<p><u>Note</u>: It is not possible to explicitly cast <b>null</b> to a given type, only implicit type casting automatically done by the compiler is allowed.</p>
<p>Examples:</p>
<pre>a: declare pointer! [integer!]
a: null ;-- valid assignment, 'a type is defined
b: null ;-- invalid assignment, type of b cannot
;-- be deduced by the compiler
foo: func [s [c-string!] return: [c-string!]][
if s = null [
print "error"
return null
]
return uppercase s
]
b: foo "test" ;-- will set b to "TEST"
b: foo null ;-- will print "error" and set b to null
</pre>
<h4 id="section-4.8.8">4.8.8 C void pointer</h4>
<p>There is no specific support in Red/System for C-like void pointers. The official way is to use a pointer! [byte!] type to represent C void* pointers.</p>
<p>For pointers to c-string! or struct! variables, a pointer variable can be used then dereferenced and converted using type casting to the target type.</p>
<p>Example:</p>
<pre>p-buffer!: alias struct! [buffer [c-string!]]
set-hello: function [
s [p-buffer!]
][
s/buffer: "hello"
s ;-- equivalent to C's char **
]
foo: func [
/local
c [p-buffer!]
][
c: declare p-buffer!
set-hello c
print c/buffer
]
foo ;-- call foo function
</pre>
<p>would print</p>
<pre>hello
</pre>
<p></p><fieldset class="fset"><legend>Runtime macro byte-ptr!</legend>
<p>The runtime defines a byte-ptr! macro (just defined as: pointer! [byte!]) to be used as an equivalent to C void* for raw memory accesses.</p>
</fieldset>
<h4 id="section-4.8.9">4.8.9 Variable pointer</h4>
<p>It is possible to get a pointer on an existing variable for the following datatypes:</p>
<ul>
<li>integer!</li>
<li>byte!</li>
<li>float!</li>
<li>float32!</li>
</ul>
<p><b>Syntax</b></p>
<pre>:<variable>
<variable> : a variable name of allowed type.
</pre>
<p>This expression will return a pointer value which type depends on the variable type, so:</p>
<p></p><div align="center">
<table class="normal">
<tbody><tr>
<th>variable</th>
<th>:variable</th>
</tr><tr>
<td>integer!</td>
<td>pointer! [integer!]</td>
</tr><tr>
<td>byte!</td>
<td>pointer! [byte!]</td>
</tr><tr>
<td>float!</td>
<td>pointer! [float!]</td>
</tr><tr>
<td>float32!</td>
<td>pointer! [float32!]</td>
</tr>
</tbody></table>
</div><p></p>
<p>Example:</p>
<pre>s: declare pointer! [integer!]
a: 123
s: :a
print s/value ;-- will output 123
</pre>
<p></p><fieldset class="fset"><legend>Pointer on local variable</legend>
<p>It is allowed to get a pointer on a local variable, however, <u>special attention</u> should be provided to avoid using such pointer once the function has returned, this would result in most cases in crashes caused by stack corruption!</p>
</fieldset>
</blockquote>
<h3 id="section-4.9">4.9 Type Casting</h3>
<blockquote>
<p>Casting is achieved using the <b>AS</b> keyword followed by the target type and the value to cast.</p>
<p>Type casting is possible between value of compatible types. Compatible types are defined in the following type casting reference matrix. A run-time type conversion might be generated for some types combinations.</p>
<p><u>Note</u>: Trying to assign a value to a variable of different type without a proper type casting, will result in a compilation error.</p>
<p><b>Syntax</b></p>
<pre>as <new-type> value
as [<new-type>] value ;-- alternative syntax
<new-type> : integer! | byte! | logic! | c-string! | float! | float32! |
pointer! [integer!] | struct! [<members>] |
<alias-name>
</pre>
<p><u>Note</u>: Multiple nested type castings are not allowed and will raise a compilation error.</p>
<p>Example:</p>
<pre>foo: 0 ;-- foo is an integer variable
bar: declare pointer! [integer!] ;-- bar is a pointer variable
foo: as integer! bar ;-- type casting
bar: as pointer! [integer!] foo
</pre>
<p><br>
<b>Type casting reference matrix</b></p>
<p>Keep in mind that pointer!, c-string!, struct! and function! are passed by reference, so the casting below for these datatypes is applied on their memory address value.</p>
<p></p><div align="center">
<p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><table class="matrix">
<tbody><tr>
<th id="matrix-start">source>></th>
<th>byte!</th>
<th>integer!</th>
<th>logic!</th>
<th>c-string!</th>
<th>pointer!</th>
<th>struct!</th>
<th>float!</th>
<th>float32!</th>
<th>function!</th>
</tr><tr>
<th>byte!</th>
<td class="warn">WARNING</td>
<td class="allow">as byte! ¹</td>
<td class="allow">true<span class="arrow">»</span>#"^(01)"<br>false<span class="arrow">»</span>#"^(00)"</td>
<td class="deny">ERROR</td>
<td class="deny">ERROR</td>
<td class="deny">ERROR</td>
<td class="deny">ERROR</td>
<td class="deny">ERROR</td>
<td class="deny">ERROR</td>
</tr><tr>
<th>integer!</th>
<td class="allow">as integer!</td>
<td class="warn">WARNING</td>
<td class="allow">true<span class="arrow">»</span>1<br>false<span class="arrow">»</span>0</td>
<td class="allow">as integer!</td>
<td class="allow">as integer!</td>
<td class="allow">as integer!</td>
<td class="allow">to integer!</td>
<td class="allow">to integer!</td>
<td class="allow">as integer!</td>
</tr><tr>
<th>logic!</th>
<td class="allow">#"^(00)"<span class="arrow">»</span>false<br>else<span class="arrow">»</span>true</td>
<td class="allow">0<span class="arrow">»</span>false<br>else<span class="arrow">»</span>true</td>
<td class="warn">WARNING</td>
<td class="allow">null<span class="arrow">»</span>false<br>else<span class="arrow">»</span>true</td>
<td class="allow">null<span class="arrow">»</span>false<br>else<span class="arrow">»</span>true</td>
<td class="allow">null<span class="arrow">»</span>false<br>else<span class="arrow">»</span>true</td>
<td class="deny">ERROR</td>
<td class="deny">ERROR</td>
<td class="deny">ERROR</td>
</tr><tr>
<th>c-string!</th>
<td class="deny">ERROR</td>
<td class="allow">as c-string!</td>
<td class="deny">ERROR</td>
<td class="warn">WARNING</td>
<td class="allow">as c-string!</td>
<td class="allow">as c-string!</td>
<td class="deny">ERROR</td>
<td class="deny">ERROR</td>
<td class="deny">ERROR</td>
</tr><tr>
<th>pointer!</th>
<td class="deny">ERROR</td>
<td class="allow">as pointer!</td>
<td class="deny">ERROR</td>
<td class="allow">as pointer!</td>
<td class="warn">WARNING</td>
<td class="allow">as pointer!</td>
<td class="deny">ERROR</td>
<td class="deny">ERROR</td>
<td class="deny">ERROR</td>
</tr><tr>
<th>struct!</th>
<td class="deny">ERROR</td>
<td class="allow">as struct!</td>
<td class="deny">ERROR</td>
<td class="allow">as struct!</td>
<td class="allow">as struct!</td>
<td class="warn">WARNING</td>