Add primitives

This commit is contained in:
mars 2022-08-27 22:33:29 -06:00
parent 6cb4223942
commit be1c02a2ac
2 changed files with 67 additions and 0 deletions

View File

@ -1,6 +1,7 @@
mod ast;
mod cg;
mod parse;
mod types;
fn main() {
println!("Hello world! Please run cargo test.");

66
src/types.rs Normal file
View File

@ -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,
}
}
}