Git Product home page Git Product logo

brainfuck-c's Introduction

Brainfuck interpreter

A simple brainfuck interpreter written in C99.

Usage

bf [FILE]

Technical details

After reading the brainfuck source code the interpreter will convert it to an internal representation which is easier to optimize.

Currently, the following optimizations are applied:

  • Loops [+] or [-] directly set current value to zero
  • Consecutive +, -, < or > are collapsed to single instructions that do the equivalent arithmetic / memory operations

brainfuck-c's People

Contributors

pwseo avatar

Stargazers

Hugo Sereno Ferreira avatar

Watchers

 avatar James Cloos avatar

brainfuck-c's Issues

Optimization of consecutive calls to +, -, < and >

It should be possible to optimize the internal representation of the program's code to collapse consecutive +, -, < and > tokens and make the interpreter execute a single instruction representing all of them instead of a single instruction at a time.

Depends on #9.

Should start with 30K empty memory cells

As per language specifications, the interpreter should have at least 30K empty memory cells. Since we mem_resize the cell pool when they're not enough this isn't a problem, but we could improve things and provide 32K empty cells right from the start.

Invalid read/write operations after mem_resize()

Since we're using realloc to deal with memory resizes and at the same time using a doubly-linked list we run into problems when realloc can't resize .cells in place, since all the cells' .next and .prev potentially become invalid.

Probable fix: make .next and .prev contain indices into .cells instead of pointers.

Missing error checks everywhere

We're currently missing error checking everywhere in the application: when creating the vm, the memory store, the loop stack.

Revise internal representation of code

Current internal representation has a 1:1 correspondence to the original byte stream that composed the brainfuck source code.
This should be revised in order to support optimizations like the removal of non-executable / empty loops, collapsing of cell value changes and cell pointer moves.

Should not allow "negative" memory addresses

Memory implementation currently allows negative memory addresses (eg. moving left as a first instruction); this is a complex implementation that involves both a doubly-linked list and a memory-cell pool to avoid allocating them one by one.

Disallowing will simplify code and hopefully improve performance, while still behaving withing specification.

vm assumes 131072 code length

VM should be able to read potentially infinite amounts of code, provided the host system still has memory left; it means we have to realloc memory for the token stream.

Optimize empty adjacent loops

Any loop beginning immediately after another will never be executed and can be safely skipped (and thus, removed from the internal representation before code execution).

Rationale: given [ LOOP A ][ LOOP B ], we know that Loop A will execute until the current cell's value is zero. Then, when we enter Loop B, it will be skipped because the cell's value continues to be zero.

Depends on #9.

Reading input from stdin not supported

Script sources are currently read from stdin, which conflicts with the source for user input. Probable fix will be to accept the script's filename via command line argument, freeing up stdin for user input.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.