mindustry logic execution, map- and schematic- parsing and rendering
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use super::get_num;
use crate::{lexer::Token, memory::LVar};

super::op_enum! { pub enum MathOp1 {
    Floor,
    Not,
    Log,
    Abs,
    Rand,
    Ceil,
    Sqrt,
    Sin,
    Cos,
    Tan,
    ASin,
    ACos,
    ATan,
    Log10,
} }

macro_rules! num {
    ($fn: ident $c:expr) => {
        fn $fn(x: LVar<'_>) -> LVar<'_> {
            LVar::from($c(get_num!(x)))
        }
    };
}

macro_rules! flbop {
    ($f: expr, $fn: expr) => {
        $fn($f as u64) as f64
    };
}

num!(floor f64::floor);
fn not(x: LVar<'_>) -> LVar<'_> {
    match x {
        LVar::Num(n) => LVar::Num(flbop!(n, |n: u64| !n)),
        LVar::String(_) => LVar::null(),
    }
}
num!(log f64::ln);
num!(abs f64::abs);
const fn rand(_: LVar<'_>) -> LVar<'_> {
    LVar::Num(4.0)
}
num!(ceil f64::ceil);
num!(sqrt f64::sqrt);
num!(sin f64::sin);
num!(cos f64::cos);
num!(tan f64::tan);
num!(asin f64::asin);
num!(acos f64::acos);
num!(atan f64::atan);
num!(log10 f64::log10);

impl MathOp1 {
    pub const fn get_fn(self) -> for<'f> fn(LVar<'f>) -> LVar<'f> {
        match self {
            Self::Floor => floor,
            Self::Not => not,
            Self::Log => log,
            Self::Abs => abs,
            Self::Rand => rand,
            Self::Ceil => ceil,
            Self::Sqrt => sqrt,
            Self::Sin => sin,
            Self::Cos => cos,
            Self::Tan => tan,
            Self::ASin => asin,
            Self::ACos => acos,
            Self::ATan => atan,
            Self::Log10 => log10,
        }
    }
}