The module's implementation is contained in an implementation file, conventionally with the .b
suffix. This is the executable statements of the program. It can also be called the source file. Examples of implementation files are in the <inferno_root>
/appl
directory tree.
Program Listing 3-2 is the implementation file for the module interface file shown in Program Listing 3-1:
implement Greet;
include "sys.m";
include "draw.m";
include "greet.m";
sys: Sys;
init(ctxt: ref Draw->Context, argv: list of string) {
sys = load Sys Sys->PATH;
if ((tl argv) != nil)
sys->print("Hello, %s!\n", hd (tl argv));
else
sys->print("Hello, World!\n");
}
This implementation file contains the function definition that is declared in the module interface file.
The implementation file can also contain private data and function member declarations and definitions. For example, the implementation file for the Random Module (in the <inferno_root>
/appl/lib/rand.b
file):
implement Rand;
include "rand.m";
rsalt: big;
init(seed: int)
{
rsalt = big seed;
}
MASK: con (big 1<<63)-(big 1);
rand(modulus: int): int
{
rsalt = rsalt * big 1103515245 + big 12345;
if(modulus <= 0)
return 0;
return int (((rsalt&MASK)>>10) % big modulus);
}
bigrand(modulus: big): big
{
rsalt = rsalt * big 1103515245 + big 12345;
if(modulus <= big 0)
return big 0;
return ((rsalt&MASK)>>10) % modulus;
}
This file contains the definitions of the three public members,
init
, rand
, and bigrand
, as well as other private members, such as rsalt
and MASK
.
<inferno_root>
/dis
directory.
The executable file is the object that is loaded at run-time. This is done with the load statement. For example, to load the rand.dis
file, the load statement looks like:
rand := load Rand Rand->PATH;This is discussed more in the Using Modules section on page 3-11.