To compile Aldwych, enter the command
% aldwych <filename> -k <filename>The first file should be a file of Aldwych code, the second is the file of KLIC code that will be produced. If the
-k
option is omitted, the KLIC code will be sent to standard output.
Next enter the command
% klic -o <filename> <filename> <filename>where the first filename is the name for the object code, the second file name is the file of KLIC code produced previously, and the third file name a library file you will need in order for your code to interact with the outside world. Currently, the only such library file available is
aio.kl1
. This provides simple text input and
output.
Any Aldwych program must have a main
procedure which is used
to start execution. In this part we will consider programs that have
nothing but a main
procedure. Here is "hello world" in Aldwych:
#main == Screen<-aio\stdio(), Screen.fwrite("Hello world\n");A
main
procedure starts with #main ==
followed
by one or more Aldwych statements separated by commas and ending in a
semicolon. The symbol %
is used to start a comment,
which continues to the end of the line, so
#main == % Hello world program Screen<-aio\stdio(), Screen.fwrite("Hello world\n");is the program with a comment.
Screen<-aio\stdio()
creates a stdio
(from the description of stdio
in aio.kl1
) object
and calls it Screen
.
The statement Screen.fwrite("Hello world\n")
send this object
the message fwrite("Hello world\n")
. An fwrite
message sent to a stdio
object causes the string given as
its argument to be printed in the command window. Here, the string is a
literal, shown by being
enclosed in double-quotes. The usual convention for escape
characters applies, so \n
represents the new-line character.
Spaces and newlines count only as separators in Aldwych, so the layout in the code is not significant. It could be written, for example:
#main==Screen<-aio\stdio(),Screen.fwrite("Hello world\n");The ordering of statements is not significant. The "hello world" program could equally be written:
#main == Screen.fwrite("Hello world\n"), Screen<-aio\stdio();All statements are potentially executable concurrently. Execution of a statement is, however, suspended if it requires the value of a variable in it. Thus in the above,
Screen.fwrite("Hello world\n")
would be suspended until Screen<-aio\stdio()
has given
a value to Screen
.
Here is Aldwych code for asking for your name and then replying:
#main == Screen<-aio\stdio(), Screen.fwrite("What is your name? ") .readString->name .fwrite("Hello ">name>"\n");Here, the object named
Screen
is sent three messages
in succession, fwrite("What is your name? ")
followed
by readString->name
followed by
write("Hello ">name>"\n")
.
In general, a statement consisting of an object name followed by a
sequence of messages each preceded by a dot send each of those messages
to it in turn. The message order is always retained. The message
readString->name
causes a string (terminated by a
space or newline) to be read from the command window and put in the
variable name
. Note in Aldwych strings are not objects.
Object variables start with capital letters and non-object variables
start with small letters. Apart from this distinction between variables
to refer to objects and variables to hold values, variables do not have
types associated with them.
The second fwrite
message
shows the use of >
as a string concatenator.
The standard arithmetic operators can be used with numerical values,
with the standard precedence (+
, -
,
*
and /
with //
as the symbol
for mod
). Division is integer division, rounding down to
the nearest integer. The following reads two numbers and
prints their sum:
#main== Screen<-aio\stdio(), Screen.fwrite("Enter first number: ") .readInt->x .fwrite("Enter second number: ") .readInt->y .fwrite("Their sum is: ">+z>"\n"), z<-x+y;A
readInt
message is sent to a stdio
object to read an integer. To concatenate a string to an integer to
produce a string,>+
is used.
Other messages that may be sent to a
Note, z<-x+y
shows an example of an assignment. Note evaluation
of the fifth message to
Screen
is
suspended until this assignment completes.
stdio
object include:
getc->ch
reads a single character to the variable
ch
.
putc(ch)
prints the character in variable ch
.
ungetc(ch)
returns the character ch
back to
the reading stream.
readLine->s
reads a whole line (up to but not including
the first newline character) as a string into the variable s
.
nl
prints a new line.
writeInt(n)
prints the integer in variable n
.
writeInt(n,p)
prints n
taking at least p
spaces (filling in with blanks if necessary).
skip
reads and ignores all characters up to the next
white space character.
>-
is used to join single characters to a string.
Single assignment
A variable may not be assigned to more than once, so
#main==
Screen<-aio\stdio(),
Screen.fwrite("Enter first number: ")
.readInt->x
.fwrite("Enter second number: ")
.readInt->y
.fwrite("Their sum is: ">+z>"\n"),
z<-x+y,
z<-x*y;
would cause an error that would be detected when the Aldwych file is
compiled to KLIC. A variable may not be used unless it is
assigned to, and a variable which has been assigned must be used,
so the following contains two errors, the variable z
is
used without being assigned a value, while the variable w
is assigned a value but not used:
#main==
Screen<-aio\stdio(),
Screen.fwrite("Enter first number: ")
.readInt->x
.fwrite("Enter second number: ")
.readInt->y
.fwrite("Their sum is: ">+z>"\n"),
w<-x+y;
Note that as +
has higher precedence than >+
,
the sum program could be written:
#main==
Screen<-aio\stdio(),
Screen.fwrite("Enter first number: ")
.readInt->x
.fwrite("Enter second number: ")
.readInt->y
.fwrite("Their sum is: ">+x+y>"\n");
The expression x+y
is evaluated and the result converted
into a string which is concatenated to the message.