Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed numeric -> isize bug for range & implemented some froms #59

Merged
merged 2 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pax-compiler/src/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@ fn recurse_pratt_parse_to_string<'a>(
op0.as_str().to_string()
},
Rule::xo_symbol => {
symbolic_ids.borrow_mut().push(op0.as_str().to_string());
//for symbolic identifiers, remove any "this" or "self", then return string
convert_symbolic_binding_from_paxel_to_ril(op0)
format!("{}.get_as_int()",convert_symbolic_binding_from_paxel_to_ril(op0))
},
_ => unimplemented!("")
};
Expand All @@ -161,8 +162,9 @@ fn recurse_pratt_parse_to_string<'a>(
op2.as_str().to_string()
},
Rule::xo_symbol => {
symbolic_ids.borrow_mut().push(op2.as_str().to_string());
//for symbolic identifiers, remove any "this" or "self", then return string
convert_symbolic_binding_from_paxel_to_ril(op2)
format!("{}.get_as_int()",convert_symbolic_binding_from_paxel_to_ril(op2))
},
_ => unimplemented!("")
};
Expand Down
203 changes: 202 additions & 1 deletion pax-runtime-api/src/numeric.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::Interpolatable;
use std::cmp::Ordering;
use std::ops::{Add, Div, Mul, Neg, Rem, Sub};
use std::convert::TryFrom;

/// Numeric is a module that wraps numeric literals in Pax
/// It encapsulates the built-in Rust numeric scalar types and defines behavior across them
Expand Down Expand Up @@ -171,14 +172,212 @@ impl From<&f64> for Numeric {
Numeric::Float(*value as f64)
}
}

impl From<Numeric> for u8 {
fn from(value: Numeric) -> Self {
match value {
Numeric::Integer(i) => u8::try_from(i).unwrap_or_else(|_| unreachable!("Conversion from Numeric to u8 resulted in overflow.")),
Numeric::Float(f) => {
if (f >= u8::MIN as f64) && (f <= u8::MAX as f64) {
f as u8
} else {
unreachable!("Conversion from Numeric to u8 resulted in overflow.")
}
}
}
}
}

impl From<Numeric> for u16 {
fn from(value: Numeric) -> Self {
match value {
Numeric::Integer(i) => u16::try_from(i).unwrap_or_else(|_| unreachable!("Conversion from Numeric to u16 resulted in overflow.")),
Numeric::Float(f) => {
if (f >= u16::MIN as f64) && (f <= u16::MAX as f64) {
f as u16
} else {
unreachable!("Conversion from Numeric to u16 resulted in overflow.")
}
}
}
}
}

impl From<Numeric> for u32 {
fn from(value: Numeric) -> Self {
match value {
Numeric::Integer(i) => u32::try_from(i).unwrap_or_else(|_| unreachable!("Conversion from Numeric to u32 resulted in overflow.")),
Numeric::Float(f) => {
if (f >= u32::MIN as f64) && (f <= u32::MAX as f64) {
f as u32
} else {
unreachable!("Conversion from Numeric to u32 resulted in overflow.")
}
}
}
}
}

impl From<Numeric> for u64 {
fn from(value: Numeric) -> Self {
match value {
Numeric::Integer(i) => u64::try_from(i).unwrap_or_else(|_| unreachable!("Conversion from Numeric to u64 resulted in overflow.")),
Numeric::Float(f) => {
if (f >= u64::MIN as f64) && (f <= u64::MAX as f64) {
f as u64
} else {
unreachable!("Conversion from Numeric to u64 resulted in overflow.")
}
}
}
}
}

impl From<Numeric> for u128 {
fn from(value: Numeric) -> Self {
match value {
Numeric::Integer(i) => u128::try_from(i).unwrap_or_else(|_| unreachable!("Conversion from Numeric to u128 resulted in overflow.")),
Numeric::Float(f) => {
if (f >= u128::MIN as f64) && (f <= u128::MAX as f64) {
f as u128
} else {
unreachable!("Conversion from Numeric to u128 resulted in overflow.")
}
}
}
}
}

impl From<Numeric> for usize {
fn from(value: Numeric) -> Self {
match value {
Numeric::Integer(i) => usize::try_from(i).unwrap_or_else(|_| unreachable!("Conversion from Numeric to usize resulted in overflow.")),
Numeric::Float(f) => {
if (f >= usize::MIN as f64) && (f <= usize::MAX as f64) {
f as usize
} else {
unreachable!("Conversion from Numeric to usize resulted in overflow.")
}
}
}
}
}

impl From<Numeric> for i8 {
fn from(value: Numeric) -> Self {
match value {
Numeric::Integer(i) => i8::try_from(i).unwrap_or_else(|_| unreachable!("Conversion from Numeric to i8 resulted in overflow.")),
Numeric::Float(f) => {
if (f >= i8::MIN as f64) && (f <= i8::MAX as f64) {
f as i8
} else {
unreachable!("Conversion from Numeric to i8 resulted in overflow.")
}
}
}
}
}

impl From<Numeric> for i16 {
fn from(value: Numeric) -> Self {
match value {
Numeric::Integer(i) => i16::try_from(i).unwrap_or_else(|_| unreachable!("Conversion from Numeric to i16 resulted in overflow.")),
Numeric::Float(f) => {
if (f >= i16::MIN as f64) && (f <= i16::MAX as f64) {
f as i16
} else {
unreachable!("Conversion from Numeric to i16 resulted in overflow.")
}
}
}
}
}

impl From<Numeric> for i32 {
fn from(value: Numeric) -> Self {
match value {
Numeric::Integer(i) => i32::try_from(i).unwrap_or_else(|_| unreachable!("Conversion from Numeric to i32 resulted in overflow.")),
Numeric::Float(f) => {
if (f >= i32::MIN as f64) && (f <= i32::MAX as f64) {
f as i32
} else {
unreachable!("Conversion from Numeric to i32 resulted in overflow.")
}
}
}
}
}

impl From<Numeric> for i64 {
fn from(value: Numeric) -> Self {
match value {
Numeric::Integer(i) => i64::try_from(i).unwrap_or_else(|_| unreachable!("Conversion from Numeric to i64 resulted in overflow.")),
Numeric::Float(f) => {
if (f >= i64::MIN as f64) && (f <= i64::MAX as f64) {
f as i64
} else {
unreachable!("Conversion from Numeric to i64 resulted in overflow.")
}
}
}
}
}

impl From<Numeric> for i128 {
fn from(value: Numeric) -> Self {
match value {
Numeric::Integer(i) => i128::try_from(i).unwrap_or_else(|_| unreachable!("Conversion from Numeric to i128 resulted in overflow.")),
Numeric::Float(f) => {
if (f >= i128::MIN as f64) && (f <= i128::MAX as f64) {
f as i128
} else {
unreachable!("Conversion from Numeric to i128 resulted in overflow.")
}
}
}
}
}

impl From<Numeric> for isize {
fn from(value: Numeric) -> Self {
match value {
Numeric::Integer(i) => isize::try_from(i).unwrap_or_else(|_| unreachable!("Conversion from Numeric to isize resulted in overflow.")),
Numeric::Float(f) => {
if (f >= isize::MIN as f64) && (f <= isize::MAX as f64) {
f as isize
} else {
unreachable!("Conversion from Numeric to isize resulted in overflow.")
}
}
}
}
}

impl From<Numeric> for f32 {
fn from(value: Numeric) -> Self {
let f = match value {
Numeric::Integer(i) => i as f64,
Numeric::Float(f) => f,
};
if (f >= f32::MIN as f64) && (f <= f32::MAX as f64) {
f as f32
} else {
unreachable!("Conversion from Numeric to f32 resulted in overflow.")
}
}
}

impl From<Numeric> for f64 {
fn from(value: Numeric) -> Self {
match value {
Numeric::Integer(i) => i as Self,
Numeric::Integer(i) => i as f64,
Numeric::Float(f) => f,
}
}
}



impl Numeric {
fn widen(
f: fn(f64, f64) -> f64,
Expand Down Expand Up @@ -306,6 +505,8 @@ impl PartialOrd<Self> for Numeric {
}
}



/// Helper functions to ease working with built-ins

impl Mul<Numeric> for f64 {
Expand Down