August 13, 2012

Leng: Syntax (1)

I've got to be convinced that one of the reasons that there's so many refactoring tools out there for Java and so few for C++ is the extremely difficult that it's to write a parser for the second.

The language is charged with old compiler tagging and at the same time some features that make it really hard to parse.

Semicolon

I've been writing quite a bit of Lua lately and I keep getting surprised when I go back to C++ and the compiler starts complaining about missing semicolons. I don't want them in Leng.
Instead, the delimiting character will be the new line, with two exceptions:
- Expressions in brackets (of any type) require the closing one before finishing.
- Assignment requires at least one value before finishing
Unfortunately, that doesn't allow us to do funky stuff like:
 a = b
  + c
instead, you'd be doing:
 a = b + c
 // or
 a =
  b + c
 // or
 a = (b
  + c)
Like Lua, I plan to accept semicolons if you ever want to use them, but believe me, 99% of the time, they are just clutter that just removes clarity from the code. After two days you won't miss them at all.

Now, I don't want to follow the Lua path completely here as I feel its a bit undefined. Lua tries to look ahead the next statement to see if it could be part of the current one and kind of appends it. That can lead to unexpected results like:
function f()
 return
 assert(true, "never called")
end
What would you expect to get here? Well, tabbing is deceptive in this example. The assert does fire and the function returns true (the value that assert returns). Even though Lua makes it a syntactic error to have any statement after a return except for the end of a block, look-ahead parsing bends the rule in a rather obscure way.

Preprocessor and Templates

As you could expect, the preprocessor won't be a part of Leng. Instead, I plan to expand generic programming beyond templates. I hope this will add a new level of versatility to the language while making it safer and easier to digest (for both, tools and humans).

For templates, as much as I love the look of angle brackets <>, I'm considering replacing them with square brackets []. Replacing classic C array syntax, there would be a special templated class Array[C, N]with an overloaded operator() to access the values.




No comments:

Post a Comment