lib/std/lang/package.rad 1.4 KiB raw
1
//! A *package* is a compilation unit tracked by the Radiance compiler.
2
3
use std::mem;
4
use std::lang::ast;
5
use std::lang::module;
6
use std::lang::strings;
7
8
/// Maximum number of packages processed in a single invocation.
9
pub const MAX_PACKAGES: u32 = 4;
10
/// Maximum byte length for a package name.
11
pub const MAX_PACKAGE_NAME_LEN: u32 = 64;
12
13
/// Errors produced while configuring packages.
14
pub union PackageError {
15
    /// The provided name exceeds the buffer.
16
    NameTooLong,
17
}
18
19
/// A compilation unit.
20
pub record Package {
21
    /// Package identifier (index in the package array).
22
    id: u16,
23
    /// Package name.
24
    name: *[u8],
25
    /// Root module identifier for this package, or `nil` if not yet registered.
26
    // TODO: This shouldn't be optional.
27
    rootModuleId: ?u16,
28
}
29
30
/// Initialize `pkg` with the provided name and ID.
31
pub fn init(
32
    pkg: *mut Package,
33
    id: u16,
34
    name: *[u8],
35
    pool: *mut strings::Pool
36
) -> *mut Package {
37
    pkg.id = id;
38
    pkg.name = strings::intern(pool, name);
39
    pkg.rootModuleId = nil;
40
41
    return pkg;
42
}
43
44
/// Register a module described by the file path.
45
pub fn registerModule(pkg: *mut Package, graph: *mut module::ModuleGraph, filePath: *[u8]) -> u16
46
    throws (module::ModuleError)
47
{
48
    let modId = try module::registerFromPath(graph, pkg.id, pkg.rootModuleId, filePath);
49
    // First registered module becomes the root.
50
    if pkg.rootModuleId == nil {
51
        pkg.rootModuleId = modId;
52
    }
53
    return modId;
54
}