Update page 'P# 1.0 Draft Specification'
@@ -71,7 +71,7 @@ A variable is an identifier, which holds a value. In programming we say that we
|
|||||||
A constant is an identifier for a value which cannot change during the execution of the script. Constants are created with the define() function. Constants differ from variables; we cannot assign a different value to an existing constant.
|
A constant is an identifier for a value which cannot change during the execution of the script. Constants are created with the define() function. Constants differ from variables; we cannot assign a different value to an existing constant.
|
||||||
|
|
||||||
## 4. Basic concept
|
## 4. Basic concept
|
||||||
All the P# code is surrounded by two delimiters, <% and %>. Everything behind these two delimiters is considered as text and output to stdout. Thanks to that P# code can be easily placed into HTML code.
|
TODO
|
||||||
|
|
||||||
### 4.1. Script startup
|
### 4.1. Script startup
|
||||||
TODO
|
TODO
|
||||||
@@ -82,9 +82,7 @@ If the return type of the script's entry point method is int, the value returned
|
|||||||
### 4.3. Console Output
|
### 4.3. Console Output
|
||||||
Output from P# scripts is sent to the console is CLI SAPI is being used. If same script is launched when using CGI or FastCGI SAPIs, the output will be sent to the browser.
|
Output from P# scripts is sent to the console is CLI SAPI is being used. If same script is launched when using CGI or FastCGI SAPIs, the output will be sent to the browser.
|
||||||
|
|
||||||
<%
|
print('Hello world from P#');
|
||||||
print('Hello world from P#');
|
|
||||||
%>
|
|
||||||
|
|
||||||
The print keyword does not add a new line to the output, script author has to put it manually.
|
The print keyword does not add a new line to the output, script author has to put it manually.
|
||||||
|
|
||||||
@@ -113,10 +111,8 @@ P# provides a set of predefined types called the simple types. The simple types
|
|||||||
### 4.7. Variable Interpolation
|
### 4.7. Variable Interpolation
|
||||||
Variable interpolation is replacing variables with their values inside string literals. Another names for variable interpolation are: variable substitution or variable expansion.
|
Variable interpolation is replacing variables with their values inside string literals. Another names for variable interpolation are: variable substitution or variable expansion.
|
||||||
|
|
||||||
<%
|
int $age = 20;
|
||||||
int $age = 20;
|
print("I'm $age years old.");
|
||||||
print("I'm $age years old.");
|
|
||||||
%>
|
|
||||||
|
|
||||||
In above example, the $age variable is replaced with the value 20 in the string enclosed by double quotes. However, this does not work if single quotes are used. In this case, no interpolation happens and no special characters are working.
|
In above example, the $age variable is replaced with the value 20 in the string enclosed by double quotes. However, this does not work if single quotes are used. In this case, no interpolation happens and no special characters are working.
|
||||||
|
|
||||||
@@ -136,19 +132,15 @@ The bool type represents Boolean logical quantities. The possible values of type
|
|||||||
### 5.2. Callback Values
|
### 5.2. Callback Values
|
||||||
Callbacks can be denoted in P# language by callback type hint. This pseudo-type is used mostly for anonymous functions.
|
Callbacks can be denoted in P# language by callback type hint. This pseudo-type is used mostly for anonymous functions.
|
||||||
|
|
||||||
<%
|
callback $callme = int() {
|
||||||
callback $callme = int() {
|
return 7;
|
||||||
return 7;
|
}
|
||||||
}
|
int $var = $callme(); // value is 7
|
||||||
int $var = $callme(); // value is 7
|
|
||||||
%>
|
|
||||||
|
|
||||||
What is more, P# allows a language constructs in the context of a function call:
|
What is more, P# allows a language constructs in the context of a function call:
|
||||||
|
|
||||||
<%
|
callback $echo = 'print';
|
||||||
callback $echo = 'print';
|
$echo('Hello world'); // Calls the print function on the fly
|
||||||
$echo('Hello world'); // Calls the print function on the fly
|
|
||||||
%>
|
|
||||||
|
|
||||||
### 5.3. Character Values
|
### 5.3. Character Values
|
||||||
The char type holds a single character. The first 128 code points (0–127) of Unicode correspond to the letters and symbols on a standard U.S. keyboard. These first 128 code points are the same as those the ASCII character set defines. The second 128 code points (128–255) represent special characters, such as Latin-based alphabet letters, accents, currency symbols, and fractions.
|
The char type holds a single character. The first 128 code points (0–127) of Unicode correspond to the letters and symbols on a standard U.S. keyboard. These first 128 code points are the same as those the ASCII character set defines. The second 128 code points (128–255) represent special characters, such as Latin-based alphabet letters, accents, currency symbols, and fractions.
|
||||||
@@ -162,23 +154,19 @@ P# have standardized integers, which are stored in 8 bytes (64 bits) regardless
|
|||||||
### 5.6. Mixed Values
|
### 5.6. Mixed Values
|
||||||
Mixed pseudo-type indicates that a variable might accept multiple (but not necessarily all) types. It is not a primitive, and thus it cannot be used as typehints.
|
Mixed pseudo-type indicates that a variable might accept multiple (but not necessarily all) types. It is not a primitive, and thus it cannot be used as typehints.
|
||||||
|
|
||||||
<%
|
mixed $var;
|
||||||
mixed $var;
|
$var = NULL;
|
||||||
$var = NULL;
|
var_dump($var); // NULL
|
||||||
var_dump($var); // NULL
|
$var = 'Test';
|
||||||
$var = 'Test';
|
var_dump($var); // string(4) "Test"
|
||||||
var_dump($var); // string(4) "Test"
|
$var = 7;
|
||||||
$var = 7;
|
var_dump($var); // int(7)
|
||||||
var_dump($var); // int(7)
|
|
||||||
%>
|
|
||||||
|
|
||||||
### 5.7. Object Values
|
### 5.7. Object Values
|
||||||
An object pseudo type is used for storing a pointer to the object.
|
An object pseudo type is used for storing a pointer to the object.
|
||||||
|
|
||||||
<%
|
object $system;
|
||||||
object $system;
|
$system = new System();
|
||||||
$system = new System();
|
|
||||||
%>
|
|
||||||
|
|
||||||
### 5.8. Resource Values
|
### 5.8. Resource Values
|
||||||
A resource is a special variable, holding a reference to an external resource, like special handle to opened file, or database connection. Resources are created and used by special functions.
|
A resource is a special variable, holding a reference to an external resource, like special handle to opened file, or database connection. Resources are created and used by special functions.
|
||||||
@@ -216,16 +204,14 @@ Null is a special data type. Basically, the data type means non existent, not kn
|
|||||||
### 5.12. Arrays
|
### 5.12. Arrays
|
||||||
Array is a complex data type which handles a collection of elements. Each of the elements can be accessed by an index. In P#, just like in PHP, arrays are more complex. An array in P# is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more.
|
Array is a complex data type which handles a collection of elements. Each of the elements can be accessed by an index. In P#, just like in PHP, arrays are more complex. An array in P# is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more.
|
||||||
|
|
||||||
<%
|
int $arr1[];
|
||||||
int $arr1[];
|
int $arr2[] = {0, 0}
|
||||||
int $arr2[] = {0, 0}
|
$arr1 = {1, 2, 3, 4, 5};
|
||||||
$arr1 = {1, 2, 3, 4, 5};
|
$arr1[] = 6;
|
||||||
$arr1[] = 6;
|
$arr1[] = {7};
|
||||||
$arr1[] = {7};
|
$arr1[] = {'test' => 8};
|
||||||
$arr1[] = {'test' => 8};
|
$arr1[] = $arr2;
|
||||||
$arr1[] = $arr2;
|
var_dump($arr1);
|
||||||
var_dump($arr1);
|
|
||||||
%>
|
|
||||||
|
|
||||||
Above example will create a multi-dimensional associative array containing integers. The output of var_dump() function should return the following output:
|
Above example will create a multi-dimensional associative array containing integers. The output of var_dump() function should return the following output:
|
||||||
|
|
||||||
@@ -255,10 +241,8 @@ Type casting in P# works much as it does in C/C++ - the name of the desired type
|
|||||||
|
|
||||||
An example:
|
An example:
|
||||||
|
|
||||||
<%
|
string $str = "45";
|
||||||
string $str = "45";
|
int $integer = 7 + (int) $str; // contains value: 52
|
||||||
int $integer = 7 + (int) $str; // contains value: 52
|
|
||||||
%>
|
|
||||||
|
|
||||||
### 5.14. References
|
### 5.14. References
|
||||||
References in P# are a means to access the same variable content by different names. They are not like C pointers; for instance, they does not allow to perform pointer arithmetic using them, they are not actual memory addresses, and so on. Instead, they are symbol table aliases. There are three basic operations performed using references: assigning by reference, passing by reference, and returning by reference.
|
References in P# are a means to access the same variable content by different names. They are not like C pointers; for instance, they does not allow to perform pointer arithmetic using them, they are not actual memory addresses, and so on. Instead, they are symbol table aliases. There are three basic operations performed using references: assigning by reference, passing by reference, and returning by reference.
|
||||||
@@ -266,35 +250,29 @@ References in P# are a means to access the same variable content by different na
|
|||||||
#### 5.14.1. Assign By Reference
|
#### 5.14.1. Assign By Reference
|
||||||
In the first of these, P# references allow to make two variables refer to the same content.
|
In the first of these, P# references allow to make two variables refer to the same content.
|
||||||
|
|
||||||
<%
|
int &$a, $b;
|
||||||
int &$a, $b;
|
$a = $b;
|
||||||
$a = $b;
|
|
||||||
%>
|
|
||||||
|
|
||||||
It means that $a and $b point to the same content.
|
It means that $a and $b point to the same content.
|
||||||
|
|
||||||
#### 5.14.2. Return Reference
|
#### 5.14.2. Return Reference
|
||||||
When a function returns a reference, it returns an implicit pointer to its return value. This way, a function can be used on the left side of an assignment statement.
|
When a function returns a reference, it returns an implicit pointer to its return value. This way, a function can be used on the left side of an assignment statement.
|
||||||
|
|
||||||
<%
|
int $table[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
|
||||||
int $table[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
|
|
||||||
|
|
||||||
int &refFunc(int $i) {
|
int &refFunc(int $i) {
|
||||||
return $table[$i]; // returns a reference to the ith element
|
return $table[$i]; // returns a reference to the ith element
|
||||||
}
|
}
|
||||||
%>
|
|
||||||
|
|
||||||
#### 5.14.3. Pass By Reference
|
#### 5.14.3. Pass By Reference
|
||||||
This is done by making a local variable in a function and a variable in the calling scope referencing the same content.
|
This is done by making a local variable in a function and a variable in the calling scope referencing the same content.
|
||||||
|
|
||||||
<%
|
int func(int &$val) {
|
||||||
int func(int &$val) {
|
$val++;
|
||||||
$val++;
|
}
|
||||||
}
|
int $var = 5;
|
||||||
int $var = 5;
|
func($var);
|
||||||
func($var);
|
print("\$var = $var"); // outputs "$var = 6"
|
||||||
print("\$var = $var"); // outputs "$var = 6"
|
|
||||||
%>
|
|
||||||
|
|
||||||
## 6. Operators
|
## 6. Operators
|
||||||
Expressions are constructed from operands and operators. The operators of an expression indicate which operations to apply to the operands. There are three kinds of operators:
|
Expressions are constructed from operands and operators. The operators of an expression indicate which operations to apply to the operands. There are three kinds of operators:
|
||||||
@@ -336,9 +314,7 @@ What is the outcome of this expression, 9 or 1? The multiplication, deletion, an
|
|||||||
### 6.3. Assignment Operator
|
### 6.3. Assignment Operator
|
||||||
The assignment operator = assigns a value to a variable. A variable is a placeholder for a value. In P#, like in PHP or Perl, a variable begins with a $ character.
|
The assignment operator = assigns a value to a variable. A variable is a placeholder for a value. In P#, like in PHP or Perl, a variable begins with a $ character.
|
||||||
|
|
||||||
<%
|
$x = 1;
|
||||||
$x = 1;
|
|
||||||
%>
|
|
||||||
|
|
||||||
### 6.4. Arithmetic Operators
|
### 6.4. Arithmetic Operators
|
||||||
P# offers the following arithmetic operators:
|
P# offers the following arithmetic operators:
|
||||||
@@ -351,9 +327,7 @@ P# offers the following arithmetic operators:
|
|||||||
### 6.5. Concatenating Strings
|
### 6.5. Concatenating Strings
|
||||||
In P# concatenating strings is nothing more than adding strings to each other with the addition operator.
|
In P# concatenating strings is nothing more than adding strings to each other with the addition operator.
|
||||||
|
|
||||||
<%
|
string $str = "Hello" + ' ' + 'World!';
|
||||||
string $str = "Hello" + ' ' + 'World!';
|
|
||||||
%>
|
|
||||||
|
|
||||||
### 6.6. Boolean Operators
|
### 6.6. Boolean Operators
|
||||||
In P#, there are and (&&), or (||), xor (^^) and negation (!) boolean operators. With boolean operators logical operations are performed. These are often used with if and while keywords.
|
In P#, there are and (&&), or (||), xor (^^) and negation (!) boolean operators. With boolean operators logical operations are performed. These are often used with if and while keywords.
|
||||||
@@ -384,10 +358,8 @@ The compound assignment operators consist of two operators. They are shorthand o
|
|||||||
### 6.10. Ternary Operator
|
### 6.10. Ternary Operator
|
||||||
Unlike the standard PHP engine, the ternary operator under P# evaluates left-to-right, associates right-to-left. That is, there is no need for parenthesis for nested ternary operators.
|
Unlike the standard PHP engine, the ternary operator under P# evaluates left-to-right, associates right-to-left. That is, there is no need for parenthesis for nested ternary operators.
|
||||||
|
|
||||||
<%
|
string $var = true ? 'true' : false? 't' : 'f';
|
||||||
string $var = true ? 'true' : false? 't' : 'f';
|
print($var);
|
||||||
print($var);
|
|
||||||
%>
|
|
||||||
|
|
||||||
The above expression in PHP will output 't' and this is an unexpected result, but if the same code run using the P# will output 'true' as expected.
|
The above expression in PHP will output 't' and this is an unexpected result, but if the same code run using the P# will output 'true' as expected.
|
||||||
|
|
||||||
@@ -402,49 +374,43 @@ The if keyword is used to check if an expression is true. If it is true, a state
|
|||||||
|
|
||||||
Often there is a need to execute a statement if a certain condition is met, and a different statement if the condition is not met. This is what else is for. Else extends an if statement to execute a statement in case the expression in the if statement evaluates to FALSE. For example, the following code would display a is greater than b if $a is greater than $b, and a is NOT greater than b otherwise:
|
Often there is a need to execute a statement if a certain condition is met, and a different statement if the condition is not met. This is what else is for. Else extends an if statement to execute a statement in case the expression in the if statement evaluates to FALSE. For example, the following code would display a is greater than b if $a is greater than $b, and a is NOT greater than b otherwise:
|
||||||
|
|
||||||
<%
|
if ($a > $b) {
|
||||||
if ($a > $b) {
|
print("a is greater than b");
|
||||||
print("a is greater than b");
|
} else {
|
||||||
} else {
|
print("a is NOT greater than b");
|
||||||
print("a is NOT greater than b");
|
}
|
||||||
}
|
|
||||||
%>
|
|
||||||
|
|
||||||
Additionally, P# implements known from PHP, the elseif statement. as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to FALSE. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to TRUE. For example, the following code would display a is bigger than b, a equal to b or a is smaller than b:
|
Additionally, P# implements known from PHP, the elseif statement. as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to FALSE. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to TRUE. For example, the following code would display a is bigger than b, a equal to b or a is smaller than b:
|
||||||
|
|
||||||
<%
|
if ($a > $b) {
|
||||||
if ($a > $b) {
|
print("a is bigger than b");
|
||||||
print("a is bigger than b");
|
} elseif ($a == $b) {
|
||||||
} elseif ($a == $b) {
|
print("a is equal to b");
|
||||||
print("a is equal to b");
|
} else {
|
||||||
} else {
|
print("a is smaller than b");
|
||||||
print("a is smaller than b");
|
}
|
||||||
}
|
|
||||||
%>
|
|
||||||
|
|
||||||
### 7.2. Switch Statement
|
### 7.2. Switch Statement
|
||||||
The switch statement is a selection control flow statement. It allows the value of a variable or expression to control the flow of program execution via a multiway branch. It creates multiple branches in a simpler way than using the if, elseif statements. The switch statement works with two other keywords: case and break. The case keyword is used to test a label against a value from the round brackets. If the label equals to the value, the statement following the case is executed. The break keyword is used to jump out of the switch statement. There is an optional default statement. If none of the labels equals the value, the default statement is executed.
|
The switch statement is a selection control flow statement. It allows the value of a variable or expression to control the flow of program execution via a multiway branch. It creates multiple branches in a simpler way than using the if, elseif statements. The switch statement works with two other keywords: case and break. The case keyword is used to test a label against a value from the round brackets. If the label equals to the value, the statement following the case is executed. The break keyword is used to jump out of the switch statement. There is an optional default statement. If none of the labels equals the value, the default statement is executed.
|
||||||
|
|
||||||
<%
|
$domain = 'de';
|
||||||
$domain = 'de';
|
switch ($domain) {
|
||||||
switch ($domain) {
|
case 'us':
|
||||||
case 'us':
|
print("United States\n");
|
||||||
print("United States\n");
|
break;
|
||||||
break;
|
case 'de':
|
||||||
case 'de':
|
print("Germany\n");
|
||||||
print("Germany\n");
|
break;
|
||||||
break;
|
case 'sk':
|
||||||
case 'sk':
|
print("Slovakia\n");
|
||||||
print("Slovakia\n");
|
break;
|
||||||
break;
|
case 'pl':
|
||||||
case 'pl':
|
print("Poland\n");
|
||||||
print("Poland\n");
|
break;
|
||||||
break;
|
default:
|
||||||
default:
|
print("Other\n");
|
||||||
print("Other\n");
|
break;
|
||||||
break;
|
}
|
||||||
}
|
|
||||||
%>
|
|
||||||
|
|
||||||
### 7.3. While Loop
|
### 7.3. While Loop
|
||||||
The while is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. This is the general form of the while loop:
|
The while is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. This is the general form of the while loop:
|
||||||
@@ -454,133 +420,83 @@ The while is a control flow statement that allows code to be executed repeatedly
|
|||||||
|
|
||||||
The while loop executes the statement when the expression is evaluated to true. The statement is a simple statement terminated by a semicolon or a compound statement enclosed in curly brackets.
|
The while loop executes the statement when the expression is evaluated to true. The statement is a simple statement terminated by a semicolon or a compound statement enclosed in curly brackets.
|
||||||
|
|
||||||
<%
|
int $i = 0;
|
||||||
int $i = 0;
|
while ($i < 5) {
|
||||||
while ($i < 5) {
|
print("P# Language\n");
|
||||||
print("P# Language\n");
|
$i++;
|
||||||
$i++;
|
}
|
||||||
}
|
|
||||||
%>
|
|
||||||
|
|
||||||
### 7.4. Do-While Loop
|
### 7.4. Do-While Loop
|
||||||
The do while loop is a version of the while loop. The difference is that this version is guaranteed to run at least once.
|
The do while loop is a version of the while loop. The difference is that this version is guaranteed to run at least once.
|
||||||
|
|
||||||
<%
|
int $count = 0;
|
||||||
int $count = 0;
|
do {
|
||||||
do {
|
print("$count\n");
|
||||||
print("$count\n");
|
} while ($count != 0)
|
||||||
} while ($count != 0)
|
|
||||||
%>
|
|
||||||
|
|
||||||
In above example, first the iteration is executed and then the truth expression is evaluated.
|
In above example, first the iteration is executed and then the truth expression is evaluated.
|
||||||
|
|
||||||
### 7.5. For Loop
|
### 7.5. For Loop
|
||||||
The for loop does the same thing as the while loop. Only it puts all three phases, initialization, testing and updating into one place, between the round brackets. It is mainly used when the number of iteration is know before entering the loop.
|
The for loop does the same thing as the while loop. Only it puts all three phases, initialization, testing and updating into one place, between the round brackets. It is mainly used when the number of iteration is know before entering the loop.
|
||||||
|
|
||||||
<%
|
string $days[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
|
||||||
string $days[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
|
"Saturday", "Sunday" };
|
||||||
"Saturday", "Sunday" };
|
int $len = sizeof($days);
|
||||||
int $len = sizeof($days);
|
|
||||||
|
|
||||||
for (int $i = 0; $i < $len; $i++) {
|
for (int $i = 0; $i < $len; $i++) {
|
||||||
print("$days[$i]\n");
|
print("$days[$i]\n");
|
||||||
}
|
}
|
||||||
%>
|
|
||||||
|
|
||||||
The three phases are divided by semicolons. First, the $i counter is initiated. The initiation part takes place only once. Next, the test is conducted. If the result of the test is true, the statement is executed. Finally, the counter is incremented. This is one cycle. The for loop iterates until the test expression is false.
|
The three phases are divided by semicolons. First, the $i counter is initiated. The initiation part takes place only once. Next, the test is conducted. If the result of the test is true, the statement is executed. Finally, the counter is incremented. This is one cycle. The for loop iterates until the test expression is false.
|
||||||
|
|
||||||
### 7.6. Foreach Loop
|
### 7.6. Foreach Loop
|
||||||
The foreach construct simplifies traversing over collections of data. It has no explicit counter. The foreach statement goes through the array one by one and the current value is copied to a variable defined in the construct. In P#, it can be used to traverse over an array.
|
The foreach construct simplifies traversing over collections of data. It has no explicit counter. The foreach statement goes through the array one by one and the current value is copied to a variable defined in the construct. In P#, it can be used to traverse over an array.
|
||||||
|
|
||||||
<%
|
string $days[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
|
||||||
string $days[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
|
"Saturday", "Sunday" };
|
||||||
"Saturday", "Sunday" };
|
string $day;
|
||||||
string $day;
|
foreach ($days as $day) {
|
||||||
foreach ($days as $day) {
|
print("$day\n");
|
||||||
print("$day\n");
|
}
|
||||||
}
|
|
||||||
%>
|
|
||||||
|
|
||||||
The usage of the foreach statement is straightforward. The $days is the array that gets iterated through. The $day is the temporary variable that has the current value from the array. The foreach statement goes through all the days and prints them to the console.
|
The usage of the foreach statement is straightforward. The $days is the array that gets iterated through. The $day is the temporary variable that has the current value from the array. The foreach statement goes through all the days and prints them to the console.
|
||||||
|
|
||||||
There is another syntax of the foreach statement. Below form will additionally assign the current element's key to the $key variable on each iteration.
|
There is another syntax of the foreach statement. Below form will additionally assign the current element's key to the $key variable on each iteration.
|
||||||
|
|
||||||
<%
|
string $benelux[] = { 'be' => 'Belgium', 'lu' => 'Luxembourgh', 'nl' => 'Netherlands' };
|
||||||
string $benelux[] = { 'be' => 'Belgium', 'lu' => 'Luxembourgh', 'nl' => 'Netherlands' };
|
string $key, $value;
|
||||||
string $key, $value;
|
foreach ($benelux as (string) $key => $value) {
|
||||||
foreach ($benelux as (string) $key => $value) {
|
print("$key is $value\n");
|
||||||
print("$key is $value\n");
|
}
|
||||||
}
|
|
||||||
%>
|
|
||||||
|
|
||||||
Casting a $key to string in above example can be useful, because P# supports arrays with both numeric and associative indexes. This means that if above array contained additional row with integer-based key, it would be casted to string automatically.
|
Casting a $key to string in above example can be useful, because P# supports arrays with both numeric and associative indexes. This means that if above array contained additional row with integer-based key, it would be casted to string automatically.
|
||||||
|
|
||||||
### 7.7. Break & Continue Statements
|
### 7.7. Break & Continue Statements
|
||||||
The break statement is used to terminate the loop. The continue statement is used to skip a part of the loop and continue with the next iteration of the loop.
|
The break statement is used to terminate the loop. The continue statement is used to skip a part of the loop and continue with the next iteration of the loop.
|
||||||
|
|
||||||
<%
|
int $val;
|
||||||
int $val;
|
while (true) {
|
||||||
while (true) {
|
$val = rand(1, 30);
|
||||||
$val = rand(1, 30);
|
print("$val ");
|
||||||
print("$val ");
|
if ($val == 22) {
|
||||||
if ($val == 22) {
|
break;
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
%>
|
}
|
||||||
|
|
||||||
In above example, endless loop has been defined. There is only one way to jump out of a such loop—using the break statement. Above code assigns a random value from 1 to 30 and prints it. If the value equals to 22, script finishes the endless while loop.
|
In above example, endless loop has been defined. There is only one way to jump out of a such loop—using the break statement. Above code assigns a random value from 1 to 30 and prints it. If the value equals to 22, script finishes the endless while loop.
|
||||||
|
|
||||||
<%
|
int $num = 0;
|
||||||
int $num = 0;
|
while ($num < 1000) {
|
||||||
while ($num < 1000) {
|
$num++;
|
||||||
$num++;
|
if (($num % 2) == 0)
|
||||||
if (($num % 2) == 0)
|
continue;
|
||||||
continue;
|
print("$num ");
|
||||||
print("$num ");
|
}
|
||||||
}
|
|
||||||
%>
|
|
||||||
|
|
||||||
Above example prints a list of numbers that cannot be divided by 2 without a remainder, during a while loop, which iterates through numbers from 1 to 999.
|
Above example prints a list of numbers that cannot be divided by 2 without a remainder, during a while loop, which iterates through numbers from 1 to 999.
|
||||||
|
|
||||||
## 8. Functions
|
## 8. Functions
|
||||||
A function is a piece of code in a larger program. The function performs a specific task. The advantages of using functions are:
|
P# is fully object-oriented programming language. However, it has a support for built-in functions. The built-in functions are part of the P# language.
|
||||||
* Reducing duplication of code
|
|
||||||
* Decomposing complex problems into simpler pieces
|
|
||||||
* Improving clarity of the code
|
|
||||||
* Reuse of code
|
|
||||||
* Information hiding
|
|
||||||
|
|
||||||
There are two basic types of functions. Built-in functions and user defined ones. The built-in functions are part of the P# language. The user defined functions are created by application programmers to cover their needs. Below example shows how to define a function:
|
|
||||||
|
|
||||||
<%
|
|
||||||
void myFunc() {
|
|
||||||
print('Hello world from my first function!');
|
|
||||||
}
|
|
||||||
%>
|
|
||||||
|
|
||||||
### 8.1 Return Keyword
|
|
||||||
The return keyword is used to return a value from the function. A function may or may not return a value.
|
|
||||||
|
|
||||||
### 8.2 Function Arguments
|
|
||||||
Most functions accept arguments. Arguments are values that are sent to the function. The functions process the values and possibly return some outcome.
|
|
||||||
|
|
||||||
<%
|
|
||||||
void myFunc(string $who) {
|
|
||||||
print('Hello ' + $who + ' from my first function!');
|
|
||||||
}
|
|
||||||
|
|
||||||
%>
|
|
||||||
|
|
||||||
### 8.3 Implicit Values
|
|
||||||
The arguments in P# functions may have implicit values. An implicit value is used if no value is provided. P# lets use any complex expressions including function calls, math or string expressions and so on as a default values for function arguments.
|
|
||||||
|
|
||||||
<%
|
|
||||||
void test(string $name = 'P# ' + rand_str(4), int $age = 10 * 2 + 5) {
|
|
||||||
print("Name = $name\n");
|
|
||||||
print("Age = $age\n");
|
|
||||||
}
|
|
||||||
%>
|
|
||||||
|
|
||||||
## 9. Anonymous Functions
|
## 9. Anonymous Functions
|
||||||
Anonymous functions, also known as closures, allow the creation of functions which have no specified name. In P#, anonymous function arguments can take a default value exactly like standard function arguments. The default value associated with the function argument can be any complex expression including function calls.
|
Anonymous functions, also known as closures, allow the creation of functions which have no specified name. In P#, anonymous function arguments can take a default value exactly like standard function arguments. The default value associated with the function argument can be any complex expression including function calls.
|
||||||
@@ -596,27 +512,28 @@ Anonymous functions, also known as closures, allow the creation of functions whi
|
|||||||
Anonymous function can be defined in a method body.
|
Anonymous function can be defined in a method body.
|
||||||
|
|
||||||
## 10. Static Variables & Constant
|
## 10. Static Variables & Constant
|
||||||
P# lets use any complex expressions including function calls as an initialization values for your static variables and constants.
|
P# lets use any complex expressions including method calls as an initialization values for your static variables and constants.
|
||||||
|
|
||||||
<%
|
public void test() {
|
||||||
void test() {
|
/* Random number between 0 and 1023 as the initialization value for the static variable */
|
||||||
/* Random number between 0 and 1023 as the initialization value for the static variable */
|
static int $salt = rand() & 0x3FF;
|
||||||
static int $salt = rand() & 0x3FF;
|
/* Echo the random number and increment it's value by 1*/
|
||||||
/* Echo the random number and increment it's value by 1*/
|
print($salt++ + "\n");
|
||||||
print($salt++ + "\n");
|
}
|
||||||
}
|
|
||||||
|
public void __construct() {
|
||||||
/* Random string of length 6 for our test constant */
|
/* Random string of length 6 for our test constant */
|
||||||
const string MY_CONST = rand_str(6);
|
const string MY_CONST = rand_str(6);
|
||||||
test(); /* a random number between 0 and 1023 [ex: 596 ] */
|
$this->test(); /* a random number between 0 and 1023 [ex: 596 ] */
|
||||||
test(); /* 597 */
|
$this->test(); /* 597 */
|
||||||
test(); /* 598 */
|
$this->test(); /* 598 */
|
||||||
print("MY_CONST value = " + MY_CONST);
|
print("MY_CONST value = " + MY_CONST);
|
||||||
%>
|
}
|
||||||
|
|
||||||
In above example, the static variable $salt takes as its initialization value a random number between 0 and 1023 inclusive generated by the built-in rand() function while the constant MY_CONST takes at its initialization value a random string generated by the built-in rand_str() function. Also note that the 'const' construct can be used outside a class definition.
|
In above example, the static variable $salt takes as its initialization value a random number between 0 and 1023 inclusive generated by the built-in rand() function while the constant MY_CONST takes at its initialization value a random string generated by the built-in rand_str() function. Also note that the 'const' construct can be used outside a class definition.
|
||||||
|
|
||||||
## 11. Object-Oriented Programming
|
## 11. Object-Oriented Programming
|
||||||
There are three widely used programming paradigms there: procedural programming, functional programming, and object-oriented programming. P# supports both functional and object-oriented programming (OOP). This is a programming paradigm that uses objects and their interactions to design applications and computer programs. The basic programming concepts in OOP are:
|
There are three widely used programming paradigms there: procedural programming, functional programming, and object-oriented programming. P# supports only object-oriented programming (OOP). This is a programming paradigm that uses objects and their interactions to design applications and computer programs. The basic programming concepts in OOP are:
|
||||||
* Abstraction
|
* Abstraction
|
||||||
* Polymorphism
|
* Polymorphism
|
||||||
* Encapsulation
|
* Encapsulation
|
||||||
@@ -625,10 +542,8 @@ There are three widely used programming paradigms there: procedural programming,
|
|||||||
### 11.1. Objects in P\#
|
### 11.1. Objects in P\#
|
||||||
Objects are basic building blocks of a P# script. An object is a combination of data and methods. In a OOP program, we create objects. These objects communicate together through methods. Each object can receive messages, send messages, and process data. There are two steps in creating an object. First, a class needs to be defined. A class is a template for an object. It is a blueprint which describes the state and behavior that the objects of the class all share. A class can be used to create many objects. Objects created at runtime from a class are called instances of that particular class.
|
Objects are basic building blocks of a P# script. An object is a combination of data and methods. In a OOP program, we create objects. These objects communicate together through methods. Each object can receive messages, send messages, and process data. There are two steps in creating an object. First, a class needs to be defined. A class is a template for an object. It is a blueprint which describes the state and behavior that the objects of the class all share. A class can be used to create many objects. Objects created at runtime from a class are called instances of that particular class.
|
||||||
|
|
||||||
<%
|
class Demo { }
|
||||||
class Demo { }
|
object $demo_object = new Demo();
|
||||||
object $demo_object = new Demo();
|
|
||||||
%>
|
|
||||||
|
|
||||||
### 11.2. Object Attributes
|
### 11.2. Object Attributes
|
||||||
Object attributes are the data bundled in an instance of a class. The object attributes are called instance variables or member fields. An instance variable is a variable defined in a class, for which each object in the class has a separate copy.
|
Object attributes are the data bundled in an instance of a class. The object attributes are called instance variables or member fields. An instance variable is a variable defined in a class, for which each object in the class has a separate copy.
|
||||||
@@ -673,6 +588,29 @@ Methods are functions defined inside the body of a class. They are used to perfo
|
|||||||
|
|
||||||
Example above shows a Circle class. It contains one member field. It is the radius of the circle. The private keyword is an access specifier. It tells that the variable is accessible only from inside of the class. There are also 2 methods definition: area() and setRadius(). The $this keyword is a special variable which is used to access the member fields from methods. The area() method returns the area of a circle.
|
Example above shows a Circle class. It contains one member field. It is the radius of the circle. The private keyword is an access specifier. It tells that the variable is accessible only from inside of the class. There are also 2 methods definition: area() and setRadius(). The $this keyword is a special variable which is used to access the member fields from methods. The area() method returns the area of a circle.
|
||||||
|
|
||||||
|
#### 11.3.1 Return Keyword
|
||||||
|
The return keyword is used to return a value from the method. A method may or may not return a value.
|
||||||
|
|
||||||
|
#### 11.3.2 Method Arguments
|
||||||
|
Most methods accept arguments. Arguments are values that are sent to the method. The methods process the values and possibly return some outcome.
|
||||||
|
|
||||||
|
<%
|
||||||
|
void myFunc(string $who) {
|
||||||
|
print('Hello ' + $who + ' from my first function!');
|
||||||
|
}
|
||||||
|
|
||||||
|
%>
|
||||||
|
|
||||||
|
#### 11.3.3 Implicit Values
|
||||||
|
The arguments in P# methods may have implicit values. An implicit value is used if no value is provided. P# lets use any complex expressions including built-in function or method calls, math or string expressions and so on as a default values for function arguments.
|
||||||
|
|
||||||
|
<%
|
||||||
|
void test(string $name = 'P# ' + rand_str(4), int $age = 10 * 2 + 5) {
|
||||||
|
print("Name = $name\n");
|
||||||
|
print("Age = $age\n");
|
||||||
|
}
|
||||||
|
%>
|
||||||
|
|
||||||
### 11.4. Access Modifiers
|
### 11.4. Access Modifiers
|
||||||
Access modifiers set the visibility of methods and member fields. P# implements three access modifiers: public, protected, and private. The public members can be accessed from anywhere. The protected members can be accessed only within the class itself and by inherited and parent classes. The private members may only be accessed by the class that defines the member. Access modifiers protect data against accidental modifications. They make the programs more robust.
|
Access modifiers set the visibility of methods and member fields. P# implements three access modifiers: public, protected, and private. The public members can be accessed from anywhere. The protected members can be accessed only within the class itself and by inherited and parent classes. The private members may only be accessed by the class that defines the member. Access modifiers protect data against accidental modifications. They make the programs more robust.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user