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
|
use std::collections::HashMap;
use std::convert::TryInto;
use std::sync::mpsc::{SyncSender, Receiver};
use crate::actor::{Value, Type, Actorful, Slot};
use crate::number::Number;
use crate::world::World;
#[derive(Clone)]
pub struct Constant {
pub r#type: Type,
pub value: Value,
}
impl Actorful for Constant {
fn inputs(&self) -> Vec<Slot> {
vec![]
}
fn outputs(&self) -> Vec<Slot> {
vec![Slot { name: "Value".to_string(), r#type: self.r#type.clone() }]
}
fn launch(&self, _input_channels: HashMap<String, Receiver<Value>>,
output_channels: HashMap<String, SyncSender<Value>>, _world: &mut World) -> Box<dyn FnOnce() + Send> {
let value = self.value.clone();
Box::new(move || {
loop {
let _ = output_channels["Value"].try_send(value.clone());
}
})
}
fn boxed_clone(&self) -> Box<dyn Actorful + Send> {
Box::new(self.clone())
}
}
#[derive(Clone)]
pub struct Add;
impl Actorful for Add {
fn inputs(&self) -> Vec<Slot> {
vec![
Slot { name: "N1".to_string(), r#type: Type::AnyNumber },
Slot { name: "N2".to_string(), r#type: Type::AnyNumber },
]
}
fn outputs(&self) -> Vec<Slot> {
vec![Slot { name: "Result".to_string(), r#type: Type::AnyNumber }]
}
fn launch(&self, mut input_channels: HashMap<String, Receiver<Value>>,
output_channels: HashMap<String, SyncSender<Value>>, _world: &mut World) -> Box<dyn FnOnce() + Send> {
let n1 = input_channels.remove("N1").unwrap();
let n2 = input_channels.remove("N2").unwrap();
Box::new(move || {
for (n1, n2) in n1.iter().zip(n2.iter()) {
if let (Value::Number(n1), Value::Number(n2)) = (n1, n2) {
let _ = output_channels["Result"].try_send(Value::Number(n1 + n2));
}
}
})
}
fn boxed_clone(&self) -> Box<dyn Actorful + Send> {
Box::new(self.clone())
}
}
#[derive(Clone)]
pub struct Multiply;
impl Actorful for Multiply {
fn inputs(&self) -> Vec<Slot> {
vec![
Slot { name: "N1".to_string(), r#type: Type::AnyNumber },
Slot { name: "N2".to_string(), r#type: Type::AnyNumber },
]
}
fn outputs(&self) -> Vec<Slot> {
vec![Slot { name: "Result".to_string(), r#type: Type::AnyNumber }]
}
fn launch(&self, mut input_channels: HashMap<String, Receiver<Value>>,
output_channels: HashMap<String, SyncSender<Value>>, _world: &mut World) -> Box<dyn FnOnce() + Send> {
let n1 = input_channels.remove("N1").unwrap();
let n2 = input_channels.remove("N2").unwrap();
Box::new(move || {
for (n1, n2) in n1.iter().zip(n2.iter()) {
if let (Value::Number(n1), Value::Number(n2)) = (n1, n2) {
let _ = output_channels["Result"].try_send(Value::Number(n1 * n2));
}
}
})
}
fn boxed_clone(&self) -> Box<dyn Actorful + Send> {
Box::new(self.clone())
}
}
#[derive(Clone)]
pub struct RepeatValue {
pub r#type: Type,
}
impl Actorful for RepeatValue {
fn inputs(&self) -> Vec<Slot> {
let nonnegative_integer = Type::NumberInRange {
min: Some(Number::from(0)),
max: None,
};
vec![
Slot { name: "Value".to_string(), r#type: self.r#type.clone() },
Slot { name: "Count".to_string(), r#type: nonnegative_integer },
]
}
fn outputs(&self) -> Vec<Slot> {
vec![Slot { name: "List".to_string(), r#type: Type::List { contents: Box::new(self.r#type.clone()) } }]
}
fn launch(&self, mut input_channels: HashMap<String, Receiver<Value>>,
output_channels: HashMap<String, SyncSender<Value>>, _world: &mut World) -> Box<dyn FnOnce() + Send> {
let value = input_channels.remove("Value").unwrap();
let count = input_channels.remove("Count").unwrap();
Box::new(move || {
for (value, count) in value.iter().zip(count.iter()) {
if let Value::Number(count) = count {
let count: usize = count.try_into().unwrap();
let vec = vec![value; count];
let _ = output_channels["List"].try_send(Value::List(vec));
}
}
})
}
fn boxed_clone(&self) -> Box<dyn Actorful + Send> {
Box::new(self.clone())
}
}
#[derive(Clone)]
pub struct SetListItem {
pub r#type: Type,
}
impl Actorful for SetListItem {
fn inputs(&self) -> Vec<Slot> {
let nonnegative_integer = Type::NumberInRange {
min: Some(Number::from(0)),
max: None,
};
vec![
Slot { name: "List".to_string(), r#type: Type::List { contents: Box::new(self.r#type.clone()) } },
Slot { name: "Index".to_string(), r#type: nonnegative_integer },
Slot { name: "Value".to_string(), r#type: self.r#type.clone() },
]
}
fn outputs(&self) -> Vec<Slot> {
vec![Slot { name: "List".to_string(), r#type: Type::List { contents: Box::new(self.r#type.clone()) } }]
}
fn launch(&self, mut input_channels: HashMap<String, Receiver<Value>>,
output_channels: HashMap<String, SyncSender<Value>>, _world: &mut World) -> Box<dyn FnOnce() + Send> {
let list = input_channels.remove("List").unwrap();
let index = input_channels.remove("Index").unwrap();
let value = input_channels.remove("Value").unwrap();
Box::new(move || {
for ((list, index), value) in list.iter().zip(index.iter()).zip(value.iter()) {
if let (Value::List(mut list), Value::Number(index)) = (list, index) {
let index: usize = index.try_into().unwrap();
list[index] = value;
let _ = output_channels["List"].try_send(Value::List(list));
}
}
})
}
fn boxed_clone(&self) -> Box<dyn Actorful + Send> {
Box::new(self.clone())
}
}
#[derive(Clone)]
pub struct DeconstructRecord {
pub record_type: Type,
}
impl Actorful for DeconstructRecord {
fn inputs(&self) -> Vec<Slot> {
vec![Slot { name: "Record".to_string(), r#type: self.record_type.clone() }]
}
fn outputs(&self) -> Vec<Slot> {
if let Type::Record { name: _, fields } = &self.record_type {
fields.clone()
} else {
panic!("bruh that's the wrong goddamn type")
}
}
fn launch(&self, mut input_channels: HashMap<String, Receiver<Value>>,
output_channels: HashMap<String, SyncSender<Value>>, _world: &mut World) -> Box<dyn FnOnce() + Send> {
let record = input_channels.remove("Record").unwrap();
Box::new(move || {
for record in record.iter() {
if let Value::Record(fields) = record {
for (label, value) in fields {
let _ = output_channels[&label].try_send(value);
}
}
}
})
}
fn boxed_clone(&self) -> Box<dyn Actorful + Send> {
Box::new(self.clone())
}
}
|