Skip to content
kubijaku edited this page Jun 14, 2023 · 4 revisions

BF^2 Documentation

What is BF^2?

It's a programming language loosely based on a Brainf**ck. It has many more features and improvements over the base version.

What can it be used for?

Anything you want except most things ...

How to run?

You need simply to run command

  java -jar target/interpreter-1.0-SNAPSHOT-jar-with-dependencies.jar src/test/resources/FILE_NAME

and instead of FILE_NAME select one of the given files ( they will show if you press TAB button on your keyboard ) for example

  java -jar target/interpreter-1.0-SNAPSHOT-jar-with-dependencies.jar src/test/resources/expressions.bf2

You can also run your own bf2 program and run it with the same command and only changing the last path! For example:

  java -jar target/interpreter-1.0-SNAPSHOT-jar-with-dependencies.jar C:/my_programm.bf2 

Overview

Little disclaimer!

Writing in BF^2 is probably nothing like writing in your favourite language of choice.

What is it all about?

Let's imagine an array of size x_rows and y_columns. In BF^2 you're controlling an agent which is on one of the positions specified by the x and y index. You can perform different operations on the cell like for example:

  • take value from cell
  • change value in cell
  • etc.

Your agent can move all around the board (even on different levels - we will get to it!).

What is it built with?

It's mostly Java & Antlr4.

Syntax

Types

With BF^2 you have to be very creative, because it only uses type: INT, yet you can do so many things with it - you will see!

Declare the size of an Array

At the start of every program you have to declare the size of the array you'll be using. Both numbers have to be of type(int) and separated by ,

% Declare size (4,4)

4,4

Comments

While we're here let's learn how to write comments. Comment is preceded by %

% This is a comment! 

Movement

To move the agent around the board we use:

  • > move right
  • < move left
  • ^ move up
  • v move down

Putting values in cells

To put a value in the specified cell we use = operator

4,4

=0 >=1 >=2 >=3

READ_AS_INT

Math symbols

With BF^2 you can perform different arithmetic operations on cells

  • + addition
  • - subtraction
  • * multiplication
  • / division
  • ! negation

Values comparison

With BF^2 you can perform different values comparisons

  • <? smaller than
  • >? bigger than
  • =/ equals

Logical values

With BF^2 there are no normal logical values - instead you get:

  • 0 false
  • !0 true

Get value from cell

To get value that is specified in a particular cell that agent is on we use . operator

5,5

=.+5
v=3*8
v=2/2
v={^^}. + 7

READ_AS_INT

Print value from current cell

To print out the value from current cell we use @ operator like so:

4,4

>>=5@

Conditional statements

You can use if to create conditions like so:

1,7

if( ~~ 3 ){=111}
v

Loops

There are basically 2 types of loops in BF^2

  1. do
  2. do-while

Here is the syntax for the first one:

do{instruction}(number_of_loops)

Here is the syntax for the second one:

do{instruction}if(condition)

Functions

Now we get to the fun stuff! Here come the mighty functions!

5,3

func:SOME_FUNCTION_FOO{>>>>}

As you can see, we define it by:

  • typing func with : at the end
  • then there is a name
  • we open curly brackets {} and type the behaviour.

Embeded funtions

We have 3 function that are built in and help us visualize the output of the program

  1. READ_AS_STRING prints the array as the string
  2. READ_AS_INT prints the array with numbers
  3. READ_AS_COLORS calculates black-white representation of the color and prints the array of colors

Exceptions

If you f**k up you code, you will get an error - we promise! For now there are two that you have to be aware of

  1. outOfBoardException
  2. functionRedefinition