lib/std/lang/package.rad 1.2 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
11
/// A compilation unit.
12
pub record Package {
13
    /// Package identifier (index in the package array).
14
    id: u16,
15
    /// Package name.
16
    name: *[u8],
17
    /// Root module identifier for this package, or `nil` if not yet registered.
18
    // TODO: This shouldn't be optional.
19
    rootModuleId: ?u16,
20
}
21
22
/// Initialize `pkg` with the provided name and ID.
23
pub fn init(
24
    pkg: *mut Package,
25
    id: u16,
26
    name: *[u8],
27
    pool: *mut strings::Pool
28
) -> *mut Package {
29
    pkg.id = id;
30
    pkg.name = strings::intern(pool, name);
31
    pkg.rootModuleId = nil;
32
33
    return pkg;
34
}
35
36
/// Register a module described by the file path.
37
pub fn registerModule(pkg: *mut Package, graph: *mut module::ModuleGraph, filePath: *[u8]) -> u16
38
    throws (module::ModuleError)
39
{
40
    let modId = try module::registerFromPath(graph, pkg.id, pkg.rootModuleId, filePath);
41
    // First registered module becomes the root.
42
    if pkg.rootModuleId == nil {
43
        pkg.rootModuleId = modId;
44
    }
45
    return modId;
46
}