A script language of preemptive scheduling coroutine in single thread

View the Project on GitHub Water-Melon/Melang


Function is used to encapsulate a series of calculations and algorithms.

There are two mode:



@funcname (parameters) {

funcname is the name of function, it has to follow the rule:

start with letter or underscore and the rest part can be number, letter or underscore.

parameters is a series of variables, like: name, age or anything you need.


@plus(a, b)
  return a+b;

Here is a function named plus. It has two parameters named a and b. And its function is to calculate the sum of two parameters.




funcname is the name of defined function.

arguments is a series of values.


@plus(1, 2);

plus is the funcname, and 1, 2 correspond to parameter a and b.


Let’s see an example at first.

  return data + 100;

a = 0;
a = foo(a);

This program just modified a from 0 to 100, and that is what we wish it to be.

But the last statement is a little bit clumsy. How to make it better?

  data += 100;

a = 0;

We change function parameter data to be &data.

Operator & makes argument a to be a reference variable named data in function foo. Every modification on data will directly effect on a.


  1. & only can be used on the parameters of function definition.
  2. array and object will be treated as reference all the time no matter & given or not.

Lack of arguments



This program will not report error. Because interperter will set nil to data automatically.

Return function

Let’s see an example:

@foo() {
  @bar() {
  return bar;


Guess what happened?

‘bar’ will be printed on terminal.

It is possible because function definition is a statement. So we can define a function in another function’s definition.

Variable arguments

In most programming languages, they all support variable arguments. In melang, we have an alternative plan.

  s = 'argument list: ';
  for (i = 0; i < @mln_size(args); ++i) {
      s += args[i] + ' ';

@log('this', 'is', 'a', 'variable', 'arguments', 'example');

The result of this program is:

argument list: this is a variable arguments example 

The key point is variable args. It’s a internal variable in Melang function.

It is an array to record every arguments passed to this function.

Function mln_size returns the number of array elements.