Skip to content

Brainfuck interpreter written in Java. Creates the output of the BF code.

Notifications You must be signed in to change notification settings

wszlosek/Brainfuck-interpreter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Brainfuck interpreter

Table of contents

What is Brainfuck?

Brainfuck is an esoteric programming language. It was created in 1993 by Urban MĂĽller. Brainfuck is characterized by extreme minimalism. Despite its simplicity, it is complete in the sense of a Turing machine.

The entire language consists of only eight simple commands and the instruction pointer:

>     increment the data pointer (to point to the next cell to the right)

<     decrement the data pointer (to point to the next cell to the left)

+     increment the byte at the data pointer

-     decrement the byte at the data pointer

[     if the byte at the data pointer is zero, then instead of moving the instruction pointer forward 
      to the next command, jump it forward to the command after the matching ] command
      
]     if the byte at the data pointer is nonzero, then instead of moving the instruction pointer forward 
      to the next command, jump it back to the command after the matching [ command
      
,     accept one byte of input, storing its value in the byte at the data pointer

.     output the byte at the data pointer

General info

The program obtains Brainfuck code as input, and then prints its output (see the examples below for more).

The "Brainfuck interpreter" was created for personal use (especially fun). In addition, it was a repetition of the Java language.

The whole idea of the project seemed interesting, it pointed to the power of the inconspicuous Brainfuck language (in the examples, among others, the calculation of successive words of the Fibonacci sequence and other mathematical operations).

Technologies

The program was written in Java.

Solutions used in the project

I made an assumption, to simplify the project, the user enters the .BF code in the console (non-language characters will be omitted). If the . character is not used in the instruction string then we print all cells of our" machine ". Otherwise, "strict output" will be printed. The program also works with "nested loops" ([ and ]), and each memory cell's range is contained in Integer.

Examples of program effects

Ex. 1 (Hello world)

   // console 'input'
   
   ++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..
   +++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
   
   
   // console 'output'
   
   Hello World!
   

Ex. 2 (Fibonacci Sequence)

   // console 'input'
   
   >++++++++++>+>+[[+++++[>++++++++<-]>.<++++++[>--------<-]+<<<]>.>>[[-]<[>+<-]>>[<<+>+>-]>
   <[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>[-]>+>+<<<-[>+<-]]]]]]]]]]]+>>>]<<<]
   
   
   // console 'output'
   
   0
   1
   1
   2
   3
   5
   8
   13
   21
   34
   55
   89
   144
   ... (never terminates)

Ex.3 (Golden Ratio)

  // console 'input'
  
  ++++++[>++++++++<-]>+.---.>>>+>>+>++>+>+>+>>+>+++>+>>+>+>+>>+>>>>+>>>>+>++>>>>+>+<[[>>>]<<<[>>>>>>>>
  >+<<<<<<<<[>>>>>>>>>+<<<<<<<<<-]<-<<<]>>>>>>+>>>+[<<<]<<<<<[>>>+<<[>>>+<<<-]<-<<<]>>>+<<<<<<<<[<<]>>
  >>[->[<<<+>>>>[>>]>+<<<[<<]>-]<<<-[>>>+<<<-]+>>>>[>>]+>[-[-[-<->>>>>>[[>>>]>>]<<<<<[>+<-<<<]<<[<<<]>
  >>[->>>[>>>]>>[>>>]+[<<<]<<[<<<]+>>>]>>>[<[-]+>->>>]<[>>>]<<<[>+++++++++<<<<]<<[<<<]>>>[->[<<+>+>-]<
  [>+<-]>[>>[>>>]>>>>[>>>]<-<<[<<<]<<<<[<<<]>-]>>[>>>]>>>>[>>>]+[<<<]<<<<[<<<]<[>>+<<-]>+>>>]>>>>[->>>
  ]<<[>>>]<<<[>[-[-[-[-[-[-[-[-[-<->[<+>[-]>+<]]]]]]]]]]<[[>>[<<<+>>>-]<[-]<-<<<]>>>]<<<]>+>[-<-<<<[[<
  <<]<<]<<<[<<]+>>->>[>>]>>>>>>>-<[[>>>]>>]<]<[-<<<[[<<<]<<]<<<[<<]+[<<]>>->>[>>]>>>>>>[[>>>]>>]<<]<<<
  [[<<<]<<]]<[->>>>>>>+[<<+>+>-]<-[>+<-]+<[-[-[-[-[-[-[-[-[-[->>-<<<<<<<<<[<<]+>>>>>>>>->>[>>]>>>>>]]]
  ]]]]]]]<<<<<]>]<[->>>>>>>[<<<+>+>>-]<<[>>+<<-]+<[>-<[>>[>>>]>>[>>>]>>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--]<.[-]>>[>>>]>>]
  
  
  // console 'output'
  
  1.61803398874989484820458... (never terminates) 

Status

Input entered by the user in the console via java.util.Scanner. In the future, the program should read input from an external file (for example, notepad). The current form can be a bit burdensome for the user.

However, in its assumptions, the project was completed.

About

Brainfuck interpreter written in Java. Creates the output of the BF code.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages