92 lines
3.8 KiB
Markdown
92 lines
3.8 KiB
Markdown
# Tigersay
|
|
|
|
## The dOvs Course ##
|
|
|
|
This last semester I've been following my university's course on
|
|
writing a compiler from scratch. The course uses the classic
|
|
[Modern Compiler Implementation in
|
|
ML](https://www.cs.princeton.edu/%7Eappel/modern/ml/), and thus our
|
|
input language is something called **Tiger**, a toy language designed
|
|
specifically for the book. The joke goes that Tiger is one of the
|
|
least used programming languages, but one of the most supported. It is
|
|
also weird. It's like if **C** and [Standard
|
|
ML](https://en.wikipedia.org/wiki/Standard_ML) had a deformed baby:
|
|
Mutually inductive data types and functions? Sure! Primitives for both
|
|
functional and imperative coding styles? Great idea! Type inference
|
|
for variables, but not for functions. Perfect! A way to determine the
|
|
size of arrays at runtime? No, absolutely not!
|
|
|
|
![A small piece of Tiger code, demonstrating an implementation of a
|
|
linked list.](images/tiger-example.png)
|
|
|
|
The reason for these weird idiosyncrasies is obvious: nobody is
|
|
supposed to write actual programs in Tiger. The features of
|
|
Tiger are designed to illustrate how to implement them, not
|
|
necessarily to form a coherent whole.
|
|
|
|
That said, some of the features work together to allow a strange
|
|
elegance in the way some programs can be written. Recursive functions
|
|
and structure types allow one to express linked lists and maps using
|
|
relatively little code. See [the example
|
|
code](https://gitfub.space/Jmaa/tigersay/src/branch/master/tiger-linked-list.tig)
|
|
above, for an implementation of a linked list, and a function for finding the sum of
|
|
the list, using a recursive helper function. I could just as easily
|
|
have implemented it using an imperative style.
|
|
|
|
<!-- TODO: Include link to tests. -->
|
|
|
|
While following the course, my group used a testing framework,
|
|
containing many programs similar in style to the above code sniplet,
|
|
ensuring our compiler's compliance with the language. These
|
|
tests were small, and rarely tested more than a few features at once.
|
|
The question "Is our compiler actually capable of translating a _real_
|
|
program?" arose, and this lead me down the road of actually writing a
|
|
"real" program in Tiger, using its weird feature set.
|
|
|
|
## Tigersay ##
|
|
|
|
<!--start-->
|
|
|
|
![A badly drawn tiger explains its own existence: "Once upon a time,
|
|
there was a programming language named Tiger, and nobody used it. Then
|
|
some stupid student made a clone of cowsay, and that is how I was
|
|
born"](images/tigersay-example.png)
|
|
|
|
**Tigersay** is an implementation of the classic Perl program
|
|
[Cowsay](https://en.wikipedia.org/wiki/Cowsay), written in the Tiger
|
|
programming language, a toy language originating from the [Modern
|
|
Compiler Implementation in
|
|
ML/C/Java](https://www.cs.princeton.edu/%7Eappel/modern/ml/) series of
|
|
books.
|
|
|
|
It does not come compiled, nor does it come with a **Tiger** compiler.
|
|
The only way to experience the wonder that is **Tigersay** is to use
|
|
your own **Tiger** compiler. (Or find one on the internet, but that's
|
|
cheating.)
|
|
|
|
<!--end-->
|
|
|
|
The standard version of Tiger is limited in its ability to interact
|
|
with the command-line, and as a result, so is Tigersay. The only way
|
|
to interact with Tigersay is to pipe the desired text into Tigersay:
|
|
|
|
```bash
|
|
echo "Hello World" | tigersay
|
|
```
|
|
|
|
Unfortunately this also means Tigersay only supports a single face,
|
|
the default tiger one, due to having no support for command-line
|
|
arguments.
|
|
|
|
Tigersay responds to certain strings in silly ways, the most useful
|
|
ones being _help_ and _version_. Additionally it interfaces nicely
|
|
with [fortune](https://en.wikipedia.org/wiki/Fortune_(Unix\)).
|
|
|
|
![The badly drawn tiger exclaim: "I never made a mistake in my life.
|
|
I thought I did once, but I was wrong. -- Lucy Van
|
|
Pelt"](images/tigersay-fortune.png)
|
|
|
|
If you'd like to play around with Tigersay yourself, you can find the source
|
|
code in the [Gitfub repository](https://gitfub.space/Jmaa/tigersay)
|
|
|