-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariables.wil
156 lines (116 loc) · 3.53 KB
/
variables.wil
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
/*
Variable declarations begin with 'let', followed by
an identifier, and then followed by either a type
specification or an initial value, or both.
*/
let int_var : int = 1
let float_var : float = 3.6
let char_var : char = 'c'
/*
If the type specification is omitted, a type is
inferred from the initial value.
If the initial value is omitted, the variable is
initialized to zero.
*/
let bool_var = true // inferred type: bool
let long_var : long // initialized to zero
/*
Since in Wilfrid every variable must have a definitive
type, omitting both type and value in a variable
declaration is not allowed.
*/
/*
Legal variable identifiers consist of letters, numbers,
and underscores, and cannot begin with a number.
*/
let _weird_name42 = 0
/*
Number literals can be expressed in decimal,
binary, or hexadecimal form:
*/
let decimal_val : ulong = 2014144115
let binary_val : ulong = 0b11110000000110101100110
let hexadecimal_val : ulong = 0x780D6673
/*
Number literals can also use '_' as a separator
for better readability:
*/
let other_decimal_val : ulong = 1_141_151_568
let other_binary_val : ulong = 0b_11110000_00001101_11101110
/*
Primitive numeric types in Wilfrid are:
int, uint, long, ulong, and float.
Int means 32-bit integer, and long means 64-bit integer.
Uint and ulong are their unsigned versions.
Floating point values are always 32-bit.
There is also a 32-bit bool, holding either
'true' or 'false'.
*/
/*
Wilfrid allows for low-level memory manipulation
via pointers.
A pointer is just an address in memory. We can perform
arithmetic operations on it: addition and subtraction.
Pointers are always 64-bit in Wilfrid.
They are declared using a type name and a caret sign:
*/
let pointer : long^
/*
That was a pointer to a variable of type long.
Here is a pointer to a pointer to a pointer to int:
*/
let other_pointer : int^^^
// For more examples see "Pointer operations".
/*
String, as in C, are implemented as pointers to characters.
Strings are delimited by '"' and can span multiple lines:
*/
let string_var : char^ =
"example
of a multiline
string"
/*
Arrays in Wilfrid are defined by writing the size
of an array in square brackets after some other type,
like this:
*/
let array_of_ints : int[10]
/*
Array elements can be accessed using square brackets
and an index:
*/
let int_from_array := array_of_ints[4]
/*
More complex types are also supported.
Here is an array of pointers to arrays of pointers
to integers:
*/
let complex_array : int^[10]^[10]
/*
Values can be also declared as const.
Unlike in C, they are true constants and can be
used as array sizes or in cases in switch statements.
Const values are always of long integer type.
*/
const constant_value = 42
let array_of_floats : float[constant_value]
/*
Each Wilfrid program has to have a main function declared.
*/
fn main()
{
/*
Declaring local variables looks the same as
declaring global ones. The only difference is that
inside functions we cannot declare const variables.
*/
let local_var := 12
/*
Let's print some values using the built-in printf function:
*/
printf("Bool value example: %u\\n", bool_var)
printf("Float value example: %ff\\n", float_var)
printf("Character value example: %c\\n", char_var)
printf("Integer value example: %i\\n", local_var)
printf("Pointer to long value example: %p\\n", pointer)
}