demo.odin 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452
  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 "core:runtime"
  9. import "core:intrinsics"
  10. import "core:math/big"
  11. /*
  12. The Odin programming language is fast, concise, readable, pragmatic and open sourced.
  13. It is designed with the intent of replacing C with the following goals:
  14. * simplicity
  15. * high performance
  16. * built for modern systems
  17. * joy of programming
  18. # Installing Odin
  19. Getting Started - https://odin-lang.org/docs/install/
  20. Instructions for downloading and install the Odin compiler and libraries.
  21. # Learning Odin
  22. Overview of Odin - https://odin-lang.org/docs/overview/
  23. An overview of the Odin programming language.
  24. Frequently Asked Questions (FAQ) - https://odin-lang.org/docs/faq/
  25. Answers to common questions about Odin.
  26. */
  27. the_basics :: proc() {
  28. fmt.println("\n# the basics")
  29. { // The Basics
  30. fmt.println("Hellope")
  31. // Lexical elements and literals
  32. // A comment
  33. my_integer_variable: int // A comment for documentaton
  34. // Multi-line comments begin with /* and end with */. Multi-line comments can
  35. // also be nested (unlike in C):
  36. /*
  37. You can have any text or code here and
  38. have it be commented.
  39. /*
  40. NOTE: comments can be nested!
  41. */
  42. */
  43. // String literals are enclosed in double quotes and character literals in single quotes.
  44. // Special characters are escaped with a backslash \
  45. some_string := "This is a string"
  46. _ = 'A' // unicode codepoint literal
  47. _ = '\n'
  48. _ = "C:\\Windows\\notepad.exe"
  49. // Raw string literals are enclosed with single back ticks
  50. _ = `C:\Windows\notepad.exe`
  51. // The length of a string in bytes can be found using the built-in `len` procedure:
  52. _ = len("Foo")
  53. _ = len(some_string)
  54. // Numbers
  55. // Numerical literals are written similar to most other programming languages.
  56. // A useful feature in Odin is that underscores are allowed for better
  57. // readability: 1_000_000_000 (one billion). A number that contains a dot is a
  58. // floating point literal: 1.0e9 (one billion). If a number literal is suffixed
  59. // with i, is an imaginary number literal: 2i (2 multiply the square root of -1).
  60. // Binary literals are prefixed with 0b, octal literals with 0o, and hexadecimal
  61. // literals 0x. A leading zero does not produce an octal constant (unlike C).
  62. // In Odin, if a numeric constant can be represented by a type without
  63. // precision loss, it will automatically convert to that type.
  64. x: int = 1.0 // A float literal but it can be represented by an integer without precision loss
  65. // Constant literals are “untyped” which means that they can implicitly convert to a type.
  66. y: int // `y` is typed of type `int`
  67. y = 1 // `1` is an untyped integer literal which can implicitly convert to `int`
  68. z: f64 // `z` is typed of type `f64` (64-bit floating point number)
  69. z = 1 // `1` is an untyped integer literal which can be implicitly converted to `f64`
  70. // No need for any suffixes or decimal places like in other languages
  71. // CONSTANTS JUST WORK!!!
  72. // Assignment statements
  73. h: int = 123 // declares a new variable `h` with type `int` and assigns a value to it
  74. h = 637 // assigns a new value to `h`
  75. // `=` is the assignment operator
  76. // You can assign multiple variables with it:
  77. a, b := 1, "hello" // declares `a` and `b` and infers the types from the assignments
  78. b, a = "byte", 0
  79. // Note: `:=` is two tokens, `:` and `=`. The following are equivalent,
  80. /*
  81. i: int = 123
  82. i: = 123
  83. i := 123
  84. */
  85. // Constant declarations
  86. // Constants are entities (symbols) which have an assigned value.
  87. // The constant’s value cannot be changed.
  88. // The constant’s value must be able to be evaluated at compile time:
  89. X :: "what" // constant `X` has the untyped string value "what"
  90. // Constants can be explicitly typed like a variable declaration:
  91. Y : int : 123
  92. Z :: Y + 7 // constant computations are possible
  93. _ = my_integer_variable
  94. _ = x
  95. }
  96. }
  97. control_flow :: proc() {
  98. fmt.println("\n# control flow")
  99. { // Control flow
  100. // For loop
  101. // Odin has only one loop statement, the `for` loop
  102. // Basic for loop
  103. for i := 0; i < 10; i += 1 {
  104. fmt.println(i)
  105. }
  106. // NOTE: Unlike other languages like C, there are no parentheses `( )` surrounding the three components.
  107. // Braces `{ }` or a `do` are always required
  108. for i := 0; i < 10; i += 1 { }
  109. // for i := 0; i < 10; i += 1 do fmt.print()
  110. // The initial and post statements are optional
  111. i := 0
  112. for ; i < 10; {
  113. i += 1
  114. }
  115. // These semicolons can be dropped. This `for` loop is equivalent to C's `while` loop
  116. i = 0
  117. for i < 10 {
  118. i += 1
  119. }
  120. // If the condition is omitted, an infinite loop is produced:
  121. for {
  122. break
  123. }
  124. // Range-based for loop
  125. // The basic for loop
  126. for j := 0; j < 10; j += 1 {
  127. fmt.println(j)
  128. }
  129. // can also be written
  130. for j in 0..<10 {
  131. fmt.println(j)
  132. }
  133. for j in 0..=9 {
  134. fmt.println(j)
  135. }
  136. // Certain built-in types can be iterated over
  137. some_string := "Hello, 世界"
  138. for character in some_string { // Strings are assumed to be UTF-8
  139. fmt.println(character)
  140. }
  141. some_array := [3]int{1, 4, 9}
  142. for value in some_array {
  143. fmt.println(value)
  144. }
  145. some_slice := []int{1, 4, 9}
  146. for value in some_slice {
  147. fmt.println(value)
  148. }
  149. some_dynamic_array := [dynamic]int{1, 4, 9}
  150. defer delete(some_dynamic_array)
  151. for value in some_dynamic_array {
  152. fmt.println(value)
  153. }
  154. some_map := map[string]int{"A" = 1, "C" = 9, "B" = 4}
  155. defer delete(some_map)
  156. for key in some_map {
  157. fmt.println(key)
  158. }
  159. // Alternatively a second index value can be added
  160. for character, index in some_string {
  161. fmt.println(index, character)
  162. }
  163. for value, index in some_array {
  164. fmt.println(index, value)
  165. }
  166. for value, index in some_slice {
  167. fmt.println(index, value)
  168. }
  169. for value, index in some_dynamic_array {
  170. fmt.println(index, value)
  171. }
  172. for key, value in some_map {
  173. fmt.println(key, value)
  174. }
  175. // The iterated values are copies and cannot be written to.
  176. // The following idiom is useful for iterating over a container in a by-reference manner:
  177. for _, idx in some_slice {
  178. some_slice[idx] = (idx+1)*(idx+1)
  179. }
  180. // If statements
  181. x := 123
  182. if x >= 0 {
  183. fmt.println("x is positive")
  184. }
  185. if y := -34; y < 0 {
  186. fmt.println("y is negative")
  187. }
  188. if y := 123; y < 0 {
  189. fmt.println("y is negative")
  190. } else if y == 0 {
  191. fmt.println("y is zero")
  192. } else {
  193. fmt.println("y is positive")
  194. }
  195. // Switch statement
  196. // A switch statement is another way to write a sequence of if-else statements.
  197. // In Odin, the default case is denoted as a case without any expression.
  198. switch arch := ODIN_ARCH; arch {
  199. case "386":
  200. fmt.println("32-bit")
  201. case "amd64":
  202. fmt.println("64-bit")
  203. case: // default
  204. fmt.println("Unsupported architecture")
  205. }
  206. // Odin’s `switch` is like one in C or C++, except that Odin only runs the selected case.
  207. // This means that a `break` statement is not needed at the end of each case.
  208. // Another important difference is that the case values need not be integers nor constants.
  209. // To achieve a C-like fall through into the next case block, the keyword `fallthrough` can be used.
  210. one_angry_dwarf :: proc() -> int {
  211. fmt.println("one_angry_dwarf was called")
  212. return 1
  213. }
  214. switch j := 0; j {
  215. case 0:
  216. case one_angry_dwarf():
  217. }
  218. // A switch statement without a condition is the same as `switch true`.
  219. // This can be used to write a clean and long if-else chain and have the
  220. // ability to break if needed
  221. switch {
  222. case x < 0:
  223. fmt.println("x is negative")
  224. case x == 0:
  225. fmt.println("x is zero")
  226. case:
  227. fmt.println("x is positive")
  228. }
  229. // A `switch` statement can also use ranges like a range-based loop:
  230. switch c := 'j'; c {
  231. case 'A'..='Z', 'a'..='z', '0'..='9':
  232. fmt.println("c is alphanumeric")
  233. }
  234. switch x {
  235. case 0..<10:
  236. fmt.println("units")
  237. case 10..<13:
  238. fmt.println("pre-teens")
  239. case 13..<20:
  240. fmt.println("teens")
  241. case 20..<30:
  242. fmt.println("twenties")
  243. }
  244. }
  245. { // Defer statement
  246. // A defer statement defers the execution of a statement until the end of
  247. // the scope it is in.
  248. // The following will print 4 then 234:
  249. {
  250. x := 123
  251. defer fmt.println(x)
  252. {
  253. defer x = 4
  254. x = 2
  255. }
  256. fmt.println(x)
  257. x = 234
  258. }
  259. // You can defer an entire block too:
  260. {
  261. bar :: proc() {}
  262. defer {
  263. fmt.println("1")
  264. fmt.println("2")
  265. }
  266. cond := false
  267. defer if cond {
  268. bar()
  269. }
  270. }
  271. // Defer statements are executed in the reverse order that they were declared:
  272. {
  273. defer fmt.println("1")
  274. defer fmt.println("2")
  275. defer fmt.println("3")
  276. }
  277. // Will print 3, 2, and then 1.
  278. if false {
  279. f, err := os.open("my_file.txt")
  280. if err != 0 {
  281. // handle error
  282. }
  283. defer os.close(f)
  284. // rest of code
  285. }
  286. }
  287. { // When statement
  288. /*
  289. The when statement is almost identical to the if statement but with some differences:
  290. * Each condition must be a constant expression as a when
  291. statement is evaluated at compile time.
  292. * The statements within a branch do not create a new scope
  293. * The compiler checks the semantics and code only for statements
  294. that belong to the first condition that is true
  295. * An initial statement is not allowed in a when statement
  296. * when statements are allowed at file scope
  297. */
  298. // Example
  299. when ODIN_ARCH == "386" {
  300. fmt.println("32 bit")
  301. } else when ODIN_ARCH == "amd64" {
  302. fmt.println("64 bit")
  303. } else {
  304. fmt.println("Unsupported architecture")
  305. }
  306. // The when statement is very useful for writing platform specific code.
  307. // This is akin to the #if construct in C’s preprocessor however, in Odin,
  308. // it is type checked.
  309. }
  310. { // Branch statements
  311. cond, cond1, cond2 := false, false, false
  312. one_step :: proc() { fmt.println("one_step") }
  313. beyond :: proc() { fmt.println("beyond") }
  314. // Break statement
  315. for cond {
  316. switch {
  317. case:
  318. if cond {
  319. break // break out of the `switch` statement
  320. }
  321. }
  322. break // break out of the `for` statement
  323. }
  324. loop: for cond1 {
  325. for cond2 {
  326. break loop // leaves both loops
  327. }
  328. }
  329. // Continue statement
  330. for cond {
  331. if cond2 {
  332. continue
  333. }
  334. fmt.println("Hellope")
  335. }
  336. // Fallthrough statement
  337. // Odin’s switch is like one in C or C++, except that Odin only runs the selected
  338. // case. This means that a break statement is not needed at the end of each case.
  339. // Another important difference is that the case values need not be integers nor
  340. // constants.
  341. // fallthrough can be used to explicitly fall through into the next case block:
  342. switch i := 0; i {
  343. case 0:
  344. one_step()
  345. fallthrough
  346. case 1:
  347. beyond()
  348. }
  349. }
  350. }
  351. named_proc_return_parameters :: proc() {
  352. fmt.println("\n# named proc return parameters")
  353. foo0 :: proc() -> int {
  354. return 123
  355. }
  356. foo1 :: proc() -> (a: int) {
  357. a = 123
  358. return
  359. }
  360. foo2 :: proc() -> (a, b: int) {
  361. // Named return values act like variables within the scope
  362. a = 321
  363. b = 567
  364. return b, a
  365. }
  366. fmt.println("foo0 =", foo0()) // 123
  367. fmt.println("foo1 =", foo1()) // 123
  368. fmt.println("foo2 =", foo2()) // 567 321
  369. }
  370. explicit_procedure_overloading :: proc() {
  371. fmt.println("\n# explicit procedure overloading")
  372. add_ints :: proc(a, b: int) -> int {
  373. x := a + b
  374. fmt.println("add_ints", x)
  375. return x
  376. }
  377. add_floats :: proc(a, b: f32) -> f32 {
  378. x := a + b
  379. fmt.println("add_floats", x)
  380. return x
  381. }
  382. add_numbers :: proc(a: int, b: f32, c: u8) -> int {
  383. x := int(a) + int(b) + int(c)
  384. fmt.println("add_numbers", x)
  385. return x
  386. }
  387. add :: proc{add_ints, add_floats, add_numbers}
  388. add(int(1), int(2))
  389. add(f32(1), f32(2))
  390. add(int(1), f32(2), u8(3))
  391. add(1, 2) // untyped ints coerce to int tighter than f32
  392. add(1.0, 2.0) // untyped floats coerce to f32 tighter than int
  393. add(1, 2, 3) // three parameters
  394. // Ambiguous answers
  395. // add(1.0, 2)
  396. // add(1, 2.0)
  397. }
  398. struct_type :: proc() {
  399. fmt.println("\n# struct type")
  400. // A struct is a record type in Odin. It is a collection of fields.
  401. // Struct fields are accessed by using a dot:
  402. {
  403. Vector2 :: struct {
  404. x: f32,
  405. y: f32,
  406. }
  407. v := Vector2{1, 2}
  408. v.x = 4
  409. fmt.println(v.x)
  410. // Struct fields can be accessed through a struct pointer:
  411. v = Vector2{1, 2}
  412. p := &v
  413. p.x = 1335
  414. fmt.println(v)
  415. // We could write p^.x, however, it is to nice abstract the ability
  416. // to not explicitly dereference the pointer. This is very useful when
  417. // refactoring code to use a pointer rather than a value, and vice versa.
  418. }
  419. {
  420. // A struct literal can be denoted by providing the struct’s type
  421. // followed by {}. A struct literal must either provide all the
  422. // arguments or none:
  423. Vector3 :: struct {
  424. x, y, z: f32,
  425. }
  426. v: Vector3
  427. v = Vector3{} // Zero value
  428. v = Vector3{1, 4, 9}
  429. // You can list just a subset of the fields if you specify the
  430. // field by name (the order of the named fields does not matter):
  431. v = Vector3{z=1, y=2}
  432. assert(v.x == 0)
  433. assert(v.y == 2)
  434. assert(v.z == 1)
  435. }
  436. {
  437. // Structs can tagged with different memory layout and alignment requirements:
  438. a :: struct #align 4 {} // align to 4 bytes
  439. b :: struct #packed {} // remove padding between fields
  440. c :: struct #raw_union {} // all fields share the same offset (0). This is the same as C's union
  441. }
  442. }
  443. union_type :: proc() {
  444. fmt.println("\n# union type")
  445. {
  446. val: union{int, bool}
  447. val = 137
  448. if i, ok := val.(int); ok {
  449. fmt.println(i)
  450. }
  451. val = true
  452. fmt.println(val)
  453. val = nil
  454. switch v in val {
  455. case int: fmt.println("int", v)
  456. case bool: fmt.println("bool", v)
  457. case: fmt.println("nil")
  458. }
  459. }
  460. {
  461. // There is a duality between `any` and `union`
  462. // An `any` has a pointer to the data and allows for any type (open)
  463. // A `union` has as binary blob to store the data and allows only certain types (closed)
  464. // The following code is with `any` but has the same syntax
  465. val: any
  466. val = 137
  467. if i, ok := val.(int); ok {
  468. fmt.println(i)
  469. }
  470. val = true
  471. fmt.println(val)
  472. val = nil
  473. switch v in val {
  474. case int: fmt.println("int", v)
  475. case bool: fmt.println("bool", v)
  476. case: fmt.println("nil")
  477. }
  478. }
  479. Vector3 :: distinct [3]f32
  480. Quaternion :: distinct quaternion128
  481. // More realistic examples
  482. {
  483. // NOTE(bill): For the above basic examples, you may not have any
  484. // particular use for it. However, my main use for them is not for these
  485. // simple cases. My main use is for hierarchical types. Many prefer
  486. // subtyping, embedding the base data into the derived types. Below is
  487. // an example of this for a basic game Entity.
  488. Entity :: struct {
  489. id: u64,
  490. name: string,
  491. position: Vector3,
  492. orientation: Quaternion,
  493. derived: any,
  494. }
  495. Frog :: struct {
  496. using entity: Entity,
  497. jump_height: f32,
  498. }
  499. Monster :: struct {
  500. using entity: Entity,
  501. is_robot: bool,
  502. is_zombie: bool,
  503. }
  504. // See `parametric_polymorphism` procedure for details
  505. new_entity :: proc($T: typeid) -> ^Entity {
  506. t := new(T)
  507. t.derived = t^
  508. return t
  509. }
  510. entity := new_entity(Monster)
  511. switch e in entity.derived {
  512. case Frog:
  513. fmt.println("Ribbit")
  514. case Monster:
  515. if e.is_robot { fmt.println("Robotic") }
  516. if e.is_zombie { fmt.println("Grrrr!") }
  517. fmt.println("I'm a monster")
  518. }
  519. }
  520. {
  521. // NOTE(bill): A union can be used to achieve something similar. Instead
  522. // of embedding the base data into the derived types, the derived data
  523. // in embedded into the base type. Below is the same example of the
  524. // basic game Entity but using an union.
  525. Entity :: struct {
  526. id: u64,
  527. name: string,
  528. position: Vector3,
  529. orientation: Quaternion,
  530. derived: union {Frog, Monster},
  531. }
  532. Frog :: struct {
  533. using entity: ^Entity,
  534. jump_height: f32,
  535. }
  536. Monster :: struct {
  537. using entity: ^Entity,
  538. is_robot: bool,
  539. is_zombie: bool,
  540. }
  541. // See `parametric_polymorphism` procedure for details
  542. new_entity :: proc($T: typeid) -> ^Entity {
  543. t := new(Entity)
  544. t.derived = T{entity = t}
  545. return t
  546. }
  547. entity := new_entity(Monster)
  548. switch e in entity.derived {
  549. case Frog:
  550. fmt.println("Ribbit")
  551. case Monster:
  552. if e.is_robot { fmt.println("Robotic") }
  553. if e.is_zombie { fmt.println("Grrrr!") }
  554. }
  555. // NOTE(bill): As you can see, the usage code has not changed, only its
  556. // memory layout. Both approaches have their own advantages but they can
  557. // be used together to achieve different results. The subtyping approach
  558. // can allow for a greater control of the memory layout and memory
  559. // allocation, e.g. storing the derivatives together. However, this is
  560. // also its disadvantage. You must either preallocate arrays for each
  561. // derivative separation (which can be easily missed) or preallocate a
  562. // bunch of "raw" memory; determining the maximum size of the derived
  563. // types would require the aid of metaprogramming. Unions solve this
  564. // particular problem as the data is stored with the base data.
  565. // Therefore, it is possible to preallocate, e.g. [100]Entity.
  566. // It should be noted that the union approach can have the same memory
  567. // layout as the any and with the same type restrictions by using a
  568. // pointer type for the derivatives.
  569. /*
  570. Entity :: struct {
  571. ...
  572. derived: union{^Frog, ^Monster},
  573. }
  574. Frog :: struct {
  575. using entity: Entity,
  576. ...
  577. }
  578. Monster :: struct {
  579. using entity: Entity,
  580. ...
  581. }
  582. new_entity :: proc(T: type) -> ^Entity {
  583. t := new(T)
  584. t.derived = t
  585. return t
  586. }
  587. */
  588. }
  589. }
  590. using_statement :: proc() {
  591. fmt.println("\n# using statement")
  592. // using can used to bring entities declared in a scope/namespace
  593. // into the current scope. This can be applied to import names, struct
  594. // fields, procedure fields, and struct values.
  595. Vector3 :: struct{x, y, z: f32}
  596. {
  597. Entity :: struct {
  598. position: Vector3,
  599. orientation: quaternion128,
  600. }
  601. // It can used like this:
  602. foo0 :: proc(entity: ^Entity) {
  603. fmt.println(entity.position.x, entity.position.y, entity.position.z)
  604. }
  605. // The entity members can be brought into the procedure scope by using it:
  606. foo1 :: proc(entity: ^Entity) {
  607. using entity
  608. fmt.println(position.x, position.y, position.z)
  609. }
  610. // The using can be applied to the parameter directly:
  611. foo2 :: proc(using entity: ^Entity) {
  612. fmt.println(position.x, position.y, position.z)
  613. }
  614. // It can also be applied to sub-fields:
  615. foo3 :: proc(entity: ^Entity) {
  616. using entity.position
  617. fmt.println(x, y, z)
  618. }
  619. }
  620. {
  621. // We can also apply the using statement to the struct fields directly,
  622. // making all the fields of position appear as if they on Entity itself:
  623. Entity :: struct {
  624. using position: Vector3,
  625. orientation: quaternion128,
  626. }
  627. foo :: proc(entity: ^Entity) {
  628. fmt.println(entity.x, entity.y, entity.z)
  629. }
  630. // Subtype polymorphism
  631. // It is possible to get subtype polymorphism, similar to inheritance-like
  632. // functionality in C++, but without the requirement of vtables or unknown
  633. // struct layout:
  634. Colour :: struct {r, g, b, a: u8}
  635. Frog :: struct {
  636. ribbit_volume: f32,
  637. using entity: Entity,
  638. colour: Colour,
  639. }
  640. frog: Frog
  641. // Both work
  642. foo(&frog.entity)
  643. foo(&frog)
  644. frog.x = 123
  645. // Note: using can be applied to arbitrarily many things, which allows
  646. // the ability to have multiple subtype polymorphism (but also its issues).
  647. // Note: using’d fields can still be referred by name.
  648. }
  649. }
  650. implicit_context_system :: proc() {
  651. fmt.println("\n# implicit context system")
  652. // In each scope, there is an implicit value named context. This
  653. // context variable is local to each scope and is implicitly passed
  654. // by pointer to any procedure call in that scope (if the procedure
  655. // has the Odin calling convention).
  656. // The main purpose of the implicit context system is for the ability
  657. // to intercept third-party code and libraries and modify their
  658. // functionality. One such case is modifying how a library allocates
  659. // something or logs something. In C, this was usually achieved with
  660. // the library defining macros which could be overridden so that the
  661. // user could define what he wanted. However, not many libraries
  662. // supported this in many languages by default which meant intercepting
  663. // third-party code to see what it does and to change how it does it is
  664. // not possible.
  665. c := context // copy the current scope's context
  666. context.user_index = 456
  667. {
  668. context.allocator = my_custom_allocator()
  669. context.user_index = 123
  670. what_a_fool_believes() // the `context` for this scope is implicitly passed to `what_a_fool_believes`
  671. }
  672. // `context` value is local to the scope it is in
  673. assert(context.user_index == 456)
  674. what_a_fool_believes :: proc() {
  675. c := context // this `context` is the same as the parent procedure that it was called from
  676. // From this example, context.user_index == 123
  677. // An context.allocator is assigned to the return value of `my_custom_allocator()`
  678. assert(context.user_index == 123)
  679. // The memory management procedure use the `context.allocator` by
  680. // default unless explicitly specified otherwise
  681. china_grove := new(int)
  682. free(china_grove)
  683. _ = c
  684. }
  685. my_custom_allocator :: mem.nil_allocator
  686. _ = c
  687. // By default, the context value has default values for its parameters which is
  688. // decided in the package runtime. What the defaults are are compiler specific.
  689. // To see what the implicit context value contains, please see the following
  690. // definition in package runtime.
  691. }
  692. parametric_polymorphism :: proc() {
  693. fmt.println("\n# parametric polymorphism")
  694. print_value :: proc(value: $T) {
  695. fmt.printf("print_value: %T %v\n", value, value)
  696. }
  697. v1: int = 1
  698. v2: f32 = 2.1
  699. v3: f64 = 3.14
  700. v4: string = "message"
  701. print_value(v1)
  702. print_value(v2)
  703. print_value(v3)
  704. print_value(v4)
  705. fmt.println()
  706. add :: proc(p, q: $T) -> T {
  707. x: T = p + q
  708. return x
  709. }
  710. a := add(3, 4)
  711. fmt.printf("a: %T = %v\n", a, a)
  712. b := add(3.2, 4.3)
  713. fmt.printf("b: %T = %v\n", b, b)
  714. // This is how `new` is implemented
  715. alloc_type :: proc($T: typeid) -> ^T {
  716. t := cast(^T)alloc(size_of(T), align_of(T))
  717. t^ = T{} // Use default initialization value
  718. return t
  719. }
  720. copy_slice :: proc(dst, src: []$T) -> int {
  721. n := min(len(dst), len(src))
  722. if n > 0 {
  723. mem.copy(&dst[0], &src[0], n*size_of(T))
  724. }
  725. return n
  726. }
  727. double_params :: proc(a: $A, b: $B) -> A {
  728. return a + A(b)
  729. }
  730. fmt.println(double_params(12, 1.345))
  731. { // Polymorphic Types and Type Specialization
  732. Table_Slot :: struct($Key, $Value: typeid) {
  733. occupied: bool,
  734. hash: u32,
  735. key: Key,
  736. value: Value,
  737. }
  738. TABLE_SIZE_MIN :: 32
  739. Table :: struct($Key, $Value: typeid) {
  740. count: int,
  741. allocator: mem.Allocator,
  742. slots: []Table_Slot(Key, Value),
  743. }
  744. // Only allow types that are specializations of a (polymorphic) slice
  745. make_slice :: proc($T: typeid/[]$E, len: int) -> T {
  746. return make(T, len)
  747. }
  748. // Only allow types that are specializations of `Table`
  749. allocate :: proc(table: ^$T/Table, capacity: int) {
  750. c := context
  751. if table.allocator.procedure != nil {
  752. c.allocator = table.allocator
  753. }
  754. context = c
  755. table.slots = make_slice(type_of(table.slots), max(capacity, TABLE_SIZE_MIN))
  756. }
  757. expand :: proc(table: ^$T/Table) {
  758. c := context
  759. if table.allocator.procedure != nil {
  760. c.allocator = table.allocator
  761. }
  762. context = c
  763. old_slots := table.slots
  764. defer delete(old_slots)
  765. cap := max(2*len(table.slots), TABLE_SIZE_MIN)
  766. allocate(table, cap)
  767. for s in old_slots {
  768. if s.occupied {
  769. put(table, s.key, s.value)
  770. }
  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 {
  808. return -1
  809. }
  810. index := int(hash % u32(len(table.slots)))
  811. for table.slots[index].occupied {
  812. if table.slots[index].hash == hash {
  813. if table.slots[index].key == key {
  814. return index
  815. }
  816. }
  817. if index += 1; index >= len(table.slots) {
  818. index = 0
  819. }
  820. }
  821. return -1
  822. }
  823. get_hash :: proc(s: string) -> u32 { // fnv32a
  824. h: u32 = 0x811c9dc5
  825. for i in 0..<len(s) {
  826. h = (h ~ u32(s[i])) * 0x01000193
  827. }
  828. return h
  829. }
  830. table: Table(string, int)
  831. for i in 0..=36 { put(&table, "Hellope", i) }
  832. for i in 0..=42 { put(&table, "World!", i) }
  833. found, _ := find(&table, "Hellope")
  834. fmt.printf("`found` is %v\n", found)
  835. found, _ = find(&table, "World!")
  836. fmt.printf("`found` is %v\n", found)
  837. // I would not personally design a hash table like this in production
  838. // but this is a nice basic example
  839. // A better approach would either use a `u64` or equivalent for the key
  840. // and let the user specify the hashing function or make the user store
  841. // the hashing procedure with the table
  842. }
  843. { // Parametric polymorphic union
  844. Error :: enum {
  845. Foo0,
  846. Foo1,
  847. Foo2,
  848. Foo3,
  849. }
  850. Para_Union :: union($T: typeid) {T, Error}
  851. r: Para_Union(int)
  852. fmt.println(typeid_of(type_of(r)))
  853. fmt.println(r)
  854. r = 123
  855. fmt.println(r)
  856. r = Error.Foo0 // r = .Foo0 is allow too, see implicit selector expressions below
  857. fmt.println(r)
  858. }
  859. { // Polymorphic names
  860. foo :: proc($N: $I, $T: typeid) -> (res: [N]T) {
  861. // `N` is the constant value passed
  862. // `I` is the type of N
  863. // `T` is the type passed
  864. fmt.printf("Generating an array of type %v from the value %v of type %v\n",
  865. typeid_of(type_of(res)), N, typeid_of(I))
  866. for i in 0..<N {
  867. res[i] = T(i*i)
  868. }
  869. return
  870. }
  871. T :: int
  872. array := foo(4, T)
  873. for v, i in array {
  874. assert(v == T(i*i))
  875. }
  876. // Matrix multiplication
  877. mul :: proc(a: [$M][$N]$T, b: [N][$P]T) -> (c: [M][P]T) {
  878. for i in 0..<M {
  879. for j in 0..<P {
  880. for k in 0..<N {
  881. c[i][j] += a[i][k] * b[k][j]
  882. }
  883. }
  884. }
  885. return
  886. }
  887. x := [2][3]f32{
  888. {1, 2, 3},
  889. {3, 2, 1},
  890. }
  891. y := [3][2]f32{
  892. {0, 8},
  893. {6, 2},
  894. {8, 4},
  895. }
  896. z := mul(x, y)
  897. assert(z == {{36, 24}, {20, 32}})
  898. }
  899. }
  900. prefix_table := [?]string{
  901. "White",
  902. "Red",
  903. "Green",
  904. "Blue",
  905. "Octarine",
  906. "Black",
  907. }
  908. threading_example :: proc() {
  909. if ODIN_OS == "darwin" {
  910. // TODO: Fix threads on darwin/macOS
  911. return
  912. }
  913. fmt.println("\n# threading_example")
  914. { // Basic Threads
  915. fmt.println("\n## Basic Threads")
  916. worker_proc :: proc(t: ^thread.Thread) {
  917. for iteration in 1..=5 {
  918. fmt.printf("Thread %d is on iteration %d\n", t.user_index, iteration)
  919. fmt.printf("`%s`: iteration %d\n", prefix_table[t.user_index], iteration)
  920. time.sleep(1 * time.Millisecond)
  921. }
  922. }
  923. threads := make([dynamic]^thread.Thread, 0, len(prefix_table))
  924. defer delete(threads)
  925. for in prefix_table {
  926. if t := thread.create(worker_proc); t != nil {
  927. t.init_context = context
  928. t.user_index = len(threads)
  929. append(&threads, t)
  930. thread.start(t)
  931. }
  932. }
  933. for len(threads) > 0 {
  934. for i := 0; i < len(threads); /**/ {
  935. if t := threads[i]; thread.is_done(t) {
  936. fmt.printf("Thread %d is done\n", t.user_index)
  937. thread.destroy(t)
  938. ordered_remove(&threads, i)
  939. } else {
  940. i += 1
  941. }
  942. }
  943. }
  944. }
  945. { // Thread Pool
  946. fmt.println("\n## Thread Pool")
  947. task_proc :: proc(t: ^thread.Task) {
  948. index := t.user_index % len(prefix_table)
  949. for iteration in 1..=5 {
  950. fmt.printf("Worker Task %d is on iteration %d\n", t.user_index, iteration)
  951. fmt.printf("`%s`: iteration %d\n", prefix_table[index], iteration)
  952. time.sleep(1 * time.Millisecond)
  953. }
  954. }
  955. pool: thread.Pool
  956. thread.pool_init(pool=&pool, thread_count=3)
  957. defer thread.pool_destroy(&pool)
  958. for i in 0..<30 {
  959. thread.pool_add_task(pool=&pool, procedure=task_proc, data=nil, user_index=i)
  960. }
  961. thread.pool_start(&pool)
  962. thread.pool_wait_and_process(&pool)
  963. }
  964. }
  965. array_programming :: proc() {
  966. fmt.println("\n# array programming")
  967. {
  968. a := [3]f32{1, 2, 3}
  969. b := [3]f32{5, 6, 7}
  970. c := a * b
  971. d := a + b
  972. e := 1 + (c - d) / 2
  973. fmt.printf("%.1f\n", e) // [0.5, 3.0, 6.5]
  974. }
  975. {
  976. a := [3]f32{1, 2, 3}
  977. b := swizzle(a, 2, 1, 0)
  978. assert(b == [3]f32{3, 2, 1})
  979. c := swizzle(a, 0, 0)
  980. assert(c == [2]f32{1, 1})
  981. assert(c == 1)
  982. }
  983. {
  984. Vector3 :: distinct [3]f32
  985. a := Vector3{1, 2, 3}
  986. b := Vector3{5, 6, 7}
  987. c := (a * b)/2 + 1
  988. d := c.x + c.y + c.z
  989. fmt.printf("%.1f\n", d) // 22.0
  990. cross :: proc(a, b: Vector3) -> Vector3 {
  991. i := swizzle(a, 1, 2, 0) * swizzle(b, 2, 0, 1)
  992. j := swizzle(a, 2, 0, 1) * swizzle(b, 1, 2, 0)
  993. return i - j
  994. }
  995. cross_shorter :: proc(a, b: Vector3) -> Vector3 {
  996. i := a.yzx * b.zxy
  997. j := a.zxy * b.yzx
  998. return i - j
  999. }
  1000. blah :: proc(a: Vector3) -> f32 {
  1001. return a.x + a.y + a.z
  1002. }
  1003. x := cross(a, b)
  1004. fmt.println(x)
  1005. fmt.println(blah(x))
  1006. }
  1007. }
  1008. map_type :: proc() {
  1009. fmt.println("\n# map type")
  1010. m := make(map[string]int)
  1011. defer delete(m)
  1012. m["Bob"] = 2
  1013. m["Ted"] = 5
  1014. fmt.println(m["Bob"])
  1015. delete_key(&m, "Ted")
  1016. // If an element of a key does not exist, the zero value of the
  1017. // element will be returned. To check to see if an element exists
  1018. // can be done in two ways:
  1019. elem, ok := m["Bob"]
  1020. exists := "Bob" in m
  1021. _, _ = elem, ok
  1022. _ = exists
  1023. }
  1024. implicit_selector_expression :: proc() {
  1025. fmt.println("\n# implicit selector expression")
  1026. Foo :: enum {A, B, C}
  1027. f: Foo
  1028. f = Foo.A
  1029. f = .A
  1030. BAR :: bit_set[Foo]{.B, .C}
  1031. switch f {
  1032. case .A:
  1033. fmt.println("HITHER")
  1034. case .B:
  1035. fmt.println("NEVER")
  1036. case .C:
  1037. fmt.println("FOREVER")
  1038. }
  1039. my_map := make(map[Foo]int)
  1040. defer delete(my_map)
  1041. my_map[.A] = 123
  1042. my_map[Foo.B] = 345
  1043. fmt.println(my_map[.A] + my_map[Foo.B] + my_map[.C])
  1044. }
  1045. partial_switch :: proc() {
  1046. fmt.println("\n# partial_switch")
  1047. { // enum
  1048. Foo :: enum {
  1049. A,
  1050. B,
  1051. C,
  1052. D,
  1053. }
  1054. f := Foo.A
  1055. switch f {
  1056. case .A: fmt.println("A")
  1057. case .B: fmt.println("B")
  1058. case .C: fmt.println("C")
  1059. case .D: fmt.println("D")
  1060. case: fmt.println("?")
  1061. }
  1062. #partial switch f {
  1063. case .A: fmt.println("A")
  1064. case .D: fmt.println("D")
  1065. }
  1066. }
  1067. { // union
  1068. Foo :: union {int, bool}
  1069. f: Foo = 123
  1070. switch in f {
  1071. case int: fmt.println("int")
  1072. case bool: fmt.println("bool")
  1073. case:
  1074. }
  1075. #partial switch in f {
  1076. case bool: fmt.println("bool")
  1077. }
  1078. }
  1079. }
  1080. cstring_example :: proc() {
  1081. fmt.println("\n# cstring_example")
  1082. W :: "Hellope"
  1083. X :: cstring(W)
  1084. Y :: string(X)
  1085. w := W
  1086. _ = w
  1087. x: cstring = X
  1088. y: string = Y
  1089. z := string(x)
  1090. fmt.println(x, y, z)
  1091. fmt.println(len(x), len(y), len(z))
  1092. fmt.println(len(W), len(X), len(Y))
  1093. // IMPORTANT NOTE for cstring variables
  1094. // len(cstring) is O(N)
  1095. // cast(string)cstring is O(N)
  1096. }
  1097. bit_set_type :: proc() {
  1098. fmt.println("\n# bit_set type")
  1099. {
  1100. Day :: enum {
  1101. Sunday,
  1102. Monday,
  1103. Tuesday,
  1104. Wednesday,
  1105. Thursday,
  1106. Friday,
  1107. Saturday,
  1108. }
  1109. Days :: distinct bit_set[Day]
  1110. WEEKEND :: Days{.Sunday, .Saturday}
  1111. d: Days
  1112. d = {.Sunday, .Monday}
  1113. e := d + WEEKEND
  1114. e += {.Monday}
  1115. fmt.println(d, e)
  1116. ok := .Saturday in e // `in` is only allowed for `map` and `bit_set` types
  1117. fmt.println(ok)
  1118. if .Saturday in e {
  1119. fmt.println("Saturday in", e)
  1120. }
  1121. X :: .Saturday in WEEKEND // Constant evaluation
  1122. fmt.println(X)
  1123. fmt.println("Cardinality:", card(e))
  1124. }
  1125. {
  1126. x: bit_set['A'..='Z']
  1127. #assert(size_of(x) == size_of(u32))
  1128. y: bit_set[0..=8; u16]
  1129. fmt.println(typeid_of(type_of(x))) // bit_set[A..=Z]
  1130. fmt.println(typeid_of(type_of(y))) // bit_set[0..=8; u16]
  1131. x += {'F'}
  1132. assert('F' in x)
  1133. x -= {'F'}
  1134. assert('F' not_in x)
  1135. y += {1, 4, 2}
  1136. assert(2 in y)
  1137. }
  1138. {
  1139. Letters :: bit_set['A'..='Z']
  1140. a := Letters{'A', 'B'}
  1141. b := Letters{'A', 'B', 'C', 'D', 'F'}
  1142. c := Letters{'A', 'B'}
  1143. assert(a <= b) // 'a' is a subset of 'b'
  1144. assert(b >= a) // 'b' is a superset of 'a'
  1145. assert(a < b) // 'a' is a strict subset of 'b'
  1146. assert(b > a) // 'b' is a strict superset of 'a'
  1147. assert(!(a < c)) // 'a' is a not strict subset of 'c'
  1148. assert(!(c > a)) // 'c' is a not strict superset of 'a'
  1149. }
  1150. }
  1151. deferred_procedure_associations :: proc() {
  1152. fmt.println("\n# deferred procedure associations")
  1153. @(deferred_out=closure)
  1154. open :: proc(s: string) -> bool {
  1155. fmt.println(s)
  1156. return true
  1157. }
  1158. closure :: proc(ok: bool) {
  1159. fmt.println("Goodbye?", ok)
  1160. }
  1161. if open("Welcome") {
  1162. fmt.println("Something in the middle, mate.")
  1163. }
  1164. }
  1165. reflection :: proc() {
  1166. fmt.println("\n# reflection")
  1167. Foo :: struct {
  1168. x: int `tag1`,
  1169. y: string `json:"y_field"`,
  1170. z: bool, // no tag
  1171. }
  1172. id := typeid_of(Foo)
  1173. names := reflect.struct_field_names(id)
  1174. types := reflect.struct_field_types(id)
  1175. tags := reflect.struct_field_tags(id)
  1176. assert(len(names) == len(types) && len(names) == len(tags))
  1177. fmt.println("Foo :: struct {")
  1178. for tag, i in tags {
  1179. name, type := names[i], types[i]
  1180. if tag != "" {
  1181. fmt.printf("\t%s: %T `%s`,\n", name, type, tag)
  1182. } else {
  1183. fmt.printf("\t%s: %T,\n", name, type)
  1184. }
  1185. }
  1186. fmt.println("}")
  1187. for tag, i in tags {
  1188. if val, ok := reflect.struct_tag_lookup(tag, "json"); ok {
  1189. fmt.printf("json: %s -> %s\n", names[i], val)
  1190. }
  1191. }
  1192. }
  1193. quaternions :: proc() {
  1194. // Not just an April Fool's Joke any more, but a fully working thing!
  1195. fmt.println("\n# quaternions")
  1196. { // Quaternion operations
  1197. q := 1 + 2i + 3j + 4k
  1198. r := quaternion(5, 6, 7, 8)
  1199. t := q * r
  1200. fmt.printf("(%v) * (%v) = %v\n", q, r, t)
  1201. v := q / r
  1202. fmt.printf("(%v) / (%v) = %v\n", q, r, v)
  1203. u := q + r
  1204. fmt.printf("(%v) + (%v) = %v\n", q, r, u)
  1205. s := q - r
  1206. fmt.printf("(%v) - (%v) = %v\n", q, r, s)
  1207. }
  1208. { // The quaternion types
  1209. q128: quaternion128 // 4xf32
  1210. q256: quaternion256 // 4xf64
  1211. q128 = quaternion(1, 0, 0, 0)
  1212. q256 = 1 // quaternion(1, 0, 0, 0)
  1213. }
  1214. { // Built-in procedures
  1215. q := 1 + 2i + 3j + 4k
  1216. fmt.println("q =", q)
  1217. fmt.println("real(q) =", real(q))
  1218. fmt.println("imag(q) =", imag(q))
  1219. fmt.println("jmag(q) =", jmag(q))
  1220. fmt.println("kmag(q) =", kmag(q))
  1221. fmt.println("conj(q) =", conj(q))
  1222. fmt.println("abs(q) =", abs(q))
  1223. }
  1224. { // Conversion of a complex type to a quaternion type
  1225. c := 1 + 2i
  1226. q := quaternion256(c)
  1227. fmt.println(c)
  1228. fmt.println(q)
  1229. }
  1230. { // Memory layout of Quaternions
  1231. q := 1 + 2i + 3j + 4k
  1232. a := transmute([4]f64)q
  1233. fmt.println("Quaternion memory layout: xyzw/(ijkr)")
  1234. fmt.println(q) // 1.000+2.000i+3.000j+4.000k
  1235. fmt.println(a) // [2.000, 3.000, 4.000, 1.000]
  1236. }
  1237. }
  1238. unroll_for_statement :: proc() {
  1239. fmt.println("\n#'#unroll for' statements")
  1240. // '#unroll for' works the same as if the 'inline' prefix did not
  1241. // exist but these ranged loops are explicitly unrolled which can
  1242. // be very very useful for certain optimizations
  1243. fmt.println("Ranges")
  1244. #unroll for x, i in 1..<4 {
  1245. fmt.println(x, i)
  1246. }
  1247. fmt.println("Strings")
  1248. #unroll for r, i in "Hello, 世界" {
  1249. fmt.println(r, i)
  1250. }
  1251. fmt.println("Arrays")
  1252. #unroll for elem, idx in ([4]int{1, 4, 9, 16}) {
  1253. fmt.println(elem, idx)
  1254. }
  1255. Foo_Enum :: enum {
  1256. A = 1,
  1257. B,
  1258. C = 6,
  1259. D,
  1260. }
  1261. fmt.println("Enum types")
  1262. #unroll for elem, idx in Foo_Enum {
  1263. fmt.println(elem, idx)
  1264. }
  1265. }
  1266. where_clauses :: proc() {
  1267. fmt.println("\n#procedure 'where' clauses")
  1268. { // Sanity checks
  1269. simple_sanity_check :: proc(x: [2]int)
  1270. where len(x) > 1,
  1271. type_of(x) == [2]int {
  1272. fmt.println(x)
  1273. }
  1274. }
  1275. { // Parametric polymorphism checks
  1276. cross_2d :: proc(a, b: $T/[2]$E) -> E
  1277. where intrinsics.type_is_numeric(E) {
  1278. return a.x*b.y - a.y*b.x
  1279. }
  1280. cross_3d :: proc(a, b: $T/[3]$E) -> T
  1281. where intrinsics.type_is_numeric(E) {
  1282. x := a.y*b.z - a.z*b.y
  1283. y := a.z*b.x - a.x*b.z
  1284. z := a.x*b.y - a.y*b.z
  1285. return T{x, y, z}
  1286. }
  1287. a := [2]int{1, 2}
  1288. b := [2]int{5, -3}
  1289. fmt.println(cross_2d(a, b))
  1290. x := [3]f32{1, 4, 9}
  1291. y := [3]f32{-5, 0, 3}
  1292. fmt.println(cross_3d(x, y))
  1293. // Failure case
  1294. // i := [2]bool{true, false}
  1295. // j := [2]bool{false, true}
  1296. // fmt.println(cross_2d(i, j))
  1297. }
  1298. { // Procedure groups usage
  1299. foo :: proc(x: [$N]int) -> bool
  1300. where N > 2 {
  1301. fmt.println(#procedure, "was called with the parameter", x)
  1302. return true
  1303. }
  1304. bar :: proc(x: [$N]int) -> bool
  1305. where 0 < N,
  1306. N <= 2 {
  1307. fmt.println(#procedure, "was called with the parameter", x)
  1308. return false
  1309. }
  1310. baz :: proc{foo, bar}
  1311. x := [3]int{1, 2, 3}
  1312. y := [2]int{4, 9}
  1313. ok_x := baz(x)
  1314. ok_y := baz(y)
  1315. assert(ok_x == true)
  1316. assert(ok_y == false)
  1317. }
  1318. { // Record types
  1319. Foo :: struct($T: typeid, $N: int)
  1320. where intrinsics.type_is_integer(T),
  1321. N > 2 {
  1322. x: [N]T,
  1323. y: [N-2]T,
  1324. }
  1325. T :: i32
  1326. N :: 5
  1327. f: Foo(T, N)
  1328. #assert(size_of(f) == (N+N-2)*size_of(T))
  1329. }
  1330. }
  1331. when ODIN_OS == "windows" {
  1332. foreign import kernel32 "system:kernel32.lib"
  1333. }
  1334. foreign_system :: proc() {
  1335. fmt.println("\n#foreign system")
  1336. when ODIN_OS == "windows" {
  1337. // It is sometimes necessarily to interface with foreign code,
  1338. // such as a C library. In Odin, this is achieved through the
  1339. // foreign system. You can “import” a library into the code
  1340. // using the same semantics as a normal import declaration.
  1341. // This foreign import declaration will create a
  1342. // “foreign import name” which can then be used to associate
  1343. // entities within a foreign block.
  1344. foreign kernel32 {
  1345. ExitProcess :: proc "stdcall" (exit_code: u32) ---
  1346. }
  1347. // Foreign procedure declarations have the cdecl/c calling
  1348. // convention by default unless specified otherwise. Due to
  1349. // foreign procedures do not have a body declared within this
  1350. // code, you need append the --- symbol to the end to distinguish
  1351. // it as a procedure literal without a body and not a procedure type.
  1352. // The attributes system can be used to change specific properties
  1353. // of entities declared within a block:
  1354. @(default_calling_convention = "std")
  1355. foreign kernel32 {
  1356. @(link_name="GetLastError") get_last_error :: proc() -> i32 ---
  1357. }
  1358. // Example using the link_prefix attribute
  1359. @(default_calling_convention = "std")
  1360. @(link_prefix = "Get")
  1361. foreign kernel32 {
  1362. LastError :: proc() -> i32 ---
  1363. }
  1364. }
  1365. }
  1366. ranged_fields_for_array_compound_literals :: proc() {
  1367. fmt.println("\n#ranged fields for array compound literals")
  1368. { // Normal Array Literal
  1369. foo := [?]int{1, 4, 9, 16}
  1370. fmt.println(foo)
  1371. }
  1372. { // Indexed
  1373. foo := [?]int{
  1374. 3 = 16,
  1375. 1 = 4,
  1376. 2 = 9,
  1377. 0 = 1,
  1378. }
  1379. fmt.println(foo)
  1380. }
  1381. { // Ranges
  1382. i := 2
  1383. foo := [?]int {
  1384. 0 = 123,
  1385. 5..=9 = 54,
  1386. 10..<16 = i*3 + (i-1)*2,
  1387. }
  1388. #assert(len(foo) == 16)
  1389. fmt.println(foo) // [123, 0, 0, 0, 0, 54, 54, 54, 54, 54, 8, 8, 8, 8, 8]
  1390. }
  1391. { // Slice and Dynamic Array support
  1392. i := 2
  1393. foo_slice := []int {
  1394. 0 = 123,
  1395. 5..=9 = 54,
  1396. 10..<16 = i*3 + (i-1)*2,
  1397. }
  1398. assert(len(foo_slice) == 16)
  1399. fmt.println(foo_slice) // [123, 0, 0, 0, 0, 54, 54, 54, 54, 54, 8, 8, 8, 8, 8]
  1400. foo_dynamic_array := [dynamic]int {
  1401. 0 = 123,
  1402. 5..=9 = 54,
  1403. 10..<16 = i*3 + (i-1)*2,
  1404. }
  1405. assert(len(foo_dynamic_array) == 16)
  1406. fmt.println(foo_dynamic_array) // [123, 0, 0, 0, 0, 54, 54, 54, 54, 54, 8, 8, 8, 8, 8]
  1407. }
  1408. }
  1409. deprecated_attribute :: proc() {
  1410. @(deprecated="Use foo_v2 instead")
  1411. foo_v1 :: proc(x: int) {
  1412. fmt.println("foo_v1")
  1413. }
  1414. foo_v2 :: proc(x: int) {
  1415. fmt.println("foo_v2")
  1416. }
  1417. // NOTE: Uncomment to see the warning messages
  1418. // foo_v1(1)
  1419. }
  1420. range_statements_with_multiple_return_values :: proc() {
  1421. // IMPORTANT NOTE(bill, 2019-11-02): This feature is subject to be changed/removed
  1422. fmt.println("\n#range statements with multiple return values")
  1423. My_Iterator :: struct {
  1424. index: int,
  1425. data: []i32,
  1426. }
  1427. make_my_iterator :: proc(data: []i32) -> My_Iterator {
  1428. return My_Iterator{data = data}
  1429. }
  1430. my_iterator :: proc(it: ^My_Iterator) -> (val: i32, idx: int, cond: bool) {
  1431. if cond = it.index < len(it.data); cond {
  1432. val = it.data[it.index]
  1433. idx = it.index
  1434. it.index += 1
  1435. }
  1436. return
  1437. }
  1438. data := make([]i32, 6)
  1439. for _, i in data {
  1440. data[i] = i32(i*i)
  1441. }
  1442. {
  1443. it := make_my_iterator(data)
  1444. for val in my_iterator(&it) {
  1445. fmt.println(val)
  1446. }
  1447. }
  1448. {
  1449. it := make_my_iterator(data)
  1450. for val, idx in my_iterator(&it) {
  1451. fmt.println(val, idx)
  1452. }
  1453. }
  1454. {
  1455. it := make_my_iterator(data)
  1456. for {
  1457. val, _, cond := my_iterator(&it)
  1458. if !cond {
  1459. break
  1460. }
  1461. fmt.println(val)
  1462. }
  1463. }
  1464. }
  1465. soa_struct_layout :: proc() {
  1466. fmt.println("\n#SOA Struct Layout")
  1467. {
  1468. Vector3 :: struct {x, y, z: f32}
  1469. N :: 2
  1470. v_aos: [N]Vector3
  1471. v_aos[0].x = 1
  1472. v_aos[0].y = 4
  1473. v_aos[0].z = 9
  1474. fmt.println(len(v_aos))
  1475. fmt.println(v_aos[0])
  1476. fmt.println(v_aos[0].x)
  1477. fmt.println(&v_aos[0].x)
  1478. v_aos[1] = {0, 3, 4}
  1479. v_aos[1].x = 2
  1480. fmt.println(v_aos[1])
  1481. fmt.println(v_aos)
  1482. v_soa: #soa[N]Vector3
  1483. v_soa[0].x = 1
  1484. v_soa[0].y = 4
  1485. v_soa[0].z = 9
  1486. // Same syntax as AOS and treat as if it was an array
  1487. fmt.println(len(v_soa))
  1488. fmt.println(v_soa[0])
  1489. fmt.println(v_soa[0].x)
  1490. fmt.println(&v_soa[0].x)
  1491. v_soa[1] = {0, 3, 4}
  1492. v_soa[1].x = 2
  1493. fmt.println(v_soa[1])
  1494. // Can use SOA syntax if necessary
  1495. v_soa.x[0] = 1
  1496. v_soa.y[0] = 4
  1497. v_soa.z[0] = 9
  1498. fmt.println(v_soa.x[0])
  1499. // Same pointer addresses with both syntaxes
  1500. assert(&v_soa[0].x == &v_soa.x[0])
  1501. // Same fmt printing
  1502. fmt.println(v_aos)
  1503. fmt.println(v_soa)
  1504. }
  1505. {
  1506. // Works with arrays of length <= 4 which have the implicit fields xyzw/rgba
  1507. Vector3 :: distinct [3]f32
  1508. N :: 2
  1509. v_aos: [N]Vector3
  1510. v_aos[0].x = 1
  1511. v_aos[0].y = 4
  1512. v_aos[0].z = 9
  1513. v_soa: #soa[N]Vector3
  1514. v_soa[0].x = 1
  1515. v_soa[0].y = 4
  1516. v_soa[0].z = 9
  1517. }
  1518. {
  1519. // SOA Slices
  1520. // Vector3 :: struct {x, y, z: f32}
  1521. Vector3 :: struct {x: i8, y: i16, z: f32}
  1522. N :: 3
  1523. v: #soa[N]Vector3
  1524. v[0].x = 1
  1525. v[0].y = 4
  1526. v[0].z = 9
  1527. s: #soa[]Vector3
  1528. s = v[:]
  1529. assert(len(s) == N)
  1530. fmt.println(s)
  1531. fmt.println(s[0].x)
  1532. a := s[1:2]
  1533. assert(len(a) == 1)
  1534. fmt.println(a)
  1535. d: #soa[dynamic]Vector3
  1536. append_soa(&d, Vector3{1, 2, 3}, Vector3{4, 5, 9}, Vector3{-4, -4, 3})
  1537. fmt.println(d)
  1538. fmt.println(len(d))
  1539. fmt.println(cap(d))
  1540. fmt.println(d[:])
  1541. }
  1542. { // soa_zip and soa_unzip
  1543. fmt.println("\nsoa_zip and soa_unzip")
  1544. x := []i32{1, 3, 9}
  1545. y := []f32{2, 4, 16}
  1546. z := []b32{true, false, true}
  1547. // produce an #soa slice the normal slices passed
  1548. s := soa_zip(a=x, b=y, c=z)
  1549. // iterate over the #soa slice
  1550. for v, i in s {
  1551. fmt.println(v, i) // exactly the same as s[i]
  1552. // NOTE: 'v' is NOT a temporary value but has a specialized addressing mode
  1553. // which means that when accessing v.a etc, it does the correct transformation
  1554. // internally:
  1555. // s[i].a === s.a[i]
  1556. fmt.println(v.a, v.b, v.c)
  1557. }
  1558. // Recover the slices from the #soa slice
  1559. a, b, c := soa_unzip(s)
  1560. fmt.println(a, b, c)
  1561. }
  1562. }
  1563. constant_literal_expressions :: proc() {
  1564. fmt.println("\n#constant literal expressions")
  1565. Bar :: struct {x, y: f32}
  1566. Foo :: struct {a, b: int, using c: Bar}
  1567. FOO_CONST :: Foo{b = 2, a = 1, c = {3, 4}}
  1568. fmt.println(FOO_CONST.a)
  1569. fmt.println(FOO_CONST.b)
  1570. fmt.println(FOO_CONST.c)
  1571. fmt.println(FOO_CONST.c.x)
  1572. fmt.println(FOO_CONST.c.y)
  1573. fmt.println(FOO_CONST.x) // using works as expected
  1574. fmt.println(FOO_CONST.y)
  1575. fmt.println("-------")
  1576. ARRAY_CONST :: [3]int{1 = 4, 2 = 9, 0 = 1}
  1577. fmt.println(ARRAY_CONST[0])
  1578. fmt.println(ARRAY_CONST[1])
  1579. fmt.println(ARRAY_CONST[2])
  1580. fmt.println("-------")
  1581. FOO_ARRAY_DEFAULTS :: [3]Foo{{}, {}, {}}
  1582. fmt.println(FOO_ARRAY_DEFAULTS[2].x)
  1583. fmt.println("-------")
  1584. Baz :: enum{A=5, B, C, D}
  1585. ENUM_ARRAY_CONST :: [Baz]int{.A ..= .C = 1, .D = 16}
  1586. fmt.println(ENUM_ARRAY_CONST[.A])
  1587. fmt.println(ENUM_ARRAY_CONST[.B])
  1588. fmt.println(ENUM_ARRAY_CONST[.C])
  1589. fmt.println(ENUM_ARRAY_CONST[.D])
  1590. fmt.println("-------")
  1591. Partial_Baz :: enum{A=5, B, C, D=16}
  1592. #assert(len(Partial_Baz) < len(#partial [Partial_Baz]int))
  1593. PARTIAL_ENUM_ARRAY_CONST :: #partial [Partial_Baz]int{.A ..= .C = 1, .D = 16}
  1594. fmt.println(PARTIAL_ENUM_ARRAY_CONST[.A])
  1595. fmt.println(PARTIAL_ENUM_ARRAY_CONST[.B])
  1596. fmt.println(PARTIAL_ENUM_ARRAY_CONST[.C])
  1597. fmt.println(PARTIAL_ENUM_ARRAY_CONST[.D])
  1598. fmt.println("-------")
  1599. STRING_CONST :: "Hellope!"
  1600. fmt.println(STRING_CONST[0])
  1601. fmt.println(STRING_CONST[2])
  1602. fmt.println(STRING_CONST[3])
  1603. fmt.println(STRING_CONST[0:5])
  1604. fmt.println(STRING_CONST[3:][:4])
  1605. }
  1606. union_maybe :: proc() {
  1607. fmt.println("\n#union #maybe")
  1608. // NOTE: This is already built-in, and this is just a reimplementation to explain the behaviour
  1609. Maybe :: union($T: typeid) #maybe {T}
  1610. i: Maybe(u8)
  1611. p: Maybe(^u8) // No tag is stored for pointers, nil is the sentinel value
  1612. #assert(size_of(i) == size_of(u8) + size_of(u8))
  1613. #assert(size_of(p) == size_of(^u8))
  1614. i = 123
  1615. x := i.?
  1616. y, y_ok := p.?
  1617. p = &x
  1618. z, z_ok := p.?
  1619. fmt.println(i, p)
  1620. fmt.println(x, &x)
  1621. fmt.println(y, y_ok)
  1622. fmt.println(z, z_ok)
  1623. }
  1624. dummy_procedure :: proc() {
  1625. fmt.println("dummy_procedure")
  1626. }
  1627. explicit_context_definition :: proc "c" () {
  1628. // Try commenting the following statement out below
  1629. context = runtime.default_context()
  1630. fmt.println("\n#explicit context definition")
  1631. dummy_procedure()
  1632. }
  1633. relative_data_types :: proc() {
  1634. fmt.println("\n#relative data types")
  1635. x: int = 123
  1636. ptr: #relative(i16) ^int
  1637. ptr = &x
  1638. fmt.println(ptr^)
  1639. arr := [3]int{1, 2, 3}
  1640. s := arr[:]
  1641. rel_slice: #relative(i16) []int
  1642. rel_slice = s
  1643. fmt.println(rel_slice)
  1644. fmt.println(rel_slice[:])
  1645. fmt.println(rel_slice[1])
  1646. }
  1647. or_else_operator :: proc() {
  1648. fmt.println("\n#'or_else'")
  1649. // IMPORTANT NOTE: 'or_else' is an experimental feature and subject to change/removal
  1650. {
  1651. m: map[string]int
  1652. i: int
  1653. ok: bool
  1654. if i, ok = m["hellope"]; !ok {
  1655. i = 123
  1656. }
  1657. // The above can be mapped to 'or_else'
  1658. i = m["hellope"] or_else 123
  1659. assert(i == 123)
  1660. }
  1661. {
  1662. // 'or_else' can be used with type assertions too, as they
  1663. // have optional ok semantics
  1664. v: union{int, f64}
  1665. i: int
  1666. i = v.(int) or_else 123
  1667. i = v.? or_else 123 // Type inference magic
  1668. assert(i == 123)
  1669. m: Maybe(int)
  1670. i = m.? or_else 456
  1671. assert(i == 456)
  1672. }
  1673. }
  1674. or_return_operator :: proc() {
  1675. fmt.println("\n#'or_return'")
  1676. // IMPORTANT NOTE: 'or_return' is an experimental feature and subject to change/removal
  1677. //
  1678. // The concept of 'or_return' will work by popping off the end value in a multiple
  1679. // valued expression and checking whether it was not 'nil' or 'false', and if so,
  1680. // set the end return value to value if possible. If the procedure only has one
  1681. // return value, it will do a simple return. If the procedure had multiple return
  1682. // values, 'or_return' will require that all parameters be named so that the end
  1683. // value could be assigned to by name and then an empty return could be called.
  1684. Error :: enum {
  1685. None,
  1686. Something_Bad,
  1687. Something_Worse,
  1688. The_Worst,
  1689. Your_Mum,
  1690. }
  1691. caller_1 :: proc() -> Error {
  1692. return .None
  1693. }
  1694. caller_2 :: proc() -> (int, Error) {
  1695. return 123, .None
  1696. }
  1697. caller_3 :: proc() -> (int, int, Error) {
  1698. return 123, 345, .None
  1699. }
  1700. foo_1 :: proc() -> Error {
  1701. // This can be a common idiom in many code bases
  1702. n0, err := caller_2()
  1703. if err != nil {
  1704. return err
  1705. }
  1706. // The above idiom can be transformed into the following
  1707. n1 := caller_2() or_return
  1708. // And if the expression is 1-valued, it can be used like this
  1709. caller_1() or_return
  1710. // which is functionally equivalent to
  1711. if err1 := caller_1(); err1 != nil {
  1712. return err1
  1713. }
  1714. // Multiple return values still work with 'or_return' as it only
  1715. // pops off the end value in the multi-valued expression
  1716. n0, n1 = caller_3() or_return
  1717. return .None
  1718. }
  1719. foo_2 :: proc() -> (n: int, err: Error) {
  1720. // It is more common that your procedure turns multiple values
  1721. // If 'or_return' is used within a procedure multiple parameters (2+),
  1722. // then all the parameters must be named so that the remaining parameters
  1723. // so that a bare 'return' statement can be used
  1724. // This can be a common idiom in many code bases
  1725. x: int
  1726. x, err = caller_2()
  1727. if err != nil {
  1728. return
  1729. }
  1730. // The above idiom can be transformed into the following
  1731. y := caller_2() or_return
  1732. _ = y
  1733. // And if the expression is 1-valued, it can be used like this
  1734. caller_1() or_return
  1735. // which is functionally equivalent to
  1736. if err1 := caller_1(); err1 != nil {
  1737. err = err1
  1738. return
  1739. }
  1740. // If using a non-bare 'return' statement is required, setting the return values
  1741. // using the normal idiom is a better choice and clearer to read.
  1742. if z, zerr := caller_2(); zerr != nil {
  1743. return -345 * z, zerr
  1744. }
  1745. // If the other return values need to be set depending on what the end value is,
  1746. // the 'defer if' idiom is can be used
  1747. defer if err != nil {
  1748. n = -1
  1749. }
  1750. n = 123
  1751. return
  1752. }
  1753. foo_1()
  1754. foo_2()
  1755. }
  1756. arbitrary_precision_maths :: proc() {
  1757. fmt.println("\n# core:math/big")
  1758. print_bigint :: proc(name: string, a: ^big.Int, base := i8(10), print_name := true, newline := true, print_extra_info := true) {
  1759. big.assert_if_nil(a)
  1760. as, err := big.itoa(a, base)
  1761. defer delete(as)
  1762. cb := big.internal_count_bits(a)
  1763. if print_name {
  1764. fmt.printf(name)
  1765. }
  1766. if err != nil {
  1767. fmt.printf(" (Error: %v) ", err)
  1768. }
  1769. fmt.printf(as)
  1770. if print_extra_info {
  1771. fmt.printf(" (base: %v, bits: %v, digits: %v)", base, cb, a.used)
  1772. }
  1773. if newline {
  1774. fmt.println()
  1775. }
  1776. }
  1777. a, b, c, d, e, f, res := &big.Int{}, &big.Int{}, &big.Int{}, &big.Int{}, &big.Int{}, &big.Int{}, &big.Int{}
  1778. defer big.destroy(a, b, c, d, e, f, res)
  1779. // How many bits should the random prime be?
  1780. bits := 64
  1781. // Number of Rabin-Miller trials, -1 for automatic.
  1782. trials := -1
  1783. // Default prime generation flags
  1784. flags := big.Primality_Flags{}
  1785. err := big.internal_random_prime(a, bits, trials, flags)
  1786. if err != nil {
  1787. fmt.printf("Error %v while generating random prime.\n", err)
  1788. } else {
  1789. print_bigint("Random Prime A: ", a, 10)
  1790. fmt.printf("Random number iterations until prime found: %v\n", big.RANDOM_PRIME_ITERATIONS_USED)
  1791. }
  1792. // If we want to pack this Int into a buffer of u32, how many do we need?
  1793. count := big.internal_int_pack_count(a, u32)
  1794. buf := make([]u32, count)
  1795. defer delete(buf)
  1796. written: int
  1797. written, err = big.internal_int_pack(a, buf)
  1798. fmt.printf("\nPacked into u32 buf: %v | err: %v | written: %v\n", buf, err, written)
  1799. // If we want to pack this Int into a buffer of bytes of which only the bottom 6 bits are used, how many do we need?
  1800. nails := 2
  1801. count = big.internal_int_pack_count(a, u8, nails)
  1802. byte_buf := make([]u8, count)
  1803. defer delete(byte_buf)
  1804. written, err = big.internal_int_pack(a, byte_buf, nails)
  1805. fmt.printf("\nPacked into buf of 6-bit bytes: %v | err: %v | written: %v\n", byte_buf, err, written)
  1806. // Pick another random big Int, not necesssarily prime.
  1807. err = big.random(b, 2048)
  1808. print_bigint("\n2048 bit random number: ", b)
  1809. // Calculate GCD + LCM in one fell swoop
  1810. big.gcd_lcm(c, d, a, b)
  1811. print_bigint("\nGCD of random prime A and random number B: ", c)
  1812. print_bigint("\nLCM of random prime A and random number B (in base 36): ", d, 36)
  1813. }
  1814. matrix_type :: proc() {
  1815. fmt.println("\n# matrix type")
  1816. // A matrix is a mathematical type built into Odin. It is a regular array of numbers,
  1817. // arranged in rows and columns
  1818. {
  1819. // The following represents a matrix that has 2 rows and 3 columns
  1820. m: matrix[2, 3]f32
  1821. m = matrix[2, 3]f32{
  1822. 1, 9, -13,
  1823. 20, 5, -6,
  1824. }
  1825. // Element types of integers, float, and complex numbers are supported by matrices.
  1826. // There is no support for booleans, quaternions, or any compound type.
  1827. // Indexing a matrix can be used with the matrix indexing syntax
  1828. // This mirrors othe type usages: type on the left, usage on the right
  1829. elem := m[1, 2] // row 1, column 2
  1830. assert(elem == -6)
  1831. // Scalars act as if they are scaled identity matrices
  1832. // and can be assigned to matrices as them
  1833. b := matrix[2, 2]f32{}
  1834. f := f32(3)
  1835. b = f
  1836. fmt.println("b", b)
  1837. fmt.println("b == f", b == f)
  1838. }
  1839. { // Matrices support multiplication between matrices
  1840. a := matrix[2, 3]f32{
  1841. 2, 3, 1,
  1842. 4, 5, 0,
  1843. }
  1844. b := matrix[3, 2]f32{
  1845. 1, 2,
  1846. 3, 4,
  1847. 5, 6,
  1848. }
  1849. fmt.println("a", a)
  1850. fmt.println("b", b)
  1851. c := a * b
  1852. #assert(type_of(c) == matrix[2, 2]f32)
  1853. fmt.tprintln("c = a * b", c)
  1854. }
  1855. { // Matrices support multiplication between matrices and arrays
  1856. m := matrix[4, 4]f32{
  1857. 1, 2, 3, 4,
  1858. 5, 5, 4, 2,
  1859. 0, 1, 3, 0,
  1860. 0, 1, 4, 1,
  1861. }
  1862. v := [4]f32{1, 5, 4, 3}
  1863. // treating 'v' as a column vector
  1864. fmt.println("m * v", m * v)
  1865. // treating 'v' as a row vector
  1866. fmt.println("v * m", v * m)
  1867. // Support with non-square matrices
  1868. s := matrix[2, 4]f32{ // [4][2]f32
  1869. 2, 4, 3, 1,
  1870. 7, 8, 6, 5,
  1871. }
  1872. w := [2]f32{1, 2}
  1873. r: [4]f32 = w * s
  1874. fmt.println("r", r)
  1875. }
  1876. { // Component-wise operations
  1877. // if the element type supports it
  1878. // Not support for '/', '%', or '%%' operations
  1879. a := matrix[2, 2]i32{
  1880. 1, 2,
  1881. 3, 4,
  1882. }
  1883. b := matrix[2, 2]i32{
  1884. -5, 1,
  1885. 9, -7,
  1886. }
  1887. c0 := a + b
  1888. c1 := a - b
  1889. c2 := a & b
  1890. c3 := a | b
  1891. c4 := a ~ b
  1892. c5 := a &~ b
  1893. // component-wise multiplication
  1894. // since a * b would be a standard matrix multiplication
  1895. c6 := hadamard_product(a, b)
  1896. fmt.println("a + b", c0)
  1897. fmt.println("a - b", c1)
  1898. fmt.println("a & b", c2)
  1899. fmt.println("a | b", c3)
  1900. fmt.println("a ~ b", c4)
  1901. fmt.println("a &~ b", c5)
  1902. fmt.println("hadamard_product(a, b)", c6)
  1903. }
  1904. { // Submatrix casting square matrices
  1905. // Casting a square matrix to another square matrix with same element type
  1906. // is supported.
  1907. // If the cast is to a smaller matrix type, the top-left submatrix is taken.
  1908. // If the cast is to a larger matrix type, the matrix is extended with zeros
  1909. // everywhere and ones in the diagonal for the unfilled elements of the
  1910. // extended matrix.
  1911. mat2 :: distinct matrix[2, 2]f32
  1912. mat4 :: distinct matrix[4, 4]f32
  1913. m2 := mat2{
  1914. 1, 3,
  1915. 2, 4,
  1916. }
  1917. m4 := mat4(m2)
  1918. assert(m4[2, 2] == 1)
  1919. assert(m4[3, 3] == 1)
  1920. fmt.println("m2", m2)
  1921. fmt.println("m4", m4)
  1922. fmt.println("mat2(m4)", mat2(m4))
  1923. assert(mat2(m4) == m2)
  1924. }
  1925. { // Casting non-square matrices
  1926. // Casting a matrix to another matrix is allowed as long as they share
  1927. // the same element type and the number of elements (rows*columns).
  1928. // Matrices in Odin are stored in column-major order, which means
  1929. // the casts will preserve this element order.
  1930. mat2x4 :: distinct matrix[2, 4]f32
  1931. mat4x2 :: distinct matrix[4, 2]f32
  1932. x := mat2x4{
  1933. 1, 3, 5, 7,
  1934. 2, 4, 6, 8,
  1935. }
  1936. y := mat4x2(x)
  1937. fmt.println("x", x)
  1938. fmt.println("y", y)
  1939. }
  1940. // TECHNICAL INFORMATION: the internal representation of a matrix in Odin is stored
  1941. // in column-major format
  1942. // e.g. matrix[2, 3]f32 is internally [3][2]f32 (with different a alignment requirement)
  1943. // Column-major is used in order to utilize (SIMD) vector instructions effectively on
  1944. // modern hardware, if possible.
  1945. //
  1946. // Unlike normal arrays, matrices try to maximize alignment to allow for the (SIMD) vectorization
  1947. // properties whilst keeping zero padding (either between columns or at the end of the type).
  1948. //
  1949. // Zero padding is a compromise for use with third-party libraries, instead of optimizing for performance.
  1950. // Padding between columns was not taken even if that would have allowed each column to be loaded
  1951. // individually into a SIMD register with the correct alignment properties.
  1952. //
  1953. // Currently, matrices are limited to a maximum of 16 elements (rows*columns), and a minimum of 1 element.
  1954. // This is because matrices are stored as values (not a reference type), and thus operations on them will
  1955. // be stored on the stack. Restricting the maximum element count minimizing the possibility of stack overflows.
  1956. // Built-in Procedures (Compiler Level)
  1957. // transpose(m)
  1958. // transposes a matrix
  1959. // outer_product(a, b)
  1960. // takes two array-like data types and returns the outer product
  1961. // of the values in a matrix
  1962. // hadamard_product(a, b)
  1963. // component-wise multiplication of two matrices of the same type
  1964. // matrix_flatten(m)
  1965. // converts the matrix into a flatten array of elements
  1966. // in column-major order
  1967. // Example:
  1968. // m := matrix[2, 2]f32{
  1969. // x0, x1,
  1970. // y0, y1,
  1971. // }
  1972. // array: [4]f32 = matrix_flatten(m)
  1973. // assert(array == {x0, y0, x1, y1})
  1974. // conj(x)
  1975. // conjugates the elements of a matrix for complex element types only
  1976. // Built-in Procedures (Runtime Level) (all square matrix procedures)
  1977. // determinant(m)
  1978. // adjugate(m)
  1979. // inverse(m)
  1980. // inverse_transpose(m)
  1981. // hermitian_adjoint(m)
  1982. // matrix_trace(m)
  1983. // matrix_minor(m)
  1984. }
  1985. main :: proc() {
  1986. when true {
  1987. the_basics()
  1988. control_flow()
  1989. named_proc_return_parameters()
  1990. explicit_procedure_overloading()
  1991. struct_type()
  1992. union_type()
  1993. using_statement()
  1994. implicit_context_system()
  1995. parametric_polymorphism()
  1996. array_programming()
  1997. map_type()
  1998. implicit_selector_expression()
  1999. partial_switch()
  2000. cstring_example()
  2001. bit_set_type()
  2002. deferred_procedure_associations()
  2003. reflection()
  2004. quaternions()
  2005. unroll_for_statement()
  2006. where_clauses()
  2007. foreign_system()
  2008. ranged_fields_for_array_compound_literals()
  2009. deprecated_attribute()
  2010. range_statements_with_multiple_return_values()
  2011. threading_example()
  2012. soa_struct_layout()
  2013. constant_literal_expressions()
  2014. union_maybe()
  2015. explicit_context_definition()
  2016. relative_data_types()
  2017. or_else_operator()
  2018. or_return_operator()
  2019. arbitrary_precision_maths()
  2020. matrix_type()
  2021. }
  2022. }