From 928b391e3d6fe343351d15c5fb24c338bac57818 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Wed, 15 Oct 2025 11:28:29 +1300 Subject: Add debug operator for expressions The operator will print the value that is currently at the top of the expression stack as an info-level log message, to help with debugging complex expressions. --- src/types/expression_stack.rs | 14 ++++++++++++++ src/types/operator.rs | 3 +++ 2 files changed, 17 insertions(+) diff --git a/src/types/expression_stack.rs b/src/types/expression_stack.rs index 25710d2..3f80dc8 100644 --- a/src/types/expression_stack.rs +++ b/src/types/expression_stack.rs @@ -70,6 +70,7 @@ impl ExpressionStack { Operator::Length => { pop!(a); push!(op_length(a)) }, Operator::Sum => { pop!(a); push!(op_sum(a)) }, Operator::Absolute => { pop!(a); push!(op_absolute(a)) }, + Operator::Debug => { pop!(a); op_debug(&a, &source); push!(Ok(a)) }, } return Ok(()); } @@ -184,6 +185,19 @@ fn op_sum(l: IntermediateValue) -> Result { } fn op_absolute(l: IntermediateValue) -> Result { Ok(from_isize(to_isize(l)?.wrapping_abs())) } +fn op_debug(l: &IntermediateValue, source: &SourceSpan) { + let (type_name, value) = match l { + IntermediateValue::Integer(integer) => + ("integer", integer.to_string()), + IntermediateValue::List(list) => { + let strings: Vec = list.iter().map(|t| t.value.to_string()).collect(); + ("list", format!("[{}]", strings.join(" "))) + } + IntermediateValue::Block(_) => unreachable!("Block value in expression"), + }; + let message = format!("Printing {type_name} value in expression at {}", source.location()); + log_info(&message, Some(format!(" --> {value}\n"))); +} /// Find the number of bits required to hold an integer. diff --git a/src/types/operator.rs b/src/types/operator.rs index e360567..8a76e52 100644 --- a/src/types/operator.rs +++ b/src/types/operator.rs @@ -21,6 +21,7 @@ pub enum Operator { Length, Sum, Absolute, + Debug, } impl Operator { @@ -61,6 +62,7 @@ impl Operator { "" => Some(Operator::Length), "" => Some(Operator::Sum), "" => Some(Operator::Absolute), + "" => Some(Operator::Debug), _ => None, } } @@ -90,6 +92,7 @@ impl std::fmt::Display for Operator { Operator::Length => "", Operator::Sum => "", Operator::Absolute => "", + Operator::Debug => "", }; write!(f, "{string}") } -- cgit v1.2.3-70-g09d2