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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use std::{cell::RefCell, hash::Hash};

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

pub enum DataType {
	Boolean,
	Number,
	String,
	Memory,
	Scope,
	None,
	Or(Box<DataType>, Box<DataType>),
	Any,
}

impl DataType {
	pub fn from_string(string: &String) -> DataType {
		if string == "boolean" {
			DataType::Boolean
		} else if string == "number" {
			DataType::Number
		} else if string == "string" {
			DataType::String
		} else if string == "memory" {
			DataType::Memory
		} else if string == "scope" {
			DataType::Scope
		} else if string == "none" {
			DataType::None
		} else if string == "any" {
			DataType::Any
		} else {
			panic!("Invalid type string '{}'.", string)
		}
	}

	pub fn to_string(&self) -> String {
		match self {
			DataType::Boolean => String::from("boolean"),
			DataType::Number => String::from("number"),
			DataType::String => String::from("string"),
			DataType::Memory => String::from("memory"),
			DataType::Scope => String::from("scope"),
			DataType::None => String::from("none"),
			DataType::Any => String::from("any"),
			DataType::Or(a, b) => a.to_string() + " | " + &b.to_string(),
		}
	}

	pub fn matches(&self, data: &Data) -> bool {
		match self {
			DataType::Boolean => pat_check!(Data::Boolean(_) = data),
			DataType::Number => pat_check!(Data::Number(_) = data),
			DataType::String => pat_check!(Data::String(_) = data),
			DataType::Memory => pat_check!(Data::Memory { .. } = data),
			DataType::Scope => pat_check!(Data::Scope(_) = data),
			DataType::None => pat_check!(Data::None = data),
			DataType::Or(a, b) => a.matches(data) || b.matches(data),
			DataType::Any => true,
		}
	}
}

#[derive(Clone, Debug)]
pub enum Data {
	Boolean(bool),
	Number(f64),
	String(String),
	Memory { scope: ScopeRef, name: String },
	Scope(ScopeRef),
	None,
}

impl Data {
	pub fn to_string(&self) -> String {
		match self {
			Data::Boolean(v) => {
				if *v {
					String::from("true")
				} else {
					String::from("false")
				}
			}
			Data::Number(v) => v.to_string(),
			Data::String(s) => s.clone(),
			Data::Memory { scope: _, name } => format!("<{}>", name),
			Data::Scope(scope) => RefCell::borrow(&scope).to_string(),
			Data::None => String::from("[none]"),
		}
	}

	pub fn get_type(&self) -> DataType {
		match self {
			Data::Boolean(_) => DataType::Boolean,
			Data::Number(_) => DataType::Number,
			Data::String(_) => DataType::String,
			Data::Memory { .. } => DataType::Memory,
			Data::Scope(_) => DataType::Scope,
			Data::None => DataType::None,
		}
	}
}

impl PartialEq for Data {
	fn eq(&self, other: &Self) -> bool {
		match (self, other) {
			(Self::Boolean(l0), Self::Boolean(r0)) => l0 == r0,
			(Self::Number(l0), Self::Number(r0)) => l0 == r0,
			(Self::String(l0), Self::String(r0)) => l0 == r0,
			(Self::None, Self::None) => true,
			_ => false,
		}
	}
}

impl Default for Data {
	fn default() -> Self {
		Data::None
	}
}

#[derive(Clone, Debug, PartialEq)]
pub struct StaticData(Data);

impl StaticData {
	pub fn from(data: Data) -> Self {
		match data {
			Data::Memory { .. } => panic!("Tried to cast type memory as static."),
			Data::Scope(_) => panic!("Tried to cast type scope as static."),
			_ => Self(data),
		}
	}

	pub fn inner(&self) -> &Data {
		&self.0
	}
}

impl Eq for StaticData {}

impl Hash for StaticData {
	fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
		self.0.get_type().to_string().hash(state);
		match &self.0 {
			Data::Boolean(v) => v.hash(state),
			Data::Number(v) => v.to_string().hash(state),
			Data::String(v) => v.hash(state),
			_ => (),
		};
	}
}