From 058938fed4562d4802c7b7d8dc52524dbde354cb Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Sun, 27 Apr 2025 16:17:32 +1200 Subject: Implement operator for expressions The operator returns the absolute value of an integer. --- src/types/expression_stack.rs | 1 + src/types/operator.rs | 3 +++ 2 files changed, 4 insertions(+) (limited to 'src/types') diff --git a/src/types/expression_stack.rs b/src/types/expression_stack.rs index 8e1c87d..228fa2d 100644 --- a/src/types/expression_stack.rs +++ b/src/types/expression_stack.rs @@ -63,6 +63,7 @@ impl ExpressionStack { Operator::BitNot => { pop!(a); push!(!a) }, Operator::Length => { pop!(a); push!(width(a) as isize) }, Operator::Tally => { pop!(a); push!(tally(a) as isize) }, + Operator::Absolute => { pop!(a); push!(a.wrapping_abs()) }, } return Ok(()); } diff --git a/src/types/operator.rs b/src/types/operator.rs index d5b5e94..da4dfb4 100644 --- a/src/types/operator.rs +++ b/src/types/operator.rs @@ -20,6 +20,7 @@ pub enum Operator { BitNot, Length, Tally, + Absolute, } impl Operator { @@ -59,6 +60,7 @@ impl Operator { "" => Some(Operator::BitNot), "" => Some(Operator::Length), "" => Some(Operator::Tally), + "" => Some(Operator::Absolute), _ => None, } } @@ -87,6 +89,7 @@ impl std::fmt::Display for Operator { Operator::BitNot => "", Operator::Length => "", Operator::Tally => "", + Operator::Absolute => "", }; write!(f, "{string}") } -- cgit v1.2.3-70-g09d2