Skip to content
Adam
Language

Modules

Every source file is one module. A program is an exe module; units hold the code programs import, the standard library's among them.

Examples: res\examples\language\modules holds small, commented programs for this page's features, each listing the output it prints; run one with adam build <file> -r.

Kinds

A module's first line names its kind and its name, and the name is the file's name without .adam, in the same case:

  • module exe <name>; builds an executable. Only an exe has a body, its begin .. end., and an exe must have one.
  • module dll <name>; builds a shared library and module lib <name>; a static library.
  • module unit <name>; builds nothing of its own: it is compiled into every program that imports it.

A dll, a lib and a unit end with end. alone. A dotted name, such as Adam.Compiler.Lexer, lives in Adam.Compiler.Lexer.adam.

Imports

import names the units a module uses. import Foo finds Foo.adam in the importing file's folder, then in the module paths: the standard library's folder first, then each folder the main module adds with @addmodulepath, in order.

modules_init.adam
module exe modules_init;

import System.Collections;

var
  GList: List<int64>;

initialize
begin
  println("initialize runs first");
end;

finalize
begin
  println("finalize runs last");
end;

begin
  GList.Add(3);
  GList.Add(5);
  println("{} items, the second is {}", GList.Count, GList[1]);
end.
output on x86_64-windows-gnu and x86_64-linux-gnu
initialize runs first
2 items, the second is 5
finalize runs last

An imported unit's public names are reached as Unit.Name, or bare. When two imports export the same bare name, the one from the last of them in the import clause is meant, and a unit's own imports are not seen through it.

A declaration is private to its module unless it is written public, and only a module-level declaration can be public. The runtime's public names, Halt and ExitCode among them, are in scope in every module with no import.

Initialize and finalize

A module may have an initialize and a finalize section, each at most once, after its declarations. Each is a routine with no parameters that the compiler calls: every imported unit's initialize runs before its importer's, then the exe's body runs, and the finalize sections run in the reverse order when the process ends, through Halt or an exception nothing caught too.

↑ ↓ moveEnter openEsc close