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
8 changes: 8 additions & 0 deletions src/uu/expr/src/syntax_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ pub enum AstNode {
operands: OperandsList,
},
}

impl AstNode {
fn debug_dump(&self) {
self.debug_dump_impl(1);
}

fn debug_dump_impl(&self, depth: usize) {
for _ in 0..depth {
print!("\t",);
Expand Down Expand Up @@ -71,12 +73,14 @@ impl AstNode {
operands,
})
}

fn new_leaf(token_idx: usize, value: &str) -> Box<Self> {
Box::new(Self::Leaf {
token_idx,
value: value.into(),
})
}

pub fn evaluate(&self) -> Result<String, String> {
match self {
Self::Leaf { value, .. } => Ok(value.clone()),
Expand Down Expand Up @@ -154,6 +158,7 @@ impl AstNode {
},
}
}

pub fn operand_values(&self) -> Result<Vec<String>, String> {
if let Self::Node {
operands, op_type, ..
Expand Down Expand Up @@ -257,6 +262,7 @@ fn ast_from_rpn(rpn: &mut TokenStack) -> Result<Box<AstNode>, String> {
}
}
}

fn maybe_ast_node(
token_idx: usize,
op_type: &str,
Expand Down Expand Up @@ -520,13 +526,15 @@ fn prefix_operator_substr(values: &[String]) -> String {
fn bool_as_int(b: bool) -> u8 {
u8::from(b)
}

fn bool_as_string(b: bool) -> String {
if b {
"1".to_string()
} else {
"0".to_string()
}
}

fn value_as_bool(s: &str) -> bool {
if s.is_empty() {
return false;
Expand Down
4 changes: 4 additions & 0 deletions src/uu/expr/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub enum Token {
value: String,
},
}

impl Token {
fn new_infix_op(v: &str, left_assoc: bool, precedence: u8) -> Self {
Self::InfixOp {
Expand All @@ -46,6 +47,7 @@ impl Token {
value: v.into(),
}
}

fn new_value(v: &str) -> Self {
Self::Value { value: v.into() }
}
Expand All @@ -56,12 +58,14 @@ impl Token {
_ => false,
}
}

fn is_a_number(&self) -> bool {
match self {
Self::Value { value, .. } => value.parse::<BigInt>().is_ok(),
_ => false,
}
}

fn is_a_close_paren(&self) -> bool {
matches!(*self, Self::ParClose)
}
Expand Down