pancakestack/lib.rs
1//! # Pancake Stack
2//! This is a Rust implementation of the [Pancake Stack](https://esolangs.org/wiki/Pancake_Stack) esoteric programming language.
3//! This crate includes a parser and an interpreter.
4//!
5//! > Pancake Stack is a stack-based esoteric programming language created by
6//! > User [JWinslow23](https://esolangs.org/wiki/User:JWinslow23)
7//! > in [2013](https://en.wikipedia.org/wiki/2013), in which programs require you to
8//! > manipulate a stack of [pancakes](https://i.ytimg.com/vi/FLd00Bx4tOk/maxresdefault.jpg).
9//!
10//!
11//! **Basic Usage**
12//!
13//! A program can be parsed with [`pancakestack::parse_program_str`](./parse/fn.parse_program_str.html) and run with [`pancakestack::run_program`](./interpret/fn.run_program.html).
14//!
15//! ```rust
16//! # use std::fs::File;
17//! # use std::io::Read;
18//! # fn run() {
19//! // load program from file
20//! let mut file = File::open("example.pancake").unwrap();
21//! let mut program_str = String::new();
22//! file.read_to_string(&mut program_str).unwrap();
23//!
24//! // parse the program
25//! let program = pancakestack::parse_program_str(&program_str);
26//!
27//! // run the program
28//! pancakestack::run_program(&program, std::io::stdin(), std::io::stdout()).unwrap();
29//! # }
30//! ```
31//!
32//! Alternatively you can run a program from a [str](https://doc.rust-lang.org/std/primitive.str.html) or a [Read](https://doc.rust-lang.org/std/io/trait.Read.html) with [`pancakestack::run_program_str`](./interpret/fn.run_program_str.html) and [`pancakestack::run_program_from_read`](./interpret/fn.run_program_from_read.html) respectively.
33//!
34//! ```rust
35//! # use std::fs::File;
36//! # use std::io::Read;
37//! # fn run() {
38//! // load script file
39//! let mut file = File::open("example.pancake").unwrap();
40//!
41//! // write program into string
42//! let mut program = String::new();
43//! file.read_to_string(&mut program).unwrap();
44//!
45//! pancakestack::run_program_str(&program, std::io::stdin(), std::io::stdout()).unwrap();
46//! # }
47//! ```
48//!
49//! ```rust
50//! # use std::fs::File;
51//! # fn run() {
52//! // open script file
53//! let mut file = File::open("example.pancake").unwrap();
54//!
55//! // run the script directly from the file
56//! pancakestack::run_program_from_read(file, std::io::stdin(), std::io::stdout()).unwrap();
57//! # }
58//! ```
59//!
60//! All `pancakestack::run_*`methods accept a [`Read`](https://doc.rust-lang.org/std/io/trait.Read.html) as the input of the script and a [`Write`](https://doc.rust-lang.org/std/io/trait.Write.html) as the output.
61//!
62//! The examples until now used [`stdin()`](https://doc.rust-lang.org/std/io/fn.stdin.html) and [`stdout()`](https://doc.rust-lang.org/std/io/fn.stdout.html), but it is possible to use anything implementing [`Read`](https://doc.rust-lang.org/std/io/trait.Read.html) and [`Write`](https://doc.rust-lang.org/std/io/trait.Write.html) respectively.
63//! The following example shows the use of strings as input and output:
64//!
65//! ```rust
66//! # use std::fs::File;
67//! # fn run() {
68//! let file = File::open("example.pancake").unwrap();
69//! let input = b"some input";
70//! let mut output_buf = Vec::new();
71//! pancakestack::run_program_from_read(file, &input[..], &mut output_buf).unwrap();
72//! let output = std::str::from_utf8(&output_buf).unwrap();
73//! # }
74//! ```
75//!
76//!
77//! **Construct programs**
78//!
79//! A program can be parsed from a [`str`](https://doc.rust-lang.org/std/str/) with [`pancakestack::run_program_str`](./interpret/fn.run_program_str.html). A single line (=command) can be parsed with [`Command::from_line`](./parse/enum.Command.html#method.from_line).
80//!
81//! Parsed programs are slices of [`Command`](./parse/enum.Command.html)s and can be run with [`pancakestack::run_program`](./interpret/fn.run_program.html).
82//!
83//! ```rust
84//! use pancakestack::Command;
85//!
86//! let program = [
87//! Command::PutThisPancakeOnTop("test".into()),
88//! Command::ShowMeAPancake,
89//! Command::EatAllOfThePancakes
90//! ];
91//! pancakestack::run_program(&program, std::io::stdin(), std::io::stdout()).unwrap();
92//!
93
94pub mod interpret;
95pub mod parse;
96
97pub use interpret::*;
98pub use parse::*;