From 0ddd9bca2a3f8444cdfdd0a8afb292479396c4f2 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Sat, 8 Feb 2025 21:18:14 +1300 Subject: Support namespaces when resolving symbols A definition can resolve a reference in the same or a deeper namespace, allowing for proper scoping and shadowing. Multiple definitions in the same namespace cannot share a name. --- src/source_unit.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/source_unit.rs') diff --git a/src/source_unit.rs b/src/source_unit.rs index 7ee6993..bc6d4ab 100644 --- a/src/source_unit.rs +++ b/src/source_unit.rs @@ -115,6 +115,29 @@ pub struct Symbol { pub role: SymbolRole, } +impl Symbol { + /// True if this symbol is a valid definition for a reference symbol + pub fn defines(&self, reference: &Symbol) -> bool { + self.name == reference.name && + std::iter::zip(&self.namespace, &reference.namespace).all(|(a, b)| a == b) + } +} + +impl PartialEq for Symbol { + fn eq(&self, other: &Symbol) -> bool { + self.name == other.name && self.namespace == other.namespace + } +} + +impl std::fmt::Debug for Symbol { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + for name in &self.namespace { + write!(f, "{name}:")? + } + write!(f, "{}", self.name) + } +} + #[derive(Copy, Clone, Debug, PartialEq)] pub enum SymbolRole { Definition(DefinitionType), -- cgit v1.2.3-70-g09d2