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
#![allow(dead_code)]
#![allow(unused_macros)]
mod runtime;
mod lang;
mod convert;
mod random;
mod util;
mod collections;
mod stdlib;
mod value;
pub mod compiler;
pub use collections::*;
pub use value::*;
pub use convert::*;
use crate::lang::{Sequence};
use crate::compiler::{RantCompiler, Reporter, ErrorKind as CompilerErrorKind};
use crate::runtime::*;
use std::{path::Path, rc::Rc, cell::RefCell};
use random::RantRng;
pub const BUILD_VERSION: &str = env!("CARGO_PKG_VERSION");
pub const RANT_VERSION: &str = "4.0";
pub(crate) type RantString = smartstring::alias::CompactString;
#[derive(Debug)]
pub struct Rant {
rng: Rc<RantRng>,
debug_mode: bool,
globals: RantMapRef,
}
impl Rant {
pub fn new() -> Self {
Self::with_seed(0)
}
pub fn with_seed(seed: u64) -> Self {
Self::with_options(RantOptions {
seed,
.. Default::default()
})
}
pub fn with_options(options: RantOptions) -> Self {
let mut rant = Self {
debug_mode: options.debug_mode,
globals: Rc::new(RefCell::new(RantMap::new())),
rng: Rc::new(RantRng::new(options.seed))
};
if options.use_stdlib {
rant.load_stdlib();
rant.set_default_globals();
}
rant
}
fn load_stdlib(&mut self) {
let mut globals = self.globals.borrow_mut();
stdlib::load_stdlib(&mut globals);
}
fn set_default_globals(&mut self) {
let mut globals = self.globals.borrow_mut();
globals.raw_set("RANT_VERSION", RantValue::String(RANT_VERSION.to_owned()));
}
}
impl Default for Rant {
fn default() -> Self {
Self::new()
}
}
impl Rant {
#[must_use = "compiling a program without storing or running it achieves nothing"]
pub fn compile<R: Reporter>(&self, source: &str, reporter: &mut R) -> Result<RantProgram, CompilerErrorKind> {
RantCompiler::compile_string(source, reporter, self.debug_mode)
}
#[must_use = "compiling a program without storing or running it achieves nothing"]
pub fn compile_quiet(&self, source: &str) -> Result<RantProgram, CompilerErrorKind> {
RantCompiler::compile_string(source, &mut (), self.debug_mode)
}
#[must_use = "compiling a program without storing or running it achieves nothing"]
pub fn compile_file<P: AsRef<Path>, R: Reporter>(&self, path: P, reporter: &mut R) -> Result<RantProgram, CompilerErrorKind> {
RantCompiler::compile_file(path, reporter, self.debug_mode)
}
#[must_use = "compiling a program without storing or running it achieves nothing"]
pub fn compile_file_quiet<P: AsRef<Path>>(&self, path: P) -> Result<RantProgram, CompilerErrorKind> {
RantCompiler::compile_file(path, &mut (), self.debug_mode)
}
pub fn globals(&self) -> RantMapRef {
Rc::clone(&self.globals)
}
pub fn set_global(&mut self, key: &str, value: RantValue) {
self.globals.borrow_mut().raw_set(key, value);
}
pub fn get_global(&self, key: &str) -> Option<RantValue> {
self.globals.borrow().raw_get(key).cloned()
}
pub fn seed(&self) -> u64 {
self.rng.seed()
}
pub fn set_seed(&mut self, seed: u64) {
self.rng = Rc::new(RantRng::new(seed));
}
pub fn reset_seed(&mut self) {
let seed = self.rng.seed();
self.rng = Rc::new(RantRng::new(seed));
}
pub fn run(&mut self, program: &RantProgram) -> RuntimeResult<RantValue> {
let mut vm = VM::new(self.rng.clone(), self, program);
vm.run()
}
pub fn run_into_string(&mut self, program: &RantProgram) -> RuntimeResult<String> {
let mut vm = VM::new(self.rng.clone(), self, program);
Ok(vm.run()?.to_string())
}
}
pub struct RantOptions {
pub use_stdlib: bool,
pub debug_mode: bool,
pub seed: u64,
}
impl Default for RantOptions {
fn default() -> Self {
Self {
use_stdlib: true,
debug_mode: false,
seed: 0,
}
}
}
#[derive(Debug)]
pub struct RantProgram {
name: Option<RantString>,
root: Rc<Sequence>
}
impl RantProgram {
pub(crate) fn new(root: Rc<Sequence>) -> Self {
Self {
name: None,
root,
}
}
pub fn with_name<S: ToString>(self, name: S) -> Self {
Self {
root: self.root,
name: Some(RantString::from(name.to_string()))
}
}
pub fn name(&self) -> Option<&str> {
self.name.as_ref().map(|s| s.as_str())
}
}