Melang

Logo

A script language of time-sharing scheduling coroutine in single thread

View the Project on GitHub Water-Melon/Melang

System

Import

sys = Import('sys');
print

Print argument.

sys.print(var);

Input:

Return value:

dump

Output detail of the given argument.

This is a built-in function, not implemented in dynamic library.

Dump(var);

Input:

Return value:

Example

sys = Import('sys');

a = [1, 2, 3];
sys.print(a);
Dump(a);

The output is:

[1, 2, 3, ]
  var <Var>    Refer  Alias name: var  valueRef: 2, udata <0x0>, func <0x0>, notModify: false
    <ARRAY>
    ALL ELEMENTS:
      Index: 0
      Value:
        Normal  valueRef: 1, udata <0x0>, func <0x0>, notModify: false
        <INT> 1
      Index: 1
      Value:
        Normal  valueRef: 1, udata <0x0>, func <0x0>, notModify: false
        <INT> 2
      Index: 2
      Value:
        Normal  valueRef: 1, udata <0x0>, func <0x0>, notModify: false
        <INT> 3
    KEY ELEMENTS:
    Refs: 2
size

Get array length.

sys.size(&array);

Input:

Return value:

Example:

sys = Import('sys');

sys.print(sys.size([1, 2, 3]));

Output:

3
is_int

Find whether the type of a variable is integer.

sys.is_int(var);

Input:

Return value:

Example:

sys = Import('sys');

sys.print(sys.is_int(1));
sys.print(sys.is_int('1'));

Output:

true
false
is_real

Find whether the type of a variable is real.

sys.is_real(var);

Input:

Return value:

Example:

sys = Import('sys');

sys.print(sys.is_real(1));
sys.print(sys.is_real(1.0));

Output:

false
true
is_str

Find whether the type of a variable is string.

sys.is_str(var);

Input:

Return value:

Example:

sys.print(sys.is_str('123'));

Output:

true
is_nil

Find whether the type of a variable is nil.

sys.is_nil(var);

Input:

Return value:

Example:

sys.print(sys.is_nil(0));
sys.print(sys.is_nil(nil));

Output:

false
true
is_bool

Find whether the type of a variable is bool.

sys.is_bool(var);

Input:

Return value:

Example:

sys.print(sys.is_bool(0));
sys.print(sys.is_bool(false));

Output:

false
true
is_obj

Find whether the type of a variable is object.

is_obj(var);

Input:

Return value:

Example:

sys = Import('sys');

set {}
o = $set;
sys.print(sys.is_obj(o));

Output:

true
is_func

Find whether the type of a variable is function.

sys.is_func(var);

Input:

Return value:

Example:

sys = Import('sys');

@foo() {}
sys.print(sys.is_func(foo));

Output:

true
is_array

Find whether the type of a variable is array.

sys.is_array(var);

Input:

Return value:

Example:

sys = Import('sys');

sys.print(sys.is_array([1, 2]));
sys.print(sys.is_array(['key1': 1, 'key2':2]));

Output:

true
true
int

Convert the variable to an integer value.

sys.int(var);

Input:

Return value:

Example:

sys = Import('sys');

sys.print(sys.int(1.2));
sys.print(sys.int('1'));

Output:

1
1
bool

Convert the variable to a boolean value.

sys.bool(var);

Input:

Return value:

Example:

sys = Import('sys');

sys.print(sys.bool(1.2));
sys.print(sys.bool(0));
sys.print(sys.bool(nil));
sys.print(sys.bool([]));
sys.print(sys.bool(''));

Output:

true
false
false
false
false
real

Convert the variable to a real number.

sys.real(var);

Input:

Return value:

Example:

sys = Import('sys');

sys.print(sys.real(1));
sys.print(sys.real('1.2'));

Output:

1.000000
1.200000
str

Convert the variable to a string.

sys.str(var);

Input:

Return value:

Example:

sys = Import('sys');

Dump(sys.str(1));
Dump(sys.str(1.2));

Output:

  var <Var>    Refer  Alias name: var  valueRef: 1, udata <0x0>, func <0x0>, notModify: false
    <STRING> '1'
  var <Var>    Refer  Alias name: var  valueRef: 1, udata <0x0>, func <0x0>, notModify: false
    <STRING> '1.200000'
obj

Convert the array or object to an object.

sys.obj(var);

Input:

Return value:

Example:

Dump(sys.obj([1, 2]));
Dump(sys.obj(['name': 'Tom', 'age': 18]));

Output:

  var <Var>    Refer  Alias name: var  valueRef: 1, udata <0x0>, func <0x0>, notModify: false
    <OBJECT> In Set '<anonymous>'  setRef: <unknown> objRef: 2
  var <Var>    Refer  Alias name: var  valueRef: 1, udata <0x0>, func <0x0>, notModify: false
    <OBJECT> In Set '<anonymous>'  setRef: <unknown> objRef: 2
      Normal  Alias name: name  valueRef: 1, udata <0x0>, func <0x0>, notModify: false
      <STRING> 'Tom'
      Normal  Alias name: age  valueRef: 1, udata <0x0>, func <0x0>, notModify: false
      <INT> 18

As we can see in output, common array will not generate any properties in object.

array

Convert the array or object to an array.

sys.array(var);

Input:

Return value:

Example:

sys = Import('sys');

Human {
  name;
  age;
}
o = $Human;
o.name = 'Tom';
o.age = 18;
arr = sys.array(o);
sys.print(arr);
sys.print(arr['name']);

Output:

[Tom, 18, ]
Tom
keys

Return all the keys or a subset of the keys of an array.

sys.keys(&array);

Input:

Return value:

Example:

sys = Import('sys');

a = ['name': 'Tom', 'age': 18];
sys.print(sys.keys(a));

Output:

[name, age, ]
merge

Merge two arrays

sys.merge(array1, array2);

Input:

Return value:

Example:

sys = Import('sys');

a = ['name': 'Tom', 'age': 18];
b = ['name': 'Sam', 'age': 19];
c = [1, 2, 3];
d = [2, 3, 4];
sys.print(sys.merge(a, b));
sys.print(sys.merge(c, d));
ret = sys.merge(b, c);
sys.print(ret);
sys.print(ret['name']);

Output:

[Sam, 19, ]
[1, 2, 3, 2, 3, 4, ]
[Sam, 19, 1, 2, 3, ]
Sam
has

Checks whether the symbol string thing in owner.

sys.has(owner, thing);

Input:

There are threee types of owner:

The thing is an array key or a string name.

Return value:

There are values will be returned:

Example:

sys = Import('sys');

a = ['name': 'Tom', 'age': 18];
sys.print(sys.has(a, 'name'));

Output:

true
type

Get the type of input argument.

sys.type(var);

Input:

Return value:

Example:

sys = Import('sys');

a = ['name': 'Tom', 'age': 18];
SetA {}
@foo () {}

sys.print(sys.type(1));
sys.print(sys.type(1.2));
sys.print(sys.type('abc'));
sys.print(sys.type([1, 2]));
sys.print(sys.type($SetA));
sys.print(sys.type(foo));
sys.print(sys.type(sys.obj(a)));

Output:

int
real
string
array
SetA
function
object
getter

Get property value from an object.

sys.getter(&obj, prop);

Input:

Return value:

Example:

sys = Import('sys');

Human {
  name;
}
o = $Human;
o.name = 'Tom';
sys.print(sys.getter(o, 'name'));

Output:

Tom
setter

Set a property with its value in an object.

sys.setter(&obj, prop, &val);

Input:

Return value:

Example:

sys = Import('sys');

Human {
  name;
}
o = $Human;
o.name = 'Tom';
sys.print(sys.setter(o, 'age', 18));
sys.print(sys.setter(o, 'age'));

Output:

18
18
mkdir

Create a directory.

sys.mkdir(path, mode);

Input:

Return value:

Example:

sys = Import('sys');

sys.mkdir('/tmp/aaa');
$ ls -d /tmp/aaa

Output:

/tmp/aaa
remove

Remove directory from file system.

sys.remove(path);

Input:

Return value:

Example:

sys = Import('sys');

sys.print(sys.remove('/tmp/aaa'));

Output:

true
exist

Check if the file or directory exists.

sys.exist(path);

Input:

Return value:

Example:

sys = Import('sys');

sys.print(sys.exist('/tmp'));

Output:

true
lsdir

List all files and directories under the specified path.

sys.lsdir(path);

Input:

Return value:

Example:

sys = Import('sys');

sys.print(sys.path('/'));

Output:

[tmp, run, etc, lib64, home, sbin, proc, sys, ., usr, lost+found, root, boot, media, .., dev, bin, opt, lib, mnt, var, srv, ]
isdir

Check the given path is directory or not.

sys.isdir(path);

Input:

Return value:

Example:

sys = Import('sys');

sys.print(sys.exist('/'));

Output:

true
time

Get the current time seconds.

sys.time();

Input: none

Return value:

Example:

sys.print(sys.time());

Output:

1628567103
utctime

Return a UTC time string.

sys.utctime(tm);

Input:

Return value:

Example:

sys.print(sys.utctime(sys.time()));

Output:

10/25/2023 02:56:32 UTC
cron

Parse cron format expression and get the next allowed timestamp.

sys.cron(exp, timestamp);

Input:

Return value:

Example:

sys = Import('sys');

tm = sys.time();
sys.print(tm);
sys.print(sys.cron('* * * * *', tm));

Output:

1628676762
1628676822
diff

Compute the difference of arrays.

sys.diff(&array1, &array2);

Input:

Return value:

Example:

sys = Import('sys');

sys.print(sys.diff([1,2,3,4,5], [2,4,5]));

Output:

[1, 3, ]
key_diff

Compute the difference of arrays by keys.

sys.key_diff(&array1, &array2);

Input:

Return value:

Example:

sys = Import('sys');

sys.print(sys.key_diff(['aaa':1,2,'ccc':3,'ddd':4, 5], ['aaa':1,2,3]););

Output:

[3, 4, ]
exec

Execute shell command in Melang.

sys.exec(cmd, bufsize, pid, uid, gid, qname);

Description: Execute shell command or list all running commands.

Input:

Return value:

Example:

sys = Import('sys');

sys.exec('ls /');

Output:

bin
boot
dev
etc
...
import

Import dynamic library extension into current scope. Dynamic library may contain functions, collections, variables, etc.

This is a built-in function.

Import(name);

Input:

Return value:

Example:

Import('test');
//test.c -> test.so or test.dll in current directory
#include <stdio.h>
#include "mln_lang.h"
int init(mln_lang_ctx_t *ctx)
{
    printf("%s\n", __FUNCTION__);
    return 0;
}

Output:

init
msleep

Execute shell command in Melang.

sys.msleep(msec);

Input:

Return value:

Example:

sys = Import('sys');

sys.msleep(1000); //1 second
path

For the path given by the parameter, escape the starting @ symbol to the current directory path of the script.

sys.path(path);

Input:

Return value:

Example:

// xxx/test.m

sys = Import('sys');

sys.path('@/a.m');

Run script

melang xxx/test.m

The output is

xxx/a.m