demo.odin 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #import "fmt.odin";
  2. #import "atomic.odin";
  3. #import "hash.odin";
  4. #import "math.odin";
  5. #import "mem.odin";
  6. #import "opengl.odin";
  7. #import "os.odin";
  8. main :: proc() {
  9. /*
  10. Version 0.1.1
  11. Added:
  12. * Dynamic Arrays `[dynamic]Type`
  13. * Dynamic Maps `map[Key]Value`
  14. * Dynamic array and map literals
  15. * Custom struct alignemnt `struct #align 8 { bar: i8 }`
  16. * Allow `_` in numbers
  17. * Variadic `append`
  18. * fmt.sprint*
  19. * Entities prefixes with an underscore do not get exported on imports
  20. * Overloaded `free` for pointers, slices, strings, dynamic arrays, and dynamic maps
  21. * enum types have an implict `names` field, a []string of all the names in that enum
  22. * immutable variables are "completely immutable" - rules need a full explanation
  23. * `slice_to_bytes` - convert any slice to a slice of bytes
  24. * `union_cast` allows for optional ok check
  25. * Record type field `names` (struct/raw_union/enum)
  26. * ?: ternary operator
  27. * Unions with variants and common fields
  28. * New built-in procedures
  29. - `delete` to delete map entries `delete(m, key)`
  30. - `clear` to clear dynamic maps and arrays `clear(map_or_array)`
  31. - `reserve` to reserve space for the dynamic maps and arrays `reserve(map_or_array)`
  32. * Unexported entities and fields using an underscore prefix
  33. Removed:
  34. * Maybe/option types
  35. * Remove `type` keyword and other "reserved" keywords
  36. * `compile_assert` and `assert`return the value of the condition for semantic reasons
  37. Changed:
  38. * thread_local -> #thread_local
  39. * #include -> #load
  40. * Files only get checked if they are actually used
  41. * match x in y {} // For type match statements
  42. * Version numbering now starts from 0.1.0 and uses the convention:
  43. - major.minor.patch
  44. * Core library additions to Windows specific stuff
  45. Fixes:
  46. * Many fmt.* fixes
  47. * Overloading bug due to comparison of named types
  48. * Overloading bug due to `#import .` collision
  49. * disallow a `cast` from pointers of unions
  50. * Minor bugs in generated IR code for slices
  51. To come very Soon™:
  52. * Linux and OS X builds (unofficial ones do exist already)
  53. */
  54. {
  55. Fruit :: enum {
  56. APPLE,
  57. BANANA,
  58. COCONUT,
  59. }
  60. fmt.println(Fruit.names);
  61. }
  62. {
  63. m: map[f32]int;
  64. reserve(m, 16);
  65. defer free(m);
  66. m[1.0] = 1278;
  67. m[2.0] = 7643;
  68. m[3.0] = 564;
  69. _, ok := m[3.0];
  70. c := m[3.0];
  71. assert(ok && c == 564);
  72. fmt.print("map[");
  73. i := 0;
  74. for val, key in m {
  75. if i > 0 {
  76. fmt.print(", ");
  77. }
  78. fmt.printf("%v=%v", key, val);
  79. i += 1;
  80. }
  81. fmt.println("]");
  82. }
  83. {
  84. m := map[string]u32{
  85. "a" = 56,
  86. "b" = 13453,
  87. "c" = 7654,
  88. };
  89. defer free(m);
  90. c := m["c"];
  91. _, ok := m["c"];
  92. assert(ok && c == 7654);
  93. fmt.println(m);
  94. }
  95. {
  96. x: [dynamic]f64;
  97. reserve(x, 16);
  98. defer free(x);
  99. append(x, 2_000_000.500_000, 3, 5, 7);
  100. for p, i in x {
  101. if i > 0 { fmt.print(", "); }
  102. fmt.print(p);
  103. }
  104. fmt.println();
  105. }
  106. {
  107. x := [dynamic]f64{2_000_000.500_000, 3, 5, 7};
  108. defer free(x);
  109. fmt.println(x);
  110. }
  111. {
  112. Vec3 :: [vector 3]f32;
  113. x := Vec3{1, 2, 3};
  114. y := Vec3{4, 5, 6};
  115. fmt.println(x < y);
  116. fmt.println(x + y);
  117. fmt.println(x - y);
  118. fmt.println(x * y);
  119. fmt.println(x / y);
  120. for i in x {
  121. fmt.println(i);
  122. }
  123. compile_assert(size_of([vector 7]bool) == size_of([7]bool));
  124. compile_assert(size_of([vector 7]i32) == size_of([7]i32));
  125. // align_of([vector 7]i32) != align_of([7]i32) // this may be the case
  126. }
  127. }