Closed thekid closed 13 years ago
Experiment shows this could be done via auto_prepend_file.
friebe, Sat, 28 Jan 2006 18:21:05 +0100
Maybe static imports would also be a worth a try?
<?php
import static util·cmd·Console·writeLine;
writeLine('Hello');
?>
friebe, Sun, 29 Jan 2006 02:11:23 +0100
The meta-compiler should take care of keeping the current line numbers consistent across files. One will be able to associate an error with a code line. (My) experience shows, this is an important feature.
kiesel, Tue, 31 Jan 2006 15:38:38 +0100
One may think about creating multiple compilers - or, one compiler with multiple modes: eg. debugging, release. The debug compiler would add more checks on type hints or return types while the release compiler would skip those in favor of speed.
kiesel, Tue, 31 Jan 2006 15:40:24 +0100
Rewrote this RFC completely - removed PHP4 and PHP6, added all features currently implemented.
friebe, Tue, 10 Nov 2009 13:20:04 +0100
Scope of Change
XP will become its own language, that is, PHP with syntax additions, some of which have been described in xp-framework/rfc #8. (JIT-)Compilers will be created to create PHP 5.2 and PHP 5.3 (with namespaces) sourcecode from the new language.
Rationale
Designing the XP programming language we had the following goals in mind:
Generally speaking, we've tried to follow the "less is more" principle and tried making the syntax more concise as to what it's doing.
Functionality
Like in the XP framework, the entry point is always a class. In their most simple form, these classes have a static
main()
method. An example:Now you will already start noticing things:
Compilation
To run the above example, it first needs to be compiled:
The compilation process may produce warnings and errors. The latter lead to a failure, while warnings are only informational. Examples of errors are parse errors as well as syntactical and structural errors where the compiled code itself would be erroneous - like method bodies in interface declarations or unresolveable types. Warnings depend on the error handler installed in the compiler - in a JIT-compiler, where things have to go fast, no warnings may be issued at all as their computation takes time. In the standard error handler, type mismatches or missing members are warnings, for example. In a more pedantic version, missing api docs or usage of deprecated features will be reported.
Typing
The XP language knows about the following types:
The following elements need to be typed:
Local variables don't need to be typed or declared. Their type will be inferred on initialization (D).
Example:
Namespaces
Namespaces are called "packages" in the XP language. A package does not exist as its own entity, instead, classes belong to a one by declaration:
Imports
Importing is a compile-time feature to enable to use short versions of names but actually mean the longer ones. The "Hello World" example from above could be rewritten as follows:
Also available are static imports which makes writing a line to the console even shorter to write:
At the same time, to avoid name clashes with PHP's native functions, these also need to be imported:
To enable rapid prototyping, type import on demand can be used:
Chaining
It is now syntactically possible to continue writing after
new
and to use array offsets on method return values:Varargs syntax
To create functions that accept a variable amount of arguments -
printf
is probably the most famous one of them, you have to resort tofunc_get_args()
in PHP userland. The XP language supports this feature by adding...
to the parameter's type:This will make the
format
variable contain the format string andvalues
consist of an array with two values (the length and the class name).Changed foreach
The
foreach
loop has changed from the form you know it in PHP to one inspired by C#.Ternary shortcut
The ternary shortcut supported in PHP 5.3 upwards will be supported by the XP language:
Array syntax
The
array
keyword from the PHP language has been replaced by the shorter form with square brackets. By means of an extension array length can be determined by using thelength
pseudo-member.Arrays can also have types:
Map syntax
The
array
keyword in PHP can also declare maps. In XP language, this has been changed:Maps can also have types:
Class literal
Every class has a static member called
$class
which will retrieve thelang.XPClass
object associated with it.Finally: Finally
Especially for cleaning up - and yes, even in 2009 with the amount of memory and computing power we have available - it is still necessary to ensure, for example, file handles are properly closed:
Enumerations
The XP framework already offers type-safe enumerations. These were originally introduced in xp-framework/rfc #132 and are now supported with an easier-to-type syntax:
Enumerations may also have methods:
Members can have methods attached, too:
Annotations
Also supported for quite a while in the XP Framework are annotations. They use "#" one-line comments and are parsed from the class' source when accessed inside the reflection API (see also xp-framework/rfc #16). The XP language can do without this workaround, of course!
What happens technically: Annotations are stored in the xp registry. The reflection API will retrieve them from there instead of having to parse the class files at runtime.
Anonymous instance creation
To generate "throw-away" instances the XP framework provides the
newinstance()
functionality, originally described in xp-framework/rfc #80. With the downside of having to declare the class body inside a string and the added overhead of runtime evaluation, this feature is now not only more elegant to write but classes created this way will also be declared at compile time:Properties
Properties are special member variables that instead of directly accessing a class field may have methods attached. This way, we can create short syntax but still stay flexible if we need to change the underlying implementation.
Internally, this is implemented by compiling
__get()
and__set()
interceptors.Indexers
The PHP language allows for userland overloading of array operations via the
ArrayAccess
interface and itsoffset*
methods. This is kind of different from the usual PHP approach with__
"magic" methods - in the XP language, it's the property syntax again:With syntactic sugar
A keyword "with" is added.
Usage:
What happens technically: The with statement, essentialy a no-op, is resolved to the following sourcecode:
Throws clause
Methods will be allowed to have a declarative throws-clause. Unlike the throws-clause in Java, blocks executing a function or method with a throws clause and not handling the list of contained exception will not lead to a compile-time error.
Declaration:
The thrown exceptions can be retrieved by means of the reflection API.
What happens technically: The thrown exceptions will be stored in the xp registry. The reflection API can retrieve them from there.
Security considerations
n/a
Speed impact
Overall development experience will be slower because of the necessary compilation step. The generated sourcecode will show the same runtime performance, in some cases even better.
Dependencies
None, the compiler will emit valid PHP sourcecode using the XP framework. Compiled classes can even use source classes and vice versa.
Related documents