From 4b53479153806e3a1e7a4944313f77dc262117b2 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Sat, 26 Apr 2025 09:09:59 +1200 Subject: Implement operator for expressions The operator returns the number of set bits in the binary representation of an integer. For negative numbers, only a single sign bit is counted in the result. --- src/types/expression_stack.rs | 8 ++++++++ src/types/operator.rs | 3 +++ 2 files changed, 11 insertions(+) diff --git a/src/types/expression_stack.rs b/src/types/expression_stack.rs index d14d808..8e1c87d 100644 --- a/src/types/expression_stack.rs +++ b/src/types/expression_stack.rs @@ -62,6 +62,7 @@ impl ExpressionStack { Operator::BitXor => { pop!(b); pop!(a); push!(a ^ b) }, Operator::BitNot => { pop!(a); push!(!a) }, Operator::Length => { pop!(a); push!(width(a) as isize) }, + Operator::Tally => { pop!(a); push!(tally(a) as isize) }, } return Ok(()); } @@ -76,6 +77,13 @@ pub fn width(value: isize) -> u32 { } } +pub fn tally(value: isize) -> u32 { + let width = width(value); + let mask = 2i32.pow(width) -1; + let value = (value as usize) & (mask as usize); + return value.count_ones(); +} + pub enum StackError { Underflow, MultipleReturnValues, diff --git a/src/types/operator.rs b/src/types/operator.rs index af607e3..d5b5e94 100644 --- a/src/types/operator.rs +++ b/src/types/operator.rs @@ -19,6 +19,7 @@ pub enum Operator { BitXor, BitNot, Length, + Tally, } impl Operator { @@ -57,6 +58,7 @@ impl Operator { "" => Some(Operator::BitXor), "" => Some(Operator::BitNot), "" => Some(Operator::Length), + "" => Some(Operator::Tally), _ => None, } } @@ -84,6 +86,7 @@ impl std::fmt::Display for Operator { Operator::BitXor => "", Operator::BitNot => "", Operator::Length => "", + Operator::Tally => "", }; write!(f, "{string}") } -- cgit v1.2.3-70-g09d2