demo.odin 62 KB

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