nixpulvis / brainfuck

A simple brainfuck interpreter in Rust.
http://nixpulvis.com/brainfuck
23 stars 3 forks source link

Define the Interface for a Program #13

Closed nixpulvis closed 8 years ago

nixpulvis commented 8 years ago

Right now we interact with the program in a very direct way, tied to the implementation of the string underlying holding the source code.

What we want is an API for what a program is.

nixpulvis commented 8 years ago

A program's state is conceptually an ordered list containing Instructions. The main action on this list is getting the program's instruction at the given instruction pointer. The list represents the logic of the program, without any notion of the syntax of the program, so in this way it's abstract, however since it's not a tree Abstract Syntax Tree (AST) is not valid, but Abstract Syntax List is, so this is hereby dubbed the ASL.

pub fn parse(source: &str) -> Program
pub fn instruction_at(&self, iptr: usize) -> Instruction

Internally however a few actions should be performed after building the list, but before returning a program. Programs must be valid, so the underlying invariants of the ASL must hold for the semantics of the language.

fn check(...) -> Result<..., ...>

In the same way it's possible we could develop optimizations for this language. There could be something like a optimize function as well.

Last for convenience, we provide a way to create a Program from a file with from_file.

nixpulvis commented 8 years ago

Opps forgot the interface for asking about brackets.

nixpulvis commented 8 years ago

The instructions SkipForward and SkipBackward should be modified to contain the instruction pointer of the instruction to skip to.