Skip to content

A new general-purposed programming language for Colibri VM

Notifications You must be signed in to change notification settings

tasosxak/nectar-lang

Repository files navigation


Nectar is a new general-purposed programming language. It comprises a combination of both high-level and low-level language features. Nectar supports object-orentied and procedural programming.

Nectar's futures include:

Feature Description
Portability The compiler of Nectar produces bytecode for colibri virtual machine. So you can execute the same bytecode files on a wide variety of different platforms (Windows,Linux,MacOS etc).
Scalability You can add libraries and Nectar provides better structure for this.
Learnability Nectar has clean and easy syntax that allows new developers to learn quickly and easy.
Readability Nectar has similar syntax to that of many programming languages (such a Javascript,Ruby, Swift,C++,Java).

Installation

Version

Υou can check the version of the compiler you are using by typing:

$ nectar version

Create new project

Writing the below command you can create a new project:

$ nectar create name_of_project

The above command creates the below project tree:

name_of_project
    |
    |__ src
          |__ name_of_project.nec
    |
    |__ exec
  • src : This folder includes all sources files of project.
  • exec: This folder includes the executable file (.cex) of project.

If you open the name_of_project.nec file in src folder you will see the below snippet of code:

func main()->int {
  say "Hello world!";
  return 0;
}

Build your project

After project creation let's see how to build it. Writing the below command you can build your project:

$ nectar build name_of_project

The above command initially calls the prenectar program to unite all the nectar files of your project and saves the result in the name_of_project.nectar file. After this, the nectar compiler is called, which it produces the bytecode in name_of_project.cex. The name_of_project.cex is stored in exec folder.

Run your project

After project build let's see how to run it. Writing the below command you can run your project:

$ nectar run name_of_project

The above command calls the colibri virtual machine with name_of_project.cex as input. Colibri-VM executes the bytecode.

Keywords

The first step of compilation named "Lexical Analysis" and splits the input file to tokens. In this phase, the compiler recognizes the reserved keywords which are:

false true int return if else while break say read try times use unless not and or real puts bool func class pub abstract var

Syntax

Let's see some commands. In nectar you can declare functions, classes and variables. The execution of nectar program starts from the main function which is one and only one.

  • Import libraries:
use math

With this command you can import other nectar files. You should write use statements at the top of your program.

  • Class Declaration:
pub class Person {


	@name:str;
	@surname:str;
	@height:real;
	@age:int;


	pub new(name:str,surname:str,height:real,age:int)->Person {

		@name = name;
		@surname = surname;
		@height = height;
		@age = age;

		return self;
	}

	pub name()->str {
		return @name;
	}

	pub age()->int {
		return @age;
	}

	pub height()->real {
		return @height;
	}


}

func main()-> int {

	var me:Person;
	
	me = Person.new("Tasos","Temperekidis",1.73,21);
	say "My name is " , me.name() , " and I am " , me.age(), " years old!";

	return 0;
}
  • Fields:
@age:int;

With the above way you can declare a field in class. Fields are strictly private.

  • Methods:
  pub new(name:str,surname:str,height:real,age:int)->Person {

	@name = name;
	@surname = surname;
	@height = height;
	@age = age;

	return self;
 }

Methods are by default private. You can make them public by adding the keyword pub to the beginning of declaration.

  • Shared Methods:
class Console {
  $shared
  pub write(msg:str)->str {
    say msg;
    return msg;
   }
}
 

Shared methods are functions which you can call them without creating a new object. For example:

Console.write("Hello world!");
  • Functions:
func main()-> int {

	var me:Person;

	me = Person.new("Tasos","Temperekidis",1.73,21);
	say "My name is " , me.name() , " and I am " , me.age(), " years old!";

	return 0;
}

With the above way you can declare a function in your program.Instead of method declaration, the declaration of function starts with the keyword func.

  • Variable Declaration:
 var x:int;

You can declare a variable in methods and functions.

  • Variable Definition & Declaration:
 var x:int = 12;

You can declare & define a variable in methods and functions.

  • Assignment:
   x = 53;
  • Math & Assignment:
   x += 53; # x = x + 53
   y -= x;  # y = y - x
   z *= y+5;  # z = z * ( y+ 5)
   x /= 3;  # x = x / 3
   x++;  # x = x + 1
   ++x;  # x = x + 1
   y**; #  y = y*y
   **y; #  y = y*y
   z--; # z = z-1
   --z; # z = z-1
  • Division & Integer Division:

Nectar has two operators for division.

Division:

   say 5 / 2;  # 2.5

Integer Division:

   say 5 // 2; #2
  • Swapping:
   var x:int = 5;
   var y:int = 6;
   x,y = y,x;   # x=6 and y=5
  • Print with new line:
   say "hello world!";

Prints out the message "hello world!" and new line.

  • Print without new line:
   say "hello ", "world ", "!";

Prints out the message "hello world !".

  • Read:
   read "Give me a number: ",x;

Prints out the message "Give me a number" and waits for input. When the user gives an input and press the enter button,this command assigns the input to the variable x.

  • if statement:
   if( age < 18 ) {
      say "you are kid!";
   }
  • if statement with else:
   if( age < 18 ) {
      say "you are kid!";
   }
   else {
      say "you are man!";
   }
  • While loop:
   while(counter < 15){
    read "Give me a number", x;
    say x;
    counter++;
   }
  • Times loop:
   15 times say "Hello world";
  • For each iteration:
   for word in "hello" {
   	say word;
   }

Single Statements

Single Statements are statements which you can write in one line, fastly. The command must be assignment, print,read, expression.

  • command if expression:
   say "Hello world" if x>5;

This command will execute the code before if , if the expression after if is true.

  • command unless expression:
   y++ unless x>5;

This command will execute the code before unless , if the expression after unless is false.

Build-in types

  • String:

Adding strings:

   say "Hello " + "World " + "!!!"; # output: Hello World !!!

String multiplication:

   say 3 * "Hello!";  # output: Hello!Hello!Hello!

What is Colibri?

Colibri is a virtual machine that reads line per line the bytecode and executes it. This process consists of three phases.

  • Load: In this phase, Colibri-VM pushes the bytecode into the stack.
  • Compilation: In this phase, Colibri-VM compiles the bytecode (creates the classes, the labels,the variables etc)
  • Execution: In this phase, Colibri-VM executes the bytecode, starting from main function.

Contributing

You may contribute in several ways like fixing bugs, improving documentation and examples or translating any document here to your language. Find more information in CONTRIBUTING.md. 🌺

About

A new general-purposed programming language for Colibri VM

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published