34 \ May 2019 \ http://www.phparch.com
Memory Abstractions
Internal Apparatus
In fact self-compiling compilers^1 are
common, but my first sight of one was
a shock!
Anthony Ferrara has written a PHP
compiler in PHP^2. It takes advantage
of an extremely promising feature of
PHP 7.4. called the Foreign Function
Interface^3 (FFI). That doesn’t sound
promising, but the concept is some-
thing Python programmers have
proven useful over the years. It allows
regular application code access to what
the compiler is doing, the runtime data
structures, and so on. I haven’t used FFI
yet myself, but it could be quite useful
for exploring how PHP works.
Writing the Algol compiler in Algol
is weird enough, but writing a PHP
compiler in PHP is both weird and
slow (as Ferrara also notes). Algol’s a
compiled language whereas PHP’s an
interpreted language. Algol, like its
descendant C, runs blindingly fast as
compared to an interpreted language.
To me, at least, it makes more sense to
have the compiler written in a language
other than itself. Write the compiler
in your language of choice, and it can
start compiling your newly-invented
programming language. Tools, known
as compiler compilers^4 or compiler
generators, have been helping this
process for several decades. To me, it
makes sense there’s that gap between C
and PHP to be crossed.
This month we’re talking about
memory abstractions. That topic
becomes unbelievably boring in a
remarkably short time. I’m not able to
make it interesting. So, let’s back up.
What problem are we trying to solve?
We’re aiming to bridge the gap
between designing PHP software—we
know how to do that—and under-
standing how the PHP compiler works.
1 self-compiling compilers:
https://phpa.me/wikip-bootstrapping
2 PHP compiler in PHP:
https://github.com/ircmaxell/php-compiler
3 Foreign Function Interface:
https://wiki.php.net/rfc/ffi
4 compiler compilers:
https://phpa.me/wikip-compiler2
It’s more important that we learn
concepts, rather than details.
The Project
Rather than taking the concepts head
on—which becomes overwhelmingly
boring, even by my standards—we’re
going to take on a project. This project
is specifically designed to achieve
weirdness, and it has a backstory from
more than 30 years ago.
What’s the point of aiming to achieve
weirdness? I intended to create a
learning/teaching tool. The weirdness, I
hoped, would make it more interesting.
In the PHP world, it’s becoming
more and more common to deal with
third-party APIs. In my experience,
the trickiest part can be the sequencing
of events. It can be challenging (and
frustrating) figuring out how to get the
steps in the right order for the correct
wor k fl ow.
Because a third party controls the API,
learning to use that API can be more
difficult. Specifically, how could we
learn to work with text message (SMS)
providers? I’m picturing something like
a chatbot that sends and receives text
messages to my phone. The problem is
that my phone doesn’t do any logging,
and the SMS provider also doesn’t do
any useful logging. I need to put some
PHP code in the middle to keep track of
the traffic—something like the Charles
Web Proxy^5.
Luckily, I have just the thing, a text-
based game program from 1986. It was
written into the mainframe’s operating
system and took over the mainframe’s
hard-wired operator console. We could
use my iPhone as the operator console,
communicating with the game program
via text message (both sending and
receiving).
So now the game program is itself an
API, communicating with its console
(my iPhone) via third-party SMS APIs.
It’s PHP code, and we could certainly
have a web page showing the inter-
actions. That is, we could have a page
scrolling requests and responses like
5 Charles Web Proxy:
https://www.charlesproxy.com
the browser “developer tools” console
or Charles Proxy. It becomes a learning
tool, and the game aspect hopefully
makes it more interesting than other-
wise.
What does this API-learning project
have to do with memory abstractions?
Quite a bit, as it turned out! That’s why
it became this month’s topic.
Let’s do some time travel and find a
possible bug hidden in the back story.
Adventure
Colossal Cave Adventure^6 , written
by Will Crowther for the PDP-10
mainframe, is the first known work
of interactive fiction, appearing in
- Don Woods expanded upon the
game in 1977. We’ll soon be gathering
requirements, which means we need
to learn a bit more about this famous
game:
In the game, the player controls
a character through simple text
commands to explore a cave
rumored to be filled with wealth.
Players earn predetermined points
for acquiring treasure and escap-
ing the cave alive, with the goal
to earn the maximum number of
points offered. The concept bore out
from Crowther’s background as a
caving enthusiast, with the game’s
cave structured loosely around the
Mammoth Cave system in Kentucky.
The game—known simply as Adven-
ture since it was the only one—proved
to be an accurate map of the Kentucky
cave system. Crowther enjoyed playing
Dungeons & Dragons, created only a
year earlier. He created the computer-
ized simulation of his cave explorations
for his daughters, adding elements of
the role-playing he’d learned through
Dungeons & Dragons. He created the
game to be controlled through natu-
ral-language input. Figure 1 shows the
opening screen of the game.
6 Colossal Cave Adventure:
https://phpa.me/colossal-cave-adventure