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
//! The valid types of the piske programming language.

use sindra::Type;
use std::fmt;

/// The types available in the piske programming language. Implements the sindra `Type` trait.
#[derive(Copy, Debug, Clone, Hash, PartialEq)]
pub enum PType {
    /// Built-in string type
    String,
    /// Floating-point numbers
    Float,
    /// Integers (signed)
    Int,
    /// Boolean (true / false)
    Boolean,
    /// Complex
    Complex,
    /// Set (collection)
    Set,
    /// Empty type
    Void
}
impl Type for PType {
    fn name(&self) -> &str {
        match *self {
            PType::String => "string",
            PType::Float => "float",
            PType::Int => "int",
            PType::Boolean => "bool",
            PType::Complex => "complex",
            PType::Set => "set",
            PType::Void => "void",
        }
    }
}
impl<'a> From<&'a str> for PType {
    fn from(s: &'a str) -> PType {
        match s {
            "string" => PType::String,
            "float" => PType::Float,
            "int" => PType::Int,
            "bool" => PType::Boolean,
            "complex" => PType::Complex,
            "set" => PType::Set,
            _ => PType::Void,
        }
    }
}

impl fmt::Display for PType {
    fn fmt(&self, f: &mut fmt::Formatter) -> ::std::result::Result<(), fmt::Error> {
        write!(f, "{}", self.name())
    }
}