Ferite Introduction

by "Blag" - Senior Developer Evangelist

Return to Geeky Thursday

What is Ferite?


Small scripting engine.


Lightweight - small memory and CPU footprint.


Similar to C and Java.


Designed to be embedded in other applications.

How to install Ferite?


On Linux, make sure to install this first

sudo apt-get install libpcre3 libpcre3-dev


Then download https://sourceforge.net/projects/ferite/files/ferite/1.1/ferite-1.1.17.tar.gz/download. and unzip it


Then do sudo ./configure.

sudo make.

sudo make install.

sudo ldconfig.

We can call it like this

ferite


If you have geany, ferite is already configured -;)

Who uses Ferite?


No idea -:( I tried to look online, but found nothing...


Starting out...


Ferite doesn't come with a REPL...so we need to create a file a call from the compiler


We can use the .fe extension

Basic Concepts


Printing on the screen is easy...

We have one line and multi-line comments...

				
uses "console";


Console.print("This is Ferite!");

Console.println("This is Ferite!");



//This is a Ferite comment


/* This is a multi-line

comment */ 
				

Arrays are easy...


				
uses "console";


array numbers = [1,2,3];


Console.println(numbers);

//[1, 2, 3]


Console.println(numbers[0]);

//1
				

Some fun with Arrays...

				
uses "console", "array";


array numbers = [1,2,3];

array a = [7, 8];

array b = [9, 10];

array c = [];

array lang = ["Spanish" => "Peru", 

              "English" => "US", 
              
              "German" => "Germany"];


Console.println(Array.size(numbers));

//3


Array.push(numbers,4);

Console.println(numbers);

//[1,2,3,4]
				

Some fun with Arrays...

				
Array.pop(numbers);

Console.println(numbers);

//[1,2,3]



c = Array.append(a, b);

Console.println(c);

//[7,8,9,10]



Console.println(Array.last(c));

//10
				
				
Array.map( lang ) using (key, value){
	
    Console.println("'$key' is spoken in '$value'");
	
};



//'German' is spoken in 'Germany'

//'English' is spoken in 'US'

//'Spanish' is spoken in 'Peru'
				

Functions


Functions need to return a value, otherwise it will return void


				
uses "console";


function concat(string a, string b){

    return(a + " " + b);

}


Console.println(concat("Hello","Ferite"));


//Hello Ferite
				

Classes


They work almost like in any regular Scripting Language


				
uses "console";


class Person{

    string name;

    number age;

	

    function constructor(string name, number age){

        self.name = name;

        self.age = age;

    }    
  		
				
    function becomeOlder(number by){

        self.age += by;

    }

}



object Blag = new Person("Blag",43);

Console.println(Blag.name);

//Blag

Console.println(Blag.age);

//43

Blag.becomeOlder(1);

Console.println(Blag.age);

//44

				

Code Structure


One important thing to notice about Ferite, is the code structure.


				
//uses goes first...as here you call the libraries



//functions go second...provided that you have any 

//functions



//variables go third...and cannot be declared 

//anywhere else...



//The rest of the code goes here...
				

Fibonacci List


Finally...we're going to make our first application...


So grab your favorite text editor and get ready...


Name your file "fibonacci.fe"


				
uses "console", "string";


function fib(number num, number a, number b){

    string result;

    if (a > 0 && num > 1){

        result = result + " " + (a + b) + 
        
                 fib(num - 1, a + b, a);

    }else if (a == 0){

        result = "${a}" + " " + b + " " + (a + b) + 
        
                 fib(num - 1, a + b, b);

    }

    return result;

}
				
				
string sNum;

number num;

Console.println("Enter a number: ");

sNum = Console.readln();

num = String.toNumber(String.trim(sNum,"\n"));

Console.println(fib(num, 0, 1));
				

Open the Terminal and go to your source code folder...

ferite "Name_of_File.fe"


When we run it we're going to see...


Making an LED Number App


This is one of my favorite codes of all time...


Name your file "LED_Numbers.fe"


				
uses "console", "regexp", "string";


array leds;

string num;

number i = 0, j = 0;

array line;


leds["0"] = " _  ,| | ,|_| ";

leds["1"] = "  ,| ,| ";

leds["2"] = " _  , _| ,|_  ";

leds["3"] = "_  ,_| ,_| ";

leds["4"] = "    ,|_| ,  | ";

leds["5"] = " _  ,|_  , _| ";

leds["6"] = " _  ,|_  ,|_| ";
				
				
leds["7"] = "_   , |  , |  ";

leds["8"] = " _  ,|_| ,|_| ";

leds["9"] = " _  ,|_| , _| ";


Console.println("Enter a number: ");

num = Console.readln();


for(i = 0; i < 3; i++){

    for(j = 0; j < String.length(num) - 1; j++){

        line = Regexp.split(',',
               
               leds[String.toNumber(num[j])]);

        Console.print(line[i]);

    }

    Console.println("");

}
				

When we run it we're going to see...


Random Names


This App will generate 100,000 random names using two 16 elements arrays


We will measure the runtime


Name your file "Random_Names.fe"


				
uses "console", "math";


number i = 0;


array names = ["Anne","Gigi","Blag","Juergen","Marek",

               "Ingo","Lars","Julia", "Danielle",
               
               "Rocky","Julien","Uwe","Myles",
               
               "Mike","Steven","Fanny"];



array last_names = ["Hardy","Read","Tejada",

                    "Schmerder","Kowalkiewicz",
                    
                    "Sauerzapf","Karg","Satsuta",
                    
                    "Keene","Ongkowidjojo",

                    "Vayssiere","Kylau","Fenlon",

                    "Flynn","Taylor","Tan"];
				
				
array full_names;


number seed = Math.seed();


void random = Math.srand(seed);


do{

   full_names[] = names[Math.randomNumber(0,15)] + 
    
                  " " +

                  last_names[Math.randomNumber(0,15)];

    i++;

}while(i <= 100000);
			

When we run it we're going to see...

How this behaves in Python?

And what about Go?

Decimal to Romans


This App will create a Roman Numeral based on a decimal number


This will include some nice commands...


Name your file "Decimal_to_Roman.fe"


				
uses "console";

function roman_number(number num, array keys, 

                      array roman_Table){

    string result;

    while (num > 0){

        Array.each(keys) using (value){

            if (num >= value){

            result += Roman_Table["${value}"];

                num -= value;

                return false;

            }

        };

    };

    return result;

}
			
				
string sNum;

number num;

array Roman_Table;

array keys = [1000,900,500,400,100,90,50,40,10,5,4,1];



Roman_Table["1000"] = "M";

Roman_Table["900"] = "CM";

Roman_Table["500"] = "D";

Roman_Table["400"] = "CD";

Roman_Table["100"] = "C";

Roman_Table["90"] = "XC";

Roman_Table["50"] = "L";
			
				
Roman_Table["40"] = "XL";

Roman_Table["10"] = "X";

Roman_Table["5"] = "V";

Roman_Table["4"] = "IV";

Roman_Table["1"] = "I";



Console.println("Enter a number: ");

sNum = Console.readln();

num = String.toNumber(String.trim(sNum,"\n"));

Console.println(roman_number(num, keys, Roman_Table));
				

When we run it we're going to see...


Count Letters


In this example we're going to read a file and count how many time a letter appears...


Call your file "count_letters.fe" (all in lowercase).


Create a file called "readme.txt" with the following text...


"This is a text file that we're going to read using Ferite"


				
uses "console", "string", "filesystem";

string line;

number len, i = 0;

array counter;


object file = File.open("readme.txt");

line = String.trim(file.toString(),"\n");

len = String.length(line);

for(i = 0; i < len; i++){

    if (Array.keyExists(counter, line[i])){

        counter[line[i]] += 1;

    }else{

        counter[line[i]] = 1;

    }

}
				
				
Array.map(counter) using (key, value){

    Console.println("'$key' => '$value'");

};
				

When we run it we're going to see...


That's it for now


Ferite is a pretty nice programming language


Sadly, last commit was made 11 years ago...


Totally love the clear separation between variables and code...


Ferite could had a great future...hopefully...it will resurrect one day...

Contact Information


Blag --> blag@blagarts.com

@Blag on Twitter

Go back home...