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
use std::{cell::RefCell, rc::Rc};

use crate::scope::{Scope, ScopeRef};

#[macro_export]
macro_rules! arg_check {
	($arg: expr, $t: pat => $e: literal ) => {
		let $t = $arg else {
			panic!($e, $arg.get_type().to_string())
		};
	};
}

#[macro_export]
macro_rules! pat_check {
	($pat: pat = $value: expr) => {
		if let $pat = $value {
			true
		} else {
			false
		}
	};
}

#[macro_export]
macro_rules! as_type {
	($expr: expr => $t: ty, $err: literal) => {
		match $expr.as_any().downcast_ref::<$t>() {
			Some(obj) => obj,
			None => panic!($err),
		}
	};
}

#[macro_export]
macro_rules! as_mut_type {
	($expr: expr => $t: ty, $err: literal) => {
		match $expr.as_mut().downcast_mut::<$t>() {
			Some(obj) => obj,
			None => panic!($err),
		}
	};
}

pub fn make_ref<T: Scope + 'static>(scope: T) -> ScopeRef {
	Rc::new(RefCell::new(scope))
}