Use flags for DT

next
Sayan Nandan 2 years ago
parent 5d527408cd
commit b918af6d96
No known key found for this signature in database
GPG Key ID: 8BC07A0A4D41DD52

@ -41,11 +41,47 @@ macro_rules! multi_assert_eq {
}
macro_rules! enum_impls {
($for:ident<$lt:lifetime> => {$($other:ty as $me:ident),*$(,)?}) => {
$(impl<$lt> ::core::convert::From<$other> for $for<$lt> {fn from(v: $other) -> Self {Self::$me(v.into())}})*
};
($for:ty => {$($other:ty as $me:ident),*$(,)?}) => {
$(impl ::core::convert::From<$other> for $for {fn from(v: $other) -> Self {Self::$me(v.into())}})*
}
};
}
macro_rules! assertions {
($($assert:expr),*$(,)?) => {$(const _:()=::core::assert!($assert);)*}
}
macro_rules! constgrp {
($(#[$attr:meta])* $vis:vis struct $group:ident: $ty:ty { $($const:ident = $expr:expr),* $(,)?}) => (
$(#[$attr])* $vis struct $group {r#const: $ty}
impl $group {
$(pub const $const: Self = Self { r#const: $expr };)*
#[inline(always)] pub const fn d(&self) -> $ty { self.r#const }
const _BASE_HB: $ty = 1 << (<$ty>::BITS - 1);
#[inline(always)] pub const fn name(&self) -> &'static str {
match self.r#const {$(capture if capture == $expr => ::core::stringify!($const),)* _ => ::core::unreachable!()}
}
}
impl ::core::fmt::Debug for $group {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
::core::write!(f, "{}::{}", ::core::stringify!($group), Self::name(self))
}
}
);
}
macro_rules! union {
($(#[$attr:meta])* $vis:vis union $name:ident $tail:tt) => (union!(@parse [$(#[$attr])* $vis union $name] [] $tail););
($(#[$attr:meta])* $vis:vis union $name:ident<$($lt:lifetime),*> $tail:tt) => (union!(@parse [$(#[$attr])* $vis union $name<$($lt),*>] [] $tail););
(@parse $decl:tt [$($head:tt)*] {}) => (union!(@defeat0 $decl [$($head)*]););
(@parse $decl:tt [$($head:tt)*] {$(#[$attr:meta])* $vis:vis !$ident:ident:$ty:ty,$($tail:tt)*}) => (
union!(@parse $decl [$($head)* $(#[$attr])* $vis $ident: ::core::mem::ManuallyDrop::<$ty>,] {$($tail)*});
);
(@parse $decl:tt [$($head:tt)*] {$(#[$attr:meta])* $vis:vis $ident:ident:$ty:ty,$($tail:tt)*}) => (
union!(@parse $decl [$($head)* $(#[$attr])* $vis $ident: $ty, ] { $($tail)* });
);
(@defeat0 [$($decls:tt)*] [$($head:tt)*]) => (union!(@defeat1 $($decls)* { $($head)* }););
(@defeat1 $i:item) => ($i);
}

@ -26,7 +26,7 @@
// TODO(@ohsayan): Change the underlying structures, there are just rudimentary ones used during integration with the QL
use super::ql::{lexer::Lit, RawSlice};
use super::ql::lexer::Lit;
/// A [`DataType`] represents the underlying data-type, although this enumeration when used in a collection will always
/// be of one type.
@ -54,8 +54,6 @@ pub enum DataType {
/// elements to ensure correctness in this specific context
/// FIXME(@ohsayan): Try enforcing this somehow
List(Vec<Self>),
/// **☢ WARNING:** Not an actual data type but MUST be translated into an actual data type
AnonymousTypeNeedsEval(RawSlice),
}
enum_impls! {
@ -71,13 +69,16 @@ enum_impls! {
impl DataType {
#[inline(always)]
pub(super) fn clone_from_lit(lit: &Lit) -> Self {
/// ## Safety
///
/// Ensure validity of Lit::Bin
pub(super) unsafe fn clone_from_lit(lit: &Lit) -> Self {
match lit {
Lit::Str(s) => DataType::String(s.clone()),
Lit::Bool(b) => DataType::Boolean(*b),
Lit::UnsignedInt(u) => DataType::UnsignedInt(*u),
Lit::SignedInt(i) => DataType::SignedInt(*i),
Lit::Bin(l) => DataType::AnonymousTypeNeedsEval(l.clone()),
Lit::Bin(l) => DataType::Binary(l.as_slice().to_owned()),
}
}
}
@ -88,23 +89,30 @@ impl<const N: usize> From<[DataType; N]> for DataType {
}
}
#[repr(u8, align(1))]
pub enum DataKind {
// primitive: integer unsigned
UInt8 = 0,
UInt16 = 1,
Uint32 = 2,
UInt64 = 3,
// primitive: integer unsigned
SInt8 = 4,
SInt16 = 5,
SInt32 = 6,
SInt64 = 7,
// primitive: misc
Bool = 8,
// compound: flat
String = 9,
Binary = 10,
// compound: recursive
List = 11,
constgrp! {
#[derive(PartialEq, Eq, Clone, Copy)]
pub struct DataKind: u8 {
// primitive: integer unsigned
UINT8 = 0,
UINT16 = 1,
UINT32 = 2,
UINT64 = 3,
// primitive: integer unsigned
SINT8 = 4,
SINT16 = 5,
SINT32 = 6,
SINT64 = 7,
// primitive: misc
BOOL = 8,
// primitive: floating point
FLOAT32 = 9,
FLOAT64 = 10,
// compound: flat
STR = 11,
STR_BX = Self::_BASE_HB | Self::STR.d(),
BIN = 12,
BIN_BX = Self::_BASE_HB | Self::BIN.d(),
// compound: recursive
LIST = 13,
}
}

@ -237,7 +237,10 @@ pub(super) fn parse_list(
let mut prev_nlist_dscr = None;
while i < l && okay && !stop {
let d = match &tok[i] {
Token::Lit(l) => DataType::clone_from_lit(l),
Token::Lit(l) => unsafe {
// UNSAFE(@ohsayan): Token LT0 guarantees LT0 > LT1 for lit
DataType::clone_from_lit(l)
},
Token::Symbol(Symbol::TtOpenSqBracket) => {
// a nested list
let mut nested_list = Vec::new();
@ -294,7 +297,10 @@ pub(super) fn parse_data_tuple_syntax(tok: &[Token]) -> (Vec<Option<DataType>>,
let mut data = Vec::new();
while i < l && okay && !stop {
match &tok[i] {
Token::Lit(l) => data.push(Some(DataType::clone_from_lit(l))),
Token::Lit(l) => data.push(Some(unsafe {
// UNSAFE(@ohsayan): Token LT0 guarantees LT0 > LT1 for lit
DataType::clone_from_lit(l)
})),
Token::Symbol(Symbol::TtOpenSqBracket) => {
// ah, a list
let mut l = Vec::new();
@ -351,7 +357,10 @@ pub(super) fn parse_data_map_syntax<'a>(
// UNSAFE(@ohsayan): Token lifetime ensures slice validity
id.as_slice()
},
Some(DataType::clone_from_lit(l)),
Some(unsafe {
// UNSAFE(@ohsayan): Token LT0 guarantees LT0 > LT1 for lit
DataType::clone_from_lit(l)
}),
)
.is_none();
}

@ -99,7 +99,7 @@ impl RawSlice {
const unsafe fn new_from_str(s: &str) -> Self {
Self::new(s.as_bytes().as_ptr(), s.as_bytes().len())
}
unsafe fn as_slice(&self) -> &[u8] {
pub unsafe fn as_slice(&self) -> &[u8] {
slice::from_raw_parts(self.ptr.as_ptr(), self.len)
}
unsafe fn as_str(&self) -> &str {

@ -243,7 +243,6 @@ mod num_tests {
assert_eq!(i, SRC.len());
assert_eq!(x, -128);
}
#[test]
pub(crate) fn ndecub_i8_lb_of() {
const SRC: &[u8] = b"-129\n";

Loading…
Cancel
Save