#![allow(
clippy::enum_glob_use,
clippy::too_many_lines,
clippy::wildcard_imports
)]
#![allow(
clippy::cast_precision_loss,
clippy::cast_possible_truncation,
clippy::cast_sign_loss
)]
use super::{
Abc, Stats, count_boolean_slot, count_each_operand, count_negated_operand, wrapped_operand,
};
use crate::*;
const TCL_ASSIGNMENT_COMMANDS: &[&str] = &["incr", "append", "lappend"];
fn tcl_wrapper_operand<'a>(node: &Node<'a>) -> Option<(Node<'a>, bool)> {
match node.kind_id().into() {
Tcl::Expr => wrapped_operand(node).map(|o| (o, false)),
Tcl::UnaryExpr
if node
.child(0)
.is_some_and(|c| c.kind_id() == Tcl::BANG as u16) =>
{
wrapped_operand(node).map(|o| (o, true))
}
_ => None,
}
}
fn tcl_condition_scores_itself(expr: &Node) -> bool {
match expr.kind_id().into() {
Tcl::TernaryExpr => true,
Tcl::BinopExpr => expr.children().any(|token| {
matches!(
token.kind_id().into(),
Tcl::EQEQ
| Tcl::BANGEQ
| Tcl::LT
| Tcl::GT
| Tcl::LTEQ
| Tcl::GTEQ
| Tcl::Eq
| Tcl::Ne
| Tcl::In
| Tcl::Ni
| Tcl::AMPAMP
| Tcl::PIPEPIPE
)
}),
_ => false,
}
}
fn tcl_count_condition(condition: &Node, conditions: &mut f64) {
count_boolean_slot(
condition,
tcl_wrapper_operand,
tcl_condition_scores_itself,
conditions,
);
}
fn tcl_count_chain_operands(chain: &Node, conditions: &mut f64) {
if chain.kind_id() == Tcl::BinopExpr {
count_each_operand(chain, tcl_count_condition, conditions);
}
}
fn tcl_ternary_slots<'a>(
node: &Node<'a>,
) -> (Option<Node<'a>>, Option<Node<'a>>, Option<Node<'a>>) {
let (mut condition, mut consequence, mut alternative) = (None, None, None);
let mut seen_question = false;
let mut seen_colon = false;
for child in node.children() {
match child.kind_id().into() {
Tcl::QMARK => seen_question = true,
Tcl::COLON => seen_colon = true,
_ if !child.is_named() => {}
_ if seen_colon => alternative = alternative.or(Some(child)),
_ if seen_question => consequence = consequence.or(Some(child)),
_ => condition = Some(child),
}
}
(condition, consequence, alternative)
}
fn tcl_walk_ternary(node: &Node, conditions: &mut f64) {
let (condition, consequence, alternative) = tcl_ternary_slots(node);
if let Some(condition) = condition {
tcl_count_condition(&condition, conditions);
}
for branch in [consequence, alternative].into_iter().flatten() {
count_negated_operand(
&branch,
tcl_wrapper_operand,
tcl_condition_scores_itself,
conditions,
);
}
}
fn tcl_condition_expr<'a>(node: &Node<'a>) -> Option<Node<'a>> {
node.children()
.find(|child| matches!(child.kind_id().into(), Tcl::Expr))
}
impl Abc for TclCode {
fn compute<'a>(
node: &Node<'a>,
code: &'a [u8],
ancestors: Ancestors<'a, '_>,
stats: &mut Stats,
) {
match node.kind_id().into() {
Tcl::Set => {
stats.assignments += 1.;
}
Tcl::Command => {
if tcl_command_is_assignment(node, code) {
stats.assignments += 1.;
} else {
stats.branches += 1.;
}
}
Tcl::EQEQ
| Tcl::BANGEQ
| Tcl::LT
| Tcl::GT
| Tcl::LTEQ
| Tcl::GTEQ
| Tcl::Eq
| Tcl::Ne
| Tcl::In
| Tcl::Ni
| Tcl::Else => {
stats.conditions += 1.;
}
Tcl::If | Tcl::While => {
if let Some(expr) = tcl_condition_expr(node) {
tcl_count_condition(&expr, &mut stats.conditions);
}
}
Tcl::Elseif => {
stats.conditions += 1.;
if let Some(expr) = tcl_condition_expr(node) {
tcl_count_condition(&expr, &mut stats.conditions);
}
}
Tcl::TernaryExpr => {
stats.conditions += 1.;
tcl_walk_ternary(node, &mut stats.conditions);
}
Tcl::AMPAMP | Tcl::PIPEPIPE => {
if let Some(chain) = ancestors.parent(node) {
tcl_count_chain_operands(&chain, &mut stats.conditions);
}
}
_ => {}
}
}
}
fn tcl_command_is_assignment(node: &Node, code: &[u8]) -> bool {
crate::lang_helpers::tcl::tcl_command_name(node, code)
.is_some_and(|name| TCL_ASSIGNMENT_COMMANDS.contains(&name))
}