A swahili based programming language
Swlang is just another programming language that uses swahili keywords and actions. This is just a fun side project that will help anyone understand the inner workings of compilers and interpreters
// This is my first swlang program
sema("Salamu, Dunia!");
Translation
- sema -> say
- Salamu -> Hello
- Dunia -> World
Swlang is a high-level dynamically typed programming language
- Dynamic Typing - Variables can store values of any type, and a single variable can even store values of different types at different times.
- Automatic Memory Management - Swlang uses a Garbage Collector (GC) for memory management. Therefore there is no need to stress yourself with memory leaks, the GC will take care of that for you
Swlang uses ukweli
and uwongo
which are Swahili words that translate to true
and false
respectively in English.
ukweli; //true
uwongo; //false
Swlang only features double-precision floating point numbers since they can represent a wide range of integers that covers a lot of teritory while keeping things simple.
12; //integer
12.59; //decimal
String literals are enclosed in double quotes.
"Salamu, Dunia!"; // string literal
"J"; // character
hakuna
represents "null"/"no value".
There are various expressions supported in Swlang
Swlang features the basic arithmetic expressions from other popular language.
1 + 2; //addition - kujumuisha
a - b; //subtraction - kuondoa
c * d; //multiplication - kuzidisha
4 / 2; //division - kugawa
These operations always return a boolean
a < b; //less than
b > a; //greater than
a <= b; //less than or equal to
b >= a; //grater than or equal to
a == b; // equal to
!ukweli; //equates to uwongo. !true -> false
!uwongo; //equates to ukweli. !false -> true
There is also control flow expressions.
ukweli na uwongo; //true and false -> false
ukweli na ukweli; //true and true -> true
ukweli ama uwongo; //true or false -> true
uwongo ama ukweli; //false or true -> true
chombo jumla = (1 + 2) * 4;
In Swlang statements are followed by a semi-colon to indicate the end of the statement;
sema("Salamu, Dunia!");
You can also use curly braces {}
to wrap your statements/expressions in a code block
{
chombo c = a + b;
sema("Salamu, Dunia");
}
Variables are declared using the chombo
keyword. Chombo is a swahili word for container and I usually think of variables as containers which store different items/values. The chombo
keyword is equivalent to var
/let
in other popular languages.
chombo a = 1;
sema(a); //"a"
chombo jina = "Juma";
sema(jina); //"Juma"
Control flow mainly involves if statements and loops.
ikiwa(a > b) {
sema("ndio");
} isipo {
sema("la");
}
While loop:
chombo a = 1;
mradi(a < 10) {
sema(a);
a = a + 1;
}
For loop:
kwakila (chombo i = 0; i < 10; i++) {
sema(i);
}
Calling functions
jumuisha(a, b);
Defining functions
tendo jumuisha(a, b) {
rudisha a + b;
}
Functions are first class in Swlang meaning they are real values that you can get a reference to, store in variables, pass around etc.
tendo jumuisha(a, b) {
return a + b;
}
tendo tokeo(a) {
return a;
}
sema(tokeo(jumuisha)(1, 2)); // "3"
Swlang is an object-oriented language featuring classes, inheritance, polymorphism etc. To create classes use the keyword ramani
which is the swahili word for blueprint
. The reason I chose this name is because I think of classes as blueprints for an object with its properties and behaviours.
ramani Mnyama {
chombo jina;
chombo miaka;
tembea() {
sema("Mimi natembea");
}
ongea() {
sema("Mimi naongea");
}
}
chombo ndovu = Mnyama();
ndovu.jina = "Ndovu";
ndovu.miaka = 20;
Swlang also supports inheritance.
ramani Ndege hurithi Mnyama {
prperuka() {
sema("Mimi na peperuka.")
}
}
chombo kanga = Ndege();
kanga.jina = "Kanga";
kanga.miaka = 22;