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
#[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! document {
  ($($x:tt)*) => {
    stringify!($($x)*)
  }
}

#[macro_export]
macro_rules! function {
  ($name:literal, $docs:expr, $($x:tt)*) => {
    {
      const DOC: &'static str = $docs;
      ($name, DOC, $($x)*
      )
    }
  };
}

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

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

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

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

  ($file:ident + $heap:ident: > $y:ident) => {
    interpreter::warn("WARN: Deprecated library feature `>`, migrate to the $: syntax instead!");
    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", $file);
    }
  };
  ($file:ident + $heap:ident: str $y:ident) => {};

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

#[macro_export]
macro_rules! get_as {
  ($file:ident + $heap:ident: $ty:ident $y:ident) => {
    let interpreter::types::BufValue::$ty($y) = $y else {
      interpreter::error("Invalid Format or Varible not found!", $file);
    };
  };
}

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

  ($file:ident + $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!", $file);
    };
  };
}