Skip to content

A compiler from a higher level Psudo-Code to BrainFuck.

License

Notifications You must be signed in to change notification settings

venunathan12/BrainCooked

Repository files navigation

BrainCooked

A compiler from a higher level Psudo-Code to BrainFuck.

Version : Beta 1.3.1

Please check the presentation Working Explained.pdf for descriptive explanation of how this compiler functions.

Introduction

Being one of the simplest languages ever written, Brainf**k is ideal for hardware implementation of computer systems. But its simplicity makes it hard to write complex programs.

This project is a stepping stone towards addressing this issue. A compiler from a high level language is exactly what Brainf**k needs.

The following is a compiler from a high level Psudo-Code which can produce equivalent Brainf**k code. The compiled code can be tested with the Interpreter which is present with this project.

File Hierarchy

BrainCooked
│
└───Examples
│   └───Example1.pc
│   └───Example2.pc
│   ...
│
└───Source.pc
└───Instruction.txt
└───Input.txt
└───Output.txt
│
└───Brain.cpp
│
└───Compiler.py
└───Assembler.py
└───GenTemplate.py
└───WriteIns.py
└───Compiled.txt
└───Template.txt
│
└───README.md
└───LICENSE
└───Working Explained.pdf

Usage

This project contains the following components:

  • A Brainf**k interpreter written in C++
  • A Compiler to Brainf**k written in Python

Usage : Brainf**k Interpreter

The Interpreter is generated by compiling the Brain.cpp file using g++.

Once compiled and run, the binary will open the file named Instruction.txt and start executing Brainf**k commands till the file is exhausted. The input when command ',' is run is taken from file Input.txt. The output of command '.' is sent to both stdout and file Output.txt.

After it completes execution, it will display the first 64 cells in memory of the program. Local variables of the main functions can be seen from the 13th cell onwards, in the same order as they were declared.

Usage : Writing Code

It is highly recommended that you have a look at the Source.pc file and the files in the Example folder to understand the syntax of the Psudo-Code used.

The following rules must be adhered to when writing the Source.pc file:

  • The execution starts with the first function written in the file. Hence the first function must be the main function, though it can be named in any other way.
  • Each function must have either an end or return statement.
  • Only the program written in Source.pc file will be compiled. The file cannot be renamed.

The following commands can be used within body of functions:

  • int __ __ __ ...
    • Declares int variables in local scope named after the words which follows the command.
  • intarr __ __
    • Array of integers can be declared one at a time.
    • The first parameter after the 'intarr' keyword is the length of the array.
    • The next is the name of the array to be declared in local scope.
  • if __ ... else ... endif
    • Code written between the keywords 'if' and 'else' will be executed if the variable following the if keyword stores a positive value. Otherwise the code written in the body of 'else' will be executed.
  • while __ ... endwhile
    • If the variable following the while keyword is non-zero, the body of the loop is executed. The condotion is checked again and the cycle repeats.
  • end
    • Causes the program to exit
  • return __
    • Closes a function and returns the value stored in variable following the keyword.

The following modifications can be made to a variable:

  • Var set __
    • The quantity to the right of the 'set' keyword can only be a whole number. This number is copied into Var.
  • Var = __
    • The quantity on right of the equality sign can be another variable, in which case the value of the other variable is copied to Var.
    • The quantity on right of the equality sign can be a function, in which case the return value of the function is copied to Var.
  • Var <- __ (for internal purposes)
    • Empties the other variable and increments Var by the by the value the other variable stored.
  • Var -= __ (for internal purposes)
    • Decrements both Var and the other variable till one of them becomes zero.
    • Note : The variable Var must have been declared before the other variable.
    • Note : This operator must only be used on the last two variables declared (for safety).
    • Note : This operator is dangerous if not correctly used. So consider using the SUB function seen in examples.
  • Var rawinput
    • The ASCII value of next character in Input.txt file is copied into the variable Var.
  • Var rawoutput
    • Prints to Program Output and Output.txt file, the character whose ASCII value is that stored in variable Var.
  • Var storein __ __
    • Used to copy the value in a variable to an array element.
    • The parameter after the 'storein' keyword is the name of the array.
    • The next parameter is the index of the element which must be accessed. It can be a scalar number, or another variable.
  • Var fetchfrom __ __
    • Used to copy the value of an array element to a local variable.
    • The parameter after the 'fetchfrom' keyword is the name of the array.
    • The next parameter is the index of the element which must be accessed. It can be a scalar number, or another variable.

Usage : Compiling

The Python file Compiler.py, when run will read Source.pc file and produce a Compiled.txt file.

The GenTemplate.py file can be run to generate Template.txt file.

The WriteIns.py file when run will load Template.txt and Compiled.txt and produce Instruction.txt, which can be executed with the Brainf**k interpreter when run with the binary in the same directory. This file is executed internally by Compiler.py after it has completed writing Compiled.txt.

The default instruction limit is 256 lines. But it can be improved by increasing the AddrSize variables in the python files.

TLDR

Open terminal or cmd on your device and execute the following commands

g++ -o Brain Brain.cpp

python3 Compiler.py

./Brain

Ignore the warning during g++ compilation.

If any of the lines fail to execute, then sit and cry.

If everything works file you should see the 10th Fibonacci numbers, 55 in the output.

Releases

No releases published

Packages

No packages published