sbrain/lib.rs
1//! SBrain, or Semantic Brain, is a set of extensions to the famous language by Urban Müller
2//! designed to make it more amenable to genetic programming. Additions include a stack, a general-
3//! purpose register, and useful bitwise operations.
4//!
5//! This crate provides an implementation of the SBrain specification designed to be used for
6//! genetic programming. See the `specification` pseudomodule for the complete specification.
7//!
8//! Here's a quick example:
9//!
10//! ```
11//! # use sbrain::*;
12//! let program = source_to_tape(",[.,]");
13//! let mut input = make_input_vec(b"Hello, world!");
14//! let mut output = make_output_vec();
15//! SBrainVM::new(Some(&mut input), Some(&mut output), &program)
16//! .expect("Could not build machine")
17//! .run(Some(1000)).expect("I/O failed");
18//!
19//! let output = output.into_inner();
20//! assert_eq!(&output, b"Hello, world!")
21//! ```
22
23mod machine;
24mod source;
25pub mod specification;
26mod tapes;
27
28pub use machine::*;
29pub use source::source_to_tape;
30pub use tapes::{make_input_vec, make_output_vec, tape_to_string};
31
32use std::io;
33
34/// The type of a data cell
35pub type MData = u8;
36/// The type of a pointer to a cell.
37pub type MAddr = u16;
38
39/// Converts the given source code to a SBrain executable and runs it, taking input from stdin and doing output on stdout.
40///
41/// # Panics
42/// Panics if there is an I/O error with standard in or standard out.
43pub fn simple_run(source: &str) -> u8 {
44 let program = source_to_tape(source);
45 SBrainVM::new(Some(&mut io::stdin()), Some(&mut io::stdout()), &program)
46 .expect("Could not build machine")
47 .run(None)
48 .expect("Unable to run program")
49 .1
50 .expect("Program did not terminate")
51}