summaryrefslogtreecommitdiff
path: root/src/types
diff options
context:
space:
mode:
Diffstat (limited to 'src/types')
-rw-r--r--src/types/expression_stack.rs9
-rw-r--r--src/types/operator.rs3
2 files changed, 12 insertions, 0 deletions
diff --git a/src/types/expression_stack.rs b/src/types/expression_stack.rs
index 4d26eb2..d14d808 100644
--- a/src/types/expression_stack.rs
+++ b/src/types/expression_stack.rs
@@ -61,11 +61,20 @@ impl ExpressionStack {
Operator::BitOr => { pop!(b); pop!(a); push!(a | b) },
Operator::BitXor => { pop!(b); pop!(a); push!(a ^ b) },
Operator::BitNot => { pop!(a); push!(!a) },
+ Operator::Length => { pop!(a); push!(width(a) as isize) },
}
return Ok(());
}
}
+/// Find the number of bits required to hold an integer.
+pub fn width(value: isize) -> u32 {
+ match value.cmp(&0) {
+ std::cmp::Ordering::Less => (-value).ilog2() + 2,
+ std::cmp::Ordering::Equal => 0,
+ std::cmp::Ordering::Greater => value.ilog2() + 1,
+ }
+}
pub enum StackError {
Underflow,
diff --git a/src/types/operator.rs b/src/types/operator.rs
index a7e7b9b..af607e3 100644
--- a/src/types/operator.rs
+++ b/src/types/operator.rs
@@ -18,6 +18,7 @@ pub enum Operator {
BitOr,
BitXor,
BitNot,
+ Length,
}
impl Operator {
@@ -55,6 +56,7 @@ impl Operator {
"<or>" => Some(Operator::BitOr),
"<xor>" => Some(Operator::BitXor),
"<not>" => Some(Operator::BitNot),
+ "<len>" => Some(Operator::Length),
_ => None,
}
}
@@ -81,6 +83,7 @@ impl std::fmt::Display for Operator {
Operator::BitOr => "<or>",
Operator::BitXor => "<xor>",
Operator::BitNot => "<not>",
+ Operator::Length => "<len>",
};
write!(f, "{string}")
}