demo.odin 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. package main
  2. import "core:fmt"
  3. import "core:mem"
  4. import "core:os"
  5. when os.OS == "windows" {
  6. import "core:thread"
  7. }
  8. @(link_name="general_stuff")
  9. general_stuff :: proc() {
  10. fmt.println("# general_stuff");
  11. { // `do` for inline statements rather than block
  12. foo :: proc() do fmt.println("Foo!");
  13. if false do foo();
  14. for false do foo();
  15. when false do foo();
  16. if false do foo();
  17. else do foo();
  18. }
  19. { // Removal of `++` and `--` (again)
  20. x: int;
  21. x += 1;
  22. x -= 1;
  23. }
  24. { // Casting syntaxes
  25. i := i32(137);
  26. ptr := &i;
  27. _ = (^f32)(ptr);
  28. // ^f32(ptr) == ^(f32(ptr))
  29. _ = cast(^f32)ptr;
  30. _ = (^f32)(ptr)^;
  31. _ = (cast(^f32)ptr)^;
  32. // Questions: Should there be two ways to do it?
  33. }
  34. /*
  35. * Remove *_val_of built-in procedures
  36. * size_of, align_of, offset_of
  37. * type_of, type_info_of, typeid_of
  38. */
  39. { // `expand_to_tuple` built-in procedure
  40. Foo :: struct {
  41. x: int,
  42. b: bool,
  43. }
  44. f := Foo{137, true};
  45. x, b := expand_to_tuple(f);
  46. fmt.println(f);
  47. fmt.println(x, b);
  48. fmt.println(expand_to_tuple(f));
  49. }
  50. {
  51. // .. open range
  52. // ..< half-closed range
  53. for in 0..2 {} // 0, 1, 2
  54. for in 0..<2 {} // 0, 1
  55. }
  56. { // Multiple sized booleans
  57. x0: bool; // default
  58. x1: b8 = true;
  59. x2: b16 = false;
  60. x3: b32 = true;
  61. x4: b64 = false;
  62. fmt.printf("x0: %T = %v;\n", x0, x0);
  63. fmt.printf("x1: %T = %v;\n", x1, x1);
  64. fmt.printf("x2: %T = %v;\n", x2, x2);
  65. fmt.printf("x3: %T = %v;\n", x3, x3);
  66. fmt.printf("x4: %T = %v;\n", x4, x4);
  67. // Having specific sized booleans is very useful when dealing with foreign code
  68. // and to enforce specific alignment for a boolean, especially within a struct
  69. }
  70. { // `distinct` types
  71. // Originally, all type declarations would create a distinct type unless #type_alias was present.
  72. // Now the behaviour has been reversed. All type declarations create a type alias unless `distinct` is present.
  73. // If the type expression is `struct`, `union`, `enum`, `proc`, or `bit_field`, the types will always been distinct.
  74. Int32 :: i32;
  75. #assert(Int32 == i32);
  76. My_Int32 :: distinct i32;
  77. #assert(My_Int32 != i32);
  78. My_Struct :: struct{x: int};
  79. #assert(My_Struct != struct{x: int});
  80. My_Struct2 :: My_Struct;
  81. #assert(My_Struct2 == My_Struct);
  82. }
  83. {
  84. X :: 123;
  85. when #defined(X) {
  86. fmt.println("X is defined");
  87. } else {
  88. fmt.println("X is not defined");
  89. }
  90. when #defined(Y) {
  91. fmt.println("Y is defined");
  92. } else {
  93. fmt.println("Y is not defined");
  94. }
  95. }
  96. { // Labelled control blocks
  97. block: {
  98. if true {
  99. fmt.println("break block;");
  100. break block;
  101. }
  102. }
  103. {
  104. branch: if true {
  105. fmt.println("break branch;");
  106. break branch;
  107. }
  108. }
  109. {
  110. loop: for true {
  111. fmt.println("break loop;");
  112. break loop;
  113. }
  114. }
  115. {
  116. cases: switch {
  117. case:
  118. fmt.println("break cases;");
  119. break cases;
  120. }
  121. }
  122. }
  123. }
  124. union_type :: proc() {
  125. fmt.println("\n# union_type");
  126. {
  127. val: union{int, bool};
  128. val = 137;
  129. if i, ok := val.(int); ok {
  130. fmt.println(i);
  131. }
  132. val = true;
  133. fmt.println(val);
  134. val = nil;
  135. switch v in val {
  136. case int: fmt.println("int", v);
  137. case bool: fmt.println("bool", v);
  138. case: fmt.println("nil");
  139. }
  140. }
  141. {
  142. // There is a duality between `any` and `union`
  143. // An `any` has a pointer to the data and allows for any type (open)
  144. // A `union` has as binary blob to store the data and allows only certain types (closed)
  145. // The following code is with `any` but has the same syntax
  146. val: any;
  147. val = 137;
  148. if i, ok := val.(int); ok {
  149. fmt.println(i);
  150. }
  151. val = true;
  152. fmt.println(val);
  153. val = nil;
  154. switch v in val {
  155. case int: fmt.println("int", v);
  156. case bool: fmt.println("bool", v);
  157. case: fmt.println("nil");
  158. }
  159. }
  160. Vector3 :: struct {x, y, z: f32};
  161. Quaternion :: struct {x, y, z, w: f32};
  162. // More realistic examples
  163. {
  164. // NOTE(bill): For the above basic examples, you may not have any
  165. // particular use for it. However, my main use for them is not for these
  166. // simple cases. My main use is for hierarchical types. Many prefer
  167. // subtyping, embedding the base data into the derived types. Below is
  168. // an example of this for a basic game Entity.
  169. Entity :: struct {
  170. id: u64,
  171. name: string,
  172. position: Vector3,
  173. orientation: Quaternion,
  174. derived: any,
  175. }
  176. Frog :: struct {
  177. using entity: Entity,
  178. jump_height: f32,
  179. }
  180. Monster :: struct {
  181. using entity: Entity,
  182. is_robot: bool,
  183. is_zombie: bool,
  184. }
  185. // See `parametric_polymorphism` procedure for details
  186. new_entity :: proc($T: typeid) -> ^Entity {
  187. t := new(T);
  188. t.derived = t^;
  189. return t;
  190. }
  191. entity := new_entity(Monster);
  192. switch e in entity.derived {
  193. case Frog:
  194. fmt.println("Ribbit");
  195. case Monster:
  196. if e.is_robot do fmt.println("Robotic");
  197. if e.is_zombie do fmt.println("Grrrr!");
  198. fmt.println("I'm a monster");
  199. }
  200. }
  201. {
  202. // NOTE(bill): A union can be used to achieve something similar. Instead
  203. // of embedding the base data into the derived types, the derived data
  204. // in embedded into the base type. Below is the same example of the
  205. // basic game Entity but using an union.
  206. Entity :: struct {
  207. id: u64,
  208. name: string,
  209. position: Vector3,
  210. orientation: Quaternion,
  211. derived: union {Frog, Monster},
  212. }
  213. Frog :: struct {
  214. using entity: ^Entity,
  215. jump_height: f32,
  216. }
  217. Monster :: struct {
  218. using entity: ^Entity,
  219. is_robot: bool,
  220. is_zombie: bool,
  221. }
  222. // See `parametric_polymorphism` procedure for details
  223. new_entity :: proc($T: typeid) -> ^Entity {
  224. t := new(Entity);
  225. t.derived = T{entity = t};
  226. return t;
  227. }
  228. entity := new_entity(Monster);
  229. switch e in entity.derived {
  230. case Frog:
  231. fmt.println("Ribbit");
  232. case Monster:
  233. if e.is_robot do fmt.println("Robotic");
  234. if e.is_zombie do fmt.println("Grrrr!");
  235. }
  236. // NOTE(bill): As you can see, the usage code has not changed, only its
  237. // memory layout. Both approaches have their own advantages but they can
  238. // be used together to achieve different results. The subtyping approach
  239. // can allow for a greater control of the memory layout and memory
  240. // allocation, e.g. storing the derivatives together. However, this is
  241. // also its disadvantage. You must either preallocate arrays for each
  242. // derivative separation (which can be easily missed) or preallocate a
  243. // bunch of "raw" memory; determining the maximum size of the derived
  244. // types would require the aid of metaprogramming. Unions solve this
  245. // particular problem as the data is stored with the base data.
  246. // Therefore, it is possible to preallocate, e.g. [100]Entity.
  247. // It should be noted that the union approach can have the same memory
  248. // layout as the any and with the same type restrictions by using a
  249. // pointer type for the derivatives.
  250. /*
  251. Entity :: struct {
  252. ..
  253. derived: union{^Frog, ^Monster},
  254. }
  255. Frog :: struct {
  256. using entity: Entity,
  257. ..
  258. }
  259. Monster :: struct {
  260. using entity: Entity,
  261. ..
  262. }
  263. new_entity :: proc(T: type) -> ^Entity {
  264. t := new(T);
  265. t.derived = t;
  266. return t;
  267. }
  268. */
  269. }
  270. }
  271. parametric_polymorphism :: proc() {
  272. fmt.println("# parametric_polymorphism");
  273. print_value :: proc(value: $T) {
  274. fmt.printf("print_value: %T %v\n", value, value);
  275. }
  276. v1: int = 1;
  277. v2: f32 = 2.1;
  278. v3: f64 = 3.14;
  279. v4: string = "message";
  280. print_value(v1);
  281. print_value(v2);
  282. print_value(v3);
  283. print_value(v4);
  284. fmt.println();
  285. add :: proc(p, q: $T) -> T {
  286. x: T = p + q;
  287. return x;
  288. }
  289. a := add(3, 4);
  290. fmt.printf("a: %T = %v\n", a, a);
  291. b := add(3.2, 4.3);
  292. fmt.printf("b: %T = %v\n", b, b);
  293. // This is how `new` is implemented
  294. alloc_type :: proc($T: typeid) -> ^T {
  295. t := cast(^T)alloc(size_of(T), align_of(T));
  296. t^ = T{}; // Use default initialization value
  297. return t;
  298. }
  299. copy_slice :: proc(dst, src: []$T) -> int {
  300. n := min(len(dst), len(src));
  301. if n > 0 {
  302. mem.copy(&dst[0], &src[0], n*size_of(T));
  303. }
  304. return n;
  305. }
  306. double_params :: proc(a: $A, b: $B) -> A {
  307. return a + A(b);
  308. }
  309. fmt.println(double_params(12, 1.345));
  310. { // Polymorphic Types and Type Specialization
  311. Table_Slot :: struct(Key, Value: typeid) {
  312. occupied: bool,
  313. hash: u32,
  314. key: Key,
  315. value: Value,
  316. }
  317. TABLE_SIZE_MIN :: 32;
  318. Table :: struct(Key, Value: typeid) {
  319. count: int,
  320. allocator: mem.Allocator,
  321. slots: []Table_Slot(Key, Value),
  322. }
  323. // Only allow types that are specializations of a (polymorphic) slice
  324. make_slice :: proc($T: typeid/[]$E, len: int) -> T {
  325. return make(T, len);
  326. }
  327. // Only allow types that are specializations of `Table`
  328. allocate :: proc(table: ^$T/Table, capacity: int) {
  329. c := context;
  330. if table.allocator.procedure != nil do c.allocator = table.allocator;
  331. context = c;
  332. table.slots = make_slice(type_of(table.slots), max(capacity, TABLE_SIZE_MIN));
  333. }
  334. expand :: proc(table: ^$T/Table) {
  335. c := context;
  336. if table.allocator.procedure != nil do c.allocator = table.allocator;
  337. context = c;
  338. old_slots := table.slots;
  339. defer delete(old_slots);
  340. cap := max(2*len(table.slots), TABLE_SIZE_MIN);
  341. allocate(table, cap);
  342. for s in old_slots do if s.occupied {
  343. put(table, s.key, s.value);
  344. }
  345. }
  346. // Polymorphic determination of a polymorphic struct
  347. // put :: proc(table: ^$T/Table, key: T.Key, value: T.Value) {
  348. put :: proc(table: ^Table($Key, $Value), key: Key, value: Value) {
  349. hash := get_hash(key); // Ad-hoc method which would fail in a different scope
  350. index := find_index(table, key, hash);
  351. if index < 0 {
  352. if f64(table.count) >= 0.75*f64(len(table.slots)) {
  353. expand(table);
  354. }
  355. assert(table.count <= len(table.slots));
  356. index = int(hash % u32(len(table.slots)));
  357. for table.slots[index].occupied {
  358. if index += 1; index >= len(table.slots) {
  359. index = 0;
  360. }
  361. }
  362. table.count += 1;
  363. }
  364. slot := &table.slots[index];
  365. slot.occupied = true;
  366. slot.hash = hash;
  367. slot.key = key;
  368. slot.value = value;
  369. }
  370. // find :: proc(table: ^$T/Table, key: T.Key) -> (T.Value, bool) {
  371. find :: proc(table: ^Table($Key, $Value), key: Key) -> (Value, bool) {
  372. hash := get_hash(key);
  373. index := find_index(table, key, hash);
  374. if index < 0 {
  375. return Value{}, false;
  376. }
  377. return table.slots[index].value, true;
  378. }
  379. find_index :: proc(table: ^Table($Key, $Value), key: Key, hash: u32) -> int {
  380. if len(table.slots) <= 0 do return -1;
  381. index := int(hash % u32(len(table.slots)));
  382. for table.slots[index].occupied {
  383. if table.slots[index].hash == hash {
  384. if table.slots[index].key == key {
  385. return index;
  386. }
  387. }
  388. if index += 1; index >= len(table.slots) {
  389. index = 0;
  390. }
  391. }
  392. return -1;
  393. }
  394. get_hash :: proc(s: string) -> u32 { // fnv32a
  395. h: u32 = 0x811c9dc5;
  396. for i in 0..<len(s) {
  397. h = (h ~ u32(s[i])) * 0x01000193;
  398. }
  399. return h;
  400. }
  401. table: Table(string, int);
  402. for i in 0..36 do put(&table, "Hellope", i);
  403. for i in 0..42 do put(&table, "World!", i);
  404. found, _ := find(&table, "Hellope");
  405. fmt.printf("`found` is %v\n", found);
  406. found, _ = find(&table, "World!");
  407. fmt.printf("`found` is %v\n", found);
  408. // I would not personally design a hash table like this in production
  409. // but this is a nice basic example
  410. // A better approach would either use a `u64` or equivalent for the key
  411. // and let the user specify the hashing function or make the user store
  412. // the hashing procedure with the table
  413. }
  414. { // Parametric polymorphic union
  415. Error :: enum {
  416. Foo0,
  417. Foo1,
  418. Foo2,
  419. Foo3,
  420. }
  421. Para_Union :: union(T: typeid) {T, Error};
  422. r: Para_Union(int);
  423. fmt.println(typeid_of(type_of(r)));
  424. fmt.println(r);
  425. r = 123;
  426. fmt.println(r);
  427. r = Error.Foo0;
  428. fmt.println(r);
  429. }
  430. { // Polymorphic names
  431. foo :: proc($N: $I, $T: typeid) -> (res: [N]T) {
  432. // `N` is the constant value passed
  433. // `I` is the type of N
  434. // `T` is the type passed
  435. fmt.printf("Generating an array of type %v from the value %v of type %v\n",
  436. typeid_of(type_of(res)), N, typeid_of(I));
  437. for i in 0..<N {
  438. res[i] = T(i*i);
  439. }
  440. return;
  441. }
  442. T :: int;
  443. array := foo(4, T);
  444. for v, i in array {
  445. assert(v == T(i*i));
  446. }
  447. }
  448. }
  449. prefix_table := [?]string{
  450. "White",
  451. "Red",
  452. "Green",
  453. "Blue",
  454. "Octarine",
  455. "Black",
  456. };
  457. threading_example :: proc() {
  458. when os.OS == "windows" {
  459. fmt.println("# threading_example");
  460. worker_proc :: proc(t: ^thread.Thread) -> int {
  461. for iteration in 1..5 {
  462. fmt.printf("Thread %d is on iteration %d\n", t.user_index, iteration);
  463. fmt.printf("`%s`: iteration %d\n", prefix_table[t.user_index], iteration);
  464. // win32.sleep(1);
  465. }
  466. return 0;
  467. }
  468. threads := make([dynamic]^thread.Thread, 0, len(prefix_table));
  469. defer delete(threads);
  470. for in prefix_table {
  471. if t := thread.create(worker_proc); t != nil {
  472. t.init_context = context;
  473. t.use_init_context = true;
  474. t.user_index = len(threads);
  475. append(&threads, t);
  476. thread.start(t);
  477. }
  478. }
  479. for len(threads) > 0 {
  480. for i := 0; i < len(threads); /**/ {
  481. if t := threads[i]; thread.is_done(t) {
  482. fmt.printf("Thread %d is done\n", t.user_index);
  483. thread.destroy(t);
  484. ordered_remove(&threads, i);
  485. } else {
  486. i += 1;
  487. }
  488. }
  489. }
  490. }
  491. }
  492. array_programming :: proc() {
  493. fmt.println("# array_programming");
  494. {
  495. a := [3]f32{1, 2, 3};
  496. b := [3]f32{5, 6, 7};
  497. c := a * b;
  498. d := a + b;
  499. e := 1 + (c - d) / 2;
  500. fmt.printf("%.1f\n", e); // [0.5, 3.0, 6.5]
  501. }
  502. {
  503. a := [3]f32{1, 2, 3};
  504. b := swizzle(a, 2, 1, 0);
  505. assert(b == [3]f32{3, 2, 1});
  506. c := swizzle(a, 0, 0);
  507. assert(c == [2]f32{1, 1});
  508. assert(c == 1);
  509. }
  510. {
  511. Vector3 :: distinct [3]f32;
  512. a := Vector3{1, 2, 3};
  513. b := Vector3{5, 6, 7};
  514. c := (a * b)/2 + 1;
  515. d := c.x + c.y + c.z;
  516. fmt.printf("%.1f\n", d); // 22.0
  517. cross :: proc(a, b: Vector3) -> Vector3 {
  518. i := swizzle(a, 1, 2, 0) * swizzle(b, 2, 0, 1);
  519. j := swizzle(a, 2, 0, 1) * swizzle(b, 1, 2, 0);
  520. return i - j;
  521. }
  522. blah :: proc(a: Vector3) -> f32 {
  523. return a.x + a.y + a.z;
  524. }
  525. x := cross(a, b);
  526. fmt.println(x);
  527. fmt.println(blah(x));
  528. }
  529. }
  530. named_proc_return_parameters :: proc() {
  531. fmt.println("# named proc return parameters");
  532. foo0 :: proc() -> int {
  533. return 123;
  534. }
  535. foo1 :: proc() -> (a: int) {
  536. a = 123;
  537. return;
  538. }
  539. foo2 :: proc() -> (a, b: int) {
  540. // Named return values act like variables within the scope
  541. a = 321;
  542. b = 567;
  543. return b, a;
  544. }
  545. fmt.println("foo0 =", foo0()); // 123
  546. fmt.println("foo1 =", foo1()); // 123
  547. fmt.println("foo2 =", foo2()); // 567 321
  548. }
  549. using_enum :: proc() {
  550. fmt.println("# using enum");
  551. using Foo :: enum {A, B, C};
  552. f0 := A;
  553. f1 := B;
  554. f2 := C;
  555. fmt.println(f0, f1, f2);
  556. fmt.println(len(Foo));
  557. }
  558. map_type :: proc() {
  559. fmt.println("# map type");
  560. // enums of type u16, u32, i16 & i32 also work
  561. Enum_u8 :: enum u8 {
  562. A = 0,
  563. B = 1 << 8 - 1,
  564. }
  565. Enum_u64 :: enum u64 {
  566. A = 0,
  567. B = 1 << 64 - 1,
  568. }
  569. Enum_i8 :: enum i8 {
  570. A = 0,
  571. B = -(1 << 7),
  572. }
  573. Enum_i64 :: enum i64 {
  574. A = 0,
  575. B = -(1 << 63),
  576. }
  577. map_u8: map[Enum_u8]u8;
  578. map_u8[Enum_u8.A] = u8(Enum_u8.B);
  579. assert(map_u8[Enum_u8.A] == u8(Enum_u8.B));
  580. fmt.println(map_u8);
  581. map_u64: map[Enum_u64]u64;
  582. map_u64[Enum_u64.A] = u64(Enum_u64.B);
  583. assert(map_u64[Enum_u64.A] == u64(Enum_u64.B));
  584. fmt.println(map_u64);
  585. map_i8: map[Enum_i8]i8;
  586. map_i8[Enum_i8.A] = i8(Enum_i8.B);
  587. assert(map_i8[Enum_i8.A] == i8(Enum_i8.B));
  588. fmt.println(map_i8);
  589. map_i64: map[Enum_i64]i64;
  590. map_i64[Enum_i64.A] = i64(Enum_i64.B);
  591. assert(map_i64[Enum_i64.A] == i64(Enum_i64.B));
  592. fmt.println(map_i64);
  593. demo_struct :: struct {
  594. member: Enum_i64,
  595. }
  596. map_string: map[string]demo_struct;
  597. map_string["Hellope!"] = demo_struct{Enum_i64.B};
  598. assert(map_string["Hellope!"].member == Enum_i64.B);
  599. assert("Hellope?" notin map_string);
  600. fmt.println(map_string);
  601. fmt.println("Hellope! in map_string:", "Hellope!" in map_string);
  602. fmt.println("Hellope? in map_string:", "Hellope?" in map_string);
  603. }
  604. implicit_selector_expression :: proc() {
  605. fmt.println("# implicit selector expression");
  606. Foo :: enum {A, B, C};
  607. f: Foo;
  608. f = .A;
  609. BAR :: bit_set[Foo]{.B, .C};
  610. switch f {
  611. case .A:
  612. fmt.println("HERE");
  613. case .B:
  614. fmt.println("NEVER");
  615. case .C:
  616. fmt.println("FOREVER");
  617. }
  618. my_map := make(map[Foo]int);
  619. defer delete(my_map);
  620. my_map[.A] = 123;
  621. my_map[Foo.B] = 345;
  622. fmt.println(my_map[.A] + my_map[Foo.B] + my_map[.C]);
  623. }
  624. explicit_procedure_overloading :: proc() {
  625. fmt.println("# explicit procedure overloading");
  626. add_ints :: proc(a, b: int) -> int {
  627. x := a + b;
  628. fmt.println("add_ints", x);
  629. return x;
  630. }
  631. add_floats :: proc(a, b: f32) -> f32 {
  632. x := a + b;
  633. fmt.println("add_floats", x);
  634. return x;
  635. }
  636. add_numbers :: proc(a: int, b: f32, c: u8) -> int {
  637. x := int(a) + int(b) + int(c);
  638. fmt.println("add_numbers", x);
  639. return x;
  640. }
  641. add :: proc{add_ints, add_floats, add_numbers};
  642. add(int(1), int(2));
  643. add(f32(1), f32(2));
  644. add(int(1), f32(2), u8(3));
  645. add(1, 2); // untyped ints coerce to int tighter than f32
  646. add(1.0, 2.0); // untyped floats coerce to f32 tighter than int
  647. add(1, 2, 3); // three parameters
  648. // Ambiguous answers
  649. // add(1.0, 2);
  650. // add(1, 2.0);
  651. }
  652. complete_switch :: proc() {
  653. fmt.println("# complete_switch");
  654. { // enum
  655. using Foo :: enum {
  656. A,
  657. B,
  658. C,
  659. D,
  660. }
  661. b := Foo.B;
  662. f := Foo.A;
  663. #complete switch f {
  664. case A: fmt.println("A");
  665. case B: fmt.println("B");
  666. case C: fmt.println("C");
  667. case D: fmt.println("D");
  668. case: fmt.println("?");
  669. }
  670. _ = b;
  671. }
  672. { // union
  673. Foo :: union {int, bool};
  674. f: Foo = 123;
  675. #complete switch in f {
  676. case int: fmt.println("int");
  677. case bool: fmt.println("bool");
  678. case:
  679. }
  680. }
  681. }
  682. cstring_example :: proc() {
  683. W :: "Hellope";
  684. X :: cstring(W);
  685. Y :: string(X);
  686. w := W;
  687. _ = w;
  688. x: cstring = X;
  689. y: string = Y;
  690. z := string(x);
  691. fmt.println(x, y, z);
  692. fmt.println(len(x), len(y), len(z));
  693. fmt.println(len(W), len(X), len(Y));
  694. // IMPORTANT NOTE for cstring variables
  695. // len(cstring) is O(N)
  696. // cast(string)cstring is O(N)
  697. }
  698. deprecated_attribute :: proc() {
  699. @(deprecated="Use foo_v2 instead")
  700. foo_v1 :: proc(x: int) {
  701. fmt.println("foo_v1");
  702. }
  703. foo_v2 :: proc(x: int) {
  704. fmt.println("foo_v2");
  705. }
  706. // NOTE: Uncomment to see the warning messages
  707. // foo_v1(1);
  708. }
  709. bit_set_type :: proc() {
  710. {
  711. using Day :: enum {
  712. Sunday,
  713. Monday,
  714. Tuesday,
  715. Wednesday,
  716. Thursday,
  717. Friday,
  718. Saturday,
  719. }
  720. Days :: distinct bit_set[Day];
  721. WEEKEND :: Days{Sunday, Saturday};
  722. d: Days;
  723. d = {Sunday, Monday};
  724. e := d | WEEKEND;
  725. e |= {Monday};
  726. fmt.println(d, e);
  727. ok := Saturday in e; // `in` is only allowed for `map` and `bit_set` types
  728. fmt.println(ok);
  729. if Saturday in e {
  730. fmt.println("Saturday in", e);
  731. }
  732. X :: Saturday in WEEKEND; // Constant evaluation
  733. fmt.println(X);
  734. fmt.println("Cardinality:", card(e));
  735. }
  736. {
  737. x: bit_set['A'..'Z'];
  738. #assert(size_of(x) == size_of(u32));
  739. y: bit_set[0..8; u16];
  740. fmt.println(typeid_of(type_of(x))); // bit_set[A..Z]
  741. fmt.println(typeid_of(type_of(y))); // bit_set[0..8; u16]
  742. incl(&x, 'F');
  743. assert('F' in x);
  744. excl(&x, 'F');
  745. assert('F' notin x);
  746. y |= {1, 4, 2};
  747. assert(2 in y);
  748. }
  749. {
  750. Letters :: bit_set['A'..'Z'];
  751. a := Letters{'A', 'B'};
  752. b := Letters{'A', 'B', 'C', 'D', 'F'};
  753. c := Letters{'A', 'B'};
  754. assert(a <= b); // 'a' is a subset of 'b'
  755. assert(b >= a); // 'b' is a superset of 'a'
  756. assert(a < b); // 'a' is a strict subset of 'b'
  757. assert(b > a); // 'b' is a strict superset of 'a'
  758. assert(!(a < c)); // 'a' is a not strict subset of 'c'
  759. assert(!(c > a)); // 'c' is a not strict superset of 'a'
  760. }
  761. }
  762. diverging_procedures :: proc() {
  763. // Diverging procedures may never return
  764. foo :: proc() -> ! {
  765. fmt.println("I'm a diverging procedure");
  766. }
  767. foo();
  768. }
  769. deferred_procedure_associations :: proc() {
  770. @(deferred_out=closure)
  771. open :: proc(s: string) -> bool {
  772. fmt.println(s);
  773. return true;
  774. }
  775. closure :: proc(ok: bool) {
  776. fmt.println("Goodbye?", ok);
  777. }
  778. if open("Welcome") {
  779. fmt.println("Something in the middle, mate.");
  780. }
  781. }
  782. main :: proc() {
  783. when true {
  784. general_stuff();
  785. union_type();
  786. parametric_polymorphism();
  787. threading_example();
  788. array_programming();
  789. named_proc_return_parameters();
  790. using_enum();
  791. map_type();
  792. implicit_selector_expression();
  793. explicit_procedure_overloading();
  794. complete_switch();
  795. cstring_example();
  796. deprecated_attribute();
  797. bit_set_type();
  798. diverging_procedures();
  799. deferred_procedure_associations();
  800. }
  801. }