Built-in Functions
Silk has a set of built-in functions to use:
print(a...)print
function can be used to print(send output to stdout) content, it can
accept anything regardless of its type and any number of
positional arguments, print will concatenate all arguments
passed to it, and it inserts a single space between them.
a=9;
print(100, 2*10, "hello", 12.32, a);
Result:
100 20 hello 12.320000 9
If
there are arrays/dictionaries in arguments, print will print all the elements in the object one by one:
a=[1,2];
b={"a":"good"};
print(100,a,b);
Result:
100 [1 , 2 ] {"a" : "good" }
printf(format, a...)printf can print(send output to stdout) formatted string.
format is
the string that contains the text to be print. It can optionally
contain embedded format tags that are replaced by the values specified
in subsequent additional arguments.
a is subsequent additional arguments.
a=9.8;
printf("%s,%d,%f", "good", 100, a);
Result:
good,100,9.8000001
printf supports the following specifier:
-
%c format character(string with 1
character), or the ASCII code of a character
- %s format string
- %d format integer
- %o format octal number
- %x format hexadecimal number
- %X format hexadecimal(capital letters)
- %f format float
You can also use the following Escape characters in format:- \\ Backslash
- \" Double quota
- \a Beep
- \b Backspace
- \n New Line
- \r Carriage Return
- \v Vertical Tab
- \t Horizontal Tab
sprintf(format , a...)sprintf is similar to printf, but it will return a formatted string instead of printing it.
a=9.8;
s=sprintf("%s,%d,%f", "good", 100, a);
print(s);
result:
good,100,9.8000001
sprintf has the same specifiers as printf.
_input()_input can read a line from the specified input device(stdin) and return it as a string.
print("Please input your number:");
s=_input();
print(s);
_getargv()_getargv
can help us get the command-line arguments when you run the silk.
The first argument is always the script file path.
If we run the following silk script file in command, and pass 100 as argument:
Silk.exe c:\test.si 100
The source code of test.si is as follows:
main()
{
args=_getargv();
print("path:",args[0]);
print("param:",args[1]);
}
Result:
path: c:\test.si
param: 100
_int(a)_int can convert string/float to integer.
s="78";
n=_int(s)+100;
print(n, _int(12.32));
Result:
178 12
_float(a)_float can convert string/integer to float.
s="78";
n=_float(s)+100;
print(n, _float(12));
Result:
178.000000 12.000000
_str(a)_str can convert integer/float to string.
i=200;
s=_str(i)+"km";
print(s,_str(12.32)+"m");
Result:
200km 12.32m
_len(a)_len can get the size of string/array/dictionary, it's equal to the method size() of string/array/dictionary.
arr=[0,1,2];
n=_len(arr);
print(n,_len("hello"),arr.size());
Result:
3 5 3
_type(a)_type can get the type of variable, it returns the description of the type.
arr=[1,2];
dict={1:"abc"};
print(_type(10),_type(1.23),_type("ok"),_type(arr),_type(dict));
Result:
INT FLOAT STRING ARRAY DICT
_copy(a)_copy can copy a object and create a new object which has the same elements.
Silk will use 'call by reference' when passing/assigning Array, Dictionary and Class object, this means different variables are pointing to the same object.
So we need to use _copy to copy and create new object.
arr1=[0,1,2];
arr2=arr1;
arr2[0]=100;
print(arr1[0],arr2[0]);
Reuslt:
100 100
Copy arr1 and generate a new one arr2, they are different objects:
arr1=[0,1,2];
arr2=_copy(arr1);
arr2[0]=100;
print(arr1[0],arr2[0]);
Result:
0 100
_createthread, _createlock, _lock, _unlock_createthread, _createlock, _lock and _unlock are used to create and synchronize threads.
Click on
Multiple threads for details.
_loadlib, _freelib, _calllib _loadlib, _freelib and _calllib are used to call the Dll library which is developed with C/C++.
Click on
Dll libraries for details.
_fun(name,a...)除了Besides the above functions, there are a lot of secondary built-in functions which can be used through the function _fun.
name is the name of the secondary function,
a is subsequent arguments of the function.
Secondary built-in functions are as follows:
1. _fun("url_escape", url)
url_escape can encode a string, especially a Url:
s=_fun("url_escape","Hello world!");
print(s)
Result:
Hello%20world%21
2. _fun("url_unescape", url)
url_unescape can decode a string which was encoded by url_escape:
s=_fun("url_unescape","Hello%20world%21");
print(s)
Result:
Hello world!
3. _fun("time_clock")
time_clock returns the number of clock ticks elapsed since the program was launched, normally the time is in microsecond.
t1=_fun("time_clock");
a=0;
for(i=0;i<10000;i++)
{
a++;
}
t2=_fun("time_clock");
print("cost:",t2-t1);
4. _fun("time_now")
time_now can get current time, measured in seconds, and return a dictionary object as follows:
{
“time”: 1605171073, //the time since the Epoch (00:00:00 UTC, January 1, 1970)
“time_str”: "Thu Nov 12 16:52:42 2020", //current time as string
“year”: 2020, //current year
“mon”: 11, //current month
“day”: 12, //current day
“hour”: 16, //current hour
“min”: 58, //current minute
“sec”: 20, //current second
}
Get the current time and print it:
t1=_fun("time_now");
print(t1["time_str"]);
5. _fun("time_rand")
time_rand returns a random integral number:
rand=_fun("time_rand");
print(rand);
6. _fun("time_sleep", ms)
time_sleep
will suspend execution of the program thread until the number of
realtime microseconds specified by the argument ms has elapsed:
print(_fun("time_now")["time_str"]);
_fun("time_sleep",2000); //suspend for 2000 ms (2 seconds)
print(_fun("time_now")["time_str"]);
7. _fun("system", cmd)
system passes the command name or program name specified by cmd to the host environment to be executed:
_fun("system","PAUSE");
_fun("system","notepad.exe");
8. _fun("encrypt", str, password)
encrypt can encrypt the string or data by XOR. it returns the encrypted data.
To decrypt the data, call the function again with the same password:
s=_fun("encrypt","Hi, are you ok?","pass123");//encrypt the string
print(s); //it's just digital gibberish because it was encrypted
s=_fun("encrypt",s,"pass123");//decrypt it with the same password
print(s); //print the plain text
9. _fun("os_platform")
os_platform can get the platform information of the Operate System, we can know Silk is running on the Windows, Linux or Mac:
s=_fun("os_platform");
print(s);
10. _fun("run", source, outputFile, args)
run
can run the silk script code, source is the source code of the silk
script, outputFile is the file which can save the output, normally
we pass empty string to print the output. args is the argument we
pass to the script, it should be a dictionary object:
source="main(){ print(\"hello, world\"); print(_getargv()[0][10]); }";
outputFile="";
args={10:"good"};//pass the argument, it must be a dictionary object.
_fun("run",source,outputFile,args);
Result:
hello, world
good
11.
Math Functions12.
File Functions13.
Socket Functions14.
CGI Functions15.
Server Scripts16.
Regex Functions