tin_lang/ir/component/
symbol.rs

1use std::fmt;
2
3use specs::Component;
4use specs::VecStorage;
5
6#[derive(Component, Clone, Debug, VisitEntities)]
7#[storage(VecStorage)]
8pub struct Symbol {
9    parts: Vec<Part>,
10}
11
12#[derive(Clone, Debug, VisitEntities)]
13pub enum Part {
14    Named(String),
15    #[allow(unused)]
16    Unnamed(u64),
17}
18
19impl Symbol {
20    pub fn new(parts: Vec<Part>) -> Self {
21        Symbol { parts }
22    }
23
24    pub fn is_empty(&self) -> bool {
25        self.parts.is_empty()
26    }
27}
28
29impl fmt::Display for Symbol {
30    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31        let mut needs_sep = false;
32
33        for part in &self.parts {
34            if needs_sep {
35                f.write_str(".")?;
36            }
37            part.fmt(f)?;
38            needs_sep = true;
39        }
40
41        Ok(())
42    }
43}
44
45impl fmt::Display for Part {
46    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
47        match *self {
48            Part::Named(ref name) => name.fmt(f),
49            Part::Unnamed(ref id) => id.fmt(f),
50        }
51    }
52}