051_values.zig 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // If you thought the last exercise was a deep dive, hold onto your
  3. // hat because we are about to descend into the computer's molten
  4. // core.
  5. //
  6. // (Shouting) DOWN HERE, THE BITS AND BYTES FLOW FROM RAM TO THE CPU
  7. // LIKE A HOT, DENSE FLUID. THE FORCES ARE INCREDIBLE. BUT HOW DOES
  8. // ALL OF THIS RELATE TO THE DATA IN OUR ZIG PROGRAMS? LET'S HEAD
  9. // BACK UP TO THE TEXT EDITOR AND FIND OUT.
  10. //
  11. // Ah, that's better. Now we can look at some familiar Zig code.
  12. //
  13. // @import() adds the imported code to your own. In this case, code
  14. // from the standard library is added to your program and compiled
  15. // with it. All of this will be loaded into RAM when it runs. Oh, and
  16. // that thing we name "const std"? That's a struct!
  17. const std = @import("std");
  18. // Remember our old RPG Character struct? A struct is really just a
  19. // very convenient way to deal with memory. These fields (gold,
  20. // health, experience) are all values of a particular size. Add them
  21. // together and you have the size of the struct as a whole.
  22. const Character = struct {
  23. gold: u32 = 0,
  24. health: u8 = 100,
  25. experience: u32 = 0,
  26. };
  27. // Here we create a character called "the_narrator" that is a constant
  28. // (immutable) instance of a Character struct. It is stored in your
  29. // program as data, and like the instruction code, it is loaded into
  30. // RAM when your program runs. The relative location of this data in
  31. // memory is hard-coded and neither the address nor the value changes.
  32. const the_narrator = Character{
  33. .gold = 12,
  34. .health = 99,
  35. .experience = 9000,
  36. };
  37. // This "global_wizard" character is very similar. The address for
  38. // this data won't change, but the data itself can since this is a var
  39. // and not a const.
  40. var global_wizard = Character{};
  41. // A function is instruction code at a particular address. Function
  42. // parameters in Zig are always immutable. They are stored in "the
  43. // stack". A stack is a type of data structure and "the stack" is a
  44. // specific bit of RAM reserved for your program. The CPU has special
  45. // support for adding and removing things from "the stack", so it is
  46. // an extremely efficient place for memory storage.
  47. //
  48. // Also, when a function executes, the input arguments are often
  49. // loaded into the beating heart of the CPU itself in registers.
  50. //
  51. // Our main() function here has no input parameters, but it will have
  52. // a stack entry (called a "frame").
  53. pub fn main() void {
  54. // Here, the "glorp" character will be allocated on the stack
  55. // because each instance of glorp is mutable and therefore unique
  56. // to the invocation of this function.
  57. var glorp = Character{
  58. .gold = 30,
  59. };
  60. // However, this "skull_farmer" character will be put in the
  61. // global immutable data even though it's defined in a function.
  62. // Since it's immutable, all invocations of the function can share
  63. // this one value.
  64. const skull_farmer = Character{};
  65. // The "reward_xp" value is interesting. It's a constant value, so
  66. // it could go with other global data. But being such a small
  67. // value, it may also simply be inlined as a literal value in your
  68. // instruction code where it is used. It's up to the compiler.
  69. const reward_xp: u32 = 200;
  70. // Now let's circle back around to that "std" struct we imported
  71. // at the top. Since it's just a regular Zig value once it's
  72. // imported, we can also assign new names for its fields and
  73. // declarations. "debug" refers to another struct and "print" is a
  74. // public function namespaced within THAT struct.
  75. //
  76. // Let's assign the std.debug.print function to a const named
  77. // "print" so that we can use this new name later!
  78. const print = ???;
  79. // Now let's look at assigning and pointing to values in Zig.
  80. //
  81. // We'll try three different ways of making a new name to access
  82. // our glorp Character and change one of its values.
  83. //
  84. // "glorp_access1" is incorrectly named! We asked Zig to set aside
  85. // memory for another Character struct. So when we assign glorp to
  86. // glorp_access1 here, we're actually assigning all of the fields
  87. // to make a copy! Now we have two separate characters.
  88. //
  89. // You don't need to fix this. But notice what gets printed in
  90. // your program's output for this one compared to the other two
  91. // assignments below!
  92. var glorp_access1: Character = glorp;
  93. glorp_access1.gold = 111;
  94. print("1:{}!. ", .{glorp.gold == glorp_access1.gold});
  95. // NOTE:
  96. //
  97. // If we tried to do this with a const Character instead of a
  98. // var, changing the gold field would give us a compiler error
  99. // because const values are immutable!
  100. //
  101. // "glorp_access2" will do what we want. It points to the original
  102. // glorp's address. Also remember that we get one implicit
  103. // dereference with struct fields, so accessing the "gold" field
  104. // from glorp_access2 looks just like accessing it from glorp
  105. // itself.
  106. var glorp_access2: *Character = &glorp;
  107. glorp_access2.gold = 222;
  108. print("2:{}!. ", .{glorp.gold == glorp_access2.gold});
  109. // "glorp_access3" is interesting. It's also a pointer, but it's a
  110. // const. Won't that disallow changing the gold value? No! As you
  111. // may recall from our earlier pointer experiments, a constant
  112. // pointer can't change what it's POINTING AT, but the value at
  113. // the address it points to is still mutable! So we CAN change it.
  114. const glorp_access3: *Character = &glorp;
  115. glorp_access3.gold = 333;
  116. print("3:{}!. ", .{glorp.gold == glorp_access3.gold});
  117. // NOTE:
  118. //
  119. // If we tried to do this with a *const Character pointer,
  120. // that would NOT work and we would get a compiler error
  121. // because the VALUE becomes immutable!
  122. //
  123. // Moving along...
  124. //
  125. // Passing arguments to functions is pretty much exactly like
  126. // making an assignment to a const (since Zig enforces that ALL
  127. // function parameters are const).
  128. //
  129. // Knowing that, see if you can make levelUp() work as expected -
  130. // it should add the specified amount to the supplied character's
  131. // experience points:
  132. print("XP before:{}, ", .{glorp.experience});
  133. levelUp(glorp, reward_xp);
  134. print("after:{}.\n", .{glorp.experience});
  135. }
  136. fn levelUp(character_access: Character, xp: u32) void {
  137. character_access.experience += xp;
  138. }
  139. // And there's more!
  140. //
  141. // Data segments (allocated at compile time) and "the stack"
  142. // (allocated at run time) aren't the only places where program data
  143. // can be stored in memory. They're just the most efficient. Sometimes
  144. // we don't know how much memory our program will need until the
  145. // program is running. Also, there is a limit to the size of stack
  146. // memory allotted to programs (often set by your operating system).
  147. // For these occasions, we have "the heap".
  148. //
  149. // You can use as much heap memory as you like (within physical
  150. // limitations, of course), but it's much less efficient to manage
  151. // because there is no built-in CPU support for adding and removing
  152. // items as we have with the stack. Also, depending on the type of
  153. // allocation, your program MAY have to do expensive work to manage
  154. // the use of heap memory. We'll learn about heap allocators later.
  155. //
  156. // Whew! This has been a lot of information. You'll be pleased to know
  157. // that the next exercise gets us back to learning Zig language
  158. // features we can use right away to do more things!