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
#[macro_export]
macro_rules! create_state {
  ($t:ty) => {
    static mut STATE: Option<std::collections::HashMap<String, $t>> = None;
  };
}

#[macro_export]
macro_rules! get_state {
  () => {
    unsafe {
      if let None = STATE {
        STATE = Some(std::collections::HashMap::new());
      }
      STATE.as_mut().unwrap()
    }
  };
}

#[macro_export]
macro_rules! module {
  ($struct:ident, $($x:tt)*) => {
    pub struct $struct;
    impl interpreter::Package for $struct {
      $($x)*
    }
  };
}

#[macro_export]
macro_rules! pkg_name {
  ($($x:tt)*) => {
    fn name(&self) -> &'static [u8] {
      $($x)*.as_bytes()
    }
  };
}

#[macro_export]
macro_rules! methods {
  ($($x:tt)*) => {
    fn methods(&self) -> interpreter::types::MethodRes {
      &[
        $($x)*
      ]
    }
  };
}

#[macro_export]
macro_rules! function {
  ($name:literal, $($x:tt)*) => {
    ($name, $($x)*
    )
  };
}

#[macro_export]
macro_rules! parse {
  ($heap:ident + $args:ident: $($x:tt $y:ident),*) => {
    #[allow(unused_variables)]
    let [_, $($y),*] = &$args[..] else {
      interpreter::error("Invalid Format!");
    };

    $(interpreter::modify!($heap: $x $y);)*;
  };
}

#[macro_export]
macro_rules! modify {
  ($heap:ident: -> $y:ident) => {
    let Some(Some($y)) = $heap.remove($y) else {
      interpreter::error("Invalid Format or Varible not found!");
    };
  };

  ($heap:ident: & $y:ident) => {
    let Some($y) = $heap.get($y) else {
      interpreter::error("Invalid Format or Varible not found!");
    };
  };

  ($heap:ident: > $y:ident) => {
    if !$y.starts_with("$") && !$y.starts_with("*") {
      interpreter::error("Invalid Variable provided!\nNote: Mutable / Moved values may not be provided to what expects piped (`>`) values");
    }
  };
  ($heap:ident: str $y:ident) => {};

  ($heap:ident: drop $y:ident) => {
    let $y: Vec<Option<String>> = vec![];
    drop($y);
  };
}

#[macro_export]
macro_rules! get_mut {
  ($heap:ident: mut $y:ident) => {
    let Some($y) = $heap.get_mut($y) else {
      interpreter::error("Invalid Format or Varible not found!");
    };
  };

  ($heap:ident: $ty:ident $y:ident) => {
    let Some(interpreter::types::BufValue::$ty($y)) = $heap.get_mut($y) else {
      interpreter::error("Invalid Format or Varible not found!");
    };
  };
}