Skip to content
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
4 changes: 2 additions & 2 deletions .testings/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ fn main() {

let raw = r#"[
["push", 5],
["push", "5"],
["add", "sht"],
["push", "5.5"],
["ln", "hlf"],
["println"]
]"#;
let tools = vm.tools();
Expand Down
82 changes: 79 additions & 3 deletions rust/src/instructions/math/arithmetic/add_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,50 @@ pub fn add_values(
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::All),
found: get_type_name(a),
found: get_type_name(a.clone()),
});
}
if !b.is_number() {
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::All),
found: get_type_name(b),
found: get_type_name(b.clone()),
});
}
if matches!(
num_type,
PrimitiveTypes::Sht | PrimitiveTypes::Int | PrimitiveTypes::Lng | PrimitiveTypes::Oct
) {
for operand in [&a, &b] {
if matches!(
operand,
&Value::Float16(_) | &Value::Float32(_) | &Value::Float64(_)
) {
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::Integer),
found: get_type_name(operand.clone()),
});
}
}
}
if matches!(
num_type,
PrimitiveTypes::Hlf | PrimitiveTypes::Flt | PrimitiveTypes::Dbl
) {
for operand in [&a, &b] {
if matches!(
operand,
&Value::Int16(_) | &Value::Int32(_) | &Value::Int64(_) | &Value::Int128(_)
) {
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::Float),
found: get_type_name(operand.clone()),
});
}
}
}
Ok(match num_type {
PrimitiveTypes::Sht => Value::Int16(add_i16in(a.as_i16(), b.as_i16())),
PrimitiveTypes::Int => Value::Int32(add_i32in(a.as_i32(), b.as_i32())),
Expand All @@ -51,7 +85,7 @@ pub fn add_values(
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::All),
found: get_type_name(a),
found: "unknown",
});
}
})
Expand All @@ -73,6 +107,48 @@ pub fn add_func(stack: &mut Stack, num_type: PrimitiveTypes, ip: usize) -> Resul
mod tests {
use super::*;
#[test]
fn float_directive_rejects_integer_operands_without_mutating_stack() {
for (a, b, found) in [
(Value::Int32(1), Value::Float32(2.0), "Integer"),
(Value::Float64(1.0), Value::Int64(2), "Long"),
] {
assert!(matches!(
add_values(a.clone(), b.clone(), PrimitiveTypes::Flt, 10),
Err(VMError::TypeMismatch { ip: 10, expected: "Float", found: actual })
if actual == found
));
let mut stack = Stack::from_vec(vec![a, b]);
let original = stack.clone();
assert!(matches!(
add_func(&mut stack, PrimitiveTypes::Flt, 11),
Err(VMError::TypeMismatch { ip: 11, expected: "Float", found: actual })
if actual == found
));
assert_eq!(stack, original);
}
}
#[test]
fn integer_directive_rejects_float_operands_without_mutating_stack() {
for (a, b, found) in [
(Value::Float32(1.0), Value::Int32(2), "Float"),
(Value::Int32(1), Value::Float64(2.0), "Double"),
] {
assert!(matches!(
add_values(a.clone(), b.clone(), PrimitiveTypes::Int, 8),
Err(VMError::TypeMismatch { ip: 8, expected: "Integer", found: actual })
if actual == found
));
let mut stack = Stack::from_vec(vec![a, b]);
let original = stack.clone();
assert!(matches!(
add_func(&mut stack, PrimitiveTypes::Int, 9),
Err(VMError::TypeMismatch { ip: 9, expected: "Integer", found: actual })
if actual == found
));
assert_eq!(stack, original);
}
}
#[test]
fn add_reports_type_mismatch_without_mutating_stack() {
let mut stack = Stack::from_vec(vec![Value::Int32(1), Value::String("invalid".into())]);
let original = stack.clone();
Expand Down
32 changes: 29 additions & 3 deletions rust/src/instructions/math/arithmetic/cos_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ use crate::types::value::Value;
use crate::utils::{expected_type::expected_type, get_type_name::get_type_name};
#[inline(always)]
pub fn cos_values(a: Value, num_type: PrimitiveTypes, ip: usize) -> Result<Value, VMError> {
if !a.is_number() {
if !matches!(
&a,
Value::Float16(_) | Value::Float32(_) | Value::Float64(_)
) {
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::Float),
found: get_type_name(a),
found: get_type_name(a.clone()),
});
}
Ok(match num_type {
Expand All @@ -34,7 +37,7 @@ pub fn cos_values(a: Value, num_type: PrimitiveTypes, ip: usize) -> Result<Value
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::Float),
found: expected_type(num_type, ExpectedCategory::All),
found: "unknown",
});
}
})
Expand Down Expand Up @@ -63,6 +66,14 @@ mod tests {
found: "String"
})
));
assert!(matches!(
cos_values(Value::Int32(1), PrimitiveTypes::Flt, 17),
Err(VMError::TypeMismatch {
ip: 17,
expected: "Float",
found: "Integer"
})
));
let mut stack = Stack::from_vec(vec![invalid]);
let original = stack.clone();
assert!(matches!(
Expand All @@ -75,4 +86,19 @@ mod tests {
));
assert_eq!(stack, original);
}
#[test]
fn accepts_cross_width_float_operands_and_returns_directive_type() {
assert!(matches!(
cos_values(Value::Float16(half::f16::ZERO), PrimitiveTypes::Flt, 19),
Ok(Value::Float32(1.0))
));
assert!(matches!(
cos_values(Value::Float64(0.0), PrimitiveTypes::Hlf, 20),
Ok(Value::Float16(value)) if value == half::f16::ONE
));
assert!(matches!(
cos_values(Value::Float64(0.0), PrimitiveTypes::Flt, 21),
Ok(Value::Float32(1.0))
));
}
}
83 changes: 80 additions & 3 deletions rust/src/instructions/math/arithmetic/div_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,50 @@ pub fn div_values(
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::All),
found: get_type_name(a),
found: get_type_name(a.clone()),
});
}
if !b.is_number() {
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::All),
found: get_type_name(b),
found: get_type_name(b.clone()),
});
}
if matches!(
num_type,
PrimitiveTypes::Sht | PrimitiveTypes::Int | PrimitiveTypes::Lng | PrimitiveTypes::Oct
) {
for operand in [&a, &b] {
if matches!(
operand,
&Value::Float16(_) | &Value::Float32(_) | &Value::Float64(_)
) {
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::Integer),
found: get_type_name(operand.clone()),
});
}
}
}
if matches!(
num_type,
PrimitiveTypes::Hlf | PrimitiveTypes::Flt | PrimitiveTypes::Dbl
) {
for operand in [&a, &b] {
if matches!(
operand,
&Value::Int16(_) | &Value::Int32(_) | &Value::Int64(_) | &Value::Int128(_)
) {
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::Float),
found: get_type_name(operand.clone()),
});
}
}
}
Ok(match num_type {
PrimitiveTypes::Sht => Value::Int16(div_i16in(a.as_i16(), b.as_i16())),
PrimitiveTypes::Int => Value::Int32(div_i32in(a.as_i32(), b.as_i32())),
Expand All @@ -51,7 +85,7 @@ pub fn div_values(
return Err(VMError::TypeMismatch {
ip,
expected: "number",
found: expected_type(num_type, ExpectedCategory::All),
found: "unknown",
});
}
})
Expand All @@ -76,6 +110,49 @@ pub fn div_func(stack: &mut Stack, num_type: PrimitiveTypes, ip: usize) -> Resul
mod tests {
use super::*;
#[test]
fn float_directive_rejects_integer_operands_without_mutating_stack() {
let a = Value::Float32(4.0);
let b = Value::Int64(2);
assert!(matches!(
div_values(a.clone(), b.clone(), PrimitiveTypes::Flt, 10),
Err(VMError::TypeMismatch {
ip: 10,
expected: "Float",
found: "Long"
})
));
let mut stack = Stack::from_vec(vec![a, b]);
let original = stack.clone();
assert!(matches!(
div_func(&mut stack, PrimitiveTypes::Flt, 11),
Err(VMError::TypeMismatch {
ip: 11,
expected: "Float",
found: "Long"
})
));
assert_eq!(stack, original);
}
#[test]
fn integer_directive_rejects_float_operands_without_mutating_stack() {
for (a, b, found) in [
(Value::Float32(1.0), Value::Int32(2), "Float"),
(Value::Int32(1), Value::Float64(2.0), "Double"),
] {
assert!(matches!(
div_values(a.clone(), b.clone(), PrimitiveTypes::Int, 8),
Err(VMError::TypeMismatch { ip: 8, expected: "Integer", found: actual }) if actual == found
));
let mut stack = Stack::from_vec(vec![a, b]);
let original = stack.clone();
assert!(matches!(
div_func(&mut stack, PrimitiveTypes::Int, 9),
Err(VMError::TypeMismatch { ip: 9, expected: "Integer", found: actual }) if actual == found
));
assert_eq!(stack, original);
}
}
#[test]
fn invalid_operands_report_type_mismatch_and_preserve_stack() {
let invalid = Value::String("invalid".into());
assert!(matches!(
Expand Down
Loading
Loading