Peeper

Modules

Files as modules, imports, and exported names.

Each file of a project is a module, and the src folder holds them.

  • import "package:path" brings a module in. path is the file's path inside src, without .peep.
  • The module's names are then used as module::Name.
  • A name that starts with an uppercase letter is exported. A lowercase name stays inside its module.
  • The core library is the package core.
examples/modules/src/geometry.peep
// Exported: the names start with an uppercase letter.
const Side = 21;

fn Area(width: i64, height: u8) -> i64 {
    return scale(width * height);
}

// Lowercase: only this module can use it.
fn scale(value: i64) -> i64 {
    return value * 2;
}
examples/modules/src/shapes/square.peep
// A module in a folder: `import "modules:shapes/square"`.
fn Of(side: i64) -> i64 {
    return side * side;
}
examples/modules/src/main.peep
// Modules: each file of a project is a module, and the folder `src` holds
// them. `import "package:path"` brings one in, where `path` is the file's
// path in `src` without `.peep`; its names are used as `module::Name`. A
// name starting with an uppercase letter is exported, a lowercase one stays
// in its module. The core library is the package `core`.
// Build: peeper build examples/modules      (writes ./modules)
// Run:   peeper run examples/modules
import "modules:geometry";
import "modules:shapes/square";
import "core:allocator";

// A constant may use one declared later, in another module.
const Perimeter = geometry::Side * 4;

fn main() {
    println("perimeter:", Perimeter);
    println("area:", geometry::Area(geometry::Side, 3u8));
    println("square:", square::Of(geometry::Side));

    let memory = allocator::Malloc(16);
    allocator::Free(memory);
    println("allocated and freed 16 bytes");
}
Output
perimeter: 84
area: 126
square: 441
allocated and freed 16 bytes

Using a lowercase name from another module is an error:

peeper check
[M0004]: `scale` is not exported from `geometry`  --> src/main.peep:4:23  | 3 | fn main() {4 |     println(geometry::scale(2));  |                       ^^^^^  |   = note: names that start with an upper case letter are exported; others are privateCompilation failed with 1 error(s)