demo.odin 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941
  1. package main
  2. import "core:fmt"
  3. import "core:mem"
  4. import "core:os"
  5. import "core:thread"
  6. import "core:time"
  7. import "core:reflect"
  8. import "intrinsics"
  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. # Installing Odin
  17. Getting Started - https://odin-lang.org/docs/install/
  18. Instructions for downloading and install the Odin compiler and libraries.
  19. # Learning Odin
  20. Overview of Odin - https://odin-lang.org/docs/overview/
  21. An overview of the Odin programming language.
  22. Frequently Asked Questions (FAQ) - https://odin-lang.org/docs/faq/
  23. Answers to common questions about Odin.
  24. */
  25. the_basics :: proc() {
  26. fmt.println("\n# the basics");
  27. { // The Basics
  28. fmt.println("Hellope");
  29. // Lexical elements and literals
  30. // A comment
  31. my_integer_variable: int; // A comment for documentaton
  32. // Multi-line comments begin with /* and end with */. Multi-line comments can
  33. // also be nested (unlike in C):
  34. /*
  35. You can have any text or code here and
  36. have it be commented.
  37. /*
  38. NOTE: comments can be nested!
  39. */
  40. */
  41. // String literals are enclosed in double quotes and character literals in single quotes.
  42. // Special characters are escaped with a backslash \
  43. some_string := "This is a string";
  44. _ = 'A'; // unicode codepoint literal
  45. _ = '\n';
  46. _ = "C:\\Windows\\notepad.exe";
  47. // Raw string literals are enclosed with single back ticks
  48. _ = `C:\Windows\notepad.exe`;
  49. // The length of a string in bytes can be found using the built-in `len` procedure:
  50. _ = len("Foo");
  51. _ = len(some_string);
  52. // Numbers
  53. // Numerical literals are written similar to most other programming languages.
  54. // A useful feature in Odin is that underscores are allowed for better
  55. // readability: 1_000_000_000 (one billion). A number that contains a dot is a
  56. // floating point literal: 1.0e9 (one billion). If a number literal is suffixed
  57. // with i, is an imaginary number literal: 2i (2 multiply the square root of -1).
  58. // Binary literals are prefixed with 0b, octal literals with 0o, and hexadecimal
  59. // literals 0x. A leading zero does not produce an octal constant (unlike C).
  60. // In Odin, if a number constant is possible to be represented by a type without
  61. // precision loss, it will automatically convert to that type.
  62. x: int = 1.0; // A float literal but it can be represented by an integer without precision loss
  63. // Constant literals are “untyped” which means that they can implicitly convert to a type.
  64. y: int; // `y` is typed of type `int`
  65. y = 1; // `1` is an untyped integer literal which can implicitly convert to `int`
  66. z: f64; // `z` is typed of type `f64` (64-bit floating point number)
  67. z = 1; // `1` is an untyped integer literals which can be implicity conver to `f64`
  68. // No need for any suffixes or decimal places like in other languages
  69. // CONSTANTS JUST WORK!!!
  70. // Assignment statements
  71. h: int = 123; // declares a new variable `h` with type `int` and assigns a value to it
  72. h = 637; // assigns a new value to `h`
  73. // `=` is the assignment operator
  74. // You can assign multiple variables with it:
  75. a, b := 1, "hello"; // declares `a` and `b` and infers the types from the assignments
  76. b, a = "byte", 0;
  77. // Note: `:=` is two tokens, `:` and `=`. The following are equivalent,
  78. /*
  79. i: int = 123;
  80. i: = 123;
  81. i := 123;
  82. */
  83. // Constant declarations
  84. // Constants are entities (symbols) which have an assigned value.
  85. // The constant’s value cannot be changed.
  86. // The constant’s value must be able to be evaluated at compile time:
  87. X :: "what"; // constant `X` has the untyped string value "what"
  88. // Constants can be explicitly typed like a variable declaration:
  89. Y : int : 123;
  90. Z :: Y + 7; // constant computations are possible
  91. _ = my_integer_variable;
  92. _ = x;
  93. }
  94. }
  95. control_flow :: proc() {
  96. fmt.println("\n# control flow");
  97. { // Control flow
  98. // For loop
  99. // Odin has only one loop statement, the `for` loop
  100. // Basic for loop
  101. for i := 0; i < 10; i += 1 {
  102. fmt.println(i);
  103. }
  104. // NOTE: Unlike other languages like C, there are no parentheses `( )` surrounding the three components.
  105. // Braces `{ }` or a `do` are always required>
  106. for i := 0; i < 10; i += 1 { }
  107. for i := 0; i < 10; i += 1 do fmt.print();
  108. // The initial and post statements are optional
  109. i := 0;
  110. for ; i < 10; {
  111. i += 1;
  112. }
  113. // These semicolons can be dropped. This `for` loop is equivalent to C's `while` loop
  114. i = 0;
  115. for i < 10 {
  116. i += 1;
  117. }
  118. // If the condition is omitted, this produces an infinite loop:
  119. for {
  120. break;
  121. }
  122. // Range-based for loop
  123. // The basic for loop
  124. for j := 0; j < 10; j += 1 {
  125. fmt.println(j);
  126. }
  127. // can also be written
  128. for j in 0..<10 {
  129. fmt.println(j);
  130. }
  131. for j in 0..9 {
  132. fmt.println(j);
  133. }
  134. // Certain built-in types can be iterated over
  135. some_string := "Hello, 世界";
  136. for character in some_string { // Strings are assumed to be UTF-8
  137. fmt.println(character);
  138. }
  139. some_array := [3]int{1, 4, 9};
  140. for value in some_array {
  141. fmt.println(value);
  142. }
  143. some_slice := []int{1, 4, 9};
  144. for value in some_slice {
  145. fmt.println(value);
  146. }
  147. some_dynamic_array := [dynamic]int{1, 4, 9};
  148. defer delete(some_dynamic_array);
  149. for value in some_dynamic_array {
  150. fmt.println(value);
  151. }
  152. some_map := map[string]int{"A" = 1, "C" = 9, "B" = 4};
  153. defer delete(some_map);
  154. for key in some_map {
  155. fmt.println(key);
  156. }
  157. // Alternatively a second index value can be added
  158. for character, index in some_string {
  159. fmt.println(index, character);
  160. }
  161. for value, index in some_array {
  162. fmt.println(index, value);
  163. }
  164. for value, index in some_slice {
  165. fmt.println(index, value);
  166. }
  167. for value, index in some_dynamic_array {
  168. fmt.println(index, value);
  169. }
  170. for key, value in some_map {
  171. fmt.println(key, value);
  172. }
  173. // The iterated values are copies and cannot be written to.
  174. // The following idiom is useful for iterating over a container in a by-reference manner:
  175. for _, idx in some_slice {
  176. some_slice[idx] = (idx+1)*(idx+1);
  177. }
  178. // If statements
  179. x := 123;
  180. if x >= 0 {
  181. fmt.println("x is positive");
  182. }
  183. if y := -34; y < 0 {
  184. fmt.println("y is negative");
  185. }
  186. if y := 123; y < 0 {
  187. fmt.println("y is negative");
  188. } else if y == 0 {
  189. fmt.println("y is zero");
  190. } else {
  191. fmt.println("y is positive");
  192. }
  193. // Switch statement
  194. // A switch statement is another way to write a sequence of if-else statements.
  195. // In Odin, the default case is denoted as a case without any expression.
  196. switch arch := ODIN_ARCH; arch {
  197. case "386":
  198. fmt.println("32-bit");
  199. case "amd64":
  200. fmt.println("64-bit");
  201. case: // default
  202. fmt.println("Unsupported architecture");
  203. }
  204. // Odin’s `switch` is like one in C or C++, except that Odin only runs the selected case.
  205. // This means that a `break` statement is not needed at the end of each case.
  206. // Another important difference is that the case values need not be integers nor constants.
  207. // To achieve a C-like fall through into the next case block, the keyword `fallthrough` can be used.
  208. one_angry_dwarf :: proc() -> int {
  209. fmt.println("one_angry_dwarf was called");
  210. return 1;
  211. }
  212. switch j := 0; j {
  213. case 0:
  214. case one_angry_dwarf():
  215. }
  216. // A switch statement without a condition is the same as `switch true`.
  217. // This can be used to write a clean and long if-else chain and have the
  218. // ability to break if needed
  219. switch {
  220. case x < 0:
  221. fmt.println("x is negative");
  222. case x == 0:
  223. fmt.println("x is zero");
  224. case:
  225. fmt.println("x is positive");
  226. }
  227. // A `switch` statement can also use ranges like a range-based loop:
  228. switch c := 'j'; c {
  229. case 'A'..'Z', 'a'..'z', '0'..'9':
  230. fmt.println("c is alphanumeric");
  231. }
  232. switch x {
  233. case 0..<10:
  234. fmt.println("units");
  235. case 10..<13:
  236. fmt.println("pre-teens");
  237. case 13..<20:
  238. fmt.println("teens");
  239. case 20..<30:
  240. fmt.println("twenties");
  241. }
  242. }
  243. { // Defer statement
  244. // A defer statement defers the execution of a statement until the end of
  245. // the scope it is in.
  246. // The following will print 4 then 234:
  247. {
  248. x := 123;
  249. defer fmt.println(x);
  250. {
  251. defer x = 4;
  252. x = 2;
  253. }
  254. fmt.println(x);
  255. x = 234;
  256. }
  257. // You can defer an entire block too:
  258. {
  259. bar :: proc() {}
  260. defer {
  261. fmt.println("1");
  262. fmt.println("2");
  263. }
  264. cond := false;
  265. defer if cond {
  266. bar();
  267. }
  268. }
  269. // Defer statements are executed in the reverse order that they were declared:
  270. {
  271. defer fmt.println("1");
  272. defer fmt.println("2");
  273. defer fmt.println("3");
  274. }
  275. // Will print 3, 2, and then 1.
  276. if false {
  277. f, err := os.open("my_file.txt");
  278. if err != 0 {
  279. // handle error
  280. }
  281. defer os.close(f);
  282. // rest of code
  283. }
  284. }
  285. { // When statement
  286. /*
  287. The when statement is almost identical to the if statement but with some differences:
  288. * Each condition must be a constant expression as a when
  289. statement is evaluated at compile time.
  290. * The statements within a branch do not create a new scope
  291. * The compiler checks the semantics and code only for statements
  292. that belong to the first condition that is true
  293. * An initial statement is not allowed in a when statement
  294. * when statements are allowed at file scope
  295. */
  296. // Example
  297. when ODIN_ARCH == "386" {
  298. fmt.println("32 bit");
  299. } else when ODIN_ARCH == "amd64" {
  300. fmt.println("64 bit");
  301. } else {
  302. fmt.println("Unsupported architecture");
  303. }
  304. // The when statement is very useful for writing platform specific code.
  305. // This is akin to the #if construct in C’s preprocessor however, in Odin,
  306. // it is type checked.
  307. }
  308. { // Branch statements
  309. cond, cond1, cond2 := false, false, false;
  310. one_step :: proc() { fmt.println("one_step"); }
  311. beyond :: proc() { fmt.println("beyond"); }
  312. // Break statement
  313. for cond {
  314. switch {
  315. case:
  316. if cond {
  317. break; // break out of the `switch` statement
  318. }
  319. }
  320. break; // break out of the `for` statement
  321. }
  322. loop: for cond1 {
  323. for cond2 {
  324. break loop; // leaves both loops
  325. }
  326. }
  327. // Continue statement
  328. for cond {
  329. if cond2 {
  330. continue;
  331. }
  332. fmt.println("Hellope");
  333. }
  334. // Fallthrough statement
  335. // Odin’s switch is like one in C or C++, except that Odin only runs the selected
  336. // case. This means that a break statement is not needed at the end of each case.
  337. // Another important difference is that the case values need not be integers nor
  338. // constants.
  339. // fallthrough can be used to explicitly fall through into the next case block:
  340. switch i := 0; i {
  341. case 0:
  342. one_step();
  343. fallthrough;
  344. case 1:
  345. beyond();
  346. }
  347. }
  348. }
  349. named_proc_return_parameters :: proc() {
  350. fmt.println("\n# named proc return parameters");
  351. foo0 :: proc() -> int {
  352. return 123;
  353. }
  354. foo1 :: proc() -> (a: int) {
  355. a = 123;
  356. return;
  357. }
  358. foo2 :: proc() -> (a, b: int) {
  359. // Named return values act like variables within the scope
  360. a = 321;
  361. b = 567;
  362. return b, a;
  363. }
  364. fmt.println("foo0 =", foo0()); // 123
  365. fmt.println("foo1 =", foo1()); // 123
  366. fmt.println("foo2 =", foo2()); // 567 321
  367. }
  368. explicit_procedure_overloading :: proc() {
  369. fmt.println("\n# explicit procedure overloading");
  370. add_ints :: proc(a, b: int) -> int {
  371. x := a + b;
  372. fmt.println("add_ints", x);
  373. return x;
  374. }
  375. add_floats :: proc(a, b: f32) -> f32 {
  376. x := a + b;
  377. fmt.println("add_floats", x);
  378. return x;
  379. }
  380. add_numbers :: proc(a: int, b: f32, c: u8) -> int {
  381. x := int(a) + int(b) + int(c);
  382. fmt.println("add_numbers", x);
  383. return x;
  384. }
  385. add :: proc{add_ints, add_floats, add_numbers};
  386. add(int(1), int(2));
  387. add(f32(1), f32(2));
  388. add(int(1), f32(2), u8(3));
  389. add(1, 2); // untyped ints coerce to int tighter than f32
  390. add(1.0, 2.0); // untyped floats coerce to f32 tighter than int
  391. add(1, 2, 3); // three parameters
  392. // Ambiguous answers
  393. // add(1.0, 2);
  394. // add(1, 2.0);
  395. }
  396. struct_type :: proc() {
  397. fmt.println("\n# struct type");
  398. // A struct is a record type in Odin. It is a collection of fields.
  399. // Struct fields are accessed by using a dot:
  400. {
  401. Vector2 :: struct {
  402. x: f32,
  403. y: f32,
  404. };
  405. v := Vector2{1, 2};
  406. v.x = 4;
  407. fmt.println(v.x);
  408. // Struct fields can be accessed through a struct pointer:
  409. v = Vector2{1, 2};
  410. p := &v;
  411. p.x = 1335;
  412. fmt.println(v);
  413. // We could write p^.x, however, it is to nice abstract the ability
  414. // to not explicitly dereference the pointer. This is very useful when
  415. // refactoring code to use a pointer rather than a value, and vice versa.
  416. }
  417. {
  418. // A struct literal can be denoted by providing the struct’s type
  419. // followed by {}. A struct literal must either provide all the
  420. // arguments or none:
  421. Vector3 :: struct {
  422. x, y, z: f32,
  423. };
  424. v: Vector3;
  425. v = Vector3{}; // Zero value
  426. v = Vector3{1, 4, 9};
  427. // You can list just a subset of the fields if you specify the
  428. // field by name (the order of the named fields does not matter):
  429. v = Vector3{z=1, y=2};
  430. assert(v.x == 0);
  431. assert(v.y == 2);
  432. assert(v.z == 1);
  433. }
  434. {
  435. // Structs can tagged with different memory layout and alignment requirements:
  436. a :: struct #align 4 {}; // align to 4 bytes
  437. b :: struct #packed {}; // remove padding between fields
  438. c :: struct #raw_union {}; // all fields share the same offset (0). This is the same as C's union
  439. }
  440. }
  441. union_type :: proc() {
  442. fmt.println("\n# union type");
  443. {
  444. val: union{int, bool};
  445. val = 137;
  446. if i, ok := val.(int); ok {
  447. fmt.println(i);
  448. }
  449. val = true;
  450. fmt.println(val);
  451. val = nil;
  452. switch v in val {
  453. case int: fmt.println("int", v);
  454. case bool: fmt.println("bool", v);
  455. case: fmt.println("nil");
  456. }
  457. }
  458. {
  459. // There is a duality between `any` and `union`
  460. // An `any` has a pointer to the data and allows for any type (open)
  461. // A `union` has as binary blob to store the data and allows only certain types (closed)
  462. // The following code is with `any` but has the same syntax
  463. val: any;
  464. val = 137;
  465. if i, ok := val.(int); ok {
  466. fmt.println(i);
  467. }
  468. val = true;
  469. fmt.println(val);
  470. val = nil;
  471. switch v in val {
  472. case int: fmt.println("int", v);
  473. case bool: fmt.println("bool", v);
  474. case: fmt.println("nil");
  475. }
  476. }
  477. Vector3 :: distinct [3]f32;
  478. Quaternion :: distinct quaternion128;
  479. // More realistic examples
  480. {
  481. // NOTE(bill): For the above basic examples, you may not have any
  482. // particular use for it. However, my main use for them is not for these
  483. // simple cases. My main use is for hierarchical types. Many prefer
  484. // subtyping, embedding the base data into the derived types. Below is
  485. // an example of this for a basic game Entity.
  486. Entity :: struct {
  487. id: u64,
  488. name: string,
  489. position: Vector3,
  490. orientation: Quaternion,
  491. derived: any,
  492. };
  493. Frog :: struct {
  494. using entity: Entity,
  495. jump_height: f32,
  496. };
  497. Monster :: struct {
  498. using entity: Entity,
  499. is_robot: bool,
  500. is_zombie: bool,
  501. };
  502. // See `parametric_polymorphism` procedure for details
  503. new_entity :: proc($T: typeid) -> ^Entity {
  504. t := new(T);
  505. t.derived = t^;
  506. return t;
  507. }
  508. entity := new_entity(Monster);
  509. switch e in entity.derived {
  510. case Frog:
  511. fmt.println("Ribbit");
  512. case Monster:
  513. if e.is_robot do fmt.println("Robotic");
  514. if e.is_zombie do fmt.println("Grrrr!");
  515. fmt.println("I'm a monster");
  516. }
  517. }
  518. {
  519. // NOTE(bill): A union can be used to achieve something similar. Instead
  520. // of embedding the base data into the derived types, the derived data
  521. // in embedded into the base type. Below is the same example of the
  522. // basic game Entity but using an union.
  523. Entity :: struct {
  524. id: u64,
  525. name: string,
  526. position: Vector3,
  527. orientation: Quaternion,
  528. derived: union {Frog, Monster},
  529. };
  530. Frog :: struct {
  531. using entity: ^Entity,
  532. jump_height: f32,
  533. };
  534. Monster :: struct {
  535. using entity: ^Entity,
  536. is_robot: bool,
  537. is_zombie: bool,
  538. };
  539. // See `parametric_polymorphism` procedure for details
  540. new_entity :: proc($T: typeid) -> ^Entity {
  541. t := new(Entity);
  542. t.derived = T{entity = t};
  543. return t;
  544. }
  545. entity := new_entity(Monster);
  546. switch e in entity.derived {
  547. case Frog:
  548. fmt.println("Ribbit");
  549. case Monster:
  550. if e.is_robot do fmt.println("Robotic");
  551. if e.is_zombie do fmt.println("Grrrr!");
  552. }
  553. // NOTE(bill): As you can see, the usage code has not changed, only its
  554. // memory layout. Both approaches have their own advantages but they can
  555. // be used together to achieve different results. The subtyping approach
  556. // can allow for a greater control of the memory layout and memory
  557. // allocation, e.g. storing the derivatives together. However, this is
  558. // also its disadvantage. You must either preallocate arrays for each
  559. // derivative separation (which can be easily missed) or preallocate a
  560. // bunch of "raw" memory; determining the maximum size of the derived
  561. // types would require the aid of metaprogramming. Unions solve this
  562. // particular problem as the data is stored with the base data.
  563. // Therefore, it is possible to preallocate, e.g. [100]Entity.
  564. // It should be noted that the union approach can have the same memory
  565. // layout as the any and with the same type restrictions by using a
  566. // pointer type for the derivatives.
  567. /*
  568. Entity :: struct {
  569. ...
  570. derived: union{^Frog, ^Monster},
  571. }
  572. Frog :: struct {
  573. using entity: Entity,
  574. ...
  575. }
  576. Monster :: struct {
  577. using entity: Entity,
  578. ...
  579. }
  580. new_entity :: proc(T: type) -> ^Entity {
  581. t := new(T);
  582. t.derived = t;
  583. return t;
  584. }
  585. */
  586. }
  587. }
  588. using_statement :: proc() {
  589. fmt.println("\n# using statement");
  590. // using can used to bring entities declared in a scope/namespace
  591. // into the current scope. This can be applied to import declarations,
  592. // import names, struct fields, procedure fields, and struct values.
  593. Vector3 :: struct{x, y, z: f32};
  594. {
  595. Entity :: struct {
  596. position: Vector3,
  597. orientation: quaternion128,
  598. };
  599. // It can used like this:
  600. foo0 :: proc(entity: ^Entity) {
  601. fmt.println(entity.position.x, entity.position.y, entity.position.z);
  602. }
  603. // The entity members can be brought into the procedure scope by using it:
  604. foo1 :: proc(entity: ^Entity) {
  605. using entity;
  606. fmt.println(position.x, position.y, position.z);
  607. }
  608. // The using can be applied to the parameter directly:
  609. foo2 :: proc(using entity: ^Entity) {
  610. fmt.println(position.x, position.y, position.z);
  611. }
  612. // It can also be applied to sub-fields:
  613. foo3 :: proc(entity: ^Entity) {
  614. using entity.position;
  615. fmt.println(x, y, z);
  616. }
  617. }
  618. {
  619. // We can also apply the using statement to the struct fields directly,
  620. // making all the fields of position appear as if they on Entity itself:
  621. Entity :: struct {
  622. using position: Vector3,
  623. orientation: quaternion128,
  624. };
  625. foo :: proc(entity: ^Entity) {
  626. fmt.println(entity.x, entity.y, entity.z);
  627. }
  628. // Subtype polymorphism
  629. // It is possible to get subtype polymorphism, similar to inheritance-like
  630. // functionality in C++, but without the requirement of vtables or unknown
  631. // struct layout:
  632. Colour :: struct {r, g, b, a: u8};
  633. Frog :: struct {
  634. ribbit_volume: f32,
  635. using entity: Entity,
  636. colour: Colour,
  637. };
  638. frog: Frog;
  639. // Both work
  640. foo(&frog.entity);
  641. foo(&frog);
  642. frog.x = 123;
  643. // Note: using can be applied to arbitrarily many things, which allows
  644. // the ability to have multiple subtype polymorphism (but also its issues).
  645. // Note: using’d fields can still be referred by name.
  646. }
  647. { // using on an enum declaration
  648. using Foo :: enum {A, B, C};
  649. f0 := A;
  650. f1 := B;
  651. f2 := C;
  652. fmt.println(f0, f1, f2);
  653. fmt.println(len(Foo));
  654. }
  655. }
  656. implicit_context_system :: proc() {
  657. fmt.println("\n# implicit context system");
  658. // In each scope, there is an implicit value named context. This
  659. // context variable is local to each scope and is implicitly passed
  660. // by pointer to any procedure call in that scope (if the procedure
  661. // has the Odin calling convention).
  662. // The main purpose of the implicit context system is for the ability
  663. // to intercept third-party code and libraries and modify their
  664. // functionality. One such case is modifying how a library allocates
  665. // something or logs something. In C, this was usually achieved with
  666. // the library defining macros which could be overridden so that the
  667. // user could define what he wanted. However, not many libraries
  668. // supported this in many languages by default which meant intercepting
  669. // third-party code to see what it does and to change how it does it is
  670. // not possible.
  671. c := context; // copy the current scope's context
  672. context.user_index = 456;
  673. {
  674. context.allocator = my_custom_allocator();
  675. context.user_index = 123;
  676. what_a_fool_believes(); // the `context` for this scope is implicitly passed to `what_a_fool_believes`
  677. }
  678. // `context` value is local to the scope it is in
  679. assert(context.user_index == 456);
  680. what_a_fool_believes :: proc() {
  681. c := context; // this `context` is the same as the parent procedure that it was called from
  682. // From this example, context.user_index == 123
  683. // An context.allocator is assigned to the return value of `my_custom_allocator()`
  684. assert(context.user_index == 123);
  685. // The memory management procedure use the `context.allocator` by
  686. // default unless explicitly specified otherwise
  687. china_grove := new(int);
  688. free(china_grove);
  689. _ = c;
  690. }
  691. my_custom_allocator :: mem.nil_allocator;
  692. _ = c;
  693. // By default, the context value has default values for its parameters which is
  694. // decided in the package runtime. What the defaults are are compiler specific.
  695. // To see what the implicit context value contains, please see the following
  696. // definition in package runtime.
  697. }
  698. parametric_polymorphism :: proc() {
  699. fmt.println("\n# parametric polymorphism");
  700. print_value :: proc(value: $T) {
  701. fmt.printf("print_value: %T %v\n", value, value);
  702. }
  703. v1: int = 1;
  704. v2: f32 = 2.1;
  705. v3: f64 = 3.14;
  706. v4: string = "message";
  707. print_value(v1);
  708. print_value(v2);
  709. print_value(v3);
  710. print_value(v4);
  711. fmt.println();
  712. add :: proc(p, q: $T) -> T {
  713. x: T = p + q;
  714. return x;
  715. }
  716. a := add(3, 4);
  717. fmt.printf("a: %T = %v\n", a, a);
  718. b := add(3.2, 4.3);
  719. fmt.printf("b: %T = %v\n", b, b);
  720. // This is how `new` is implemented
  721. alloc_type :: proc($T: typeid) -> ^T {
  722. t := cast(^T)alloc(size_of(T), align_of(T));
  723. t^ = T{}; // Use default initialization value
  724. return t;
  725. }
  726. copy_slice :: proc(dst, src: []$T) -> int {
  727. n := min(len(dst), len(src));
  728. if n > 0 {
  729. mem.copy(&dst[0], &src[0], n*size_of(T));
  730. }
  731. return n;
  732. }
  733. double_params :: proc(a: $A, b: $B) -> A {
  734. return a + A(b);
  735. }
  736. fmt.println(double_params(12, 1.345));
  737. { // Polymorphic Types and Type Specialization
  738. Table_Slot :: struct(Key, Value: typeid) {
  739. occupied: bool,
  740. hash: u32,
  741. key: Key,
  742. value: Value,
  743. };
  744. TABLE_SIZE_MIN :: 32;
  745. Table :: struct(Key, Value: typeid) {
  746. count: int,
  747. allocator: mem.Allocator,
  748. slots: []Table_Slot(Key, Value),
  749. };
  750. // Only allow types that are specializations of a (polymorphic) slice
  751. make_slice :: proc($T: typeid/[]$E, len: int) -> T {
  752. return make(T, len);
  753. }
  754. // Only allow types that are specializations of `Table`
  755. allocate :: proc(table: ^$T/Table, capacity: int) {
  756. c := context;
  757. if table.allocator.procedure != nil do c.allocator = table.allocator;
  758. context = c;
  759. table.slots = make_slice(type_of(table.slots), max(capacity, TABLE_SIZE_MIN));
  760. }
  761. expand :: proc(table: ^$T/Table) {
  762. c := context;
  763. if table.allocator.procedure != nil do c.allocator = table.allocator;
  764. context = c;
  765. old_slots := table.slots;
  766. defer delete(old_slots);
  767. cap := max(2*len(table.slots), TABLE_SIZE_MIN);
  768. allocate(table, cap);
  769. for s in old_slots do if s.occupied {
  770. put(table, s.key, s.value);
  771. }
  772. }
  773. // Polymorphic determination of a polymorphic struct
  774. // put :: proc(table: ^$T/Table, key: T.Key, value: T.Value) {
  775. put :: proc(table: ^Table($Key, $Value), key: Key, value: Value) {
  776. hash := get_hash(key); // Ad-hoc method which would fail in a different scope
  777. index := find_index(table, key, hash);
  778. if index < 0 {
  779. if f64(table.count) >= 0.75*f64(len(table.slots)) {
  780. expand(table);
  781. }
  782. assert(table.count <= len(table.slots));
  783. index = int(hash % u32(len(table.slots)));
  784. for table.slots[index].occupied {
  785. if index += 1; index >= len(table.slots) {
  786. index = 0;
  787. }
  788. }
  789. table.count += 1;
  790. }
  791. slot := &table.slots[index];
  792. slot.occupied = true;
  793. slot.hash = hash;
  794. slot.key = key;
  795. slot.value = value;
  796. }
  797. // find :: proc(table: ^$T/Table, key: T.Key) -> (T.Value, bool) {
  798. find :: proc(table: ^Table($Key, $Value), key: Key) -> (Value, bool) {
  799. hash := get_hash(key);
  800. index := find_index(table, key, hash);
  801. if index < 0 {
  802. return Value{}, false;
  803. }
  804. return table.slots[index].value, true;
  805. }
  806. find_index :: proc(table: ^Table($Key, $Value), key: Key, hash: u32) -> int {
  807. if len(table.slots) <= 0 do return -1;
  808. index := int(hash % u32(len(table.slots)));
  809. for table.slots[index].occupied {
  810. if table.slots[index].hash == hash {
  811. if table.slots[index].key == key {
  812. return index;
  813. }
  814. }
  815. if index += 1; index >= len(table.slots) {
  816. index = 0;
  817. }
  818. }
  819. return -1;
  820. }
  821. get_hash :: proc(s: string) -> u32 { // fnv32a
  822. h: u32 = 0x811c9dc5;
  823. for i in 0..<len(s) {
  824. h = (h ~ u32(s[i])) * 0x01000193;
  825. }
  826. return h;
  827. }
  828. table: Table(string, int);
  829. for i in 0..36 do put(&table, "Hellope", i);
  830. for i in 0..42 do put(&table, "World!", i);
  831. found, _ := find(&table, "Hellope");
  832. fmt.printf("`found` is %v\n", found);
  833. found, _ = find(&table, "World!");
  834. fmt.printf("`found` is %v\n", found);
  835. // I would not personally design a hash table like this in production
  836. // but this is a nice basic example
  837. // A better approach would either use a `u64` or equivalent for the key
  838. // and let the user specify the hashing function or make the user store
  839. // the hashing procedure with the table
  840. }
  841. { // Parametric polymorphic union
  842. Error :: enum {
  843. Foo0,
  844. Foo1,
  845. Foo2,
  846. Foo3,
  847. };
  848. Para_Union :: union(T: typeid) {T, Error};
  849. r: Para_Union(int);
  850. fmt.println(typeid_of(type_of(r)));
  851. fmt.println(r);
  852. r = 123;
  853. fmt.println(r);
  854. r = Error.Foo0; // r = .Foo0; is allow too, see implicit selector expressions below
  855. fmt.println(r);
  856. }
  857. { // Polymorphic names
  858. foo :: proc($N: $I, $T: typeid) -> (res: [N]T) {
  859. // `N` is the constant value passed
  860. // `I` is the type of N
  861. // `T` is the type passed
  862. fmt.printf("Generating an array of type %v from the value %v of type %v\n",
  863. typeid_of(type_of(res)), N, typeid_of(I));
  864. for i in 0..<N {
  865. res[i] = T(i*i);
  866. }
  867. return;
  868. }
  869. T :: int;
  870. array := foo(4, T);
  871. for v, i in array {
  872. assert(v == T(i*i));
  873. }
  874. // Matrix multiplication
  875. mul :: proc(a: [$M][$N]$T, b: [N][$P]T) -> (c: [M][P]T) {
  876. for i in 0..<M {
  877. for j in 0..<P {
  878. for k in 0..<N {
  879. c[i][j] += a[i][k] * b[k][j];
  880. }
  881. }
  882. }
  883. return;
  884. }
  885. x := [2][3]f32{
  886. {1, 2, 3},
  887. {3, 2, 1},
  888. };
  889. y := [3][2]f32{
  890. {0, 8},
  891. {6, 2},
  892. {8, 4},
  893. };
  894. z := mul(x, y);
  895. assert(z == {{36, 24}, {20, 32}});
  896. }
  897. }
  898. prefix_table := [?]string{
  899. "White",
  900. "Red",
  901. "Green",
  902. "Blue",
  903. "Octarine",
  904. "Black",
  905. };
  906. threading_example :: proc() {
  907. fmt.println("\n# threading_example");
  908. { // Basic Threads
  909. fmt.println("\n## Basic Threads");
  910. worker_proc :: proc(t: ^thread.Thread) {
  911. for iteration in 1..5 {
  912. fmt.printf("Thread %d is on iteration %d\n", t.user_index, iteration);
  913. fmt.printf("`%s`: iteration %d\n", prefix_table[t.user_index], iteration);
  914. time.sleep(1 * time.Millisecond);
  915. }
  916. }
  917. threads := make([dynamic]^thread.Thread, 0, len(prefix_table));
  918. defer delete(threads);
  919. for in prefix_table {
  920. if t := thread.create(worker_proc); t != nil {
  921. t.init_context = context;
  922. t.use_init_context = true;
  923. t.user_index = len(threads);
  924. append(&threads, t);
  925. thread.start(t);
  926. }
  927. }
  928. for len(threads) > 0 {
  929. for i := 0; i < len(threads); /**/ {
  930. if t := threads[i]; thread.is_done(t) {
  931. fmt.printf("Thread %d is done\n", t.user_index);
  932. thread.destroy(t);
  933. ordered_remove(&threads, i);
  934. } else {
  935. i += 1;
  936. }
  937. }
  938. }
  939. }
  940. { // Thread Pool
  941. fmt.println("\n## Thread Pool");
  942. task_proc :: proc(t: ^thread.Task) {
  943. index := t.user_index % len(prefix_table);
  944. for iteration in 1..5 {
  945. fmt.printf("Worker Task %d is on iteration %d\n", t.user_index, iteration);
  946. fmt.printf("`%s`: iteration %d\n", prefix_table[index], iteration);
  947. time.sleep(1 * time.Millisecond);
  948. }
  949. }
  950. pool: thread.Pool;
  951. thread.pool_init(pool=&pool, thread_count=3);
  952. defer thread.pool_destroy(&pool);
  953. for i in 0..<30 {
  954. thread.pool_add_task(pool=&pool, procedure=task_proc, data=nil, user_index=i);
  955. }
  956. thread.pool_start(&pool);
  957. thread.pool_wait_and_process(&pool);
  958. }
  959. }
  960. array_programming :: proc() {
  961. fmt.println("\n# array programming");
  962. {
  963. a := [3]f32{1, 2, 3};
  964. b := [3]f32{5, 6, 7};
  965. c := a * b;
  966. d := a + b;
  967. e := 1 + (c - d) / 2;
  968. fmt.printf("%.1f\n", e); // [0.5, 3.0, 6.5]
  969. }
  970. {
  971. a := [3]f32{1, 2, 3};
  972. b := swizzle(a, 2, 1, 0);
  973. assert(b == [3]f32{3, 2, 1});
  974. c := swizzle(a, 0, 0);
  975. assert(c == [2]f32{1, 1});
  976. assert(c == 1);
  977. }
  978. {
  979. Vector3 :: distinct [3]f32;
  980. a := Vector3{1, 2, 3};
  981. b := Vector3{5, 6, 7};
  982. c := (a * b)/2 + 1;
  983. d := c.x + c.y + c.z;
  984. fmt.printf("%.1f\n", d); // 22.0
  985. cross :: proc(a, b: Vector3) -> Vector3 {
  986. i := swizzle(a, 1, 2, 0) * swizzle(b, 2, 0, 1);
  987. j := swizzle(a, 2, 0, 1) * swizzle(b, 1, 2, 0);
  988. return i - j;
  989. }
  990. blah :: proc(a: Vector3) -> f32 {
  991. return a.x + a.y + a.z;
  992. }
  993. x := cross(a, b);
  994. fmt.println(x);
  995. fmt.println(blah(x));
  996. }
  997. }
  998. map_type :: proc() {
  999. fmt.println("\n# map type");
  1000. m := make(map[string]int);
  1001. defer delete(m);
  1002. m["Bob"] = 2;
  1003. m["Ted"] = 5;
  1004. fmt.println(m["Bob"]);
  1005. delete_key(&m, "Ted");
  1006. // If an element of a key does not exist, the zero value of the
  1007. // element will be returned. To check to see if an element exists
  1008. // can be done in two ways:
  1009. elem, ok := m["Bob"];
  1010. exists := "Bob" in m;
  1011. _, _ = elem, ok;
  1012. _ = exists;
  1013. }
  1014. implicit_selector_expression :: proc() {
  1015. fmt.println("\n# implicit selector expression");
  1016. Foo :: enum {A, B, C};
  1017. f: Foo;
  1018. f = Foo.A;
  1019. f = .A;
  1020. BAR :: bit_set[Foo]{.B, .C};
  1021. switch f {
  1022. case .A:
  1023. fmt.println("HERE");
  1024. case .B:
  1025. fmt.println("NEVER");
  1026. case .C:
  1027. fmt.println("FOREVER");
  1028. }
  1029. my_map := make(map[Foo]int);
  1030. defer delete(my_map);
  1031. my_map[.A] = 123;
  1032. my_map[Foo.B] = 345;
  1033. fmt.println(my_map[.A] + my_map[Foo.B] + my_map[.C]);
  1034. }
  1035. partial_switch :: proc() {
  1036. fmt.println("\n# partial_switch");
  1037. { // enum
  1038. Foo :: enum {
  1039. A,
  1040. B,
  1041. C,
  1042. D,
  1043. };
  1044. f := Foo.A;
  1045. switch f {
  1046. case .A: fmt.println("A");
  1047. case .B: fmt.println("B");
  1048. case .C: fmt.println("C");
  1049. case .D: fmt.println("D");
  1050. case: fmt.println("?");
  1051. }
  1052. #partial switch f {
  1053. case .A: fmt.println("A");
  1054. case .D: fmt.println("D");
  1055. }
  1056. }
  1057. { // union
  1058. Foo :: union {int, bool};
  1059. f: Foo = 123;
  1060. switch in f {
  1061. case int: fmt.println("int");
  1062. case bool: fmt.println("bool");
  1063. case:
  1064. }
  1065. #partial switch in f {
  1066. case bool: fmt.println("bool");
  1067. }
  1068. }
  1069. }
  1070. cstring_example :: proc() {
  1071. fmt.println("\n# cstring_example");
  1072. W :: "Hellope";
  1073. X :: cstring(W);
  1074. Y :: string(X);
  1075. w := W;
  1076. _ = w;
  1077. x: cstring = X;
  1078. y: string = Y;
  1079. z := string(x);
  1080. fmt.println(x, y, z);
  1081. fmt.println(len(x), len(y), len(z));
  1082. fmt.println(len(W), len(X), len(Y));
  1083. // IMPORTANT NOTE for cstring variables
  1084. // len(cstring) is O(N)
  1085. // cast(string)cstring is O(N)
  1086. }
  1087. bit_set_type :: proc() {
  1088. fmt.println("\n# bit_set type");
  1089. {
  1090. using Day :: enum {
  1091. Sunday,
  1092. Monday,
  1093. Tuesday,
  1094. Wednesday,
  1095. Thursday,
  1096. Friday,
  1097. Saturday,
  1098. };
  1099. Days :: distinct bit_set[Day];
  1100. WEEKEND :: Days{Sunday, Saturday};
  1101. d: Days;
  1102. d = {Sunday, Monday};
  1103. e := d | WEEKEND;
  1104. e |= {Monday};
  1105. fmt.println(d, e);
  1106. ok := Saturday in e; // `in` is only allowed for `map` and `bit_set` types
  1107. fmt.println(ok);
  1108. if Saturday in e {
  1109. fmt.println("Saturday in", e);
  1110. }
  1111. X :: Saturday in WEEKEND; // Constant evaluation
  1112. fmt.println(X);
  1113. fmt.println("Cardinality:", card(e));
  1114. }
  1115. {
  1116. x: bit_set['A'..'Z'];
  1117. #assert(size_of(x) == size_of(u32));
  1118. y: bit_set[0..8; u16];
  1119. fmt.println(typeid_of(type_of(x))); // bit_set[A..Z]
  1120. fmt.println(typeid_of(type_of(y))); // bit_set[0..8; u16]
  1121. incl(&x, 'F');
  1122. assert('F' in x);
  1123. excl(&x, 'F');
  1124. assert('F' not_in x);
  1125. y |= {1, 4, 2};
  1126. assert(2 in y);
  1127. }
  1128. {
  1129. Letters :: bit_set['A'..'Z'];
  1130. a := Letters{'A', 'B'};
  1131. b := Letters{'A', 'B', 'C', 'D', 'F'};
  1132. c := Letters{'A', 'B'};
  1133. assert(a <= b); // 'a' is a subset of 'b'
  1134. assert(b >= a); // 'b' is a superset of 'a'
  1135. assert(a < b); // 'a' is a strict subset of 'b'
  1136. assert(b > a); // 'b' is a strict superset of 'a'
  1137. assert(!(a < c)); // 'a' is a not strict subset of 'c'
  1138. assert(!(c > a)); // 'c' is a not strict superset of 'a'
  1139. }
  1140. }
  1141. deferred_procedure_associations :: proc() {
  1142. fmt.println("\n# deferred procedure associations");
  1143. @(deferred_out=closure)
  1144. open :: proc(s: string) -> bool {
  1145. fmt.println(s);
  1146. return true;
  1147. }
  1148. closure :: proc(ok: bool) {
  1149. fmt.println("Goodbye?", ok);
  1150. }
  1151. if open("Welcome") {
  1152. fmt.println("Something in the middle, mate.");
  1153. }
  1154. }
  1155. reflection :: proc() {
  1156. fmt.println("\n# reflection");
  1157. Foo :: struct {
  1158. x: int `tag1`,
  1159. y: string `json:"y_field"`,
  1160. z: bool, // no tag
  1161. };
  1162. id := typeid_of(Foo);
  1163. names := reflect.struct_field_names(id);
  1164. types := reflect.struct_field_types(id);
  1165. tags := reflect.struct_field_tags(id);
  1166. assert(len(names) == len(types) && len(names) == len(tags));
  1167. fmt.println("Foo :: struct {");
  1168. for tag, i in tags {
  1169. name, type := names[i], types[i];
  1170. if tag != "" {
  1171. fmt.printf("\t%s: %T `%s`,\n", name, type, tag);
  1172. } else {
  1173. fmt.printf("\t%s: %T,\n", name, type);
  1174. }
  1175. }
  1176. fmt.println("}");
  1177. for tag, i in tags {
  1178. if val, ok := reflect.struct_tag_lookup(tag, "json"); ok {
  1179. fmt.printf("json: %s -> %s\n", names[i], val);
  1180. }
  1181. }
  1182. }
  1183. quaternions :: proc() {
  1184. // Not just an April Fool's Joke any more, but a fully working thing!
  1185. fmt.println("\n# quaternions");
  1186. { // Quaternion operations
  1187. q := 1 + 2i + 3j + 4k;
  1188. r := quaternion(5, 6, 7, 8);
  1189. t := q * r;
  1190. fmt.printf("(%v) * (%v) = %v\n", q, r, t);
  1191. v := q / r;
  1192. fmt.printf("(%v) / (%v) = %v\n", q, r, v);
  1193. u := q + r;
  1194. fmt.printf("(%v) + (%v) = %v\n", q, r, u);
  1195. s := q - r;
  1196. fmt.printf("(%v) - (%v) = %v\n", q, r, s);
  1197. }
  1198. { // The quaternion types
  1199. q128: quaternion128; // 4xf32
  1200. q256: quaternion256; // 4xf64
  1201. q128 = quaternion(1, 0, 0, 0);
  1202. q256 = 1; // quaternion(1, 0, 0, 0);
  1203. }
  1204. { // Built-in procedures
  1205. q := 1 + 2i + 3j + 4k;
  1206. fmt.println("q =", q);
  1207. fmt.println("real(q) =", real(q));
  1208. fmt.println("imag(q) =", imag(q));
  1209. fmt.println("jmag(q) =", jmag(q));
  1210. fmt.println("kmag(q) =", kmag(q));
  1211. fmt.println("conj(q) =", conj(q));
  1212. fmt.println("abs(q) =", abs(q));
  1213. }
  1214. { // Conversion of a complex type to a quaternion type
  1215. c := 1 + 2i;
  1216. q := quaternion256(c);
  1217. fmt.println(c);
  1218. fmt.println(q);
  1219. }
  1220. { // Memory layout of Quaternions
  1221. q := 1 + 2i + 3j + 4k;
  1222. a := transmute([4]f64)q;
  1223. fmt.println("Quaternion memory layout: xyzw/(ijkr)");
  1224. fmt.println(q); // 1.000+2.000i+3.000j+4.000k
  1225. fmt.println(a); // [2.000, 3.000, 4.000, 1.000]
  1226. }
  1227. }
  1228. inline_for_statement :: proc() {
  1229. fmt.println("\n#inline for statements");
  1230. // 'inline for' works the same as if the 'inline' prefix did not
  1231. // exist but these ranged loops are explicitly unrolled which can
  1232. // be very very useful for certain optimizations
  1233. fmt.println("Ranges");
  1234. inline for x, i in 1..<4 {
  1235. fmt.println(x, i);
  1236. }
  1237. fmt.println("Strings");
  1238. inline for r, i in "Hello, 世界" {
  1239. fmt.println(r, i);
  1240. }
  1241. fmt.println("Arrays");
  1242. inline for elem, idx in ([4]int{1, 4, 9, 16}) {
  1243. fmt.println(elem, idx);
  1244. }
  1245. Foo_Enum :: enum {
  1246. A = 1,
  1247. B,
  1248. C = 6,
  1249. D,
  1250. };
  1251. fmt.println("Enum types");
  1252. inline for elem, idx in Foo_Enum {
  1253. fmt.println(elem, idx);
  1254. }
  1255. }
  1256. where_clauses :: proc() {
  1257. fmt.println("\n#procedure 'where' clauses");
  1258. { // Sanity checks
  1259. simple_sanity_check :: proc(x: [2]int)
  1260. where len(x) > 1,
  1261. type_of(x) == [2]int {
  1262. fmt.println(x);
  1263. }
  1264. }
  1265. { // Parametric polymorphism checks
  1266. cross_2d :: proc(a, b: $T/[2]$E) -> E
  1267. where intrinsics.type_is_numeric(E) {
  1268. return a.x*b.y - a.y*b.x;
  1269. }
  1270. cross_3d :: proc(a, b: $T/[3]$E) -> T
  1271. where intrinsics.type_is_numeric(E) {
  1272. x := a.y*b.z - a.z*b.y;
  1273. y := a.z*b.x - a.x*b.z;
  1274. z := a.x*b.y - a.y*b.z;
  1275. return T{x, y, z};
  1276. }
  1277. a := [2]int{1, 2};
  1278. b := [2]int{5, -3};
  1279. fmt.println(cross_2d(a, b));
  1280. x := [3]f32{1, 4, 9};
  1281. y := [3]f32{-5, 0, 3};
  1282. fmt.println(cross_3d(x, y));
  1283. // Failure case
  1284. // i := [2]bool{true, false};
  1285. // j := [2]bool{false, true};
  1286. // fmt.println(cross_2d(i, j));
  1287. }
  1288. { // Procedure groups usage
  1289. foo :: proc(x: [$N]int) -> bool
  1290. where N > 2 {
  1291. fmt.println(#procedure, "was called with the parameter", x);
  1292. return true;
  1293. }
  1294. bar :: proc(x: [$N]int) -> bool
  1295. where 0 < N,
  1296. N <= 2 {
  1297. fmt.println(#procedure, "was called with the parameter", x);
  1298. return false;
  1299. }
  1300. baz :: proc{foo, bar};
  1301. x := [3]int{1, 2, 3};
  1302. y := [2]int{4, 9};
  1303. ok_x := baz(x);
  1304. ok_y := baz(y);
  1305. assert(ok_x == true);
  1306. assert(ok_y == false);
  1307. }
  1308. { // Record types
  1309. Foo :: struct(T: typeid, N: int)
  1310. where intrinsics.type_is_integer(T),
  1311. N > 2 {
  1312. x: [N]T,
  1313. y: [N-2]T,
  1314. };
  1315. T :: i32;
  1316. N :: 5;
  1317. f: Foo(T, N);
  1318. #assert(size_of(f) == (N+N-2)*size_of(T));
  1319. }
  1320. }
  1321. when ODIN_OS == "windows" do foreign import kernel32 "system:kernel32.lib"
  1322. foreign_system :: proc() {
  1323. fmt.println("\n#foreign system");
  1324. when ODIN_OS == "windows" {
  1325. // It is sometimes necessarily to interface with foreign code,
  1326. // such as a C library. In Odin, this is achieved through the
  1327. // foreign system. You can “import” a library into the code
  1328. // using the same semantics as a normal import declaration.
  1329. // This foreign import declaration will create a
  1330. // “foreign import name” which can then be used to associate
  1331. // entities within a foreign block.
  1332. foreign kernel32 {
  1333. ExitProcess :: proc "stdcall" (exit_code: u32) ---
  1334. }
  1335. // Foreign procedure declarations have the cdecl/c calling
  1336. // convention by default unless specified otherwise. Due to
  1337. // foreign procedures do not have a body declared within this
  1338. // code, you need append the --- symbol to the end to distinguish
  1339. // it as a procedure literal without a body and not a procedure type.
  1340. // The attributes system can be used to change specific properties
  1341. // of entities declared within a block:
  1342. @(default_calling_convention = "std")
  1343. foreign kernel32 {
  1344. @(link_name="GetLastError") get_last_error :: proc() -> i32 ---
  1345. }
  1346. // Example using the link_prefix attribute
  1347. @(default_calling_convention = "std")
  1348. @(link_prefix = "Get")
  1349. foreign kernel32 {
  1350. LastError :: proc() -> i32 ---
  1351. }
  1352. }
  1353. }
  1354. ranged_fields_for_array_compound_literals :: proc() {
  1355. fmt.println("\n#ranged fields for array compound literals");
  1356. { // Normal Array Literal
  1357. foo := [?]int{1, 4, 9, 16};
  1358. fmt.println(foo);
  1359. }
  1360. { // Indexed
  1361. foo := [?]int{
  1362. 3 = 16,
  1363. 1 = 4,
  1364. 2 = 9,
  1365. 0 = 1,
  1366. };
  1367. fmt.println(foo);
  1368. }
  1369. { // Ranges
  1370. i := 2;
  1371. foo := [?]int {
  1372. 0 = 123,
  1373. 5..9 = 54,
  1374. 10..<16 = i*3 + (i-1)*2,
  1375. };
  1376. #assert(len(foo) == 16);
  1377. fmt.println(foo); // [123, 0, 0, 0, 0, 54, 54, 54, 54, 54, 8, 8, 8, 8, 8]
  1378. }
  1379. { // Slice and Dynamic Array support
  1380. i := 2;
  1381. foo_slice := []int {
  1382. 0 = 123,
  1383. 5..9 = 54,
  1384. 10..<16 = i*3 + (i-1)*2,
  1385. };
  1386. assert(len(foo_slice) == 16);
  1387. fmt.println(foo_slice); // [123, 0, 0, 0, 0, 54, 54, 54, 54, 54, 8, 8, 8, 8, 8]
  1388. foo_dynamic_array := [dynamic]int {
  1389. 0 = 123,
  1390. 5..9 = 54,
  1391. 10..<16 = i*3 + (i-1)*2,
  1392. };
  1393. assert(len(foo_dynamic_array) == 16);
  1394. fmt.println(foo_dynamic_array); // [123, 0, 0, 0, 0, 54, 54, 54, 54, 54, 8, 8, 8, 8, 8]
  1395. }
  1396. }
  1397. deprecated_attribute :: proc() {
  1398. @(deprecated="Use foo_v2 instead")
  1399. foo_v1 :: proc(x: int) {
  1400. fmt.println("foo_v1");
  1401. }
  1402. foo_v2 :: proc(x: int) {
  1403. fmt.println("foo_v2");
  1404. }
  1405. // NOTE: Uncomment to see the warning messages
  1406. // foo_v1(1);
  1407. }
  1408. range_statements_with_multiple_return_values :: proc() {
  1409. // IMPORTANT NOTE(bill, 2019-11-02): This feature is subject to be changed/removed
  1410. fmt.println("\n#range statements with multiple return values");
  1411. My_Iterator :: struct {
  1412. index: int,
  1413. data: []i32,
  1414. };
  1415. make_my_iterator :: proc(data: []i32) -> My_Iterator {
  1416. return My_Iterator{data = data};
  1417. }
  1418. my_iterator :: proc(it: ^My_Iterator) -> (val: i32, idx: int, cond: bool) {
  1419. if cond = it.index < len(it.data); cond {
  1420. val = it.data[it.index];
  1421. idx = it.index;
  1422. it.index += 1;
  1423. }
  1424. return;
  1425. }
  1426. data := make([]i32, 6);
  1427. for _, i in data {
  1428. data[i] = i32(i*i);
  1429. }
  1430. {
  1431. it := make_my_iterator(data);
  1432. for val in my_iterator(&it) {
  1433. fmt.println(val);
  1434. }
  1435. }
  1436. {
  1437. it := make_my_iterator(data);
  1438. for val, idx in my_iterator(&it) {
  1439. fmt.println(val, idx);
  1440. }
  1441. }
  1442. {
  1443. it := make_my_iterator(data);
  1444. for {
  1445. val, _, cond := my_iterator(&it);
  1446. if !cond do break;
  1447. fmt.println(val);
  1448. }
  1449. }
  1450. }
  1451. soa_struct_layout :: proc() {
  1452. // IMPORTANT NOTE(bill, 2019-11-03): This feature is subject to be changed/removed
  1453. // NOTE(bill): Most likely #soa [N]T
  1454. fmt.println("\n#SOA Struct Layout");
  1455. {
  1456. Vector3 :: struct {x, y, z: f32};
  1457. N :: 2;
  1458. v_aos: [N]Vector3;
  1459. v_aos[0].x = 1;
  1460. v_aos[0].y = 4;
  1461. v_aos[0].z = 9;
  1462. fmt.println(len(v_aos));
  1463. fmt.println(v_aos[0]);
  1464. fmt.println(v_aos[0].x);
  1465. fmt.println(&v_aos[0].x);
  1466. v_aos[1] = {0, 3, 4};
  1467. v_aos[1].x = 2;
  1468. fmt.println(v_aos[1]);
  1469. fmt.println(v_aos);
  1470. v_soa: #soa[N]Vector3;
  1471. v_soa[0].x = 1;
  1472. v_soa[0].y = 4;
  1473. v_soa[0].z = 9;
  1474. // Same syntax as AOS and treat as if it was an array
  1475. fmt.println(len(v_soa));
  1476. fmt.println(v_soa[0]);
  1477. fmt.println(v_soa[0].x);
  1478. fmt.println(&v_soa[0].x);
  1479. v_soa[1] = {0, 3, 4};
  1480. v_soa[1].x = 2;
  1481. fmt.println(v_soa[1]);
  1482. // Can use SOA syntax if necessary
  1483. v_soa.x[0] = 1;
  1484. v_soa.y[0] = 4;
  1485. v_soa.z[0] = 9;
  1486. fmt.println(v_soa.x[0]);
  1487. // Same pointer addresses with both syntaxes
  1488. assert(&v_soa[0].x == &v_soa.x[0]);
  1489. // Same fmt printing
  1490. fmt.println(v_aos);
  1491. fmt.println(v_soa);
  1492. }
  1493. {
  1494. // Works with arrays of length <= 4 which have the implicit fields xyzw/rgba
  1495. Vector3 :: distinct [3]f32;
  1496. N :: 2;
  1497. v_aos: [N]Vector3;
  1498. v_aos[0].x = 1;
  1499. v_aos[0].y = 4;
  1500. v_aos[0].z = 9;
  1501. v_soa: #soa[N]Vector3;
  1502. v_soa[0].x = 1;
  1503. v_soa[0].y = 4;
  1504. v_soa[0].z = 9;
  1505. }
  1506. {
  1507. // SOA Slices
  1508. // Vector3 :: struct {x, y, z: f32};
  1509. Vector3 :: struct {x: i8, y: i16, z: f32};
  1510. N :: 3;
  1511. v: #soa[N]Vector3;
  1512. v[0].x = 1;
  1513. v[0].y = 4;
  1514. v[0].z = 9;
  1515. s: #soa[]Vector3;
  1516. s = v[:];
  1517. assert(len(s) == N);
  1518. fmt.println(s);
  1519. fmt.println(s[0].x);
  1520. a := s[1:2];
  1521. assert(len(a) == 1);
  1522. fmt.println(a);
  1523. d: #soa[dynamic]Vector3;
  1524. append_soa(&d, Vector3{1, 2, 3}, Vector3{4, 5, 9}, Vector3{-4, -4, 3});
  1525. fmt.println(d);
  1526. fmt.println(len(d));
  1527. fmt.println(cap(d));
  1528. fmt.println(d[:]);
  1529. }
  1530. }
  1531. constant_literal_expressions :: proc() {
  1532. fmt.println("\n#constant literal expressions");
  1533. Bar :: struct {x, y: f32};
  1534. Foo :: struct {a, b: int, using c: Bar};
  1535. FOO_CONST :: Foo{b = 2, a = 1, c = {3, 4}};
  1536. fmt.println(FOO_CONST.a);
  1537. fmt.println(FOO_CONST.b);
  1538. fmt.println(FOO_CONST.c);
  1539. fmt.println(FOO_CONST.c.x);
  1540. fmt.println(FOO_CONST.c.y);
  1541. fmt.println(FOO_CONST.x); // using works as expected
  1542. fmt.println(FOO_CONST.y);
  1543. fmt.println("-------");
  1544. ARRAY_CONST :: [3]int{1 = 4, 2 = 9, 0 = 1};
  1545. fmt.println(ARRAY_CONST[0]);
  1546. fmt.println(ARRAY_CONST[1]);
  1547. fmt.println(ARRAY_CONST[2]);
  1548. fmt.println("-------");
  1549. FOO_ARRAY_DEFAULTS :: [3]Foo{{}, {}, {}};
  1550. fmt.println(FOO_ARRAY_DEFAULTS[2].x);
  1551. fmt.println("-------");
  1552. Baz :: enum{A=5, B, C, D};
  1553. ENUM_ARRAY_CONST :: [Baz]int{.A .. .C = 1, .D = 16};
  1554. fmt.println(ENUM_ARRAY_CONST[.A]);
  1555. fmt.println(ENUM_ARRAY_CONST[.B]);
  1556. fmt.println(ENUM_ARRAY_CONST[.C]);
  1557. fmt.println(ENUM_ARRAY_CONST[.D]);
  1558. fmt.println("-------");
  1559. Partial_Baz :: enum{A=5, B, C, D=16};
  1560. #assert(len(Partial_Baz) < len(#partial [Partial_Baz]int));
  1561. PARTIAL_ENUM_ARRAY_CONST :: #partial [Partial_Baz]int{.A .. .C = 1, .D = 16};
  1562. fmt.println(PARTIAL_ENUM_ARRAY_CONST[.A]);
  1563. fmt.println(PARTIAL_ENUM_ARRAY_CONST[.B]);
  1564. fmt.println(PARTIAL_ENUM_ARRAY_CONST[.C]);
  1565. fmt.println(PARTIAL_ENUM_ARRAY_CONST[.D]);
  1566. fmt.println("-------");
  1567. STRING_CONST :: "Hellope!";
  1568. fmt.println(STRING_CONST[0]);
  1569. fmt.println(STRING_CONST[2]);
  1570. fmt.println(STRING_CONST[3]);
  1571. fmt.println(STRING_CONST[0:5]);
  1572. fmt.println(STRING_CONST[3:][:4]);
  1573. }
  1574. main :: proc() {
  1575. when true {
  1576. the_basics();
  1577. control_flow();
  1578. named_proc_return_parameters();
  1579. explicit_procedure_overloading();
  1580. struct_type();
  1581. union_type();
  1582. using_statement();
  1583. implicit_context_system();
  1584. parametric_polymorphism();
  1585. array_programming();
  1586. map_type();
  1587. implicit_selector_expression();
  1588. partial_switch();
  1589. cstring_example();
  1590. bit_set_type();
  1591. deferred_procedure_associations();
  1592. reflection();
  1593. quaternions();
  1594. inline_for_statement();
  1595. where_clauses();
  1596. foreign_system();
  1597. ranged_fields_for_array_compound_literals();
  1598. deprecated_attribute();
  1599. range_statements_with_multiple_return_values();
  1600. threading_example();
  1601. soa_struct_layout();
  1602. constant_literal_expressions();
  1603. }
  1604. }