demo.odin 3.6 KB

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