Crate pancakestack
source ·Expand description
Pancake Stack
This is a Rust implementation of the Pancake Stack esoteric programming language. This crate includes a parser and an interpreter.
Pancake Stack is a stack-based esoteric programming language created by User JWinslow23 in 2013, in which programs require you to manipulate a stack of pancakes.
Basic Usage
A program can be parsed with pancakestack::parse_program_str
and run with pancakestack::run_program
.
// load program from file
let mut file = File::open("example.pancake").unwrap();
let mut program_str = String::new();
file.read_to_string(&mut program_str).unwrap();
// parse the program
let program = pancakestack::parse_program_str(&program_str);
// run the program
pancakestack::run_program(&program, std::io::stdin(), std::io::stdout()).unwrap();
Alternatively you can run a program from a str or a Read with pancakestack::run_program_str
and pancakestack::run_program_from_read
respectively.
// load script file
let mut file = File::open("example.pancake").unwrap();
// write program into string
let mut program = String::new();
file.read_to_string(&mut program).unwrap();
pancakestack::run_program_str(&program, std::io::stdin(), std::io::stdout()).unwrap();
// open script file
let mut file = File::open("example.pancake").unwrap();
// run the script directly from the file
pancakestack::run_program_from_read(file, std::io::stdin(), std::io::stdout()).unwrap();
All pancakestack::run_*
methods accept a Read
as the input of the script and a Write
as the output.
The examples until now used stdin()
and stdout()
, but it is possible to use anything implementing Read
and Write
respectively.
The following example shows the use of strings as input and output:
let file = File::open("example.pancake").unwrap();
let input = b"some input";
let mut output_buf = Vec::new();
pancakestack::run_program_from_read(file, &input[..], &mut output_buf).unwrap();
let output = std::str::from_utf8(&output_buf).unwrap();
Construct programs
A program can be parsed from a str
with pancakestack::run_program_str
. A single line (=command) can be parsed with Command::from_line
.
Parsed programs are slices of Command
s and can be run with pancakestack::run_program
.
use pancakestack::Command;
let program = [
Command::PutThisPancakeOnTop("test".into()),
Command::ShowMeAPancake,
Command::EatAllOfThePancakes
];
pancakestack::run_program(&program, std::io::stdin(), std::io::stdout()).unwrap();