scheme4r 0.2.3

Scheme interpreter for rust
Documentation
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use std::{
    cell::RefCell,
    fs::{self, OpenOptions},
    io::Write,
    path::PathBuf,
    rc::Rc,
};

use crate::{
    error::SchemeError,
    reader::{Datum, Reader},
};

pub type PortRef = Rc<RefCell<Port>>;

#[derive(Clone, Debug)]
pub struct Port {
    kind: PortKind,
    open: bool,
}

#[derive(Clone, Debug)]
enum PortKind {
    Input { source: String, position: usize },
    InputByteVector { bytes: Vec<u8>, position: usize },
    OutputString { buffer: String },
    OutputByteVector { buffer: Vec<u8> },
    OutputFile { path: PathBuf },
    StdInput { source: String, position: usize },
    StdOutput,
}

impl Port {
    pub fn stdin() -> PortRef {
        Rc::new(RefCell::new(Self {
            kind: PortKind::StdInput {
                source: String::new(),
                position: 0,
            },
            open: true,
        }))
    }

    pub fn stdout() -> PortRef {
        Rc::new(RefCell::new(Self {
            kind: PortKind::StdOutput,
            open: true,
        }))
    }

    pub fn open_input_string(source: &str) -> Result<PortRef, SchemeError> {
        Ok(Rc::new(RefCell::new(Self {
            kind: PortKind::Input {
                source: source.to_string(),
                position: 0,
            },
            open: true,
        })))
    }

    pub fn open_output_string() -> PortRef {
        Rc::new(RefCell::new(Self {
            kind: PortKind::OutputString {
                buffer: String::new(),
            },
            open: true,
        }))
    }

    pub fn open_input_bytevector(bytes: &[u8]) -> PortRef {
        Rc::new(RefCell::new(Self {
            kind: PortKind::InputByteVector {
                bytes: bytes.to_vec(),
                position: 0,
            },
            open: true,
        }))
    }

    pub fn open_output_bytevector() -> PortRef {
        Rc::new(RefCell::new(Self {
            kind: PortKind::OutputByteVector { buffer: Vec::new() },
            open: true,
        }))
    }

    pub fn open_input_file(path: &str) -> Result<PortRef, SchemeError> {
        let source = fs::read_to_string(path)
            .map_err(|err| SchemeError::io(format!("failed to read '{path}': {err}")))?;
        Ok(Rc::new(RefCell::new(Self {
            kind: PortKind::Input {
                source,
                position: 0,
            },
            open: true,
        })))
    }

    pub fn open_output_file(path: &str) -> Result<PortRef, SchemeError> {
        fs::write(path, "")
            .map_err(|err| SchemeError::io(format!("failed to create '{path}': {err}")))?;
        Ok(Rc::new(RefCell::new(Self {
            kind: PortKind::OutputFile {
                path: PathBuf::from(path),
            },
            open: true,
        })))
    }

    pub fn read_datum(&mut self) -> Result<Option<Datum>, SchemeError> {
        self.ensure_open()?;
        match &mut self.kind {
            PortKind::Input { source, position } | PortKind::StdInput { source, position } => {
                let remaining = remaining_source(source, *position);
                match Reader::new(&remaining).read_one()? {
                    Some((datum, consumed)) => {
                        *position += consumed;
                        Ok(Some(datum))
                    }
                    None => Ok(None),
                }
            }
            _ => Err(SchemeError::type_error("expected an input port for 'read'")),
        }
    }

    pub fn read_char(&mut self) -> Result<Option<char>, SchemeError> {
        self.ensure_open()?;
        match &mut self.kind {
            PortKind::Input { source, position } | PortKind::StdInput { source, position } => {
                let ch = source.chars().nth(*position);
                if ch.is_some() {
                    *position += 1;
                }
                Ok(ch)
            }
            _ => Err(SchemeError::type_error(
                "expected an input port for character reading",
            )),
        }
    }

    pub fn peek_char(&mut self) -> Result<Option<char>, SchemeError> {
        self.ensure_open()?;
        match &mut self.kind {
            PortKind::Input { source, position } | PortKind::StdInput { source, position } => {
                Ok(source.chars().nth(*position))
            }
            _ => Err(SchemeError::type_error(
                "expected an input port for character reading",
            )),
        }
    }

    pub fn char_ready(&self) -> Result<bool, SchemeError> {
        self.ensure_open()?;
        match &self.kind {
            PortKind::Input { source, position } | PortKind::StdInput { source, position } => {
                Ok(source.chars().nth(*position).is_some())
            }
            _ => Err(SchemeError::type_error(
                "expected a textual input port for 'char-ready?'",
            )),
        }
    }

    pub fn read_string(&mut self, count: usize) -> Result<Option<String>, SchemeError> {
        self.ensure_open()?;
        match &mut self.kind {
            PortKind::Input { source, position } | PortKind::StdInput { source, position } => {
                if count == 0 {
                    return Ok(Some(String::new()));
                }
                let chars = source
                    .chars()
                    .skip(*position)
                    .take(count)
                    .collect::<Vec<_>>();
                if chars.is_empty() {
                    return Ok(None);
                }
                *position += chars.len();
                Ok(Some(chars.into_iter().collect()))
            }
            _ => Err(SchemeError::type_error(
                "expected a textual input port for 'read-string'",
            )),
        }
    }

    pub fn read_bytes(&mut self, count: usize) -> Result<Option<Vec<u8>>, SchemeError> {
        self.ensure_open()?;
        match &mut self.kind {
            PortKind::InputByteVector { bytes, position } => {
                if count == 0 {
                    return Ok(Some(Vec::new()));
                }
                if *position >= bytes.len() {
                    return Ok(None);
                }
                let end = (*position + count).min(bytes.len());
                let chunk = bytes[*position..end].to_vec();
                *position = end;
                Ok(Some(chunk))
            }
            _ => Err(SchemeError::type_error(
                "expected a binary input port for bytevector reading",
            )),
        }
    }

    pub fn read_u8(&mut self) -> Result<Option<u8>, SchemeError> {
        Ok(self
            .read_bytes(1)?
            .and_then(|bytes| bytes.into_iter().next()))
    }

    pub fn peek_u8(&mut self) -> Result<Option<u8>, SchemeError> {
        self.ensure_open()?;
        match &mut self.kind {
            PortKind::InputByteVector { bytes, position } => Ok(bytes.get(*position).copied()),
            _ => Err(SchemeError::type_error(
                "expected a binary input port for 'peek-u8'",
            )),
        }
    }

    pub fn u8_ready(&self) -> Result<bool, SchemeError> {
        self.ensure_open()?;
        match &self.kind {
            PortKind::InputByteVector { bytes, position } => Ok(bytes.get(*position).is_some()),
            _ => Err(SchemeError::type_error(
                "expected a binary input port for 'u8-ready?'",
            )),
        }
    }

    pub fn read_bytes_into(&mut self, target: &mut [u8]) -> Result<Option<usize>, SchemeError> {
        self.ensure_open()?;
        match &mut self.kind {
            PortKind::InputByteVector { bytes, position } => {
                if target.is_empty() {
                    return Ok(Some(0));
                }
                if *position >= bytes.len() {
                    return Ok(None);
                }
                let end = (*position + target.len()).min(bytes.len());
                let count = end - *position;
                target[..count].copy_from_slice(&bytes[*position..end]);
                *position = end;
                Ok(Some(count))
            }
            _ => Err(SchemeError::type_error(
                "expected a binary input port for bytevector reading",
            )),
        }
    }

    pub fn write_str(&mut self, text: &str) -> Result<(), SchemeError> {
        self.ensure_open()?;
        match &mut self.kind {
            PortKind::OutputString { buffer } => {
                buffer.push_str(text);
                Ok(())
            }
            PortKind::OutputFile { path } => {
                let mut file = OpenOptions::new()
                    .append(true)
                    .create(true)
                    .open(path.as_path())
                    .map_err(|err| {
                        SchemeError::io(format!(
                            "failed to open '{}' for append: {err}",
                            path.display()
                        ))
                    })?;
                file.write_all(text.as_bytes()).map_err(|err| {
                    SchemeError::io(format!("failed to write '{}': {err}", path.display()))
                })
            }
            PortKind::StdOutput => {
                print!("{text}");
                Ok(())
            }
            PortKind::OutputByteVector { .. } => Err(SchemeError::type_error(
                "expected a textual output port for string writing",
            )),
            _ => Err(SchemeError::type_error(
                "expected an output port for writing",
            )),
        }
    }

    pub fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), SchemeError> {
        self.ensure_open()?;
        match &mut self.kind {
            PortKind::OutputByteVector { buffer } => {
                buffer.extend_from_slice(bytes);
                Ok(())
            }
            _ => Err(SchemeError::type_error(
                "expected a binary output port for bytevector writing",
            )),
        }
    }

    pub fn flush(&mut self) -> Result<(), SchemeError> {
        self.ensure_open()?;
        match &self.kind {
            PortKind::OutputString { .. }
            | PortKind::OutputByteVector { .. }
            | PortKind::OutputFile { .. }
            | PortKind::StdOutput => Ok(()),
            _ => Err(SchemeError::type_error(
                "expected an output port for 'flush-output-port'",
            )),
        }
    }

    pub fn close(&mut self) {
        self.open = false;
    }

    pub fn is_input(&self) -> bool {
        matches!(
            self.kind,
            PortKind::Input { .. } | PortKind::InputByteVector { .. } | PortKind::StdInput { .. }
        )
    }

    pub fn is_output(&self) -> bool {
        matches!(
            self.kind,
            PortKind::OutputString { .. }
                | PortKind::OutputByteVector { .. }
                | PortKind::OutputFile { .. }
                | PortKind::StdOutput
        )
    }

    pub fn is_textual(&self) -> bool {
        matches!(
            self.kind,
            PortKind::Input { .. }
                | PortKind::OutputString { .. }
                | PortKind::OutputFile { .. }
                | PortKind::StdInput { .. }
                | PortKind::StdOutput
        )
    }

    pub fn is_binary(&self) -> bool {
        matches!(
            self.kind,
            PortKind::InputByteVector { .. } | PortKind::OutputByteVector { .. }
        )
    }

    pub fn is_open(&self) -> bool {
        self.open
    }

    pub fn output_string(&self) -> Result<String, SchemeError> {
        match &self.kind {
            PortKind::OutputString { buffer } => Ok(buffer.clone()),
            _ => Err(SchemeError::type_error(
                "'get-output-string' expected a string output port",
            )),
        }
    }

    pub fn output_bytevector(&self) -> Result<Vec<u8>, SchemeError> {
        match &self.kind {
            PortKind::OutputByteVector { buffer } => Ok(buffer.clone()),
            _ => Err(SchemeError::type_error(
                "'get-output-bytevector' expected a bytevector output port",
            )),
        }
    }

    pub fn display_name(&self) -> &'static str {
        match self.kind {
            PortKind::Input { .. } | PortKind::StdInput { .. } => "input-port",
            PortKind::InputByteVector { .. } => "binary-input-port",
            PortKind::OutputString { .. } | PortKind::OutputFile { .. } | PortKind::StdOutput => {
                "output-port"
            }
            PortKind::OutputByteVector { .. } => "binary-output-port",
        }
    }

    fn ensure_open(&self) -> Result<(), SchemeError> {
        if self.open {
            Ok(())
        } else {
            Err(SchemeError::io("port is closed"))
        }
    }
}

fn remaining_source(source: &str, position: usize) -> String {
    source.chars().skip(position).collect()
}