File: demo.odin

package info (click to toggle)
cloc 2.06-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,064 kB
  • sloc: perl: 30,146; cpp: 1,219; python: 623; ansic: 334; asm: 267; makefile: 244; sh: 186; sql: 144; java: 136; ruby: 111; cs: 104; pascal: 52; lisp: 50; haskell: 35; f90: 35; cobol: 35; objc: 25; php: 22; javascript: 15; fortran: 9; ml: 8; xml: 7; tcl: 2
file content (178 lines) | stat: -rw-r--r-- 3,964 bytes parent folder | download | duplicates (3)
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
170
171
172
173
174
175
176
177
178
// portion of https://github.com/odin-lang/Odin/raw/master/examples/demo/demo.odin
package main

import "core:fmt"
import "core:mem"
import "core:os"


/*
	The Odin programming language is fast, concise, readable, pragmatic and open sourced.
	It is designed with the intent of replacing C with the following goals:
	 * simplicity
	 * high performance
	 * built for modern systems
	 * joy of programming

*/

the_basics :: proc() {
	fmt.println("\n# the basics");

	{ // The Basics
		fmt.println("Hellope");

		// Lexical elements and literals
		// A comment

		my_integer_variable: int; // A comment for documentaton

		// Multi-line comments begin with /* and end with */. Multi-line comments can
		// also be nested (unlike in C):
		/*
			You can have any text or code here and
			have it be commented.
		*/

		// Note: `:=` is two tokens, `:` and `=`. The following are equivalent,
		/*
			i: int = 123;
			i:     = 123;
			i := 123;
		*/

		_ = my_integer_variable;
		_ = x;
	}
}

control_flow :: proc() {
	fmt.println("\n# control flow");
	{ // Control flow
		// For loop
		// Odin has only one loop statement, the `for` loop

		// Basic for loop
		for i := 0; i < 10; i += 1 {
			fmt.println(i);
		}

		// NOTE: Unlike other languages like C, there are no parentheses `( )` surrounding the three components.
		// Braces `{ }` or a `do` are always required>
		for i := 0; i < 10; i += 1 { }
		for i := 0; i < 10; i += 1 do fmt.print();

		// The initial and post statements are optional
		i := 0;
		for ; i < 10; {
			i += 1;
		}

		// You can defer an entire block too:
		{
			bar :: proc() {}

			defer {
				fmt.println("1");
				fmt.println("2");
			}

			cond := false;
			defer if cond {
				bar();
			}
		}

		// Defer statements are executed in the reverse order that they were declared:
		{
			defer fmt.println("1");
			defer fmt.println("2");
			defer fmt.println("3");
		}
		// Will print 3, 2, and then 1.

		if false {
			f, err := os.open("my_file.txt");
			if err != 0 {
				// handle error
			}
			defer os.close(f);
			// rest of code
		}
	}

	{ // When statement
		/*
			The when statement is almost identical to the if statement but with some differences:

			* Each condition must be a constant expression as a when
			  statement is evaluated at compile time.
			* The statements within a branch do not create a new scope
			* The compiler checks the semantics and code only for statements
			  that belong to the first condition that is true
			* An initial statement is not allowed in a when statement
			* when statements are allowed at file scope
		*/

		// Example
		when ODIN_ARCH == "386" {
			fmt.println("32 bit");
		} else when ODIN_ARCH == "amd64" {
			fmt.println("64 bit");
		} else {
			fmt.println("Unsupported architecture");
		}
		// The when statement is very useful for writing platform specific code.
		// This is akin to the #if construct in C’s preprocessor however, in Odin,
		// it is type checked.
	}

	{ // Branch statements
		cond, cond1, cond2 := false, false, false;
		one_step :: proc() { fmt.println("one_step"); }
		beyond :: proc() { fmt.println("beyond"); }

		// Break statement
		for cond {
			switch {
			case:
				if cond {
					break; // break out of the `switch` statement
				}
			}

			break; // break out of the `for` statement
		}

		loop: for cond1 {
			for cond2 {
				break loop; // leaves both loops
			}
		}

		// Continue statement
		for cond {
			if cond2 {
				continue;
			}
			fmt.println("Hellope");
		}

		// Fallthrough statement

		// Odin’s switch is like one in C or C++, except that Odin only runs the selected
		// case. This means that a break statement is not needed at the end of each case.
		// Another important difference is that the case values need not be integers nor
		// constants.

		// fallthrough can be used to explicitly fall through into the next case block:

		switch i := 0; i {
		case 0:
			one_step();
			fallthrough;
		case 1:
			beyond();
		}
	}
}