Debian Codegraph @

tests/inputs/demo.odin

raw
1 // portion of https://github.com/odin-lang/Odin/raw/master/examples/demo/demo.odin
2 package main
3
4 import "core:fmt"
5 import "core:mem"
6 import "core:os"
7
8
9 /*
10 The Odin programming language is fast, concise, readable, pragmatic and open sourced.
11 It is designed with the intent of replacing C with the following goals:
12 * simplicity
13 * high performance
14 * built for modern systems
15 * joy of programming
16
17 */
18
19 the_basics :: proc() {
20 fmt.println("\n# the basics");
21
22 { // The Basics
23 fmt.println("Hellope");
24
25 // Lexical elements and literals
26 // A comment
27
28 my_integer_variable: int; // A comment for documentaton
29
30 // Multi-line comments begin with /* and end with */. Multi-line comments can
31 // also be nested (unlike in C):
32 /*
33 You can have any text or code here and
34 have it be commented.
35 */
36
37 // Note: `:=` is two tokens, `:` and `=`. The following are equivalent,
38 /*
39 i: int = 123;
40 i: = 123;
41 i := 123;
42 */
43
44 _ = my_integer_variable;
45 _ = x;
46 }
47 }
48
49 control_flow :: proc() {
50 fmt.println("\n# control flow");
51 { // Control flow
52 // For loop
53 // Odin has only one loop statement, the `for` loop
54
55 // Basic for loop
56 for i := 0; i < 10; i += 1 {
57 fmt.println(i);
58 }
59
60 // NOTE: Unlike other languages like C, there are no parentheses `( )` surrounding the three components.
61 // Braces `{ }` or a `do` are always required>
62 for i := 0; i < 10; i += 1 { }
63 for i := 0; i < 10; i += 1 do fmt.print();
64
65 // The initial and post statements are optional
66 i := 0;
67 for ; i < 10; {
68 i += 1;
69 }
70
71 // You can defer an entire block too:
72 {
73 bar :: proc() {}
74
75 defer {
76 fmt.println("1");
77 fmt.println("2");
78 }
79
80 cond := false;
81 defer if cond {
82 bar();
83 }
84 }
85
86 // Defer statements are executed in the reverse order that they were declared:
87 {
88 defer fmt.println("1");
89 defer fmt.println("2");
90 defer fmt.println("3");
91 }
92 // Will print 3, 2, and then 1.
93
94 if false {
95 f, err := os.open("my_file.txt");
96 if err != 0 {
97 // handle error
98 }
99 defer os.close(f);
100 // rest of code
101 }
102 }
103
104 { // When statement
105 /*
106 The when statement is almost identical to the if statement but with some differences:
107
108 * Each condition must be a constant expression as a when
109 statement is evaluated at compile time.
110 * The statements within a branch do not create a new scope
111 * The compiler checks the semantics and code only for statements
112 that belong to the first condition that is true
113 * An initial statement is not allowed in a when statement
114 * when statements are allowed at file scope
115 */
116
117 // Example
118 when ODIN_ARCH == "386" {
119 fmt.println("32 bit");
120 } else when ODIN_ARCH == "amd64" {
121 fmt.println("64 bit");
122 } else {
123 fmt.println("Unsupported architecture");
124 }
125 // The when statement is very useful for writing platform specific code.
126 // This is akin to the #if construct in C’s preprocessor however, in Odin,
127 // it is type checked.
128 }
129
130 { // Branch statements
131 cond, cond1, cond2 := false, false, false;
132 one_step :: proc() { fmt.println("one_step"); }
133 beyond :: proc() { fmt.println("beyond"); }
134
135 // Break statement
136 for cond {
137 switch {
138 case:
139 if cond {
140 break; // break out of the `switch` statement
141 }
142 }
143
144 break; // break out of the `for` statement
145 }
146
147 loop: for cond1 {
148 for cond2 {
149 break loop; // leaves both loops
150 }
151 }
152
153 // Continue statement
154 for cond {
155 if cond2 {
156 continue;
157 }
158 fmt.println("Hellope");
159 }
160
161 // Fallthrough statement
162
163 // Odin’s switch is like one in C or C++, except that Odin only runs the selected
164 // case. This means that a break statement is not needed at the end of each case.
165 // Another important difference is that the case values need not be integers nor
166 // constants.
167
168 // fallthrough can be used to explicitly fall through into the next case block:
169
170 switch i := 0; i {
171 case 0:
172 one_step();
173 fallthrough;
174 case 1:
175 beyond();
176 }
177 }
178 }