Skip to content

Commit

Permalink
dcparser: Apply explicit cast when building DCNumericType struct
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrdz committed Mar 6, 2024
1 parent 0fe59bc commit 158b08b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
19 changes: 19 additions & 0 deletions libdonet/src/dcnumeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ pub struct DCNumericType {
// These are the range and modulus values after scaling by the divisor.
modulus: DCNumber,
range: DCNumericRange,
// Specific to Donet's DC language
explicit_cast: Option<DCTypeDefinition>,
}

pub trait DCNumericTypeInterface {
Expand All @@ -122,10 +124,12 @@ pub trait DCNumericTypeInterface {
fn get_divisor(&self) -> u16;
fn get_modulus(&self) -> f64;
fn get_range(&self) -> DCNumericRange;
fn get_explicit_cast(&self) -> Option<DCTypeDefinition>;

fn set_divisor(&mut self, divisor: u16) -> Result<(), String>;
fn set_modulus(&mut self, modulus: f64) -> Result<(), String>;
fn set_range(&mut self, range: DCNumericRange) -> Result<(), String>;
fn set_explicit_cast(&mut self, dtype: DCTypeDefinition) -> Result<(), String>;

fn within_range(&self, data: Vec<u8>, length: u64) -> Result<(), String>;
}
Expand Down Expand Up @@ -200,6 +204,7 @@ impl DCNumericTypeInterface for DCNumericType {
orig_range: DCNumericRange::new(),
modulus: DCNumber::new(),
range: DCNumericRange::new(),
explicit_cast: None,
}
}

Expand All @@ -221,21 +226,30 @@ impl DCNumericTypeInterface for DCNumericType {
}
}

#[inline]
fn has_modulus(&self) -> bool {
self.orig_modulus != 0.0
}
#[inline]
fn has_range(&self) -> bool {
self.orig_range.is_empty()
}
#[inline]
fn get_divisor(&self) -> u16 {
self.divisor
}
#[inline]
fn get_modulus(&self) -> f64 {
self.orig_modulus
}
#[inline]
fn get_range(&self) -> DCNumericRange {
self.orig_range.clone()
}
#[inline]
fn get_explicit_cast(&self) -> Option<DCTypeDefinition> {
self.explicit_cast.clone()
}

fn set_divisor(&mut self, divisor: u16) -> Result<(), String> {
if divisor == 0 {
Expand Down Expand Up @@ -265,6 +279,11 @@ impl DCNumericTypeInterface for DCNumericType {
Ok(())
}

fn set_explicit_cast(&mut self, dtype: DCTypeDefinition) -> Result<(), String> {
self.explicit_cast = Some(dtype);
Ok(()) // TODO: do some sort of type check
}

fn within_range(&self, data: Vec<u8>, length: u64) -> Result<(), String> {
todo!();
}
Expand Down
18 changes: 15 additions & 3 deletions libdonet/src/dcparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,9 +668,21 @@ parser! {
// this is used for types that have arithmetic operations applied, such as division.
//
// Also because it is 2:27 AM and its giving me a shift-reduce conflict again.
numeric_type_token[nt] OpenParenthesis signed_integer_type[_] CloseParenthesis => nt,
numeric_type_token[nt] OpenParenthesis unsigned_integer_type[_] CloseParenthesis => nt,
numeric_type_token[nt] OpenParenthesis floating_point_type[_] CloseParenthesis => nt,
numeric_type_token[mut nt]
OpenParenthesis signed_integer_type[(_, dct)] CloseParenthesis => {
let _ = nt.set_explicit_cast(DCTypeDefinition::new_with_type(dct));
nt
},
numeric_type_token[mut nt]
OpenParenthesis unsigned_integer_type[(_, dct)] CloseParenthesis => {
let _ = nt.set_explicit_cast(DCTypeDefinition::new_with_type(dct));
nt
},
numeric_type_token[mut nt]
OpenParenthesis floating_point_type[(_, dct)] CloseParenthesis => {
let _ = nt.set_explicit_cast(DCTypeDefinition::new_with_type(dct));
nt
},
}

numeric_range: Option<DCNumericRange> {
Expand Down
15 changes: 13 additions & 2 deletions libdonet/src/dctype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ 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)]
#[derive(Debug, Clone, PartialEq, Eq)]
#[rustfmt::skip]
pub enum DCTypeEnum {
// Numeric Types
Expand All @@ -45,7 +45,7 @@ pub enum DCTypeEnum {
TInvalid = 21,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DCTypeDefinition {
alias: Option<String>,
pub data_type: DCTypeEnum,
Expand All @@ -54,6 +54,7 @@ pub struct DCTypeDefinition {

pub trait DCTypeDefinitionInterface {
fn new() -> Self;
fn new_with_type(dt: DCTypeEnum) -> Self;
fn generate_hash(&self, hashgen: &mut DCHashGenerator);

fn get_dc_type(&self) -> DCTypeEnum;
Expand All @@ -66,6 +67,7 @@ pub trait DCTypeDefinitionInterface {
}

impl DCTypeDefinitionInterface for DCTypeDefinition {
/// Creates a new empty DCTypeDefinition struct.
fn new() -> Self {
Self {
alias: None,
Expand All @@ -74,6 +76,15 @@ impl DCTypeDefinitionInterface for DCTypeDefinition {
}
}

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

/// Generates the hash for this DC Type element.
fn generate_hash(&self, hashgen: &mut DCHashGenerator) {
hashgen.add_int(i32::from(self.data_type.clone() as u8));
Expand Down

0 comments on commit 158b08b

Please sign in to comment.