From 6f137cd6eafacb851f85e8d13ccdfa7e2366501e Mon Sep 17 00:00:00 2001 From: Rafal Kupiec Date: Tue, 17 Jul 2018 18:21:06 +0200 Subject: [PATCH] Update page 'P# 1.0 Draft Specification' --- P%23-1.0-Draft-Specification.md | 38 ++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/P%23-1.0-Draft-Specification.md b/P%23-1.0-Draft-Specification.md index 5bfc3dc..64c7085 100644 --- a/P%23-1.0-Draft-Specification.md +++ b/P%23-1.0-Draft-Specification.md @@ -205,7 +205,7 @@ If the string is enclosed in double-quotes, P# will interpret the following esca When a string is specified in double quotes, variables are also parsed within it. ### 5.10. Void Values -Void is an empty data type and it is used as a return type of functions and methods that return no value. +Void is an empty data type and it is used as a return type of functions and methods that return no value. ### 5.11. Null Values Null is a special data type. Basically, the data type means non existent, not known or empty. In P#, a variable is NULL in three cases: @@ -260,6 +260,42 @@ An example: int $integer = 7 + (int) $str; // contains value: 52 %> +### 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. + +#### 5.14.1. Assign By Reference +In the first of these, P# references allow to make two variables refer to the same content. + + <% + int &$a, $b; + $a = $b; + %> + +It means that $a and $b point to the same content. + +#### 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. + + <% + int $table[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; + + int &refFunc(int $i) { + return $table[$i]; // returns a reference to the ith element + } + %> + +#### 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. + + <% + int func(int &$val) { + $val++; + } + int $var = 5; + func($var); + print("\$var = $var"); // outputs "$var = 6" + %> + ## 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: * Unary operators - the unary operators take one operand and use either prefix notation (such as –$x) or