<html>
<head>
<style type="text/css">
body {
font-family: "Arial", "Helvetica", sans-ser<span class="keyword">if</span>;
background-color: #ffffff;
color: #111111;
}
h2 {
text-align: center;
}
.main {
background-color: #eeeeee;
width: 800px;
margin-left: auto;
margin-right: auto;
padding-top: 10px;
padding-bottom: 20px;
padding-left: 40px;
padding-right: 40px;
}
.keyword { color: #336699; }
.constr { color: #993366; }
.string { color: #339966; }
.comment { color: #666666; }
</style>
</head>
<body>
<div class="main">
<pre>
<span class="comment">(** Some example programs in the Matzo language
*
* It is important to note that, in the Matzo language, the value of a
* variable is generally not a single value, but one of a set of values.
* Every time a variable is used, an element is drawn from this set;
* so the following program can print 1, 2, 3, 10, 20, or 30. *)</span>
x := 1 | 2 | 3;
f := { n => x }
| { n => n * 10 };
<span class="keyword">puts</span> f(x);
<span class="comment">(** The Aquan language program *)</span>
<span class="comment">(* The ::= operator is for the common case that we want to select from a
* whole bunch of literal characters or words. It is referred to as the
* 'literal assignment operator'. *)</span>
vowel ::= <span class="string">a e i o u</span>;
consonant ::= <span class="string">p t k w h m n l</span>;
<span class="comment">(* Normally, each alternative has equal weight, but explicit weights can be
* given; weights which are not given are considered to be 1. *)</span>
syll := 4: vowel | 4: consonant vowel
| vowel <span class="string">"'"</span> | consonant vowel <span class="string">"'"</span>;
<span class="comment">(* The `n @ value` syntax represents 1 to n possible repetitions of value.
* e.g. 3 @ ("a" | "b") is the same as
* ( ("a" | "b")
* | ("a" | "b") ("a" | "b")
* | ("a" | "b") ("a" | "b") ("a" | "b") )
* and is given special syntax because it is a common case in word generation.
* Below, it means that every word will have at least three and at most eight
* syllables. *)</span>
word := syll syll (6 @ syll);
<span class="keyword">puts</span> word;
<span class="comment">(** Fixing a value *)</span>
<span class="comment">(* The built-in pseudo-function `choose` allows you to force a value
* to stay the same. *)</span>
n := 1 | 2;
m := choose(n);
<span class="comment">(* The following could print either "1,2", "2,1" "1,1" or "2,2" *)</span>
<span class="keyword">puts</span> <span class="string">"{n},{n}"</span>
<span class="comment">(* The following can only print "1,1" or "2,2" *)</span>
<span class="keyword">puts</span> <span class="string">"{m},{m}"</span>
<span class="comment">(** A random person description *)</span>
<span class="comment">(* Identifiers that begin with capitals are symbols. Symbols can be
* compared and matched against. It is not allowed to use an identifier
* that begins with a capital as a variable. *)</span>
pronoun :=
{ <span class="constr">Male</span> => <span class="string">"he"</span>
| <span class="constr">Female</span> => <span class="string">"she"</span>
};
person :=
{ <span class="constr">Male</span> => <span class="string">"man"</span>
| <span class="constr">Female</span> => <span class="string">"woman"</span>
};
gender := choose(<span class="constr">Male</span> | <span class="constr">Female</span>);
hair ::= <span class="string">long short</span>;
hair_color ::= <span class="string">brown black blonde red white</span>;
eye_color ::= <span class="string">blue black brown green</span>;
mood ::= <span class="string">happy sad angry</span>;
<span class="keyword">puts</span> <span class="string">"This {person(gender)} has {hair}, {hair_color} hair and "
"{eye_color} eyes; {pronoun(gender)} appears to be "
"quite {mood}."</span>;
</pre>
</div>
</body>
</html>