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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
    This file is part of Donet.

    Copyright © 2024 Max Rodriguez

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

    Donet 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 Affero General Public License for more details.

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

//! Represents all data types supported by the DC language
//! and developer-defined type alias definitions.

use crate::globals::DgSizeTag;
use crate::hashgen::DCHashGenerator;
use strum_macros::EnumIs;

/// The DCTypeEnum variants have assigned u8 values
/// to keep compatibility with Astron's DC hash inputs.
#[repr(u8)] // 8-bit alignment, unsigned
#[derive(Debug, Clone, PartialEq, Eq)]
#[rustfmt::skip]
pub enum DCTypeEnum {
    // Numeric Types
    TInt8 = 0, TInt16 = 1, TInt32 = 2, TInt64 = 3,
    TUInt8 = 4, TChar = 8, TUInt16 = 5, TUInt32 = 6, TUInt64 = 7,
    TFloat32 = 9, TFloat64 = 10,

    // Sized Data Types (Array Types)
    TString = 11, // a string with a fixed byte length
    TVarString = 12, // a string with a variable byte length
    TBlob = 13, TVarBlob = 14,
    TBlob32 = 19, TVarBlob32 = 20,
    TArray = 15, TVarArray = 16,

    // Complex DC Types
    TStruct = 17, TMethod = 18,
    TInvalid = 21,
}

impl std::fmt::Display for DCTypeEnum {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::TInt8 => write!(f, "int8"),
            Self::TInt16 => write!(f, "int16"),
            Self::TInt32 => write!(f, "int32"),
            Self::TInt64 => write!(f, "int64"),
            Self::TUInt8 => write!(f, "uint8"),
            Self::TChar => write!(f, "char"),
            Self::TUInt16 => write!(f, "uint16"),
            Self::TUInt32 => write!(f, "uint32"),
            Self::TUInt64 => write!(f, "uint64"),
            Self::TFloat32 => write!(f, "float32"),
            Self::TFloat64 => write!(f, "float64"),
            Self::TString => write!(f, "string"),
            Self::TVarString => write!(f, "var string"),
            Self::TBlob => write!(f, "blob"),
            Self::TVarBlob => write!(f, "var blob"),
            Self::TBlob32 => write!(f, "blob32"),
            Self::TVarBlob32 => write!(f, "var blob32"),
            Self::TArray => write!(f, "array"),
            Self::TVarArray => write!(f, "var array"),
            Self::TStruct => write!(f, "struct"),
            Self::TMethod => write!(f, "method"),
            Self::TInvalid => write!(f, "invalid"),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DCTypeDefinition {
    alias: Option<String>,
    pub data_type: DCTypeEnum,
    pub size: DgSizeTag,
}

impl Default for DCTypeDefinition {
    fn default() -> Self {
        Self {
            alias: None,
            data_type: DCTypeEnum::TInvalid,
            size: 0_u16,
        }
    }
}

impl DCTypeDefinition {
    /// Creates a new empty DCTypeDefinition struct.
    pub(crate) fn new() -> Self {
        Self::default()
    }

    /// Creates a new DCTypeDefinition struct with a DC type set.
    pub fn new_with_type(dt: DCTypeEnum) -> Self {
        Self {
            alias: None,
            data_type: dt,
            size: 0_u16,
        }
    }

    /// Accumulates the properties of this DC element into the file hash.
    pub fn generate_hash(&self, hashgen: &mut DCHashGenerator) {
        hashgen.add_int(i32::from(self.data_type.clone() as u8));

        if self.alias.is_some() {
            hashgen.add_string(self.alias.clone().unwrap())
        }
    }

    pub fn get_dc_type(&self) -> DCTypeEnum {
        self.data_type.clone()
    }

    #[inline(always)]
    pub fn is_variable_length(&self) -> bool {
        self.size == 0_u16
    }

    #[inline(always)]
    pub fn get_size(&self) -> DgSizeTag {
        self.size
    }

    #[inline(always)]
    pub fn has_alias(&self) -> bool {
        self.alias.is_some()
    }

    pub fn get_alias(&self) -> Result<String, ()> {
        if self.alias.is_some() {
            Ok(self.alias.clone().unwrap())
        } else {
            Err(())
        }
    }

    pub fn set_alias(&mut self, alias: String) {
        self.alias = Some(alias);
    }
}

impl std::fmt::Display for DCTypeDefinition {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "typedef ")?;
        self.data_type.fmt(f)?;
        if self.has_alias() {
            write!(f, " ")?;
            self.alias.clone().unwrap().fmt(f)?;
        }
        write!(f, ";")?;
        writeln!(f)
    }
}

// ---------- DC Number ---------- //

#[rustfmt::skip]
#[derive(Clone, PartialEq, EnumIs)]
pub enum DCNumberType {
    None = 0, Int, UInt, Float,
}

#[repr(C)]
#[derive(Copy, Clone)] // required for unwrapping when in an option type
pub union DCNumberValueUnion {
    pub integer: i64,
    pub unsigned_integer: u64,
    pub floating_point: f64,
}

#[derive(Clone)]
pub struct DCNumber {
    pub number_type: DCNumberType,
    pub value: DCNumberValueUnion,
}

// We have to manually implement the 'PartialEq' trait
// due to the usage of a union data type.
impl PartialEq for DCNumber {
    fn eq(&self, rhs: &Self) -> bool {
        self.number_type == rhs.number_type
    }
}

impl Default for DCNumber {
    fn default() -> Self {
        Self {
            number_type: DCNumberType::None,
            value: DCNumberValueUnion { integer: 0_i64 },
        }
    }
}

impl DCNumber {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn new_integer(num: i64) -> Self {
        Self {
            number_type: DCNumberType::Int,
            value: DCNumberValueUnion { integer: num },
        }
    }

    pub fn new_unsigned_integer(num: u64) -> Self {
        Self {
            number_type: DCNumberType::UInt,
            value: DCNumberValueUnion {
                unsigned_integer: num,
            },
        }
    }

    pub fn new_floating_point(num: f64) -> Self {
        Self {
            number_type: DCNumberType::Float,
            value: DCNumberValueUnion { floating_point: num },
        }
    }
}