1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use std::vec;

use arret_syntax::datum::{DataStr, Datum};
use arret_syntax::span::Span;

use crate::hir::scope::Scope;

new_counting_id_type!(NsIdCounter, NsId);

#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct Ident {
    ns_id: NsId,
    data_name: DataStr,
}

impl Ident {
    pub fn new(ns_id: NsId, data_name: DataStr) -> Ident {
        Ident { ns_id, data_name }
    }

    pub fn ns_id(&self) -> NsId {
        self.ns_id
    }

    pub fn name(&self) -> &DataStr {
        &self.data_name
    }

    pub fn into_name(self) -> DataStr {
        self.data_name
    }

    pub fn is_underscore(&self) -> bool {
        self.data_name.as_ref() == "_"
    }

    pub fn is_ellipsis(&self) -> bool {
        self.data_name.as_ref() == "..."
    }

    pub fn is_ampersand(&self) -> bool {
        self.data_name.as_ref() == "&"
    }

    pub fn with_ns_id(&self, new_ns_id: NsId) -> Ident {
        Ident {
            ns_id: new_ns_id,
            data_name: self.data_name.clone(),
        }
    }
}

#[derive(Clone, PartialEq, Debug)]
pub enum NsDatum {
    Bool(Span, bool),
    Char(Span, char),
    Int(Span, i64),
    Float(Span, f64),
    List(Span, Box<[NsDatum]>),
    Str(Span, DataStr),
    Keyword(Span, DataStr),
    Ident(Span, Ident),
    Vector(Span, Box<[NsDatum]>),
    Map(Span, Box<[(NsDatum, NsDatum)]>),
    Set(Span, Box<[NsDatum]>),
}

impl NsDatum {
    fn map_syntax_data(vs: &[Datum]) -> Box<[NsDatum]> {
        vs.iter().map(Self::from_syntax_datum).collect()
    }

    pub fn from_syntax_datum(value: &Datum) -> NsDatum {
        match value {
            Datum::Bool(span, v) => NsDatum::Bool(*span, *v),
            Datum::Char(span, v) => NsDatum::Char(*span, *v),
            Datum::Int(span, v) => NsDatum::Int(*span, *v),
            Datum::Float(span, v) => NsDatum::Float(*span, *v),
            Datum::Str(span, v) => NsDatum::Str(*span, v.clone()),
            Datum::Sym(span, v) => {
                if v.starts_with(':') {
                    NsDatum::Keyword(*span, v.clone())
                } else {
                    NsDatum::Ident(*span, Ident::new(Scope::root_ns_id(), v.clone()))
                }
            }
            Datum::List(span, vs) => NsDatum::List(*span, Self::map_syntax_data(vs)),
            Datum::Vector(span, vs) => NsDatum::Vector(*span, Self::map_syntax_data(vs)),
            Datum::Set(span, vs) => NsDatum::Set(*span, Self::map_syntax_data(vs)),
            Datum::Map(span, vs) => NsDatum::Map(
                *span,
                vs.iter()
                    .map(|(k, v)| (NsDatum::from_syntax_datum(k), NsDatum::from_syntax_datum(v)))
                    .collect(),
            ),
        }
    }

    fn map_nsdata(vs: Box<[NsDatum]>) -> Box<[Datum]> {
        vs.into_vec()
            .into_iter()
            .map(NsDatum::into_syntax_datum)
            .collect()
    }

    pub fn into_syntax_datum(self) -> Datum {
        match self {
            NsDatum::Bool(span, v) => Datum::Bool(span, v),
            NsDatum::Char(span, v) => Datum::Char(span, v),
            NsDatum::Int(span, v) => Datum::Int(span, v),
            NsDatum::Float(span, v) => Datum::Float(span, v),
            NsDatum::Str(span, v) => Datum::Str(span, v),
            NsDatum::Keyword(span, v) => Datum::Sym(span, v),
            NsDatum::Ident(span, v) => Datum::Sym(span, v.into_name()),
            NsDatum::List(span, vs) => Datum::List(span, Self::map_nsdata(vs)),
            NsDatum::Vector(span, vs) => Datum::Vector(span, Self::map_nsdata(vs)),
            NsDatum::Set(span, vs) => Datum::Set(span, Self::map_nsdata(vs)),
            NsDatum::Map(span, vs) => Datum::Map(
                span,
                vs.into_vec()
                    .into_iter()
                    .map(|(k, v)| (k.into_syntax_datum(), v.into_syntax_datum()))
                    .collect(),
            ),
        }
    }

    pub fn span(&self) -> Span {
        match self {
            NsDatum::Bool(span, _)
            | NsDatum::Char(span, _)
            | NsDatum::Int(span, _)
            | NsDatum::Float(span, _)
            | NsDatum::Str(span, _)
            | NsDatum::Keyword(span, _)
            | NsDatum::Ident(span, _)
            | NsDatum::List(span, _)
            | NsDatum::Vector(span, _)
            | NsDatum::Set(span, _)
            | NsDatum::Map(span, _) => *span,
        }
    }

    pub fn description(&self) -> &'static str {
        match self {
            NsDatum::Bool(_, true) => "boolean true",
            NsDatum::Bool(_, false) => "boolean false",
            NsDatum::Char(_, _) => "character",
            NsDatum::Int(_, _) => "integer",
            NsDatum::Float(_, _) => "floating point number",
            NsDatum::Str(_, _) => "string",
            NsDatum::Keyword(_, _) => "keyword",
            NsDatum::Ident(_, _) => "symbol",

            NsDatum::List(_, vs) if vs.is_empty() => "empty list",
            NsDatum::List(_, _) => "list",

            NsDatum::Vector(_, vs) if vs.is_empty() => "empty vector",
            NsDatum::Vector(_, _) => "vector",

            NsDatum::Set(_, vs) if vs.is_empty() => "empty set",
            NsDatum::Set(_, _) => "set",

            NsDatum::Map(_, vs) if vs.is_empty() => "empty map",
            NsDatum::Map(_, _) => "map",
        }
    }
}

/// Iterator for NsDatum used inside the HIR
///
/// This is a specific type as we use .as_slice() to peek at data in certain places for
/// context-sensitive parsing.
pub type NsDataIter = vec::IntoIter<NsDatum>;