-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathred-system-specs.txt
3107 lines (1937 loc) · 85.8 KB
/
red-system-specs.txt
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
Red/System Language Specification
Author: Nenad Rakocevic
Date: 26/02/2012
Revision: 33
Status: reference document
Home: <a href="http://www.red-lang.org">red-lang.org</a>
===Abstract
Red/System is a dialect (<a href="http://fr.wikipedia.org/wiki/Domain-specific_programming_language" target="_new">DSL</a>) of the Red programming language. Its purpose is to provide:
*low-level system programming capabilities
*a tool to build Red's runtime low-level library
*a tool to link code and produce executables
Red/System can be seen as a C-level language with memory pointer support and a very basic and limited set of datatypes.
<u>Implementation note</u>: It is currently provided with a complete tool-chain generating executables from source files. This is a temporary state as Red/System will live inside Red, so will be embedded in Red scripts.
===Syntax
The syntax is almost the same as the one used by REBOL language, as the lexer (<a href="http://www.rebol.com/docs/words/wload.html" target="_new">LOAD</a>) is currently provided by REBOL during the bootstrapping phase. The REBOL syntax does not have a formal specification nor an exhaustive documentation, just a superficial description, but it is enough to work with. See:
*<a href="http://www.rebol.com/docs/core23/rebolcore-3.html" target="_new">http://www.rebol.com/docs/core23/rebolcore-3.html</a> (There is a typo in the table, all literals are missing a caret (^) character after the first quote)
*<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>
A complete syntax specification for both Red and Red/System will be provided during the implementation of the Red language layer.
For now, Red/System uses 8-bit character encoding (ASCII). Once proper Unicode support will be provided by the Red language layer, Red/System will switch to UTF-8 source encoding.
Here are a few practical aspects of the language syntax:
---Delimiters
String delimiters: double quotes
"this is a string"
{This is
a multiline
string.
}
Block of code delimiters: square brackets
if a > 0 [print "TRUE"]
either a > 0 [print "TRUE"][print "FALSE"]
while [a > 0][print "loop" a: a - 1]
Path separator: slash (denotes a hierarchical relation)
s: struct [i [integer!] b [byte!]]
s/i: 123
s/b: #"A"
---Free-form syntax
Red/System (and Red) inherits the free-form syntax of the REBOL language. The only syntactic constraints are putting a whitespace (in the large sense) between tokens and correctly pairing delimiters.
Examples of valid code:
while [a > 0][print "loop" a: a - 1]
while [a > 0]
[print "loop" a: a - 1]
while [
a > 0
][
print "loop"
a: a - 1
]
Code guidelines are not yet available. They will follow standard REBOL practices.
---Comments
Inline comment:
;this is a commented line
print "hello world" ; this is another comment
Multiline comment:
comment {
This is a
multiline
comment
}
Usage rules:
*Inline comments are allowed anywhere in the source code
*Multi-line comments are allowed anywhere in the source code, except in expressions. Exemple:
a: 1 + comment {5} 4 ; this will produce a compilation error
===Variables
Variables are labels used to represent a memory location. The labels (called <b>identifiers</b> from now) are formed by sequences of printable characters without any blank (space, newlines or tabulation). Printable characters are defined as any one-byte character in the 20h-7Eh range that can be printed out in system's console excepting the following ones (used as delimiters or reserved for some datatypes literals):
[ ] { } " ( ) / \ @ # $ % ^ , : ; < >
There is a restriction on the first character, the following characters are forbidden in the first position, but allowed at other positions:
0 1 2 3 4 5 6 7 8 9 '
Also there is a another restriction to avoid letting the compiler mistake an hex integer for a variable name. Variable names starting with A-F letters consisting of 2, 4 or 8 A-F and 0-9 characters and ending with h are not allowed.
All identifiers (variables and function names) are <u>case-insensitive</u>.
\note Unicode support
As indicated in the Syntax section, Unicode support is not available during the bootstrapping phase, it will be available at Red layer, so Red/System will inherit it once rewritten in Red.
/note
---Setting a value
Variables can hold any value of the available datatypes. This can be the real value (like integer! or pointer!) or a reference to the real value as is the case for struct! or c-string!). To assign a value to a variable, use a colon character at the end of the variable identifier.
foo: 123
bar: "hello"
\note Multiple assignments
Multiple assignments, like a: b: 123, are not currently supported in Red/System. Such feature will be added in the future at some point, probably on the rewrite of Red/System in Red.
/note
---Getting a value
Just use the variable name without any decoration to get its value or to pass it as a function's argument.
bar: "hello"
print bar
will output:
hello
---Variable's type
Variables do have a type. Variables do not need to be declared before being used, but they require to be <u>initialized</u> anyway. Function local variables require to be declared, but the type specification part can be skipped if the variable is properly initialized. For example:
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
]
are valid variable usages.
Initializations have to be done at root level of code, attempt to initialize from a block of code will result in a compilation error.
foo: 123 ;-- valid initialization
if a < b [foo: 123] ;-- invalid initialization
<u>Note</u>: A function body block is considered a root level.
Allowed types for variables are:
*integer!
*byte!
*float!
*float32!
*logic!
*c-string!
*struct!
*pointer!
===Datatypes
---Integer!
+++Literal format
decimal form : 1234
decimal negative form : -1234
hexadecimal form : 04D2h
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 :
-2147483648 to 2147483647
<b>Hexadecimal format</b>
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:
00000000h to FFFFFFFFh
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).
\note Hex literal form design decision
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.
/note
---Byte!
The byte! datatype's purpose is to represent unsigned integers in the 0-255 range. byte! is an alias for the internal uint8! datatype, both will be accepted, but byte! is the official one.
+++Syntax
#"<character>"
#"^<character>"
#"^(hexadecimal)"
#"^(name)"
Examples:
#"a"
#"A"
#"5"
#"^A"
#"^(1A)"
#"^(back)"
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.
+++Casting
Casting is allowed to some extent (see section "4.9 Type Casting").
foo: as integer! #"a" ;-- foo holds 97
bar: as byte! foo ;-- bar holds #"a"
<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>
---Float!
The float! datatype represents an IEEE-754 double precision floating point number. Float! memory size is 64-bit.
<u>Note</u>: <b>float64!</b> can be used as an alias to <b>float!</b>.
+++Syntax
<sign><digits>.<digits>
or using scientific notation:
<sign><digits>E<exponent>
<sign><digits>.<digits>E<exponent>
where:
<sign> : an optional + or - symbol
<digits> : one or more digits
<exponent> : a positive or negative integer
Examples:
0.0
1.0
-12345.6789
3.14159265358979
-1E3
+1.23456E-265
A maximum of 16 digits are accepted for literal float! values, if more are specified, they will be dropped.
<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>.
+++Casting
It is allowed to apply a type casting transformation on a float! value to convert it to a float32! value.
Examples:
pi: 3.14159265358979
pi-32: as float32! pi
print pi-32
will output
3.1415927
+++Math operations
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).
The modulo and remainder operators have a special meaning when used on float! values:
* modulo (//): floating point remainder (result is a float!).
* remainder (%): IEEE-754 rounded remainder (result is an integer!).
---Float32!
The float32! datatype represents an IEEE-754 single precision floating point number. Float! memory size is 32-bit.
\note Float32! purpose
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.
/note
+++Syntax
There is no literal form for float32! datatype values. To load a float32! constant, the method consist in provindg a float! literal and prefix it with a type casting to float32!.
Example:
pi32: as float32! 3.1415927
<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>.
+++Casting
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).
Examples:
s: as float32! 3.1415927
print [
as float! s lf
as integer! s
]
will output
3.14159270000000
1518260631
+++Math operations
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).
The modulo and remainder operators have a special meaning when used on float32! values:
* modulo (//): floating point remainder (result is a float32!).
* remainder (%): IEEE-754 rounded remainder (result is an integer!).
---Logic!
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.
As a first class datatype, you can pass logic values and variables as function arguments or use them as function's return value.
+++Literal format
true
false
Using a literal to initialize a logic variable:
foo: true
either foo [print "true"][print "false"]
will output:
true
Using a conditional expression to initialize a logic variable:
bar: 2 > 5
either bar [print "true"][print "false"]
will output:
false
---C-string!
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.
+++Syntax
Literal c-strings are defined using double quotes delimiters or a pair of matching curly braces:
foo: "I am a c-string"
bar: {I am
a multiline
c-string
}
\note C-string null byte ending
You don't have to add a null byte to literal c-strings, it is added automatically during compilation.
/note
+++C-string length
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:
a: length? "Hello" ;-- here length? will return 5
\note Length? vs Size?
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.
/note
+++C-string arithmetic
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.
Syntax
<c-string> + <n>
<c-string> - <n>
<c-string> : c-string variable
<n> : expression resulting in an integer! value
Example
s: "hello" ;-- let's suppose s points to address 4000000h
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
+++Accessing bytes
It is possible to access individual bytes in a c-string using path notation:
<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
The returned value will be of the type <b>byte!</b>.
Examples:
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)
\note Important notes
*In contrary to the C language, indexes in Red/System are <b>one-based</b>.
*The behaviour of path with an index out of bounds is not yet defined. (It is better to avoid it)
/note
Example of a variable used as index:
c: 4
foo/c => #"m" ;-- byte! value (109)
A simple way to traverse a c-string would be:
foo: "I am a c-string"
bar: foo
until [
print bar/1
bar: bar + 1
bar/1 = null-byte
]
will output:
I am a c-string
Similarly, it is also possible to modify the c-string's bytes using path notation with an ending colon:
<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
For example:
foo: "I am a c-string"
foo/3: #"-"
c: 4
foo/c: #"-"
print foo
will output
I -- a c-string
---Struct!
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.
<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.
+++Declaration
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.
declare struct! [
<member> [<datatype>]
...
]
<member> : a valid identifier
<datatype> : integer! | byte! | pointer! [integer! | byte!] | logic! |
float! | float32! | c-string! | struct! [<members>]
The returned value of DECLARE STRUCT! is the memory address of the newly created struct! value.
<u>Note</u>: Struct members are all initialized to 0 when a new literal struct! is declared.
+++Usage
s: declare struct! [
a [integer!]
b [c-string!]
c [struct! [d [integer!]]]
]
In this example, the struct value has 3 members: a, b, c, each with a different datatype. The c member is a struct! value pointer, it needs to be assigned to a struct value to be used. So a correct initialization for the c member would be:
s/c: declare struct! [d [integer!]]
+++Accessing members
Member access is achieved using path notation. Syntax is:
<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>
From last example, that would give:
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
+++Struct arithmetic
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.
Syntax
<struct> + <n>
<struct> - <n>
<struct> : struct variable
<n> : integer value
Examples
p: declare struct! [ ;-- let suppose p = 4000000h
a [integer!]
b [pointer! [integer!]]
] ;-- struct memory size would be 8 bytes
p: p + 1 ;-- now p = 40000008h
<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.
+++Aliases
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.
<u>Notes</u>:
* 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.
* Aliased names live in their own namespace, so they cannot interfere with variable names.
Aliasing syntax:
<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 value declaration using an aliased definition:
<variable>: declare <alias>
<variable> : a struct variable
<alias> : a previously declared alias name
Struct usage example:
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
---Pointer!
Pointer datatype purpose 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! and byte! (logic! pointer is not needed).
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.
<u>Implementation note</u>: The memory size of a pointer is 4 bytes on 32-bit systems (and 8 bytes on 64-bit systems).
+++Literal format
New pointers value can be created using the following syntax:
declare pointer! [<datatype>]
<datatype>: integer! | byte! | float! | float32!
\note Possible syntactic sugar
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.
/note
Examples:
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;
\note Pointer value initialization
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 0 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).
/note
+++Declaration
Pointer declaration is only required for local pointer variables in functions' specification block. In such case, the datatype declaration can be omitted and left to the inferencer to guess. (See "Type inference" section)
pointer! [<datatype>]
<datatype>: integer! | byte! | float! | float32!
Global variables declaration examples (with C equivalents):
p: declare pointer! [integer!] ;-- int *p;
p: declare pointer! [byte!] ;-- char *p;
p: declare pointer! [float!] ;-- double *p;
Same with local variables declaration examples (with C equivalents):
func [/local p [pointer! [integer!]] ;-- int *p;
func [/local p [pointer! [byte!]] ;-- char *p;
func [/local p [pointer! [float!]] ;-- double *p;
Example of inferred pointer variable type:
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
]
+++Dereferencing
Dereferencing a pointer is the operation allowing to access 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"):
<pointer>/value ;-- read access
<pointer>/value: <value> ;-- write access
<pointer> : pointer variable
<value> : a value of same type as in pointer's definition
Usage example
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
<u>Note</u>: Remember that a pointed value is undefined (can contain an arbitrary value) until you define it explicitly
+++Pointer arithmetic
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: declare pointer! [integer!] ;-- pointed value memory size is 4 bytes
p: as [pointer! [integer!]] 4000000h
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!]] 4000000h
q: q + 1 ;-- p points now to 40000001h
q: q + 1 ;-- p points now to 40000002h
Also, additions and subtractions between pointer addresses are allowed. The result value type is, as usual, the type of left operand.
offset: p - q ;-- would store 6 in offset
;-- type of offset is pointer! [integer!]
+++Pointer path notation
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>.
<b>Syntax</b>
<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
Examples:
p: declare pointer! [integer!]
p: as [pointer! [integer!]] 4000000h
a: p/1 ;-- reads an integer! from 40000000h
p/2: a ;-- writes the integer! to 40000004h
Integer variable can also be used as index:
p: declare pointer! [integer!]
p: as [pointer! [integer!]] 4000000h
c: 2
p/c: 1234 ;-- writes 1234 (4 bytes) at 40000004h
<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.
+++Null value
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.
<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.
<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.
Examples:
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
+++C void pointer
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.
For pointers to c-string! or struct! variables, a pointer variable can be used then dereferenced and type cast to the target type.
Example:
p-buffer!: alias struct! [buffer [c-string!]]
get-hello: function [
s [p-buffer]
return: [p-buffer] ;-- returns a pointer to a c-string
][
s/buffer: "hello"
s ;-- equivalent to C's char **
]
foo: func [
/local
c [pointer! [integer!]]
s [c-string!]
][
c: get-hello
s: as c-string! c/value
print s
]
would print
hello
\note Runtime macro byte-ptr!
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.
/note
---Type Casting
Casting is achieved using the <b>AS</b> keyword followed by the target type and the value to cast.
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.
<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.
<b>Syntax</b>
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>
<u>Note</u>: Multiple nested type castings are not allowed and will raise a compilation error.
Example:
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
<br>
<b>Type casting reference matrix</b>
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.
<div align="center">
<table class="matrix">
<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">to 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">to 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">to integer!</td>
<td class="allow">to integer!</td>
<td class="allow">to integer!</td>
<td class="deny">ERROR</td>
<td class="allow">as 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">to c-string!</td>
<td class="deny">ERROR</td>
<td class="warn">WARNING</td>
<td class="allow">to c-string!</td>
<td class="allow">to 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">to pointer!</td>
<td class="deny">ERROR</td>
<td class="allow">to pointer!</td>
<td class="warn">WARNING</td>
<td class="allow">to 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">to struct!</td>
<td class="deny">ERROR</td>
<td class="allow">to struct!</td>
<td class="allow">to struct!</td>
<td class="warn">WARNING</td>
<td class="deny">ERROR</td>
<td class="deny">ERROR</td>
<td class="deny">ERROR</td>
</tr>
<th>float!</th>
<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>
<td class="warn">WARNING</td>
<td class="allow">to float!</td>
<td class="deny">ERROR</td>
</tr>
<th>float32!</th>
<td class="deny">ERROR</td>
<td class="allow">as float32!</td>
<td class="deny">ERROR</td>
<td class="deny">ERROR</td>
<td class="deny">ERROR</td>
<td class="deny">ERROR</td>
<td class="allow">to float32!</td>
<td class="warn">WARNING</td>
<td class="deny">ERROR</td>
</tr>
</table>
</div>
¹ Casting allowed, but integer values higher than 255 will be truncated, so beware!<br>
² Function! is a pseudo-type that cannot be the target of a type casting.
===Expressions
Expressions are the basic building blocks of a Red/System program. They are composed of:
* variables