-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions_and_methods.wil
More file actions
169 lines (132 loc) · 3.81 KB
/
functions_and_methods.wil
File metadata and controls
169 lines (132 loc) · 3.81 KB
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
/*
In Wilfrid functions are declared with the 'fn' keyword,
followed by a function name and optional parameters
list within parentheses. A function body is delimited
by braces.
*/
fn some_function()
{
// This function does nothing
}
/*
Function parameters are defined by a name followed
by a colon and a type.
*/
fn other_function(param_a: int, param_b: int)
{
/*
Here we can call a function that we defined previously.
Since it accepts no parameters, we invoke it by name
and empty parentheses.
*/
some_function()
/*
Printf is a special built-in function. It accepts
a variable number of parameters.
Note that the string literal passed to the printf
contains formatting specifiers. '%d' means that
we want to print an integer value.
If there is any mismatch between passed values and
the formatting string, there will be a runtime error.
Formatting specifiers are the same as in C. Some other
values are: '%u' for unsigned integers (uint) and chars,
'%lld' for long integers (long), '%llu' for unsigned long
integers (ulong), and '%p% for pointers.
*/
printf("Passed params: %d and %d\\n", param_a, param_b)
}
/*
A function can also have a return type.
Example of a function returning a boolean value:
*/
fn compare_numbers (a: int, b : int) : bool
{
return a > b
}
/*
Each Wilfrid program has to have a main function declared.
*/
fn main()
{
/*
Both newlines and whitespace don't matter in Wilfrid.
Here we have two declarations on the same line:
*/
let a := 10 let b := 20
let comparison_result = compare_numbers(a, b)
printf("Result of comparison is: %u\\n", comparison_result)
/*
This function will be declared later. Unlike in C, the order
of declarations doesn't matter in Wilfrid.
*/
method_example()
}
/*
Aside from functions, there are also methods.
Methods differ from functions in the fact that they
have a receiver. A receiver is written similarly to
a parameter, but it occurs before the function name.
Here is an example declaration:
*/
fn (a: rectangle^) add (b: rectangle^) : rectangle
{
#a = { x = a.x + b.x, y = a.y + b.y }
}
struct rectangle
{
x: int,
y: int
}
/*
Each method can have only one receiver.
There is no difference in how functions and methods work.
The only difference is the syntax of a call. Methods
require an instance of a variable that has
the type of the receiver.
Here is an example usage of the previously defined method:
*/
fn method_example()
{
let a := new rectangle()
a.x = 1
a.y = 2
let b := new rectangle()
b.x = 3
b.y = 4
a.add(b)
printf("The summed rectangle has sides %d and %d\\n", a.x, a.y)
}
/*
Functions and methods can be overloaded.
For example, here is a different method called 'add',
defined for cuboids.
*/
fn (a: cuboid^) add(b: cuboid^) : cuboid
{
#a = { x = a.x + b.x, y = a.y + b.y, z = a.z + b.z }
}
struct cuboid
{
x: int,
y: int,
z: int
}
/*
The 'extern' keyword allows for easy interoperability
with C code. To use code from C standard library,
we have to define an extern function with the same name
and signature.
Extern functions don't have a body.
Unfortunately, extern function invocation doesn't work
in the interpreter, which runs this interactive demo.
For now, extern functions work only when Wilfrid is
compiled to C.
Here are two examples:
*/
extern fn strncpy(destination: char^, source: char^, length: ulong) : char^
extern fn snprintf(buffer: char^, buffer_size: ulong, fmt: char^, variadic) : int
/*
The 'variadic' keyword is provided for interoperability
with C. It isn't allowed to be used in functions that
are not extern.
*/