Basics
The first Silk program
As usual, The first program is ”Hello World“, here is a very simple program which will print "Hello World":
main(){ print("Hello, World!");
}
A Silk program basically consists of the following parts:
Include commands, functions, classes, variables,
statements(keywords, identifiers) and comments. Every full silk
program should have a function called "main", which is always
called when the program first executes.
Let's take a look at a full silk program:
//include the base class file
#include "HttpServer.si"
//create a new class
class CMyHttpServer(port){
//derived from base class
self=CHttpServer(port);}
//get current directory
func get_curdir(){
curfile=_getargv()[0]; pos=curfile.rfind("\\");
if(pos>=0) return curfile.substr(0,pos+1);
return "";}
main()
{ server=CMyHttpServer(80);
server.set_defaultdir(get_curdir()+"webpages","index.html"); server.run();
}
1.
Keywords, the keywords of Silk, it's similar to C:
main, if, else, while, break, return, continue, for, null,
true, false, func, class, print, sprintf, printf, self, global, static
includes the following built-in function names:
_input, _getargv,_copy, _loadlib, _freelib, _calllib, _createthread, _createlock, _lock, _unlock, _len, _str, _int, _float, _type, _fun
2.
Identifiers, identifier is a name used to identify a variable, function and class, which has the following rules, it's also similar to C:
- An identifier starts with a letter A to Z, a to z, or an underscore '_'
- Any other parts consists of letters, digits (0 to 9) and underscores
- Identifier is case sensitive
- Identifier cannot be the same as any one of the above keywords
Valid identifier:strName, _nCount, str_name2
Invalid identifier:Name#12, my-Name, 2Name, if, Jack&Jones,
N.a.m.e
3.
Semicolons In Silk program, the semicolon is a statement terminator.
Basically, each individual statement must be ended with a
semicolon, At the end of the class/function defination, semicolon can
be omitted.
4.
Comments
It's the same as C.
The single line comment starts with //, and the multiline comments start with /* and terminate with the */:
/*
The function to caculate fibonacci sequence
parameter: n
return: fibonacci sequence
*/
func Fibonacci(n)
{
if (n == 0)
return 0;
else if (n <= 2)
return 1;
else
return Fibonacci(n-1)+Fibonacci(n-2);//Recursion
}
main()
{
//print the top 20 fibonacci sequence
for(i=0;i<20;i++)
print(Fibonacci(i));
}