DEV Community

Cover image for Introduction To A New Coroutine Language
Megabit
Megabit

Posted on

Introduction To A New Coroutine Language

Today's open source project is named Melang.

This is a "new" scripting language that has been around for 6 years.

Now, let me take you into the Melang world.

What is Melang

Melang is a coroutine concurrent scripting language. It is an interpreted, not compiled language.

In Melang, every script task is a coroutine. And multiple script tasks can be processed in the same thread or multithread at the same time.

This is very similar to Go's coroutines, but the coroutines in Melang do not need to actively give up CPU execution rights or call specific functions to indirectly give up CPU execution rights. Instead, the task execution is automatically switched after the interpreter performs certain operations. This also eliminates the need for developers to consider switching timing.

In Melang, all script code logic is synchronized. But it's actually done asynchronously by the interpreter. This help many developers who are not familiar with asynchronous programming to get started.

At the same time, in order to allow developers to start faster, the syntax of the language is very similar to the C, so developers who are familiar with C will easily start developing.

Then let's say Hello, world together!

//hello.m
sys = import('sys');

sys.print("Hello, World!")
Enter fullscreen mode Exit fullscreen mode

execute shell command:

$ melang hello.m
Enter fullscreen mode Exit fullscreen mode

The output is:

Hello, World!
Enter fullscreen mode Exit fullscreen mode

Coroutine Concurrency and Coroutine Pool

In Melang, each script task is isolated from each other. However, sometimes we also need to let different tasks communicate with each other to cooperate, so the message queue module is provided to make this idea possible.

However, based on this mechanism, we can easily derive a new pool model: coroutine pool.

That is, a master coroutine can be used to allocate resources, while a group of worker coroutines can receive resources through the message queue and process them individually. This is very similar to the traditional thread pool model.

Let's take a look at the simplest HTTP server implemented using a coroutine pool:

/* filename: server.m */
net = import('net');
mq = import('mq');

listenfd = net.tcp_listen('127.0.0.1', '80');
for (i = 0; i < 4; ++i) {
    eval('worker.mln', i);
}
while (1) {
    fd = net.tcp_accept(listenfd);
    mq.send('test', fd);
}
Enter fullscreen mode Exit fullscreen mode
/* filename: worker.m */
sys = import('sys');
net = import('net');
mq = import('mq');

sys.print(EVAL_DATA);
while (1) {
    fd = mq.recv('test');
    ret = net.tcp_recv(fd);
    if (ret) {
        net.tcp_send(fd, "HTTP/1.1 200 OK\r\nContent-Length: 1\r\n\r\na\r\n\r\n");
    }fi
    net.tcp_close(fd);
}
Enter fullscreen mode Exit fullscreen mode

The two pieces of code are: the main coroutine (server.m) and the worker coroutine (worker.m).

The main coroutine (server.m) creates 4 worker coroutines (worker.m), and then enters an infinite loop waiting for the establishment of the client TCP.

The worker coroutine (worker.m) is in an infinite loop, receiving the established TCP socket from the main coroutine, then receiving the request from the TCP, then sending the HTTP response, and finally closing the socket.

Next, we can start the script from the command line with the following command:

$ melang server.m -t=2
Enter fullscreen mode Exit fullscreen mode

Here, the meaning of -t=2 is to let the interpreter start two threads to process these 5 coroutines (1-server + 4-worker).

Once started, it can be accessed using tools like curl or ab.

The End

In summary, Melang is a:

  • Scripting language
  • Preemptive task scheduling language
  • Coroutine Concurrency Language
  • Languages where synchronous code is executed asynchronously

In addition, Melang also incorporates features such as reactive programming, operator overloading, reflection, injection, and more.

Thanks for reading!

Top comments (0)