Plezuro/Printable version
This is the print version of Plezuro You won't see this message or any elements not part of the book's content when you print or preview this page. |
The current, editable version of this book is available in Wikibooks, the open-content textbooks collection, at
https://en.wikibooks.org/wiki/Plezuro
introduction
The main purpose of creating of Plezuro was to enhance the software development process. It was created in 2014 while there existed many other scripting languages. However none of them had enough beautiful syntax to write the large pieces of codes without getting bored. Even Plezuro needs some improvements to achieve its main goals.
getting started
One of the main principles of the Plezuro programming language says the following: "The module, the function and the source file are equivalent one to each other." What does it mean exactly? So you can pass arguments to a module in the same way like to a function and also the module returns a value in the same way like a function.
Hello world
editAt the beginning: How can you write the Hello World script in Plezuro? It is so simply:
'Hello World!'
Of course, the design of the language could be even simpler like that:
Hello World!
But that would be much more like a mark-up language (eventually template language), not programming language. So the previous script is not correct. Moreover there exist other possibilities to write a Hello World script, ex.:
'Hello World!'.printl;
In the first script the module just returns a value (string 'Hello World!') and in the last one it prints this string to the standard output and finally returns the empty value (called just 'empty', its symbol: '()').
Comments
editFor simplicity the comments are in c++/Java/c# style. Therefore it enables commenting large blocks of code, in contrary to languages such as python or ruby (Of course you can use multi-line string in both cases but in many situations it outputs an error).
// this is a comment
/*
Another comment
*/
Variables
editLike any (including really bizarre ones almost any) languages Plezuro is based on using of variables. I bet you already know what a variable is (if not it is just a container for a piece of information). So at fist occurrence of a variable you should write the dollar sign ('$') immediately before the variable name. The name is in Java style (it allows using any Unicode letters, digits and underscore but at the beginning there must not be any letter). In the next occurrences the dollar sign is not required. Why do we write so many '$'? It seems like PHP, Perl or bash. The reason is really simple. Just for define the variable scope. It is like the 'var' keyword in Javascript (or in c♯ in case of local variables) but in Plezuro it is just shorted (one character '$' instead of four ones 'var '). It is really important feature in case of short anonymous functions and evaluation of code written in string. So let us write some strange example:
$假借字=4;
假借字+假借字^2
Now, maybe a little less strange:
$x=4;
x+x^2
The nature of the language
editUnlike other programming languages where there is just a list of instructions in a function, Plezuro is based on operators. Commands are separated with a semicolon (';') but it is only one of multiple operators. Even variable assignment and function call in intermediate code are translated into operators. Only brackets ('(', ')') and curly braces ('{', '}') are not translated into operators. What is their use? So brackets are used to change order of operators calculation while curly braces are used to write down code which should be executed in the future (or eventually never). That means curly braces are used to write a function definition, a conditional loop or a conditional expression (in these cases we have also some sort of functions - anonymous functions).
2*(3+4)^5.4
$x = 5;
{2<3}.if{x++}.else{x--};
x
Operators precedence
editTwo-arguments operators:
- ;
- :=
- =
- ,
- <->
- <<
- >>
- ?
- |
- &
- <=>
- >=
- >
- <=
- <
- !=
- ==
- ===
- =~
- -
- %
- *
- /
- ^
- Together:
* ^^ * .
- ..
- :
Single-argument operators: (However here it doesn't matter, important is just the list.
- !
- &&
- **
- #
- --
- --
- @
Important issues
editMoreover you should remember about the following:
- The whitespace characters are not included in the intermediate code (that means you may write any number of spaces, tabs and newlines between the tokens (a number/ string/ name/ operator...), of course you cannot write spaces in the middle of variable names or operators.
- One of very important issues of Plezuro is the tuple. You can use it when passing to a multiple-argument function one variable that stores all these arguments. Moreover you can use it in similar way like in Python.
built-in types
Plezuro provides some built-in classes (types). Moreover you can create your own classes. Each variable is an object of a class, like in Ruby. Therefore all variables are passes into reference. In the next chapter we will discuss the difference between reference and copying.
The list of basic built-in classes with their using:
//number
$x = 2.3e45;
$y = 0xff; //hexadecimal
$z = 072; //octal
$a = 0b11011; //binary
//string
$b = 'aaaaaaaaaaaaaaaaaa';
$c = "wfefwfwf";
$cc = '''xxx
yyy
zzz''';
//list
$d = [1,2,3,4];
//dictionary
$f = #[1,2,3,4];
//set
$g = $[3,4,5];
//error
$h = 1/0;
//class
$i = 1.class;
//package
$j = i.package;
//pair
$k = 3:4;
//procedure
$l = {1+2};
//tuple
(x,y,z,a,b,c,cc,d,e,f,g,h,i,j,k,l)
let's code a little bit
Now you should try to code something in Plezuro.
// a variable initialization
$x = 12.3;
// an incrementation
x++;
// a conditional expression
{x<15}.if({
'x'.dumpl; // dumping a variable to console, very useful when debugging
x *= 3
}).elsif({x==0},{
'x'.dumpl;
x--;
});
'x'.dumpl;
//a loop, printing to console
'\na loop'.printl;
{x<1000}.while({
'x'.dumpl;
x *= 2
});
lists
One of important types of Plezuro is the list. It is an equivalent of .NET List<object> and Java ArrayList<Object>. To create a list, just use square brackets ('[', ']'). In intermediate code it is translated to '::array()'. Elements of a list can be of any type (including list), they are passed by reference, so it is possible that a list contains itself.
$x = [1,2,3,'something',[3,4]];
'x'.dumpl;
x.each({
args.printl
});
x.each({
this.printl
});
As you just have seen above, you can print elements of a list in many ways. The simplest way is to use the dumpl function (it is appropriate to any type in Plezuro). Another one is to use the each method. 'args' means all the arguments passed to a function (in this case a tuple (value, index)) and 'this' means the argument #0 (in this case only value).
Now, I guess, you wonder to get access to a specified element of a list (get and set its value). It is pretty simple, like in other languages use square brackets.
$list = [3,9,4];
'list'.dumpl;
list[0].printl;
list[1] = 'abc';
'list'.dumpl;
});
Lists like any other variables are passed by reference.
$a = [0,0,1];
$b = a;
b[0] = 90;
a
Now, how to sort a list?
$x = [4,5,2,90,452,1,-34,20];
x.sort.printl; //sorting just by value
x.orderBy{args%10}.printl; //sorting by last digit (for negative numbers we assume the last digit is negative)
Now, a little more about the indexing. You can access to a piece of a list in a similar way like in Ruby (using '..' operator). For example a[1..4]. The difference is in this case in Ruby it returns elements from index 1 up to index 4 but in Plezuro it returns elements from index 1 up to index 3. Another interesting feature is when using commas, you can access to disjoint pieces of a list (for example first and fourth element). Indexing may return an empty object, a single variable or a tuple. Of course, you can modify an all piece of a list.