This is the famous Hello, world! program, in TOM this time -- the line numbers are for clarity; they are not present in the actual source:
1 implementation class HelloWorld 2 3 int 4 main Array argv 5 { 6 [[[stdio out] print "Hello, world!"] nl]; 7 } 8 9 end; 10 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.
In line 11 the instances of the HelloWorld class are descibed. Since we have no use for them in this example, the definition can be empty.