I'm trying to see if I can create a nice syntax for a language that looks as close to C/C++/Java as possible while allowing syntax-like constructs to be defined by the programmer.
It turns out it's not as easy as it sounds.
The problem lies in defining multi-part function. We start out easy: how do you define the following code as functions?
if (b){doWork();}else{doSomeOtherWork();}
A stab at this particular syntax-turned-function might look like this:
void if_new(boolean b, &statement) else(&elsestatement){if (b){statement();return;}elsestatement();}
That looks mighty fine (let's assume the compiler knows that our syntax with the "&statement" means that it should take a block and turn it into a function), but what about the compiler?
if_new(b) { doSomething(); }
else { doSomethingElse(); }
fooFun() { foo(); }
barFun() { bar(); }
In this case, how does the compiler know that the first line is a multi-part function and the second is not? I.e. how does it know that I want the function if_new(b)else(), but not fooFun()barFun()?
One solution would be to impose a terminating ";" tax on every {}, like this:
if_new(b) { doSomething(); }
else { doSomethingElse(); };
fooFun() { foo(); };
barFun() { bar(); };
Another solution would be to make a much looser compilation the first time, and then actually treat these functions as formal syntax extensions, so that when the compiler sees "if_new" it would recognize that an "else" that follows it should be part of the function signature.
This sounds vaguely ok until you realize that you probably would like to be able to have multi-part object methods as well.
An alternative, though syntax-altering, would be to explicitly add a linking character, something like this:
if_new(b){doSomething();}:else{doSomethingElse();}
Unfortunately this means that all other multi-part syntax would have to follow the same rules, e.g.:
if (x){...}:else{...}do{...}:while(x);try{...}:catch (Exception e){...}:finally{...}
":" was the least intrusive character I could find, but if anyone else has a better suggestion I'd be happy to hear it.
What got me thinking if this could be done was a blog entry by Neal Gafter on multi-part method names in Java, and a follow-up by Stefan Schulz.
0 comments:
Post a Comment