-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.ngc
55 lines (40 loc) · 1.29 KB
/
example.ngc
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
use std;
// Functions can be pub or private (absence of pub keyword)
// Public functions can be imported, private functions cannot.
// Each function must return a type, convetionally u8 for 'voids', where
// 0 indicates success and 1 indicates failure.
pub fn sum <i32 number1, i32 number2> :: i32 {
return number1 + number2;
}
struct Planet {
i32 mass;
i32 radius;
}
// Main is a unique function, and is the entry point of the program.
// Only one such main may exist in a program.
fn main :: u8 {
i32 some_number1 = 1;
i32 some_number2 = 3;
i32 some_number3 = sum(some_number1, some_number2);
std.iostream.println(some_number3);
[i32] some_array = [241,262];
// Array values are accessed by #index
i32 some_array_sum = sum(some_array#0, some_array#1);
std.iostream.println(some_array_sum);
if (some_array_sum != 503) {
return 1;
}
Planet planet_stack = Planet{mass: 100, radius: 10};
Planet* planet_heap = std.mem.alloc(Planet);
defer std.mem.free(planet_heap);
planet_heap.mass = 100;
planet_heap.radius = 10;
std.iostream.println("Hello, world!");
return 0;
}
test sum :: u8 {
// Where asset_eq! is a macro that compares two values
// and returns 0 if they are equal, and 1 if they are not.
assert_eq!(sum(1, 3), 4);
assert_eq!(sum(241, 262), 503);
}