dalbit_core/modifiers/
remove_number_literals.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use darklua_core::{
    nodes::{Block, DecimalNumber, Expression, NumberExpression},
    process::{DefaultVisitor, NodeProcessor, NodeVisitor},
    rules::{Context, FlawlessRule, RuleConfiguration, RuleConfigurationError, RuleProperties},
};

pub const REMOVE_NUMBER_LITERALS_MODIFIER_NAME: &str = "remove_number_literals";

struct Processor {}

impl NodeProcessor for Processor {
    fn process_expression(&mut self, exp: &mut Expression) {
        if let Expression::Number(num_exp) = exp {
            match num_exp {
                NumberExpression::Binary(binary) => {
                    let value = binary.compute_value();
                    *exp = DecimalNumber::new(value).into();
                }
                NumberExpression::Decimal(decimal) => {
                    let value = decimal.compute_value();
                    *exp = DecimalNumber::new(value).into();
                }
                _ => {}
            }
        }
    }
}

#[derive(Default, Debug)]
pub struct RemoveNumberLiterals {}

impl FlawlessRule for RemoveNumberLiterals {
    fn flawless_process(&self, block: &mut Block, _: &Context) {
        let mut processor = Processor {};
        DefaultVisitor::visit_block(block, &mut processor);
    }
}

impl RuleConfiguration for RemoveNumberLiterals {
    fn configure(&mut self, _: RuleProperties) -> Result<(), RuleConfigurationError> {
        Ok(())
    }

    fn get_name(&self) -> &'static str {
        REMOVE_NUMBER_LITERALS_MODIFIER_NAME
    }

    fn serialize_to_properties(&self) -> RuleProperties {
        RuleProperties::new()
    }
}