ClearBF

From Esolang
Jump to navigation Jump to search

ClearBF is a programming language which helps writing programs in the most famous esoteric programming language: Brainfuck. After compiling ClearBF sources, then the Brainfuck output programs may easily be compiled in their turn using any BF compiler. The syntax is basically inspired by the Tbf language.

Language overview

ClearBF is based on a C implementation using the Flex library to generate the lexical analyser for the ClearBF language.

Development team

This project was a scholar project at ENSIAS - Morocco to build a compiler in C/Flex. The team is supervised by Dr. Karim Baîna and Mrs. Mounia ABIK. Members of the team are:

  • Mohammed ASSOUKTI (Team’s leader)
  • Yasmina MOUNIR
  • Yasser ABOUKIR
  • Anass ANNOUR
  • Larbi MOUHIBBI


The Team would like to express feelings of gratitude to our professors for their coaching and supervising at ENSIAS! We'd like to thank Till Crueger for his effective and efficient help!

Motivation

As you can imagine the actual writing of the BF is just tough and a bit special. For this reason, we decided to write a new language that shows some basic concepts, but still close to the BF language. This language is named ClearBF. It is easier to write than the BF. In this case, our project is focused on a specific development of compiler of a new language called ClearBF which facilitates writing programs in BF.

Basic concepts of ClearBF

Here is roughly the basic concepts of our language:

General:

  • Each command must end with a ";"
  • Everything is case insensitive (the word "ClearBF" is the same as "CLEARBF" as "clearbf", etc..)
  • Ability to make comments using "%" to start a comment. The comment ends at the end of the line.
  • All new identifiers (variables) will be awarded automatically initialized to zero.

Basic commands:

  • "incr;" increment the current position
  • "decr;" decrement the current position
  • "put;" out of the current position as a character on the screen
  • "mvto <identifier>," the band moves to the location designated by <identifier>
  • "get;" read a character from the keyboard in the current position
  • "zero;" set the current position to zero
  • "print;" out of the string starting at the current position
  • "store <identifier>" save the value that is designated by the current position <identifier>
  • "retrieve <identifier>" restore the value of a current cell <identifier>
  • "add <identifier>," adds the value stored <identifier> to the current cell.
  • "sub <identifier>" similar to add, but subtraction

Identifiers:

Identifiers can consist of the following:

  • variable names in the form [az AZ _][AZ az _ 1-9]*
  • integers (e.g 10, 11, -1234, etc.)
  • characters'
  • strings in ""
  • "new" that ID always shows a value newly produced
  • ". " at the current position


Constructs more complex:

  • "while <test> <expr> do" Represent a loop. Once entered on the loop, pointing to the band identifier that was used in the test.
  • "begin <expr>; [<expr>; ...] end" blocks of code used to group multiple statements.
  • "declared <identifier> as <identifier>" which introduces an alias for an identifier used previously.

Example

Here is a sample of a program in ClearBF:

% Arithmetic Sequence 1 +2 +3 +4
start
 mvto b;
 mvto a;
 while 4 do begin
  incr;
  mvto b;
  add a;
 end
finish

The result of the compiler output is:

>>>>>>>>++++[-<<<<+<<<<>>>>[->+<<<<<+>>>>]>[-<+>]<<<<<>>>>>>>>]>[-<+>]<<<<<

Semantic analysis

A complete compiler includes a number of phases, lets say optional level semantics that apply to the source language, it is primarily the type checking and proper use of names, often made at the typing, but also high-level optimizations based on the semantics of language. A Turing machine consists of a control part and an infinite tape on which are written symbols char (1 byte). At the base of this band, we chose to work on a static array of size 100 * 4, which present a one-direction infinite tape. The control part which consists of a finite number of possible states and transitions which govern the calculation of the machine are defined by the instruction set of language ClearBF.

Error handling

ClearBF manipulates his language identifiers into several types. This diversity of typing requires us to do some checks to ensure the smooth operation sense. So we distinguish between four major errors:

  • NonDeclaredVar: Using an undeclared identifier.
  • IncompatibleOperationType: The type of the identifier is not compatible with the operation.
  • KeyWordReserved: Using a reserved word as an identifier.
  • OtherError: Any other error semantics not yet supported.

Algorithms

We have implemented the majority of algorithms used when translating the ClearBF code to the BF. However we've used some famous Brainfuck algorithms. For more details see Brainfuck algorithms, Brainfuck constants.

See also

External resources