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
175
176
177
178
179
180
181
182
183
use std::iter;
use std::iter::FromIterator;
use std::borrow::Borrow;
use std::fmt;

use bad_idea::BrokenF32;
use std::ops::Deref;
pub use print::*;

/// An all-in-one package for building `ATerms`. Maybe be pure, or have internal mutability to give
/// you maximally shared `ATerms`.
pub trait ATermFactory<'a, B: 'a> {
    type Rec: Rec + FromIterator<Self::ATermRef>;
    type ATerm: Deref<Target = _ATerm<Self::Rec, B>>;
    type ATermRef: Borrow<Self::ATerm>;

    fn int(&'a self, value: i32) -> Self::ATermRef {
        self.no_annos(self.t_int(value))
    }
    /// The string variant in ATerms is represented as an application with zero children!
    fn string(&'a self, value: String) -> Self::ATermRef {
        self.application(format!("{:?}", value), iter::empty())
    }
    fn real(&'a self, value: f32) -> Self::ATermRef {
        self.no_annos(self.t_real(value))
    }
    fn application<I>(&'a self, constructor: String, children: I) -> Self::ATermRef
        where I: IntoIterator<Item = Self::ATermRef>
    {
        self.no_annos(self.t_application(constructor, children))
    }
    fn list<I>(&'a self, value: I) -> Self::ATermRef
        where I: IntoIterator<Item = Self::ATermRef>
    {
        self.no_annos(self.t_list(value))
    }
    fn placeholder(&'a self, value: TermPlaceholder<Self::Rec>) -> Self::ATermRef {
        self.no_annos(self.t_placeholder(value))
    }
    fn blob(&'a self, value: B) -> Self::ATermRef {
        self.no_annos(self.t_blob(value))
    }
    fn long(&'a self, value: i64) -> Self::ATermRef {
        self.no_annos(self.t_long(value))
    }

    fn t_int(&'a self, value: i32) -> Term<Self::Rec, B> {
        Term::Int(value)
    }
    /// The string variant in ATerms is represented as an application with zero children!
    fn t_string(&'a self, value: String) -> Term<Self::Rec, B> {
        self.t_application(format!("{:?}", value), iter::empty())
    }
    fn t_real(&'a self, value: f32) -> Term<Self::Rec, B> {
        Term::Real(BrokenF32(value))
    }
    fn t_application<I>(&'a self, constructor: String, children: I) -> Term<Self::Rec, B>
        where I: IntoIterator<Item = Self::ATermRef>
    {
        Term::Application(constructor, Self::Rec::from_iter(children))
    }
    fn t_list<I>(&'a self, value: I) -> Term<Self::Rec, B>
        where I: IntoIterator<Item = Self::ATermRef>
    {
        Term::List(Self::Rec::from_iter(value))
    }
    fn t_placeholder(&'a self, value: TermPlaceholder<Self::Rec>) -> Term<Self::Rec, B> {
        Term::Placeholder(value)
    }
    fn t_blob(&'a self, value: B) -> Term<Self::Rec, B> {
        Term::Blob(value)
    }
    fn t_long(&'a self, value: i64) -> Term<Self::Rec, B> {
        Term::Long(value)
    }

    fn no_annos(&'a self, term: Term<Self::Rec, B>) -> Self::ATermRef;

    fn with_annos<A>(&'a self, term: Term<Self::Rec, B>, annos: A) -> Self::ATermRef
        where A: IntoIterator<Item = Self::ATermRef>;
}

pub trait SharedATermFactory<'a, B: 'a>: ATermFactory<'a, B> {
    fn get_shared(&'a self, value: Self::ATermRef) -> Self::ATermRef
        where Self::Rec: Clone,
              B: Clone;
}

/// The basic term type, without annotations
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
pub enum Term<Rec, Blob> {
    /// An 32bit signed integer
    Int(i32),
    /// A 64bit signed integer. This is an optional component in the spec, but it's easy to support.
    Long(i64),
    /// An 32bit floating point number, but with a broken equality so a coherent implementation of
    /// Hash could be given (if NaN != NaN, then what Hash should it get?). You shouldn't directly
    /// check equality of floating point numbers anyways because of rounding.
    Real(BrokenF32),
    /// The main thing: Application of a constructor, using a string for the constructor name.
    Application(String, Rec),
    /// A list of ATerms.
    List(Rec),
    /// A placeholder for pattern matching. Only here for compatibility with the spec, use Rust's
    /// pattern matching facilities instead!
    Placeholder(TermPlaceholder<Rec>),
    /// A Binary Large OBject. Here used to embed any other type. But note it will need to implement
    /// some traits if you still want to parse or print it.
    Blob(Blob),
}

impl<Rec, B> fmt::Display for Term<Rec, B>
    where Rec: self::Rec + ATermWrite,
          B: ATermWrite
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.to_ascii(f)
    }
}

/// These placeholders match the constructors of Term. The Application has sub-placeholders for the
/// children of the constructor. The Term placeholder is basically a wildcard, it matches anything.
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
pub enum TermPlaceholder<Rec> {
    Int,
    String,
    Real,
    Term,
    Application(Rec),
    List,
    Placeholder,
    Blob,
    Long,
}

impl<Rec> fmt::Display for TermPlaceholder<Rec>
    where Rec: self::Rec + ATermWrite
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.to_ascii(f)
    }
}

/// The annotated term. This only combines the term with the annotations, nothing special.
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
pub struct _ATerm<Rec, B> {
    /// The actual term.
    pub term: Term<Rec, B>,
    /// The annotations on the term. The spec says this is an ATerm list of pairs of ATerms. We
    /// extend this to a general list of aterms so we can support more systems (e.g. Stratego/XT).
    pub annotations: Rec,
}

impl<Rec, B> _ATerm<Rec, B>
    where Rec: Default
{
    pub fn no_annos(term: Term<Rec, B>) -> Self {
        _ATerm {
            term: term,
            annotations: Rec::default(),
        }
    }

    pub fn with_annos(term: Term<Rec, B>, annos: Rec) -> Self {
        _ATerm {
            term: term,
            annotations: annos,
        }
    }
}

impl<Rec, B> fmt::Display for _ATerm<Rec, B>
    where Rec: self::Rec + ATermWrite,
          B: ATermWrite
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.to_ascii(f)
    }
}

pub trait Rec: Default {
    fn is_empty(&self) -> bool;
}