-1

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.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Franky Chanyau
  • 1,010
  • 1
  • 15
  • 24
  • have looked in to the numerous php frameworks? –  Mar 17 '11 at 23:37
  • 1
    Some of your ideas sound like you should look into pihipi or phpreboot, haxe or the failed php on parrot implementations. Your can-I-accomplish-that question part is a bit unanswerable. Just saying that it's technically feasible. – mario Mar 17 '11 at 23:46
  • Maybe you could write a PHP interpreter IN PHP! –  Mar 17 '11 at 23:56
  • Im really just keen to know if "performance wise" its feasable. That sounds pretty answerable I'm sure? – Franky Chanyau Mar 17 '11 at 23:56
  • @Kevin yea thats what I want to do. Im not sure if your comment was hurtful sarcasm though? – Franky Chanyau Mar 17 '11 at 23:57
  • Are you asking whether or not your final product will be useful to other developers? If so, I couldn't even begin to speculate without a definition of what you are going to create. Is making language X a good idea? I dunno, tell me what it is first. – erisco Mar 17 '11 at 23:59
  • If you are attempting to do it all by yourself, then no, you are likely to fail your goal of performance parity. – mario Mar 18 '11 at 00:08
  • 1
    @FrankyChanyau Five years have passed. How long did it take for you to realize this was a nightmare idea? Or did you eventually write what amounts to a PHP transpiler in PHP? This is a nightmarish idea. Hope you survived. – dreftymac Jan 02 '17 at 21:30
  • @dreftymac Based on my abilities back then, I gave it a pretty good go. https://bitbucket.org/agrmnt/dotvee – Franky Chanyau Jan 05 '17 at 11:02
  • @dreftymac Moreover, I learnt so much and got to start and participate in discussions about some interesting concepts with the community http://stackoverflow.com/questions/8284039/hypothetical-concatenation-predicament. All that said I wouldn't say it was a nightmare exactly. Maybe more of an 8 month early access experience to how my career and ideas would eventually lead me to insanity. Send help pls. – Franky Chanyau Jan 05 '17 at 11:12
  • @FrankyChanyau: sounds like you taught yourself some principles of programming language design, which is great; but you used PHP as the basis for your study, which is ... interesting. Out of all the folks I know who have designed programming languages or parsers, I can't think of any of them who would have chosen PHP, unless it was to intentionally harm their own sanity. Congrats for surviving ^_^ – dreftymac Jan 05 '17 at 11:23

1 Answers1

1

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! ;)

NSSec
  • 4,431
  • 1
  • 27
  • 29