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
use super::{Flow, LInstruction};
use crate::{
    executor::{ExecutorContext, Memory},
    memory::{LAddress, LVar},
};
use std::io::Write as Wr;

#[derive(Debug)]
pub struct Read<'v> {
    // index guranteed to never be out of bounds
    pub(crate) index: usize,
    pub(crate) output: LAddress<'v>,
    pub(crate) container: Memory,
}

impl<'v> LInstruction<'v> for Read<'v> {
    fn run<W: Wr>(&self, exec: &mut ExecutorContext<'v, W>) -> Flow {
        let to = exec.mem(self.container)[self.index];
        let Some(out) = exec.get_mut(self.output) else {
            return Flow::Continue;
        };
        *out = LVar::from(to);
        Flow::Continue
    }
}

#[derive(Debug)]
pub struct Write<'v> {
    // index guranteed to never be out of bounds
    pub(crate) index: usize,
    pub(crate) set: LAddress<'v>,
    pub(crate) container: Memory,
}

impl<'v> LInstruction<'v> for Write<'v> {
    fn run<W: Wr>(&self, exec: &mut ExecutorContext<'v, W>) -> Flow {
        let LVar::Num(n) = exec.get(self.set) else {
            return Flow::Continue;
        };
        exec.mem(self.container)[self.index] = n;
        Flow::Continue
    }
}

#[derive(Debug)]
pub struct Print<'v> {
    pub(crate) val: LAddress<'v>,
}
impl LInstruction<'_> for Print<'_> {
    fn run<W: Wr>(&self, exec: &mut ExecutorContext<'_, W>) -> Flow {
        let v = exec.get(self.val);
        if let Some(o) = &mut exec.output {
            write!(o, "{v}").unwrap();
        }
        Flow::Continue
    }
}