Chapter 2. Quickstart

Quickstart

The Butter quickstart

In the following you will be guided through the process of making a very simple example program. This will probably give a good first impression of the language.

It is assumed that you have successfully compiled the package at this point as well as having the bcompiler and bruntime executables ready to run. If this is not the case please consult Installation section of the manual.

The classic "Hello world" example

Now its time to pick the texteditor of our choice and start editing a sourcefile. The classic example of the first program in any language is the famous "Hello world" application. This program will not do anything more than just printing out a line saying :
Hello World
In our case this program would look like this :

//
// Hello World Program
//

// main function
void main ()
{
  prs ("hello world");
  nl ();
}

// required declarations */
void prs (string s);
void nl ();
All sourcecode parts after "//" are considered to be comments. So all input after "//" until the end of the current line is discarded. Comments are only featured in programming languages to give the programmer(s) the ability to store some additional hints about the intents of their code right along with it. The remaining part of the inputfile is processed by the compiler who will find 3 top-level syntax items in the above example.

  1. The definition of the function "main"

  2. The declaration of the function "prs"

  3. The declaration of the function "nl"

The difference between definitions and declarations of functions is crucial. In order to invoke any function, it must be at least declared. Attempts to invoke a function that is neither defined nor declared will fail at the early type-checking stage of compiliation. A function that is only declared but not defined in a given sourcefile needs to be defined in another place. In order to pass the above mentioned type-checking stage of the compiler successfully the functions and the types or arguments they expect must be known somehow. This is why declarations are needed in the above case. In order to perform our task we need to make use of two externally defined functions already. These correspond to the 2 declarations in the sourcecode.

Now save the file to disk and name it for example "hello.bf". On your commandline then invoke the bcompiler executable like this:
bcompiler hello.bf
This will compile the file. If no extra information is printed to your console then the compile was successful and a new file called "hello.out" was created. In case there were errors, these will be printed out on the console and you need to check that your script conforms with the above example and try again. Now its about time to actually run the generated bytecode stored in the .out-file by invoking bruntime as follows:
bruntime hello.out
This will produce the desired result. On a unix shell the entire compilation and interpretation sequence would look like this in summary :
$ bcompiler hello.bf
$ bruntime hello.out 
hello world

In the example directory you will find more simple examples, which will demonstrate most basic features of the language. For a more in depth look at the language syntax and constructs proceed with reading this manual.