Search code examples
compiler-constructionprogramming-languagessyntaxlanguage-design

What would your own programming language look like?


What would your own (I assume perfect) programming language look like? Give a small example and explain your novel ideas!

I'm really interested in the syntax.


Solution

  • Jon is right in saying that “[d]ifferent tasks suit different languages and paradigms.” However, there are a few considerations that are largely independent of the domain. These mostly concern syntax but since code is read more often than written, I actually think that syntax matters.

    For one thing, and something that many languages do wrong, it's completely arbitrary to base the syntax off C. C actually has an exceptionally bad syntax. I'll just pick two examples.

    The first is quite uncontroversial: semicolons are unnecessary. Take the following code; the grammar is completely unambiguous and easy to parse for a compiler. Neither semicolons nor explicit line continuations are necessary.

    answer = 42
    fraction = answer * 2 /
               (answer + 1)
    Console.WriteLine(
        "Some funny number: {0}",
        fraction
    )
    

    This is actually quite similar to Python but even more permissive: the definition of fraction spans multiple lines. This is logical since the first line isn't yet complete.

    Another bone I've got to pick with C-like syntaxes are their largely implicit variable declarations. Instead of announcing clearly “I'm declaring a variable of type Foo,” all they whisper shyly is “Foo var”. Since Foo isn't even a reserved word in most cases, the programmer isn't offered a single visual hint here. I prefer VB's explicit Dim var As Foo, even thought he keyword used here is, well, quite dim.

    (C++ actually makes matters much, much worse by introducing a lot of nearly identical and often ambiguous syntaxes that mean completely different things, from variable initializations to function declarations).

    Another thing my language would have to have is static typing. It's true that dynamic typing has its uses but they are surprisingly rare. Even most “scripting languages” wouldn't really need them. I think that this is often confused with implicit typing which has rather more uses. Take (again) the example of Python. Why doesn't it offer static typing? It's already strongly typed, statical type checking would only be consequent, and would cut down on debugging quite a bit. The same goes for explicit variable declaration. I fail to see what advantages implied variable declaration offers.

    So there we've already got an outline for a language:

    • Clean syntax, avoid historical clutter. In particular:
      • No semicolons
      • No explicit line continuations à la VB
    • Explicit variable declarations
    • Static typing

    Furthermore, I'm a huge fan of certain C++ concepts, such as general-purpose templates, RAII (i.e. avoiding garbage rather than collecting it), immutability and the concept of value ranges defined via iterators. I've stated elsewhere that I believe iterators to be one of the most fundamental innovations ever. Give'em a little lipstick and you won't even recognize the ugly beast that is C++:

    for i in MyVector:
        print(i)
    

    rather than

    for (typename vector<T>::const_iterator i = MyVector.begin();
         i != MyVector.end();
         ++i)
        cout << *i << endl;
    

    Of course I'm aware that the above syntax is offered by many languages. But they all only offer watered-down versions of C++' powerful iterator concept (to use C++ terminology, the only kind of iterators that most languages know are input iterators, which are basically the least powerful iterators).

    At this point I should probably say that the sole copyright for all these ideas is mine, patents pending (in particular for the MayOrMayNotBe operator that doesn't really compare object references).