std::path
std/src/path.mo
pub type Path {
path String
}A validated filesystem path -- a plain String wrapper, following
this codebase's existing newtype idiom (Identifier/Operator/
ModulePath, lang/types.mo). Construct via Path.of (validating);
the bare Path.path constructor is for a string the caller has
already established is non-empty (a compile-time literal, or a value
it checked -- see default_output_dir in cli/src/main.mo).
pub instance Copy Path {
def copy (p : Path) : Pair Path Path := Pair.pair p p
}A Path is a String wrapper, and String is Copy (an immutable
pointer), so sharing one is a pointer copy, not an ownership move.
pub type PathError {
empty,
}The validating smart constructor -- the ONLY sanctioned way to build
a Path from an arbitrary/external string (CLI argv, a hardcoded
literal, ...). Rejects the empty string: an empty path is never a
meaningful file target, and it's the concrete shape a defaulted or
missing CLI argument can otherwise silently slip through as. This is
deliberately narrow validation, not general path-syntax checking (no
NUL-byte/..-traversal/Windows-path handling) -- fully addresses
the bug this type was introduced to prevent (a naive ++-joined
output path silently doubling up when one half was already
absolute), without taking on open-ended "what is a valid path"
scope nothing in this corpus actually needs yet.
Why Path.of rejected a string. Structured rather than a message so a
caller can branch on it or render it; to_string keeps the old text.
pub def PathError.to_string (e : PathError) : Stringpub def Path.of (s : String) : Result PathError Pathpub def Path.unsafe (s : String) : PathThe boundary constructor for a string the caller has ALREADY
established is a path: a manifest-provided path, a compile-time
literal, or a value that passed Path.of. It skips only the non-empty
check; it is not a second validating constructor.
pub def Path.to_string (p : Path) : Stringpub def Path.is_absolute (p : Path) : Boolpub def raw_path_join (a : String) (b : String) : StringLow-level, unvalidated join -- the same empty-component/trailing-
slash-handling logic lang/module.mo's own path_join used to
duplicate; that function now delegates here instead. Stays total,
not routed through Path.of: the module resolver relies on an
empty a/b behaving as a no-op, and forcing that through the
validating constructor would change that existing, working
behavior -- out of scope for this type.
An absolute b replaces a entirely (os.path.join-style
semantics). This is THE fix for the bug the Path type was
introduced to prevent -- the case that produced a mangled
/tmp//tmp/monad_v2.ll when cli/src/main.mo's own link_ir
joined a hardcoded /tmp output directory with an already-absolute
user-supplied output name via plain ++. It used to live in
Path.join alone, which left every DIRECT caller of this function
with the bug: Mote.dep_dir_entries_go joins a manifest
[dependencies.X] path onto the mote's own directory, and a bare
monad check sets that directory to ".", so an absolute path dep
came out as ".//home/.../init" and the dependency silently failed
to resolve. Fixing it here rather than at that one call site is
deliberate -- a rule kept in one place is inherited by callers that
do not exist yet, which is what Path.join's old comment was
asking for.
pub def Path.join (a : Path) (b : Path) : PathDelegates to raw_path_join, which owns the absolute-b rule for
both spellings. Operates on already-validated Path values, so the
non-empty invariant holds without re-validating (an absolute b is
returned as-is and is non-empty by validation; concatenating two
non-empty strings can't be empty either).
pub def Path.with_suffix (p : Path) (suffix : String) : PathAppend a literal suffix (extension, filename tail) -- no separator
inserted. Targets output_dir ++ "/" ++ name ++ ".ll"-style code.
pub def raw_parent_dir (s : String) : StringThe directory part of a path, or "" when there is none ("hello.mo"
has no directory, and "" means "the current one" to every caller --
notably mkdir -p, which must not be run on it).
Total and unvalidated for the same reason raw_path_join is: callers
rely on the empty result, which the validating constructor rejects.
lang/src/module.mo's extract_directory delegates here rather than
keeping its own copy, exactly as path_join delegates to
raw_path_join.
pub def Path.parent (p : Path) : Option PathThe directory part of p, or none when it has none ("hello.mo",
"/monad"): an empty Path is never constructed.
pub def Path.file_name (p : Path) : StringThe last component of p -- the whole of it when it has no /.
pub def Path.normalize (p : Path) : PathCollapse ./.. segments and repeated separators: a/../b -> b,
./x -> x, a//b -> a/b. Moved here from lang/module.mo so the
loader, the CLI and build share one implementation; pure string work,
so it never changes which file resolves, only how the winner is spelled.
An empty result becomes "." (the current directory).
pub def Path.extension (p : Path) : Option StringThe extension after the last . in the file name, without the dot.
none when there is no dot or it is the file name's first byte (a
dotfile such as .gitignore has no extension).
pub def Path.file_stem (p : Path) : StringThe file name without its extension (/a/b.mo -> b); the whole
file name when it has no extension.
pub def Path.child (p : Path) (segment : String) : PathJoin a relative segment onto a path, with Path.join's absolute-
segment rule but without requiring the segment to be a Path
(segments are literals, never validated targets).
pub def Path.directory (p : Path) : PathThe directory that contains p, or . when it has none (main.mo,
/monad): an empty Path is never constructed, and . is how every
consumer spells "here".
pub def Path.unsafe_dir (s : String) : PathA directory spelling from text, where the empty string means the
working directory and is spelled .: an empty Path is never
constructed, and . is how every consumer spells "here".
pub def Path.child_under (root : Path) (segment : String) : PathJoin a segment under a DIRECTORY root, where . is the working
directory's spelling: Path.child_under (Path.unsafe_dir "") "src"
is src, not ./src. That is the empty-first-component rule
raw_path_join already applies, kept here so a root carried as a
Path renders the same spellings it did as a string. Any other root
joins normally, and an absolute segment still replaces it.
An empty SEGMENT under a . root yields the empty spelling, exactly as
raw_path_join "" "" does -- a caller for which that is "not a path"
filters it (lang::mote's Dependency.located_dir).
pub def path_segments_go (s : String) (i : I64) (start : I64) (acc : List String) : List Strings split on /, with empty segments dropped so a leading / and any
// contribute nothing. Path.normalize restores the leading slash
from the original string, which is the only thing it can carry.
pub def path_push_segment (seg : String) (acc : List String) : List StringAccumulate one segment, skipping an empty one.
pub def normalize_segments_go (segs : List String) (acc : List String) : List Stringpath_segments_go's output with . dropped and each .. cancelling
the segment before it. A .. with nothing to cancel is KEPT, and two
leading ..s keep both -- otherwise ../init/src would normalize to
init/src and stop naming the file it found.
pub def join_path_segments (segs : List String) (acc : String) : StringRe-join segments with a single / between them and none at either end.
pub def Path.beq (a b : Path) : Boolpub instance BEq Path {
def beq (a b : Path) : Bool := Path.beq a b
}pub instance Hashable Path {
def hash (p : Path) : U64 := String.hash (Path.to_string p)
}Canonical-string hash/order, so a Path can key a map (the loader's
file table) without every caller rendering it first.
pub instance BOrd Path {
def lt (a b : Path) : Bool := BOrd.lt (Path.to_string a) (Path.to_string b)
def gt (a b : Path) : Bool := BOrd.gt (Path.to_string a) (Path.to_string b)
def lte (a b : Path) : Bool := Bool.not (BOrd.gt (Path.to_string a) (Path.to_string b))
def gte (a b : Path) : Bool := Bool.not (BOrd.lt (Path.to_string a) (Path.to_string b))
}