Entrance to Hades

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.items

let peek stack =
  match stack.items with
  | [] -> failwith "peeking an empty stack"
  | h :: _ -> h

let 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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s