aboutsummaryrefslogtreecommitdiff
path: root/code/old_demos/demo001.odin
blob: 7e80f0d1b026f45f00c98a53ec3930256ba9e3a2 (plain)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#import "fmt.odin";
#import "os.odin";
#import "mem.odin";
// #import "http_test.odin" as ht;
// #import "game.odin" as game;
// #import "punity.odin" as pn;

main :: proc() {
	// struct_padding()
	// bounds_checking()
	// type_introspection()
	// any_type()
	// crazy_introspection()
	// namespaces_and_files()
	// miscellany()
	// ht.run()
	// game.run()
	// {
	// 	init :: proc(c: ^pn.Core) {}
	// 	step :: proc(c: ^pn.Core) {}

	// 	pn.run(init, step)
	// }
}

struct_padding :: proc() {
	{
		A :: struct {
			a: u8,
			b: u32,
			c: u16,
		}

		B :: struct {
			a: [7]u8,
			b: [3]u16,
			c: u8,
			d: u16,
		}

		fmt.println("size_of(A):", size_of(A));
		fmt.println("size_of(B):", size_of(B));

		// n.b. http://cbloomrants.blogspot.co.uk/2012/07/07-23-12-structs-are-not-what-you-want.html
	}
	{
		A :: struct #ordered {
			a: u8,
			b: u32,
			c: u16,
		}

		B :: struct #ordered {
			a: [7]u8,
			b: [3]u16,
			c: u8,
			d: u16,
		}

		fmt.println("size_of(A):", size_of(A));
		fmt.println("size_of(B):", size_of(B));

		// C-style structure layout
	}
	{
		A :: struct #packed {
			a: u8,
			b: u32,
			c: u16,
		}

		B :: struct #packed {
			a: [7]u8,
			b: [3]u16,
			c: u8,
			d: u16,
		}

		fmt.println("size_of(A):", size_of(A));
		fmt.println("size_of(B):", size_of(B));

		// Useful for explicit layout
	}

	// Member sorting by priority
	// Alignment desc.
	// Size desc.
	// source order asc.

	/*
		A :: struct {
			a: u8
			b: u32
			c: u16
		}

		B :: struct {
			a: [7]u8
			b: [3]u16
			c: u8
			d: u16
		}

		Equivalent too

		A :: struct #ordered {
			b: u32
			c: u16
			a: u8
		}

		B :: struct #ordered {
			b: [3]u16
			d: u16
			a: [7]u8
			c: u8
		}
	*/
}

bounds_checking :: proc() {
	x: [4]int;
	// x[-1] = 0; // Compile Time
	// x[4]  = 0; // Compile Time

	{
		a, b := -1, 4;
		// x[a] = 0; // Runtime Time
		// x[b] = 0; // Runtime Time
	}

	// Works for arrays, strings, slices, and related procedures & operations

	{
		base: [10]int;
		s := base[2:6];
		a, b := -1, 6;

		#no_bounds_check {
			s[a] = 0;
			// #bounds_check s[b] = 0;
		}

	#no_bounds_check
		if s[a] == 0 {
			// Do whatever
		}

		// Bounds checking can be toggled explicit
		// on a per statement basis.
		// _any statement_
	}
}

type_introspection :: proc() {
	{
		info: ^Type_Info;
		x: int;

		info = type_info(int); // by type
		info = type_info_of_val(x); // by value
		// See: runtime.odin

		match type i in info {
		case Type_Info.Integer:
			fmt.println("integer!");
		case Type_Info.Float:
			fmt.println("float!");
		default:
			fmt.println("potato!");
		}

		// Unsafe cast
		integer_info := cast(^Type_Info.Integer)info;
	}

	{
		Vector2 :: struct { x, y: f32 }
		Vector3 :: struct { x, y, z: f32 }

		v1: Vector2;
		v2: Vector3;
		v3: Vector3;

		t1 := type_info_of_val(v1);
		t2 := type_info_of_val(v2);
		t3 := type_info_of_val(v3);

		fmt.println();
		fmt.print("Type of v1 is:\n\t", t1);

		fmt.println();
		fmt.print("Type of v2 is:\n\t", t2);

		fmt.println("\n");
		fmt.println("t1 == t2:", t1 == t2);
		fmt.println("t2 == t3:", t2 == t3);
	}
}

any_type :: proc() {
	a: any;

	x: int = 123;
	y: f64 = 6.28;
	z: string = "Yo-Yo Ma";
	// All types can be implicit cast to `any`
	a = x;
	a = y;
	a = z;
	a = a; // This the "identity" type, it doesn't get converted

	a = 123; // Literals are copied onto the stack first

	// any has two members
	// data      - rawptr to the data
	// type_info - pointer to the type info

	fmt.println(x, y, z);
	// See: fmt.odin
	// For variadic any procedures in action
}

crazy_introspection :: proc() {
	{
		Fruit :: enum {
			APPLE,
			BANANA,
			GRAPE,
			MELON,
			PEACH,
			TOMATO,
		}

		s: string;
		// s = enum_to_string(Fruit.PEACH);
		fmt.println(s);

		f := Fruit.GRAPE;
		// s = enum_to_string(f);
		fmt.println(s);

		fmt.println(f);
		// See: runtime.odin
	}


	{
		// NOTE(bill): This is not safe code and I would not recommend this at all
		// I'd recommend you use `match type` to get the subtype rather than
		// casting pointers

		Fruit :: enum {
			APPLE,
			BANANA,
			GRAPE,
			MELON,
			PEACH,
			TOMATO,
		}

		fruit_ti := type_info(Fruit);
		name := (cast(^Type_Info.Named)fruit_ti).name; // Unsafe casts
		info := cast(^Type_Info.Enum)type_info_base(fruit_ti); // Unsafe casts

		fmt.printf("% :: enum % {\n", name, info.base);
		for i := 0; i < info.values.count; i += 1 {
			fmt.printf("\t%\t= %,\n", info.names[i], info.values[i]);
		}
		fmt.printf("}\n");

		// NOTE(bill): look at that type-safe printf!
	}

	{
		Vector3 :: struct {x, y, z: f32}

		a := Vector3{x = 1, y = 4, z = 9};
		fmt.println(a);
		b := Vector3{x = 9, y = 3, z = 1};
		fmt.println(b);

		// NOTE(bill): See fmt.odin
	}

	// n.b. This pretty much "solves" serialization (to strings)
}

// #import "test.odin"

namespaces_and_files :: proc() {

	// test.thing()
	// test.format.println()
	// test.println()
	/*
		// Non-exporting import
		#import "file.odin"
		#import "file.odin" as file
		#import "file.odin" as .
		#import "file.odin" as _

		// Exporting import
		#include "file.odin"
	*/

	// Talk about scope rules and diagram
}

miscellany :: proc() {
	/*
		win32 `__imp__` prefix
		#dll_import
		#dll_export

		Change exported name/symbol for linking
		#link_name

		Custom calling conventions
		#stdcall
		#fastcall

		Runtime stuff
		#shared_global_scope
	*/

	// assert(false)
	// compile_assert(false)
	// panic("Panic message goes here")
}