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
#![allow(clippy::await_holding_refcell_ref)]
#![allow(clippy::collapsible_else_if)]
#![warn(anonymous_parameters, bad_style, missing_docs)]
#![warn(unused, unused_extern_crates, unused_import_braces, unused_qualifications)]
#![warn(unsafe_code)]
use endbasic_core::exec::{Machine, Result};
use std::cell::RefCell;
use std::rc::Rc;
pub mod arrays;
pub mod console;
pub mod exec;
pub mod gfx;
pub mod gpio;
pub mod help;
pub mod numerics;
pub mod program;
pub mod service;
pub mod storage;
pub mod strings;
pub mod testutils;
#[derive(Default)]
pub struct MachineBuilder {
console: Option<Rc<RefCell<dyn console::Console>>>,
gpio_pins: Option<Rc<RefCell<dyn gpio::Pins>>>,
sleep_fn: Option<exec::SleepFn>,
}
impl MachineBuilder {
pub fn with_console(mut self, console: Rc<RefCell<dyn console::Console>>) -> Self {
self.console = Some(console);
self
}
pub fn with_gpio_pins(mut self, pins: Rc<RefCell<dyn gpio::Pins>>) -> Self {
self.gpio_pins = Some(pins);
self
}
pub fn with_sleep_fn(mut self, sleep_fn: exec::SleepFn) -> Self {
self.sleep_fn = Some(sleep_fn);
self
}
pub fn get_console(&mut self) -> Rc<RefCell<dyn console::Console>> {
if self.console.is_none() {
self.console = Some(Rc::from(RefCell::from(console::TrivialConsole::default())));
}
self.console.clone().unwrap()
}
fn get_gpio_pins(&mut self) -> Rc<RefCell<dyn gpio::Pins>> {
if self.gpio_pins.is_none() {
self.gpio_pins = Some(Rc::from(RefCell::from(gpio::NoopPins::default())))
}
self.gpio_pins.as_ref().expect("Must have been initialized above").clone()
}
pub fn build(mut self) -> Result<Machine> {
let mut machine = Machine::default();
arrays::add_all(&mut machine);
console::add_all(&mut machine, self.get_console());
gfx::add_all(&mut machine, self.get_console());
gpio::add_all(&mut machine, self.get_gpio_pins());
exec::add_all(&mut machine, self.sleep_fn);
numerics::add_all(&mut machine);
strings::add_all(&mut machine);
Ok(machine)
}
pub fn make_interactive(self) -> InteractiveMachineBuilder {
InteractiveMachineBuilder::from(self)
}
}
pub struct InteractiveMachineBuilder {
builder: MachineBuilder,
program: Option<Rc<RefCell<dyn program::Program>>>,
storage: Rc<RefCell<storage::Storage>>,
service: Option<Rc<RefCell<dyn service::Service>>>,
}
impl InteractiveMachineBuilder {
fn from(builder: MachineBuilder) -> Self {
let storage = Rc::from(RefCell::from(storage::Storage::default()));
InteractiveMachineBuilder { builder, program: None, storage, service: None }
}
pub fn get_program(&mut self) -> Rc<RefCell<dyn program::Program>> {
if self.program.is_none() {
self.program = Some(Rc::from(RefCell::from(program::ImmutableProgram::default())));
}
self.program.clone().unwrap()
}
pub fn get_storage(&mut self) -> Rc<RefCell<storage::Storage>> {
self.storage.clone()
}
pub fn with_program(mut self, program: Rc<RefCell<dyn program::Program>>) -> Self {
self.program = Some(program);
self
}
pub fn with_service(mut self, service: Rc<RefCell<dyn service::Service>>) -> Self {
self.service = Some(service);
self
}
pub fn build(mut self) -> Result<Machine> {
let console = self.builder.get_console();
let program = self.get_program();
let storage = self.get_storage();
let mut machine = self.builder.build()?;
let service = match self.service {
Some(service) => service,
None => Rc::from(RefCell::from(service::CloudService::default())),
};
help::add_all(&mut machine, console.clone());
program::add_all(&mut machine, program, console.clone(), storage.clone());
service::add_all(&mut machine, service, console.clone(), storage.clone());
storage::add_all(&mut machine, console, storage);
Ok(machine)
}
}