Skip to content

Commit

Permalink
Switch float and double 馃う鈥嶁檪锔廟emove exp in decimals
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmtcn123 committed Nov 8, 2023
1 parent d9dce96 commit 943ae4d
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 44 deletions.
4 changes: 2 additions & 2 deletions ellie_engine/bytecode/src/addressing_modes.rs
Expand Up @@ -130,10 +130,10 @@ impl core::fmt::Display for AddressingModes {
}
.to_string(),
Types::Float =>
f64::from_le_bytes(value[0..mem::size_of::<f64>()].try_into().unwrap())
f32::from_le_bytes(value[0..mem::size_of::<f32>()].try_into().unwrap())
.to_string(),
Types::Double =>
f32::from_le_bytes(value[0..mem::size_of::<f32>()].try_into().unwrap())
f64::from_le_bytes(value[0..mem::size_of::<f64>()].try_into().unwrap())
.to_string(),
Types::Byte => format!("0x{}", value[0]),
Types::Bool =>
Expand Down
4 changes: 2 additions & 2 deletions ellie_engine/bytecode/src/macros.rs
Expand Up @@ -27,8 +27,8 @@ fn parse_type_text(text: String) -> Option<(Types, Vec<u8>)> {
let _value = type_text.next().unwrap();
let value = match _type {
"int" => _value.parse::<i64>().unwrap().to_le_bytes().to_vec(),
"float" => _value.parse::<f64>().unwrap().to_le_bytes().to_vec(),
"double" => _value.parse::<f32>().unwrap().to_le_bytes().to_vec(),
"float" => _value.parse::<f32>().unwrap().to_le_bytes().to_vec(),
"double" => _value.parse::<f64>().unwrap().to_le_bytes().to_vec(),
"byte" => _value.parse::<u8>().unwrap().to_le_bytes().to_vec(),
"bool" => vec![_value.parse::<bool>().unwrap().into()],
"char" => _value
Expand Down
4 changes: 2 additions & 2 deletions ellie_engine/bytecode/src/transpiler/type_resolver.rs
Expand Up @@ -23,10 +23,10 @@ pub fn convert_type(
CoreTypes::Integer(integer) => (Types::Integer, isize_to_le_bytes(integer.value, arch)),
CoreTypes::Decimal(decimal) => match decimal.value {
ellie_core::definite::types::decimal::DecimalTypeEnum::Float(float_value) => {
(Types::Float, f64_to_le_bytes(float_value, arch))
(Types::Float, f32_to_le_bytes(float_value, arch))
}
ellie_core::definite::types::decimal::DecimalTypeEnum::Double(double_value) => {
(Types::Double, f32_to_le_bytes(double_value, arch))
(Types::Double, f64_to_le_bytes(double_value, arch))
}
},
CoreTypes::Bool(bool) => (Types::Bool, (bool.value as u8).to_le_bytes().to_vec()),
Expand Down
4 changes: 2 additions & 2 deletions ellie_engine/core/src/definite/types/decimal.rs
Expand Up @@ -3,8 +3,8 @@ use serde::{Deserialize, Serialize};

#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub enum DecimalTypeEnum {
Float(f64),
Double(f32),
Float(f32),
Double(f64),
}

impl Default for DecimalTypeEnum {
Expand Down
2 changes: 1 addition & 1 deletion ellie_engine/core/src/definite/types/double.rs
Expand Up @@ -3,6 +3,6 @@ use serde::{Deserialize, Serialize};

#[derive(PartialEq, Default, Debug, Clone, Serialize, Deserialize)]
pub struct DoubleType {
pub value: f32,
pub value: f64,
pub pos: defs::Cursor,
}
Expand Up @@ -35,7 +35,7 @@ impl crate::processors::Processor for decimal_type::DecimalTypeCollector {
} else if letter_char.to_string().parse::<i8>().is_ok() {
self.point += &letter_char.to_string();
self.data.raw += &letter_char.to_string();
if let Ok(nm) = self.data.raw.parse::<f64>() {
if let Ok(nm) = self.data.raw.parse::<f32>() {
self.data.value = DecimalTypeEnum::Float(nm);
self.complete = true;
} else {
Expand All @@ -51,7 +51,7 @@ impl crate::processors::Processor for decimal_type::DecimalTypeCollector {
self.data.pos.range_end = cursor;
} else if letter_char == 'f' {
self.data.pos.range_end = cursor;
if let Ok(nm) = self.data.raw.parse::<f64>() {
if let Ok(nm) = self.data.raw.parse::<f32>() {
self.data.value = DecimalTypeEnum::Float(nm);
self.data.is_double = false;
self.complete = true;
Expand All @@ -67,7 +67,7 @@ impl crate::processors::Processor for decimal_type::DecimalTypeCollector {
}
} else if letter_char == 'd' {
self.data.pos.range_end = cursor;
if let Ok(nm) = self.data.raw.parse::<f32>() {
if let Ok(nm) = self.data.raw.parse::<f64>() {
self.data.value = DecimalTypeEnum::Double(nm);
self.complete = false;
} else {
Expand Down
8 changes: 4 additions & 4 deletions ellie_engine/vm/src/instructions/A2D.rs
Expand Up @@ -27,16 +27,16 @@ impl super::InstructionExecuter for A2D {
match current_stack.registers.A.type_id.id {
1 => {
let data = current_stack.registers.A.to_int();
current_stack.registers.A = StaticRawType::from_double(data as f32);
current_stack.registers.A = StaticRawType::from_double(data as f64);
}
2 => {
let data = current_stack.registers.A.to_float();
current_stack.registers.A = StaticRawType::from_double(data as f32);
current_stack.registers.A = StaticRawType::from_double(data as f64);
}
3 => (),
4 => {
let data = current_stack.registers.A.to_byte();
current_stack.registers.A = StaticRawType::from_double(data as f32);
current_stack.registers.A = StaticRawType::from_double(data as f64);
}
13 => {
let pointer = current_stack.registers.B.to_int() as usize;
Expand All @@ -52,7 +52,7 @@ impl super::InstructionExecuter for A2D {
match mref.type_id.id {
6 => {
let a_value = String::from_utf8(mref.data).unwrap();
match a_value.parse::<f32>() {
match a_value.parse::<f64>() {
Ok(double_value) => {
current_stack.registers.A =
StaticRawType::from_double(double_value);
Expand Down
8 changes: 4 additions & 4 deletions ellie_engine/vm/src/instructions/A2F.rs
Expand Up @@ -27,16 +27,16 @@ impl super::InstructionExecuter for A2F {
match current_stack.registers.A.type_id.id {
1 => {
let data = current_stack.registers.A.to_int();
current_stack.registers.A = StaticRawType::from_float(data as f64);
current_stack.registers.A = StaticRawType::from_float(data as f32);
}
2 => (),
3 => {
let data = current_stack.registers.A.to_byte();
current_stack.registers.A = StaticRawType::from_float(data as f64);
current_stack.registers.A = StaticRawType::from_float(data as f32);
}
4 => {
let data = current_stack.registers.A.to_byte();
current_stack.registers.A = StaticRawType::from_float(data as f64);
current_stack.registers.A = StaticRawType::from_float(data as f32);
}
13 => {
let pointer = current_stack.registers.B.to_int() as usize;
Expand All @@ -52,7 +52,7 @@ impl super::InstructionExecuter for A2F {
match mref.type_id.id {
6 => {
let a_value = String::from_utf8(mref.data).unwrap();
let float_value = a_value.parse::<f64>().unwrap();
let float_value = a_value.parse::<f32>().unwrap();
current_stack.registers.A = StaticRawType::from_float(float_value);
}
e => {
Expand Down
18 changes: 13 additions & 5 deletions ellie_engine/vm/src/instructions/EXP.rs
@@ -1,4 +1,4 @@
use alloc::format;
use alloc::{borrow::ToOwned, format};
use ellie_core::defs::PlatformArchitecture;

use crate::{
Expand Down Expand Up @@ -43,7 +43,7 @@ impl super::InstructionExecuter for EXP {
current_stack.registers.A = StaticRawType::from_int(result);
}
(2, 2) => {
let b_value = current_stack.registers.B.to_float();
/* let b_value = current_stack.registers.B.to_float();
let c_value = current_stack.registers.C.to_float();
let result = b_value.powf(c_value);
Expand All @@ -54,10 +54,14 @@ impl super::InstructionExecuter for EXP {
reason: ThreadPanicReason::FloatOverflow,
code_location: format!("{}:{}", file!(), line!()),
});
}
} */
return Err(ExecuterPanic {
reason: ThreadPanicReason::RuntimeError("EXP is todo.".to_owned()),
code_location: format!("{}:{}", file!(), line!()),
});
}
(3, 3) => {
let b_value = current_stack.registers.B.to_double();
/* let b_value = current_stack.registers.B.to_double();
let c_value = current_stack.registers.C.to_double();
let result = b_value.powf(c_value);
if result.is_finite() {
Expand All @@ -67,7 +71,11 @@ impl super::InstructionExecuter for EXP {
reason: ThreadPanicReason::DoubleOverflow,
code_location: format!("{}:{}", file!(), line!()),
});
}
} */
return Err(ExecuterPanic {
reason: ThreadPanicReason::RuntimeError("EXP is todo.".to_owned()),
code_location: format!("{}:{}", file!(), line!()),
});
}
_ => {
return Err(ExecuterPanic {
Expand Down
38 changes: 19 additions & 19 deletions ellie_engine/vm/src/raw_type.rs
Expand Up @@ -203,14 +203,14 @@ impl TypeId {
pub fn float() -> Self {
Self {
id: 2,
size: mem::size_of::<f64>(),
size: mem::size_of::<f32>(),
}
}

pub fn double() -> Self {
Self {
id: 3,
size: mem::size_of::<f32>(),
size: mem::size_of::<f64>(),
}
}

Expand Down Expand Up @@ -360,12 +360,12 @@ impl RawType {
usize::from_le_bytes(self.data.clone().try_into().unwrap())
}

pub fn to_float(&self) -> f64 {
f64::from_le_bytes(self.data.clone().try_into().unwrap())
pub fn to_float(&self) -> f32 {
f32::from_le_bytes(self.data.clone().try_into().unwrap())
}

pub fn to_double(&self) -> f32 {
f32::from_le_bytes(self.data.clone().try_into().unwrap())
pub fn to_double(&self) -> f64 {
f64::from_le_bytes(self.data.clone().try_into().unwrap())
}

pub fn to_byte(&self) -> u8 {
Expand Down Expand Up @@ -417,7 +417,7 @@ impl RawType {
RawType {
type_id: TypeId {
id: 2,
size: mem::size_of::<f64>(),
size: mem::size_of::<f32>(),
},
data,
}
Expand All @@ -427,7 +427,7 @@ impl RawType {
RawType {
type_id: TypeId {
id: 3,
size: mem::size_of::<f32>(),
size: mem::size_of::<f64>(),
},
data,
}
Expand Down Expand Up @@ -580,16 +580,16 @@ impl StaticRawType {
usize::from_le_bytes(integer)
}

/// Converts the float to a f64
/// Converts the float to a f32
/// TODO: Example doc
pub fn to_float(&self) -> f64 {
f64::from_le_bytes(self.data[0..mem::size_of::<f64>()].try_into().unwrap())
pub fn to_float(&self) -> f32 {
f32::from_le_bytes(self.data[0..mem::size_of::<f32>()].try_into().unwrap())
}

/// Converts the double to a f32
/// Converts the double to a f64
/// TODO: Example doc
pub fn to_double(&self) -> f32 {
f32::from_le_bytes(self.data[0..mem::size_of::<f32>()].try_into().unwrap())
pub fn to_double(&self) -> f64 {
f64::from_le_bytes(self.data[0..mem::size_of::<f64>()].try_into().unwrap())
}

/// Converts the byte to a u8
Expand Down Expand Up @@ -634,9 +634,9 @@ impl StaticRawType {
}
}

pub fn from_float(data: f64) -> StaticRawType {
pub fn from_float(data: f32) -> StaticRawType {
let mut bytes = [0; 8];
bytes[0..mem::size_of::<f64>()].copy_from_slice(&data.to_le_bytes());
bytes[0..mem::size_of::<f32>()].copy_from_slice(&data.to_le_bytes());
StaticRawType {
type_id: TypeId {
id: 2,
Expand All @@ -646,13 +646,13 @@ impl StaticRawType {
}
}

pub fn from_double(data: f32) -> StaticRawType {
pub fn from_double(data: f64) -> StaticRawType {
let mut bytes = [0; 8];
bytes[0..mem::size_of::<f32>()].copy_from_slice(&data.to_le_bytes());
bytes[0..mem::size_of::<f64>()].copy_from_slice(&data.to_le_bytes());
StaticRawType {
type_id: TypeId {
id: 3,
size: mem::size_of::<f32>(),
size: mem::size_of::<f64>(),
},
data: bytes,
}
Expand Down

0 comments on commit 943ae4d

Please sign in to comment.