-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmod.rs
More file actions
313 lines (265 loc) · 9.06 KB
/
mod.rs
File metadata and controls
313 lines (265 loc) · 9.06 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
use std::collections::HashMap;
use std::fmt::Display;
use anathema_state::Hex;
use crate::primitives::Primitive;
use crate::variables::VarId;
pub(crate) mod eval;
pub(crate) mod parser;
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Op {
Add,
Sub,
Div,
Mul,
Mod,
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Equality {
Eq,
NotEq,
Gt,
Gte,
Lt,
Lte,
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum LogicalOp {
And,
Or,
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum BoolOp {
Eq,
NotEq,
And,
Or,
Gt,
Gte,
Lt,
Lte,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Expression {
// Value types
Primitive(Primitive),
Str(String),
List(Vec<Self>),
Map(HashMap<String, Self>),
// Lookup a variable in the var table
Variable(VarId),
// This is specifically for the "value" of a node.
TextSegments(Vec<Self>),
// Unary
Not(Box<Self>),
Negative(Box<Self>),
// Conditionals
Equality(Box<Self>, Box<Self>, Equality),
LogicalOp(Box<Self>, Box<Self>, LogicalOp),
// Lookup
Ident(String),
Index(Box<Self>, Box<Self>),
// Operations
Op(Box<Self>, Box<Self>, Op),
// Either
Either(Box<Self>, Box<Self>),
// Range
Range(Box<Self>, Box<Self>),
// Function call
Call { fun: Box<Self>, args: Vec<Self> },
}
impl From<Box<Expression>> for Expression {
fn from(value: Box<Expression>) -> Self {
*value
}
}
impl<T: Into<Primitive>> From<T> for Expression {
fn from(value: T) -> Self {
Self::Primitive(value.into())
}
}
impl From<&str> for Expression {
fn from(value: &str) -> Self {
Self::Str(value.into())
}
}
impl Display for Expression {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Primitive(val) => write!(f, "{val}"),
Self::Str(val) => write!(f, "{val}"),
Self::Ident(s) => write!(f, "{s}"),
Self::Index(lhs, idx) => write!(f, "<{lhs}>[{idx}]"),
Self::Variable(var_id) => write!(f, "<var {var_id:?}>"),
Self::Not(expr) => write!(f, "!{expr}"),
Self::Negative(expr) => write!(f, "-{expr}"),
Self::Op(lhs, rhs, op) => {
let op = match op {
Op::Add => '+',
Op::Sub => '-',
Op::Div => '/',
Op::Mul => '*',
Op::Mod => '%',
};
write!(f, "{lhs} {op} {rhs}")
}
Self::Either(lhs, rhs) => write!(f, "{lhs} ? {rhs}"),
Self::Range(lhs, rhs) => write!(f, "{lhs} .. {rhs}"),
Self::List(list) => {
write!(
f,
"[{}]",
list.iter().map(|val| val.to_string()).collect::<Vec<_>>().join(", ")
)
}
Self::TextSegments(segments) => {
write!(
f,
"{}",
segments.iter().map(|val| val.to_string()).collect::<Vec<_>>().join("")
)
}
Self::Map(map) => {
write!(
f,
"{{{}}}",
map.iter()
.map(|(key, val)| format!("{key}: {val}"))
.collect::<Vec<_>>()
.join(", ")
)
}
Self::Equality(lhs, rhs, equality) => {
let equality = match equality {
Equality::Eq => "==",
Equality::NotEq => "!=",
Equality::Gt => ">",
Equality::Gte => ">=",
Equality::Lt => "<",
Equality::Lte => "<=",
};
write!(f, "{lhs} {equality} {rhs}")
}
Self::LogicalOp(lhs, rhs, op) => {
let op = match op {
LogicalOp::And => "&&",
LogicalOp::Or => "||",
};
write!(f, "{lhs} {op} {rhs}")
}
Self::Call { fun, args } => {
write!(
f,
"{fun}({})",
args.iter().map(|val| val.to_string()).collect::<Vec<_>>().join(", ")
)
}
}
}
}
// -----------------------------------------------------------------------------
// - Paths -
// -----------------------------------------------------------------------------
pub fn ident(p: &str) -> Box<Expression> {
Expression::Ident(p.into()).into()
}
pub fn index(lhs: Box<Expression>, rhs: Box<Expression>) -> Box<Expression> {
Expression::Index(lhs, rhs).into()
}
// -----------------------------------------------------------------------------
// - Either -
// -----------------------------------------------------------------------------
pub fn either(lhs: Box<Expression>, rhs: Box<Expression>) -> Box<Expression> {
Expression::Either(lhs, rhs).into()
}
pub fn range(lhs: Box<Expression>, rhs: Box<Expression>) -> Box<Expression> {
Expression::Range(lhs, rhs).into()
}
// -----------------------------------------------------------------------------
// - Maths -
// -----------------------------------------------------------------------------
pub fn mul(lhs: Box<Expression>, rhs: Box<Expression>) -> Box<Expression> {
Expression::Op(lhs, rhs, Op::Mul).into()
}
pub fn div(lhs: Box<Expression>, rhs: Box<Expression>) -> Box<Expression> {
Expression::Op(lhs, rhs, Op::Div).into()
}
pub fn modulo(lhs: Box<Expression>, rhs: Box<Expression>) -> Box<Expression> {
Expression::Op(lhs, rhs, Op::Mod).into()
}
pub fn sub(lhs: Box<Expression>, rhs: Box<Expression>) -> Box<Expression> {
Expression::Op(lhs, rhs, Op::Sub).into()
}
pub fn add(lhs: Box<Expression>, rhs: Box<Expression>) -> Box<Expression> {
Expression::Op(lhs, rhs, Op::Add).into()
}
pub fn greater_than(lhs: Box<Expression>, rhs: Box<Expression>) -> Box<Expression> {
Expression::Equality(lhs, rhs, Equality::Gt).into()
}
pub fn greater_than_equal(lhs: Box<Expression>, rhs: Box<Expression>) -> Box<Expression> {
Expression::Equality(lhs, rhs, Equality::Gte).into()
}
pub fn less_than(lhs: Box<Expression>, rhs: Box<Expression>) -> Box<Expression> {
Expression::Equality(lhs, rhs, Equality::Lt).into()
}
pub fn less_than_equal(lhs: Box<Expression>, rhs: Box<Expression>) -> Box<Expression> {
Expression::Equality(lhs, rhs, Equality::Lte).into()
}
// -----------------------------------------------------------------------------
// - Values -
// -----------------------------------------------------------------------------
pub fn num(int: i64) -> Box<Expression> {
Expression::Primitive(int.into()).into()
}
pub fn float(float: f64) -> Box<Expression> {
Expression::Primitive(float.into()).into()
}
pub fn chr(c: char) -> Box<Expression> {
Expression::Primitive(c.into()).into()
}
pub fn hex(h: impl Into<Hex>) -> Box<Expression> {
let h = h.into();
Expression::Primitive(h.into()).into()
}
pub fn boolean(b: bool) -> Box<Expression> {
Expression::Primitive(b.into()).into()
}
pub fn strlit(lit: &str) -> Box<Expression> {
Expression::Str(lit.into()).into()
}
// -----------------------------------------------------------------------------
// - List, map and text segment -
// -----------------------------------------------------------------------------
pub fn list<E: Into<Expression>>(input: impl IntoIterator<Item = E>) -> Box<Expression> {
let vec = input.into_iter().map(|val| val.into()).collect::<Vec<_>>();
Expression::List(vec).into()
}
pub fn text_segments<E: Into<Expression>>(input: impl IntoIterator<Item = E>) -> Box<Expression> {
let vec = input.into_iter().map(|val| val.into()).collect::<Vec<_>>();
Expression::TextSegments(vec).into()
}
pub fn map<E: Into<Expression>>(input: impl IntoIterator<Item = (&'static str, E)>) -> Box<Expression> {
let input = input.into_iter().map(|(k, v)| (k.into(), v.into()));
let hm: HashMap<String, Expression> = HashMap::from_iter(input);
Expression::Map(hm).into()
}
// -----------------------------------------------------------------------------
// - Op -
// -----------------------------------------------------------------------------
pub fn neg(expr: Box<Expression>) -> Box<Expression> {
Expression::Negative(expr).into()
}
// -----------------------------------------------------------------------------
// - Conditionals -
// -----------------------------------------------------------------------------
pub fn not(expr: Box<Expression>) -> Box<Expression> {
Expression::Not(expr).into()
}
pub fn eq(lhs: Box<Expression>, rhs: Box<Expression>) -> Box<Expression> {
Expression::Equality(lhs, rhs, Equality::Eq).into()
}
pub fn and(lhs: Box<Expression>, rhs: Box<Expression>) -> Box<Expression> {
Expression::LogicalOp(lhs, rhs, LogicalOp::And).into()
}
pub fn or(lhs: Box<Expression>, rhs: Box<Expression>) -> Box<Expression> {
Expression::LogicalOp(lhs, rhs, LogicalOp::Or).into()
}