Edit:

This is continued here.

Edit:

I love PHP. None of this post is meant to indicate otherwise. And you can love the programming languages you use. Just don’t be silly about how you talk about them.

Daydreaming

I have been writing PHP code for a long time and heard all the arguments for and against it. PHP has its warts, but it’s not unique in this area. I am invested in PHP.

Sometimes I dare to dream though. What would PHP be like if the Python fairy flitted down from the heavens and sprinkled a helping of magic dust on it? Would it just be another significant whitespace language, or could it be better?

It’s in these times; I dust off my reference PDF’s and wrestle once again with the idea of creating a parser for this magical new language.

So this time; I am going to pen my thoughts on what the language might look like, and hopefully decide, once and for all, whether or not to pursue it.

…In Code

The goal is to keep most of PHP, but hopefully lose the cruft and inconsistencies. Let’s begin with an extract of the Illuminate Auth library:

namespace Illuminate\Auth;

use Illuminate\Support\Manager;

class AuthManager extends Manager {

/**
* Create a new driver instance.
*
* @param string $driver
* @return mixed
*/
protected function createDriver($driver)
{
$guard = parent::createDriver($driver);

$guard->setCookieJar($this->app['cookie']);
$guard->setDispatcher($this->app['events']);

return $guard->setRequest(
$this->app->refresh('request', $guard, 'setRequest')
);
}

Let’s then compare this to the Flatwhite syntax I just made up:

namespace Illuminate.Auth

import Illuminate.Support.Manager

class AuthManager extends Manager

/**
* Create a new driver instance.
*/
protected mixed createDriver(string driver)
guard = parent.createDriver(driver)

guard.setCookieJar(this.app["cookie"])
guard.setDispatcher(this.app["events"])

return guard.setRequest(
this.app.refresh("request", guard, "setRequest")
)

I mentioned that I am thinking of a significant whitespace language, so it should come as no surprise that the curly braces are omitted.

I also dropped semi-colons, dollars and arrows. These all add to the noise, and parsers really should be able to understand code without them.

The only thing which should really stand out here is the type hinting in the function signatures. This is not natively supported by PHP, and would require little more than a straight string replace (from Flatwhite syntax to PHP syntax).

Anonymous functions shouldn’t behave any differently to class functions (obviously notwithstanding the scope modifiers like protected and public). Nor should variables need to be passed into the scope of the anonymous function. That’s just crazy!

Then in the loops; for and foreach can use the same keyword and we can be done with the is_string, isset and $var != null; by using var typeof type and var instanceof class syntax.

These are just some of the changes I would implement; but it should be clear that I don’t want to stray too far from PHP syntax.

…In Implementation

I want to implement Flatwhite as a PHP pre-processor. This makes sense because the source code can then be run anywhere PHP can be. I want readable Flatwhite code to become readable PHP code; optimised for performance.

For this reason; it’s important to stay within the bounds of the the language allows. There are neat tricks to allow things like scalar type hinting and automatic scoping with anonymous functions; but these should all be transformed into valid, sane PHP code.

It would be cool if the compiler were also written in PHP so that the code could also be compiled, with minimal effort, on the server side. This would also help Composer integration so that Flatwhite frameworks could easily consume PHP libraries and PHP frameworks could easily consume Flatwhite libraries. All it would take is a post-install compile script.

…In Conclusion

One of the reasons for writing this is to see if people would be interested in using, or helping to build, Flatwhite. There’s obviously lots to be done, in specification work, before work on a compiler could begin. That’s future Chris’ problem though. For now, I would just like to know if the idea is worth the effort it’s going to take.

Leave a comment (or several) here, or talk to me on Twitter: @followchrisp.