Tech News
← Back to articles

Advent of Code on the Z-Machine

read original related products more articles

So far, we have only seen procedural code, but Inform 6 is also somewhat object-oriented7 Sometimes the Z-machine is described as one of the first widely-installed object-oriented systems, but there is very little support for object-orientation in the Z-machine itself. Also zil does not support what we would today recognise as object-oriented code. It has things called objects, but they are closer to C struct s., with the idea being that messages being passed between objects is a useful way to simulate interactions in the world. It still won’t allocate objects dynamically, so for the most part it is used with singleton objects.8 It is possible to create objects during run-time, but then they come from a statically allocated fixed-size pool.

Inform 6 supports dual object hierarchies: it encodes is-a relationships through inheritance, and has-a relationships through an object tree indicating containment. We can use the first half of the second day’s puzzle to illustrate both.

To model the second day’s problem, we begin by defining an attribute indicating that a report is safe. An attribute is a boolean flag (in fact, they are called flags in zil ) that all objects start out not having, but it can be set on any of them.

In[18]:

Attribute valid ;

Then we create a class for the generic report approver.

In[19]:

Class Report_Approver with ! Store the previous value for range calculations. _prev nothing, ! Method that decides whether to accept a new value. _accept, ! Default reject method that accepts the first value, ! rejects any changes that are too large, and otherwise ! defers to the accept method. _reject [next; if (self._prev == nothing) return false; if (abs(next - self._prev) > 3) return true; return ~~self._accept(next); ], ! When appending a number, if it is rejected, remove ! the valid attribute from this approver. append [next; if (self._reject(next)) give self ~valid; self._prev = next; ], ! To reset an approver, remove the previous value ! and default back to a valid report again. reset [; self._prev = nothing; give self valid; ], has valid;

Here we can see some new features. Properties are like attributes except instead of booleans, they store values. Importantly, they can store anonymous subroutines, which are declared like normal subroutines except without a name, and inside them we have access to the implicit variable self . The keyword give sets and unsets flags on objects (sorry, I mean “assigns attributes to” objects, and “removes attributes from” objects).

As before, properties/methods that are not meant to be public are conventionally named with a leading underscore.

... continue reading