next up previous contents
Next: From source to executable Up: Introduction Previous: Basics   Contents

Getting started

An important program in any programming language is the famous `Hello, world!' It serves as a confidence test for the language installation at your site and shows you what to do to compile and run programs. This is what `Hello, world!' looks like when written in TOM (the line numbers are for clarity; they are not part of the actual source):

 1 implementation class HelloWorld
 3 int
 4   main Array argv
 5 {
 6   [[[stdio out] print "Hello, world!"] nl];
 7 }
 9 end;
11 implementation instance HelloWorld end;
The first line denotes the start of the implementation of a class named HelloWorld. The class ends with the end; at line 9. Between these lines a method is defined. A method is a piece of code associated with an object. In this case the object is the HelloWorld class object. The method we define is called main. It returns a value of type int and accepts one argument of type Array. The name of the argument is argv; the code within the method can refer to the value of the actual argument through the name argv.

Line 6 contains three nested bracketed expressions; each expression sends a message to an object. The inner expression is [stdio out]. Here, the argumentless out message is sent to the stdio class object; this object is the receiver of the message. In response to this message, the corresponding method will be invoked, in this case the out method of the stdio class. Because of this correspondence, the terms `sending a message' and `invoking a method' are synonyms and they will be used as such throughout this book.

The out method of the stdio class returns an OutputStream object to which information can be written. This stream is usually connected to the user's terminal. Execution of the main method will resume when the out method has returned.

If we call the result of the first method invocation x, the second expression becomes

[x print "Hello, world!"]

This sends to the object x the message print with a single argument, the string ` Hello, world!'. The stream object will print the string on the user's terminal and return itself. The third method invocation thus becomes

[x nl]

In response to this, the stream object will flush any buffered output to the user's terminal and emit a new-line character. Like the print method, it returns itself. This value is ignored here.

In line 7, the method body ends. We did not indicate a return value, so the default value is returned: 0.

00.5cm The main method is special, in that in every program exactly one class object must provide a main method. Upon execution of the program, this main method is invoked. When this method returns, the program terminates. The value returned by the main method is available by the calling program. On many machines, a value of 0 indicates success; any other value is assumed to indicate failure. In line 11 the instances of the HelloWorld class are descibed. Since we have no use for them, the definition can be empty.

next up previous contents
Next: From source to executable Up: Introduction Previous: Basics   Contents
Pieter J. Schoenmakers tiggr at