Skip to content

Libraries

HEMMOUDA Aymane edited this page Feb 2, 2024 · 7 revisions

By now it should be clear that the language is very basic and simple, and as mentioned before, it is the libraries that come with the language that provide it with some more power (the libs are also written in Malang of course).

The language comes with five libraries out of the box, and here they are:

The standard library [std]

include std

This is the standard basic library that contains some of the basic functions that you'd expect to have in each programming language, like comparison, "if statements", logical operators, and others..

It also contains the constants TRUE and FALSE, each equal to 1 and 0 respectively.

The math library [math]

include math

This is the math library, it contains the important mathematical constants as well some basic functions, like % modulo, floor, ceil, round, and others..

It also contains some math functions, like sin, cos, exp and others... But ATM one very important one which is the log is not yet implemented (along side some other functions). Im open to suggestions as to how to do so (the implementation should be able to handle arbitrary big numbers).

The random library [rand]

include rand

This library contains a basic LCG pseudo-random generator, and some other functions that are built on top of it; like rand(a, b) that would return a random number between a and b, and so on..

Unfortunately, the seed is always set to the same thing, and so it always produces the same sequence of random numbers..

The assertion library [assert]

include assert

A library that has a single function, one that asserts that a condition is TRUE. It does that by dividing on it, if it's FALSE then it's a ZeroDivisionError, lol.

The string library [str]

include str

And finally, the best for last, the string library. This library contains functions that allow for manipulation of Strings!

But you said the only available data type is number

Yes, but what are Strings but a series of small numbers (chars or bytes)? We only need a way to gather those small numbers in one a single number.

That is achieved by shifting the number that holds the string by 8 bits, or 2^8, or 256 to the left, for each new char that we want to add, and then adding the new ASCII char's value.

And that is basically how Strings exist in the language. I went into greater detail about that in the library file it self, you can read more about it there.

But this also means that the output is still just a Number?

Yes, and for example:

include str

print("Hello, World!")

this program would return 1468369091346906859060166438166794. That big number is the the 13 characters of the string Hello, World! where each character is shifted to the left by 258. Returning it just as number is not as amusing, and so the -i or --interpret flag can be specified to malang.py for it to try and interpret the result as a String.


Goto previous page.