-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathenums_and_switch_statements.capy
69 lines (59 loc) · 1.42 KB
/
enums_and_switch_statements.capy
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
core :: #mod("core");
Animal :: enum {
Dog: str,
Cat,
Fish: i32, // maybe this is the fish's age or something
Cow,
Chicken | 20,
Sheep: struct {
name: str,
wool_amount: f32,
fullness: f32,
},
Pig,
};
main :: () {
do_animal_stuff(Animal.Dog.("George"));
do_animal_stuff(Animal.Cat.());
do_animal_stuff(Animal.Fish.(1000));
do_animal_stuff(Animal.Cow.());
do_animal_stuff(Animal.Chicken.());
do_animal_stuff(Animal.Sheep.{
name = "hello",
wool_amount = 1.0,
fullness = 0.5,
});
do_animal_stuff(Animal.Pig.());
}
do_animal_stuff :: (animal: Animal) {
num := animal_to_num(animal);
core.print("[", num, "] ");
switch a in animal {
Cat => core.print("cat!"),
Dog => {
core.print("dog: ", a);
},
Fish => {
core.print("it was a fish (age = ", a, ")");
},
Cow => core.print("cow!!!"),
Chicken => core.print("chicken >>>"),
Pig => core.print("oink oink"),
Sheep => {
core.print("sheep: ", a.name, ", wool: ", a.wool_amount, ", fullness: ", a.fullness);
},
}
core.println();
}
animal_to_num :: (animal: Animal) -> u16 {
num := switch animal {
Cat => 100,
Dog => 200,
Fish => 300,
Cow => 400,
Chicken => 500,
Pig => 600,
Sheep => 700,
};
num
}