interpreter/macros/
mod.rs1#[macro_export]
2macro_rules! create_state {
3 ($t:ty) => {
4 static mut STATE: Option<std::collections::HashMap<String, $t>> = None;
5 };
6}
7
8#[macro_export]
9macro_rules! get_state {
10 () => {
11 unsafe {
12 if let None = STATE {
13 STATE = Some(std::collections::HashMap::new());
14 }
15 STATE.as_mut().unwrap()
16 }
17 };
18}
19
20#[macro_export]
21macro_rules! module {
32 ($struct:ident, $($x:tt)*) => {
33 pub struct $struct;
34 impl interpreter::Package for $struct {
35 $($x)*
36 }
37 };
38}
39
40#[macro_export]
41macro_rules! pkg_name {
49 ($($x:tt)*) => {
50 fn name(&self) -> &'static [u8] {
51 $($x)*.as_bytes()
52 }
53 };
54}
55
56#[macro_export]
57macro_rules! rtval_name {
65 ($x:literal) => {
66 fn name(&self) -> &'static str {
67 $x
68 }
69 };
70}
71
72#[macro_export]
73macro_rules! runtime_value {
85 (
86 $struct:ident,
87 { $(pub $x:ident: $y:ty),* },
88 $($t:tt)*
89 ) => {
90 pub struct $struct {
91 $(
92 pub $x: Option<$y>
93 ),*
94 }
95
96 impl $struct {
97 pub const fn new_const() -> Self {
98 Self {
99 $(
100 $x: None
101 ),*
102 }
103 }
104
105 pub fn new(
106 $(
107 $x: $y
108 ),*
109 ) -> Self {
110 Self {
111 $(
112 $x: Some($x)
113 ),*
114 }
115 }
116 }
117
118 impl interpreter::RuntimeValue for $struct {
119 $(
120 $t
121 )*
122 }
123 };
124}
125
126#[macro_export]
127macro_rules! document {
128 ($($x:tt)*) => {
129 stringify!($($x)*)
130 }
131}
132
133#[macro_export]
134macro_rules! function {
135 ($name:literal, $($x:tt)*) => {
136 {
137 (
138 $name,
139 $($x)*
140 )
141 }
142 };
143}
144
145#[macro_export]
146macro_rules! hashmap {
147 ($($key:expr => $value:expr),* $(,)?) => {{
148 let mut map = std::collections::HashMap::new();
149 $( map.insert($key, $value); )*
150 map
151 }};
152}
153
154#[macro_export]
155macro_rules! parse {
156 ($file:ident + $heap:ident + $args:ident:) => {};
157
158 ($file:ident + $heap:ident + $args:ident: $($x:tt $y:ident),*) => {
159 #[allow(unused_variables)]
160 let [_, $($y),*] = &(unsafe { &*$args })[..] else {
161 interpreter::error("Invalid Format!", $file);
162 };
163
164 $(
165 let $y = unsafe { &**$y };
166 )*
167
168 $(interpreter::modify!($file + $heap: $x $y);)*;
169 };
170}
171
172#[macro_export]
173macro_rules! modify {
174 ($file:ident + $heap:ident: -> $y:ident) => {
175 let $y = {
176 let $heap: &mut interpreter::types::HeapWrapper = unsafe { &mut *(&mut $heap as *mut _) };
177 let Some(Some($y)) = $heap.remove($y) else {
178 interpreter::error("Could not obtain Varible!", $file);
179 };
180
181 $y as interpreter::types::BufValue
182 };
183 };
184
185 ($file:ident + $heap:ident: & $y:ident) => {
186 let $y = {
187 let $heap: &mut interpreter::types::HeapWrapper = unsafe { &mut *(&mut $heap as *mut _) };
188 let Some($y) = $heap.get($y) else {
189 interpreter::error("Varible not found!", $file);
190 };
191
192 $y as &interpreter::types::BufValue
193 };
194 };
195
196 ($file:ident + $heap:ident: mut $y:ident) => {
197 let $y = {
198 let $heap: &mut interpreter::types::HeapWrapper = unsafe { &mut *(&mut $heap as *mut _) };
199 let Some($y) = $heap.get_mut($y) else {
200 interpreter::error("Varible not found!", $file);
201 };
202 let $y = $y as *mut interpreter::types::BufValue;
203 let $y = unsafe { &mut *$y };
204
205 $y
206 };
207 };
208 ($file:ident + $heap:ident: str $y:ident) => {};
209}
210
211#[macro_export]
212macro_rules! get_as {
213 ($file:ident + $heap:ident: $ty:ident $y:ident) => {
214 let interpreter::types::BufValue::$ty($y) = $y else {
215 interpreter::error("Variable not using the expected type!", $file);
216 };
217 };
218}
219
220#[macro_export]
221macro_rules! get_mut {
222 ($file:ident + $heap:ident: mut $y:ident) => {
223 let Some($y) = $heap.get_mut($y) else {
224 interpreter::error("get_mut!: Varible not found!", $file);
225 };
226 };
227
228 ($file:ident + $heap:ident: $ty:ident $y:ident) => {
229 let Some(interpreter::types::BufValue::$ty($y)) = $heap.get_mut($y) else {
230 interpreter::error("get_mut!: Varible not found for the given type!", $file);
231 };
232 };
233}