diff --git a/P%23-1.0-Draft-Specification.md b/P%23-1.0-Draft-Specification.md index 077ba14..a665538 100644 --- a/P%23-1.0-Draft-Specification.md +++ b/P%23-1.0-Draft-Specification.md @@ -44,7 +44,7 @@ A literal is any notation for representing a value within the PHP source code. T * NULL literal (represents a null value) #### 3.3.2 Operators and punctators -An operator is a symbol used to perform an action on some value +There are several kinds of operators and punctuators. Operators are used in expressions to describe operations involving one or more operands. Punctuators are for grouping and separating. + - * / % ++ -- = += -= *= /= .= %= @@ -52,7 +52,7 @@ An operator is a symbol used to perform an action on some value && || ! & ^ ^^ | ~ . << >> , :: ( ) [ ] { } -> : -? +? => #### 3.3.3 Keywords A keyword is a reserved word in the P# language. Keywords are used to perform a specific task in a computer program; for example, print a value, do repetitive tasks, or perform logical operations. A programmer cannot use a keyword as an ordinary variable. @@ -73,4 +73,159 @@ A constant is an identifier for a value which cannot change during the execution ## 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. -### 4.1 \ No newline at end of file +### 4.1. Script startup +TODO + +### 4.2. Script termination +If the return type of the script's entry point method is int, the value returned serves as the scripts's termination status code. The purpose of this code is to allow communication of success or failure to the execution environment. If the return type of the entry point method is void, reaching the right brace (}) that terminates that method, or executing a return statement that has no expression, results in a termination status code of 0. + +### 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. + + <% + print('Hello world from P#'); + %> + +The print keyword does not add a new line to the output, script author has to put it manually. + +### 4.4. Command-line Arguments +P# scripts can receive command line arguments. They follow the name of the program. The $argv is an array holding all arguments of a script. The $argc holds the number of arguments passed, including the name of the script. + +### 4.5. Declarations +Declarations in a P# script define the constituent elements of the program. Type declarations are used to define classes, structs, interfaces and enums. The kinds of members permitted in a type declaration depend on the form of the type declaration. For instance, class declarations can contain declarations for constants, properties, methods, instance constructors and destructors. +A declaration defines a name in the declaration space to which the declaration belongs. It is a compile-time error to have two or more declarations that introduce members with the same name in a declaration space, except in the following cases: + * Two or more namespace declarations with the same name are allowed in the same declaration space. Such namespace declarations are aggregated to form a single logical namespace and share a single declaration space. + * Two or more methods with the same name but distinct signatures are allowed in the same declaration space. + +### 4.6. P# Types +P# provides a set of predefined types called the simple types. The simple types are identified through keywords: + * bool + * callback + * char + * float + * int + * mixed + * object + * resource + * string + * void + +### 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. + + <% + int $age = 20; + 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. + +### 4.8. Including Files +P# code can be split in multiple files for bigger scripts. There are four statements in P#, which allows to join various P# files: + * include + * include_once + * require + * require_one + +## 5. Data Types & Pseudo Types +### 5.1. Boolean Values +The bool type represents Boolean logical quantities. The possible values of type bool are TRUE and FALSE. In P#, a zero integral or floating-point value, or a null pointer can be converted to the Boolean value FALSE, and a non-zero integral or floating-point value, or a non-null pointer can be converted to the Boolean value TRUE. + +### 5.2. Callback Values +Callbacks can be denoted in P# language by callback type hint. This pseudo-type is used mostly for anonymous functions. + + <% + callback $callme = int() { + return 7; + } + int $var = $callme(); + %> + +### 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. + +### 5.4. Floating-point Values +P#supports one floating-point type. The float type is represented using the 64-bit double-precision. It can represent values ranging from approximately 5.0 × 10−324 to 1.7 × 10308 with a precision of 15–16 digits. + +### 5.5. Integer Values +P# have standardized integers, which are stored in 8 bytes (64 bits) regardless of the host environment. Because there is no cross-platform way to specify 64-bit integer types P# includes typedefs for 64-bit signed integers. In P#, the int type can store integer values between -9223372036854775808 and +9223372036854775807 inclusive. + +### 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. + +### 5.7. Object Values +An object pseudo type is used for storing a pointer to the object. + + <% + object $system; + $system = new System(); + %> + +### 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. + +### 5.9. String Values +A string is series of characters. The simplest way to specify a string is to enclose it in single quotes. To specify a literal single quote, escape it with a backslash. To specify a literal backslash, double it. All other instances of backslash will be treated as a literal backslash: this means that the other escape sequences you might be used to, such as \r or \n, will be output literally as specified rather than having any special meaning. +If the string is enclosed in double-quotes, P# will interpret the following escape sequences for special characters: + * \' - single quote + * \" - double quote + * \\ - backslash + * \$ - dollar sign + * \0 - NULL + * \a - alert + * \b - backspace + * \e - escape + * \f - form feed + * \n - new line + * \r - carriage return + * \t - hortizontal tab + * \v - vertical tab + * \[0-7]{1,3} - the sequence of characters matching the regular expression is a character in octal notation, which silently overflows to fit in a byte (e.g. "\400" === "\000") + * \x[0-9A-Fa-f]{1,2} - the sequence of characters matching the regular expression is a character in hexadecimal notation + +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. + +### 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: + * it was not assigned a value + * it was assigned a special NULL constant + * it was unset with the unset() function + +### 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. + + <% + int $arr1[]; + int $arr2[] = {0, 0} + $arr1 = {1, 2, 3, 4, 5}; + $arr1[] = 6; + $arr1[] = {7}; + $arr1[] = {'test' => 8}; + $arr1[] = $arr2; + 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: + + array(10) { + [0] => int(1), + [1] => int(2), + [2] => int(3), + [3] => int(4), + [4] => int(5), + [5] => int(6), + [6] => int(7), + ["test"] => int(8), + [7] => int(0), + [8] => int(0), + } + +## Future P# Versions +This paragraph describes an ideas for future versions of P# language specification + * Pointers support + * Turn void data-type into a general purpose pointer variable + * Namespace support +