Article 1A3NF Entrance to Hades

Entrance to Hades

by
ericlippert
from Fabulous adventures in coding on (#1A3NF)

You are outside a large gate inscribed, "Abandon all hope ye who enter here!" The gate is open; beyond you can see a desolation, with a pile of mangled bodies in one corner. Thousands of voices, lamenting some hideous fate, can be heard. The gate is barred by evil spirits, who jeer at your attempts to pass.

> go south

The spirits block you from passing through the gate.

> attack the gate with the screwdriver

The gate is protected by an invisible force. It makes your teeth ache to touch it.

> ring the bell

A deep peal issues from the bell. The wraiths stop jeering and an expression of long-forgotten terror takes shape on their ashen faces. In your confusion, the candles drop to the ground (and they are out).

> take the candles

Taken.

> light a match

One of the matches starts to burn.

> light the candles with the match and read the book

The candles are now lit.
The flames flicker wildly and the earth trembles beneath your feet. The spirits cower at your unearthly power.
The match has gone out.
The prayer revererates in a deafening confusion. As the last word fades, a heart-stopping scream fills the cavern, and the spirits, sensing a greater power, flee through the walls.

> go south

Code for this episode can be found in https://github.com/ericlippert/flathead/tree/blog12.

Well that was a very dramatic introduction to the most boring module yet since I started by twiddling bits.

An evaluation stack is just a thin wrapper around a list of integers. A list of integers is already an immutable object that supports all the operations of a stack, so the implementation is pretty straightforward. (As with the local store, we have to make sure that we're turning any negative numbers that might have shown up into an unsigned 16 bit word.)

type t ={ items : int list}let empty = { items = [] }let length stack = List.length stack.itemslet peek stack = match stack.items with | [] -> failwith "peeking an empty stack" | h :: _ -> hlet pop stack = match stack.items with | [] -> failwith "popping empty stack" | _ :: t -> { items = t }let push stack item = let item = unsigned_word item in { items = item :: stack.items }let display stack = let to_string item = Printf.sprintf " %04x" item in accumulate_strings to_string stack.items

Next time on FAIC: we'll put all this together into a frame, and then make a stack of frames. We will then have code to manipulate both story state and interpreter state.


4191 b.gif?host=ericlippert.com&blog=67759120
External Content
Source RSS or Atom Feed
Feed Location http://ericlippert.com/feed
Feed Title Fabulous adventures in coding
Feed Link https://ericlippert.com/
Reply 0 comments