diff --git a/compiler/cl-ast/src/ast.rs b/compiler/cl-ast/src/ast.rs index 1a8b271..4ddf897 100644 --- a/compiler/cl-ast/src/ast.rs +++ b/compiler/cl-ast/src/ast.rs @@ -284,7 +284,7 @@ pub struct TyFn { } /// A path to an [Item] in the [Module] tree -#[derive(Clone, Debug, PartialEq, Eq, Hash)] +#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)] pub struct Path { pub absolute: bool, pub parts: Vec, diff --git a/compiler/cl-ast/src/ast_impl.rs b/compiler/cl-ast/src/ast_impl.rs index 1c8b89e..1c47709 100644 --- a/compiler/cl-ast/src/ast_impl.rs +++ b/compiler/cl-ast/src/ast_impl.rs @@ -774,3 +774,34 @@ mod convert { } } } + +mod path { + //! Utils for [Path] + use crate::{ast::Path, Identifier, PathPart}; + + impl Path { + /// Appends a [PathPart] to this [Path] + pub fn push(&mut self, part: PathPart) { + self.parts.push(part); + } + /// Removes a [PathPart] from this [Path] + pub fn pop(&mut self) -> Option { + self.parts.pop() + } + /// Concatenates `self::other`. If `other` is an absolute [Path], + /// this replaces `self` with `other` + pub fn concat(&mut self, other: &Self) -> &mut Self { + if other.absolute { + *self = other.clone(); + } else { + self.parts.extend(other.parts.iter().cloned()) + } + self + } + } + impl PathPart { + pub fn from_ident(ident: Identifier) -> Self { + Self::Ident(ident) + } + } +}