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
// Copyright (C) 2019-2020 Aleo Systems Inc.
// This file is part of the Leo library.

// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

use leo_grammar::types::{
    IntegerType as GrammarIntegerType,
    SignedIntegerType as GrammarSignedIntegerType,
    UnsignedIntegerType as GrammarUnsignedIntegerType,
};
use leo_input::types::{
    IntegerType as InputIntegerType,
    SignedIntegerType as InputSignedIntegerType,
    UnsignedIntegerType as InputUnsignedIntegerType,
};

use serde::{Deserialize, Serialize};
use std::fmt;

/// Explicit integer type
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum IntegerType {
    U8,
    U16,
    U32,
    U64,
    U128,

    I8,
    I16,
    I32,
    I64,
    I128,
}

impl From<GrammarIntegerType> for IntegerType {
    fn from(integer_type: GrammarIntegerType) -> Self {
        match integer_type {
            GrammarIntegerType::Signed(signed) => Self::from(signed),
            GrammarIntegerType::Unsigned(unsigned) => Self::from(unsigned),
        }
    }
}

impl From<GrammarUnsignedIntegerType> for IntegerType {
    fn from(integer_type: GrammarUnsignedIntegerType) -> Self {
        match integer_type {
            GrammarUnsignedIntegerType::U8Type(_type) => IntegerType::U8,
            GrammarUnsignedIntegerType::U16Type(_type) => IntegerType::U16,
            GrammarUnsignedIntegerType::U32Type(_type) => IntegerType::U32,
            GrammarUnsignedIntegerType::U64Type(_type) => IntegerType::U64,
            GrammarUnsignedIntegerType::U128Type(_type) => IntegerType::U128,
        }
    }
}

impl From<GrammarSignedIntegerType> for IntegerType {
    fn from(integer_type: GrammarSignedIntegerType) -> Self {
        match integer_type {
            GrammarSignedIntegerType::I8Type(_type) => IntegerType::I8,
            GrammarSignedIntegerType::I16Type(_type) => IntegerType::I16,
            GrammarSignedIntegerType::I32Type(_type) => IntegerType::I32,
            GrammarSignedIntegerType::I64Type(_type) => IntegerType::I64,
            GrammarSignedIntegerType::I128Type(_type) => IntegerType::I128,
        }
    }
}

impl From<InputIntegerType> for IntegerType {
    fn from(integer_type: InputIntegerType) -> Self {
        match integer_type {
            InputIntegerType::Signed(signed) => Self::from(signed),
            InputIntegerType::Unsigned(unsigned) => Self::from(unsigned),
        }
    }
}

impl From<InputUnsignedIntegerType> for IntegerType {
    fn from(integer_type: InputUnsignedIntegerType) -> Self {
        match integer_type {
            InputUnsignedIntegerType::U8Type(_type) => IntegerType::U8,
            InputUnsignedIntegerType::U16Type(_type) => IntegerType::U16,
            InputUnsignedIntegerType::U32Type(_type) => IntegerType::U32,
            InputUnsignedIntegerType::U64Type(_type) => IntegerType::U64,
            InputUnsignedIntegerType::U128Type(_type) => IntegerType::U128,
        }
    }
}

impl From<InputSignedIntegerType> for IntegerType {
    fn from(integer_type: InputSignedIntegerType) -> Self {
        match integer_type {
            InputSignedIntegerType::I8Type(_type) => IntegerType::I8,
            InputSignedIntegerType::I16Type(_type) => IntegerType::I16,
            InputSignedIntegerType::I32Type(_type) => IntegerType::I32,
            InputSignedIntegerType::I64Type(_type) => IntegerType::I64,
            InputSignedIntegerType::I128Type(_type) => IntegerType::I128,
        }
    }
}

impl fmt::Display for IntegerType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            IntegerType::U8 => write!(f, "u8"),
            IntegerType::U16 => write!(f, "u16"),
            IntegerType::U32 => write!(f, "u32"),
            IntegerType::U64 => write!(f, "u64"),
            IntegerType::U128 => write!(f, "u128"),

            IntegerType::I8 => write!(f, "i8"),
            IntegerType::I16 => write!(f, "i16"),
            IntegerType::I32 => write!(f, "i32"),
            IntegerType::I64 => write!(f, "i64"),
            IntegerType::I128 => write!(f, "i128"),
        }
    }
}