Question

Compared to some languages, PHP's syntax is a little bit long (e.g. in java calling the method of an an object is done with a period ( . ) instead of phps (->) ).

I have always wanted to write my own language but I'm only a first year bachelors student and am in no way knowledgeable enough to write a parser that converts something to byte code for the Java VM or something.

So instead I'm thinking of writing a parser in PHP that converts my syntax into PHP code then runs that using regex and stuff.

Reasons for that being: PHP is a stable language for web sites (my syntax will be suited towards web developers) I already know PHP at a professional level (or at least I think I do) I have already seen an implementation of something similar: http://tetraboy.com/lolcode/lol_core.phps This converts LolCode into PHP. And I know its just a joke but still it has been done

So since I have proof that its possible. My question is: Could I actually make something that I could use for future projects? e.g. Can I get it to be as fast or almost as fast as PHP is?

I'm going to give it a try either way just for the purposes of learning a little bit more about PHP and how languages are put together, but I'm wondering if I could actually turn the end result into something worth a damn.

Was it helpful?

Solution

If anything, this is a very fun learning project.

There are others that had the same idea with other languages. For instance, take a look at CoffeeScript, which is basically the same idea but for Javascript.

You can make this work, but you'll have to take into consideration the conversion process. If you're looking for performance you do not want this to be done realtime on each request. Because adding layers of complexity will simply slow down your code. Even if it's just by the tiniest bit. To rememdy this CoffeeScript offers a command line 'compiler' for pre-compilation but also allows for a lazy loading situation - which works nicely for development purposes. Such a setup might work for you aswell.

You'll want to look at PHP_LexerGenerator and PHP_ParserGenerator for implementing your parser. Even if the latter is no longer maintained :)

Good luck! ;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top