Add primitives
This commit is contained in:
parent
6cb4223942
commit
be1c02a2ac
|
@ -1,6 +1,7 @@
|
|||
mod ast;
|
||||
mod cg;
|
||||
mod parse;
|
||||
mod types;
|
||||
|
||||
fn main() {
|
||||
println!("Hello world! Please run cargo test.");
|
||||
|
|
|
@ -0,0 +1,66 @@
|
|||
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
|
||||
pub enum PrimitiveKind {
|
||||
U8,
|
||||
U16,
|
||||
U32,
|
||||
U64,
|
||||
I8,
|
||||
I16,
|
||||
I32,
|
||||
I64,
|
||||
F32,
|
||||
F64,
|
||||
}
|
||||
|
||||
impl PrimitiveKind {
|
||||
pub fn size(&self) -> u8 {
|
||||
use PrimitiveKind::*;
|
||||
match self {
|
||||
U8 | I8 => 1,
|
||||
U16 | I16 => 2,
|
||||
U32 | I32 | F32 => 4,
|
||||
U64 | I64 | F64 => 8,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_signed(&self) -> bool {
|
||||
use PrimitiveKind::*;
|
||||
match self {
|
||||
U8 | U16 | U32 | U64 => false,
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum PrimitiveValue {
|
||||
U8(u8),
|
||||
U16(u16),
|
||||
U32(u32),
|
||||
U64(u64),
|
||||
I8(i8),
|
||||
I16(i16),
|
||||
I32(i32),
|
||||
I64(i64),
|
||||
F32(f32),
|
||||
F64(f64),
|
||||
}
|
||||
|
||||
impl PrimitiveValue {
|
||||
pub fn kind(&self) -> PrimitiveKind {
|
||||
use PrimitiveKind as Kind;
|
||||
use PrimitiveValue::*;
|
||||
match self {
|
||||
U8(_) => Kind::U8,
|
||||
U16(_) => Kind::U16,
|
||||
U32(_) => Kind::U32,
|
||||
U64(_) => Kind::U64,
|
||||
I8(_) => Kind::I8,
|
||||
I16(_) => Kind::I16,
|
||||
I32(_) => Kind::I32,
|
||||
I64(_) => Kind::I64,
|
||||
F32(_) => Kind::F32,
|
||||
F64(_) => Kind::F64,
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue