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
#![allow(renamed_and_removed_lints, clippy::unknown_clippy_lints)]
use arithmetic_parser::{grammars::Grammar, Block};
use core::{cmp::Ordering, fmt};
use crate::{fns, Environment, Error, ExecutableModule, ModuleId, ModuleImports, Value};
pub trait VariableMap<'a, T> {
fn get_variable(&self, name: &str) -> Option<Value<'a, T>>;
fn compile_module<Id, G>(
&self,
id: Id,
block: &Block<'a, G>,
) -> Result<ExecutableModule<'a, T>, Error<'a>>
where
Id: ModuleId,
G: Grammar<'a, Lit = T>,
T: Clone + fmt::Debug,
{
ExecutableModule::builder(id, block)?
.with_imports_from(self)
.try_build()
}
}
impl<'a, T: Clone> VariableMap<'a, T> for Environment<'a, T> {
fn get_variable(&self, name: &str) -> Option<Value<'a, T>> {
self.get(name).cloned()
}
}
impl<'a, T: Clone> VariableMap<'a, T> for ModuleImports<'a, T> {
fn get_variable(&self, name: &str) -> Option<Value<'a, T>> {
self.get(name).cloned()
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct Prelude;
impl Prelude {
#[allow(clippy::missing_panics_doc)]
pub fn iter<T: Clone>(self) -> impl Iterator<Item = (&'static str, Value<'static, T>)> {
const VAR_NAMES: &[&str] = &[
"false", "true", "if", "loop", "while", "map", "filter", "fold", "push", "merge",
];
VAR_NAMES
.iter()
.map(move |&var_name| (var_name, self.get_variable(var_name).unwrap()))
}
}
impl<'a, T: Clone> VariableMap<'a, T> for Prelude {
fn get_variable(&self, name: &str) -> Option<Value<'a, T>> {
Some(match name {
"false" => Value::Bool(false),
"true" => Value::Bool(true),
"if" => Value::native_fn(fns::If),
"loop" => Value::native_fn(fns::Loop),
"while" => Value::native_fn(fns::While),
"map" => Value::native_fn(fns::Map),
"filter" => Value::native_fn(fns::Filter),
"fold" => Value::native_fn(fns::Fold),
"push" => Value::native_fn(fns::Push),
"merge" => Value::native_fn(fns::Merge),
_ => return None,
})
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct Assertions;
impl Assertions {
#[allow(clippy::missing_panics_doc)]
pub fn iter<T: fmt::Display>(self) -> impl Iterator<Item = (&'static str, Value<'static, T>)> {
const VAR_NAMES: &[&str] = &["assert", "assert_eq"];
VAR_NAMES
.iter()
.map(move |&var_name| (var_name, self.get_variable(var_name).unwrap()))
}
}
impl<'a, T: fmt::Display> VariableMap<'a, T> for Assertions {
fn get_variable(&self, name: &str) -> Option<Value<'a, T>> {
Some(match name {
"assert" => Value::native_fn(fns::Assert),
"assert_eq" => Value::native_fn(fns::AssertEq),
_ => return None,
})
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct Comparisons;
impl<'a, T> VariableMap<'a, T> for Comparisons {
fn get_variable(&self, name: &str) -> Option<Value<'a, T>> {
Some(match name {
"LESS" => Value::opaque_ref(Ordering::Less),
"EQUAL" => Value::opaque_ref(Ordering::Equal),
"GREATER" => Value::opaque_ref(Ordering::Greater),
"cmp" => Value::native_fn(fns::Compare::Raw),
"min" => Value::native_fn(fns::Compare::Min),
"max" => Value::native_fn(fns::Compare::Max),
_ => return None,
})
}
}
impl Comparisons {
#[allow(clippy::missing_panics_doc)]
pub fn iter<T>(self) -> impl Iterator<Item = (&'static str, Value<'static, T>)> {
const VAR_NAMES: &[&str] = &["LESS", "EQUAL", "GREATER", "cmp", "min", "max"];
VAR_NAMES
.iter()
.map(move |&var_name| (var_name, self.get_variable(var_name).unwrap()))
}
}