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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use super::get_num;
use crate::{lexer::Token, memory::LVar};
use std::f64::consts::PI;

super::op_enum! { pub enum MathOp2 {
    Angle,
    Add,
    Sub,
    Mul,
    Div,
    IDiv,
    Mod,
    Pow,
    Equal,
    NotEqual,
    And,
    LessThan,
    LessThanEq,
    GreaterThan,
    GreaterThanEq,
    StrictEqual,
    ShiftLeft,
    ShiftRight,
    BitOr,
    BitAnd,
    ExclusiveOr,
    Max,
    Min,
    AngleDiff,
    Len,
    Noise, // unimplemented
} }

macro_rules! num {
    ($fn:ident $closure:expr) => {
        fn $fn<'v>(a: LVar<'v>, b: LVar<'v>) -> LVar<'v> {
            LVar::from($closure(get_num!(a), get_num!(b)))
        }
    };
}
macro_rules! op {
    ($fn:ident $op:tt) => {
        fn $fn<'v>(a: LVar<'v>, b: LVar<'v>) -> LVar<'v> {
            LVar::from(get_num!(a) $op get_num!(b))
        }
    }
}
macro_rules! bop {
    ($fn: ident $op: tt) => {
        fn $fn<'v>(a: LVar<'v>, b: LVar<'v>) -> LVar<'v> {
            LVar::from(((get_num!(a) as u64) $op (get_num!(b) as u64)) as f64)
        }
    };
}
macro_rules! nofun {
    ($fn:ident $closure:expr) => {
        fn $fn<'v>(a: LVar<'v>, b: LVar<'v>) -> LVar<'v> {
            LVar::from($closure(a, b))
        }
    };
}
nofun!(eq | a, b | a == b);
nofun!(ne | a, b | a != b);
num!(and | a, b | a != 0.0 && b != 0.0);
op!(add+);
op!(sub -);
op!(mul *);
bop!(idiv /);
op!(lt <);
op!(le <=);
op!(gt >);
op!(ge >=);
op!(div /);
op!(rem %);
num!(pow f64::powf);
bop!(shl <<);
bop!(shr >>);
bop!(or |);
bop!(band &);
bop!(xor ^);
num!(max f64::max);
num!(min f64::min);
#[rustfmt::skip]
num!(angle_diff |a, b| {
    let a = a % (360.0 * PI);
    let b = b % (360.0 * PI);
    f64::min(
        if (a - b) < 0.0 { a - b + 360.0 } else { a - b },
        if (b - a) < 0.0 { b - a + 360.0 } else { b - a },
    )
});
num!(len f64::hypot);
nofun!(noise | _, _ | 9.0);
num!(angle |a: f64, b: f64| {
    let mut x = a.atan2(b) * (180.0 / PI);
    if x < 0.0 {
        x += 360.0;
    }
    x
});

impl MathOp2 {
    pub const fn get_fn(self) -> for<'f> fn(LVar<'f>, LVar<'f>) -> LVar<'f> {
        match self {
            // we kind of interpret strings as numbers so yeah
            Self::Equal | Self::StrictEqual => eq,
            Self::NotEqual => ne,
            Self::And => and,
            Self::Add => add,
            Self::Sub => sub,
            Self::Mul => mul,
            Self::IDiv => idiv,
            Self::LessThan => lt,
            Self::LessThanEq => le,
            Self::GreaterThan => gt,
            Self::GreaterThanEq => ge,
            Self::Div => div,
            Self::Mod => rem,
            Self::Pow => pow,
            Self::ShiftLeft => shl,
            Self::ShiftRight => shr,
            Self::BitOr => or,
            Self::BitAnd => band,
            Self::ExclusiveOr => xor,
            Self::Max => max,
            Self::Min => min,
            Self::AngleDiff => angle_diff,
            Self::Len => len,
            Self::Noise => noise,
            Self::Angle => angle,
        }
    }
}