May 2, 2001; William E. Drissel
Requires: conman.sav
This file is in the public domain.
Conman is a toy I used to teach myself elementary Icon. I
once vaguely heard of a program which could respond to queries
like "? Volume of the earth in tbsp".
The keywords of the language (which are not reserved) are:
load
save
print
? (same as print)
list
is and are which have the same effect
"Load" followed by an optional filename loads definitions of
units from a file. If filename is not supplied, it defaults to
"conman.sav"
"Save" makes a file for "load". Filename defaults to
"conman.sav". "Save" appends to an existing file so a user
needs to periodically edit his save file to prune it back.
"Print" and "?" are used in phrases like:
? 5 minutes in seconds
Conman replies:
5 minutes in seconds equals 300
List puts up on the screen all the defined units and the
corresponding values. Format is same as load/store format.
"Is" and "are" are used like this:
100 cm are 1 meter
The discovery of is or are causes the preceding token (in
this case "cm") to be defined. The load/store format is:
unitname "is" value
Examples:
8 furlongs is 1 mile
furlong is 1 / 8 mile
These last two are equivalent. Note spaces before and after
"/". Continuing examples:
1 fortnight is 14 days
furlong/fortnight is furlong / fortnight
inches/hour is inch / hour
After this a user might type:
? 1 furlong/fortnight in inches/hour
Conman will reply:
1 furlong/fortnight in inches/hour equals 23.57
Note: the following feature of Conman: his operators have no
precedence so the line above gets the right answer but
1 furlong/fortnight in inches / hour
gets the wrong answer. (One definition of a feature is a flaw we're
not going to fix).
____________________________________________________________
Program Notes:
The procedure, process, parses the user's string to see if it
begins with a keyword. If so, it acts accordingly. If not,
the user string is fed to isare.
Isare attempts to find "is" or "are" in the users string.
Failing to, isare feeds the string to conman which can
interpret anything. If "is" or "are" is found, the tokens
(delimited by blanks) before the "is" or "are" are stacked in
foregoing; those after are stacked in subsequent. Then the
name to be defined is popped off the foregoing and used as
the "index" into a table named values. The corresponding
number is computed as eval(subsequent) / eval(foregoing).
The procedure, stack, is based on Griswold and Griswold, "The
Icon Programming Language", p122.
The procedure, eval, unstacks the tokens from a stack one by
one until all have been considered. First, the tokens which
signify division by the next token are considered and used to
set a switch named action. Then depending on action, the
token is used to multiply the accumulator or divide it. If
eval can make the token into a number, the number is used,
failing that the token is looked up in the table named values
and the corresponding number is used. Failing both of those,
conman gripes to the user and does nothing (in effect
multiplying or dividing by 1). Finally, eval returns the
number accumulated by the operations with the tokens.
Load defaults the filename to conman.sav if the user didn't
supply one. Each line read is fed to isare. We will see
that save prepares the lines so isare can define the units.
Save uses Icon's sort to go thru the table "values". The
unit name is the left of a pair and the number stored is the
right of the pair. The word " is " is stuck between them so
isare will work.
Finally, we consider the procedure conman. During initial
design, this was perceived to be the largest part of the
effort of conman. It is a real tribute to the power of Icon
that only one non-trivial line of code is required. The
user's string is reproduced then the word "equals" followed
the result produced by eval after the user's string is
stacked.
Source code |
Program Library Page |
Icon Home Page