[Contents]
[Prev] [Next] [Limbo Basics] [Limbo Programming] [Language Definition]

A Limbo Program

To illustrate the Limbo language, here is a simple example of a Limbo program. This is a simple greeting program (a twist on the venerable "Hello, World!"). It illustrates the key parts of a basic Limbo program.

Following the line-by-line program description, use Inferno's Limbo compiler to compile the example and then run the program from an Inferno shell (command line).

Program Listing 2-1 greet.b

  1. implement Command;
  2. include "sys.m";
  3. include "draw.m";
  4. sys: Sys;
  5. Command: module {
  6. init: fn(nil: ref Draw->Context, argv: list of string);
  7. };
  8. init(nil: ref Draw->Context, argv: list of string) {
  9. sys = load Sys Sys->PATH;
  10. # check for command-line arguments
  11. if ((tl argv) != nil) {
  12. sys->print("Hello, %s!\n", hd (tl argv));
  13. } else
  14. sys->print("Hello, World!\n");
  15. }


Note: The line numbers are for reference only.
Line 1 says that this file is the implementation of the Command module. In this simple example, as is common with simple Limbo programs, the interface and implementation are in the same source file. The file name does not need to match the module name or the implementation name, although conventionally they do. In this example, implementation of Command is a special interface for modules (programs) launched at the Inferno shell.

Lines 3 and 4 include the interface declarations of two modules that the module uses. Line 3 includes the interface to the System Module (sys.m), and Line 4 includes the interface to the Draw Module (draw.m). Limbo does not have a preprocessor, as does C. Therefore, include is a keyword, not a preprocessor directive. It is analogous to C's #include directive, however. In Limbo, the file name following the include keyword is always enclosed in double quotes ("mod.m").

Line 6 declares the variable sys to be of type Sys (Limbo is case-sensitive). The Sys type is a module type, as declared in the sys.m file:

	Sys: module
	{
		...
	};

The variable sys is declared to be a handle to the functions and data of the Sys module. At this point, however, its value is nil.

Lines 8 through 10 is the declaration of the Command module that we stated was implemented in this file in Line 1. One function, init, is declared for this module. This function has two arguments, a ref Draw->Context and a list of string.

The init function and the two arguments are required of all programs that are invoked by the Inferno shell command line. It is somewhat analogous to the main function in C-it specifies where execution starts in the module and captures the graphics context and command-line arguments, if any.

The ref Draw->Context is used to grab the context of the display and is defined in draw.m, hence the reason that file was included (Line 4). This argument is required even if the program does not do any graphics. If that is the case, as with our example, the argument can be named nil.

The list of string is a list of the arguments passed to this module via the command line. Conventionally this is named argv. If the program does not expect any arguments passed from the command line, it can be named nil.

Lines 12 through 19 contain the definition of the init function, or the statements that will be executed.

Earlier, in Line 6, we mentioned that the value of sys was nil. In Line 13, via the load keyword, we create a reference to the Sys module implementation, assigned to sys. This connects the program with the Sys library module and gives the program access to the functions and data implemented in the Sys module.

Line 14 is a comment. Comments in Limbo start with a pound sign (#) and extend to the end of the line. Multi-line comments require a # at the beginning of each line.

Lines 15 through 18 print the message using the command line argument passed to init via argv. The second argument of the init function is a list of string. Lists in Limbo are a group of sequential, ordered elements. You move through the elements of a list using the hd (head) and tl (tail) operators. The first element, the head of the list, of argv is the program name. Since we are only interested in the remainder of the list, the tail, we check to see if this is nil, which is a keyword that means it contains nothing. If it is not nil (has some value) then we print the message and the head of the tail (the second element in the list). If there are no arguments, then we print the "Hello, World!" message.

The section Language Basics starting on page 2-8 discusses the semantics of the language in greater detail.



[Contents]
[Prev] [Next] [Limbo Basics] [Limbo Programming] [Language Definition]

Copyright © 1998, Lucent Technologies, Inc. All rights reserved.