I've been thinking over an interesting idea a lot recently and wanted to get it out in words. The general idea is as follows: create a functional domain specific language (DSL) that PHP can parse/interpret/compile. You might be pulling out your hair right now thinking: "why would you write a DSL in an interpreted language???" That's a fair question, and the answer to it is that it's an experiment and I want to see if I can put my ideas to work in the language I know best.
If you're a Lisp/Javascript fan then you probably picked up on the 'functional' part of this idea. I want to make a functional language. I think that they are wildly powerful and totally underrated. I also want to make something that's more approachable than Lisp though! In fact, what I've decided on is not to make a language per se, but the building blocks of a language.
The main idea is that the programmer starts by defining some simple constructs: these describe what things in the language should look like, how they are bound, and how they are called. An idea for these constructs that I'm toying around with looks like this:
<construct =function>
<definition>
function -->(<-->)<--{<-->}
</definition>
<call>
-->(<-->)
</call>
</construct>
That must look so weird. All those weird sets of arrows are simplified pattern matchings. For example, the --> will look for anything from the character left of the arrow up to the character after the arrow. In this case, if we had "function moo(" then the --> would match "moo". The <--> is somewhat more interesting because it implies using a stack and character parser to go and find the the character on the right side of the arrow that properly matches up with the character on the lest side of the arrow. So, if we had "(<-->)" and gave it ""(foo (bar))" then it would match "foo (bar)". Finally, I'm still thinking through the <-- and if it's even necessary or if the right arrow would suffice.
One of the main ideas behind this idea (hehe) is that there are no tokens or grammars per se. I want to be able to define as much as I want in these constructs (pseudo-grammars) and then have tailored PHP classes do the real work of validating the code based on the matches found from the arrows in the constructs.
Okay, so assuming I can this far (parsing), how and where do I go from there. Do I try to compile it into some ridiculously ugly PHP code? Further, what problem will this solve? Right now, it seems like I'm only creating problems for the sake of solving them.
Well, assuming the construct system is flexible enough, I think it should be possible to parse a foreign (programming) language given all the right constructs. Maybe I should stop making such grandiose claims and try to actually get some code written. :P
- Development
- PHP by Peter Goodman on Jun 29, 2007 @ 6:25pm

