C++ Resyntaxed

bearophile bearophileHUGS at lycos.com
Wed Mar 12 13:23:59 PDT 2008


This is a short article I have just found on Reddit that shows a possible alternative syntax for C++:
"A Modest Proposal: C++ Resyntaxed", Ben Werther & Damian Conway:
http://www.csse.monash.edu.au/~damian/papers/HTML/ModestProposal.html

It says:
>The language was designed to ensure that the new syntax was LALR(1) parsable, grammatically unambiguous and required no semantic feedback from parser to tokenizer.<

That reminds me of D :-)

Some C++ abstract declarators:

  int               // integer
  int *             // pointer to integer
  int *[3]          // array of 3 pointers to integer
  int (*)[3]        // pointer to array of 3 integers
  int *()           // function having no parameters, returning pointer to integer
  int (*)(double)   // pointer to function of double, returning an integer

In this new syntax they are like this, they seem more readable to me:

  int                // integer
  ^ int              // pointer to integer
  [3] ^ int          // array of 3 pointers to integer
  ^ [3] int          // pointer to array of 3 integers
  (void -> ^int)     // function having no parameters, returning pointer to integer
  ^ (double -> int)  // pointer to function taking a double, returning an integer

Are the the following D equivalents? (me being not sure shows that new syntax may be better than the current D one)

  int                    // integer
  int *                  // pointer to integer
  int*[3]                // array of 3 pointers to integer
  int[3]*                // pointer to array of 3 integers
  int * function()       // function having no parameters, returning pointer to integer
  double function(int) * // pointer to function taking a double, returning an integer
  
Notes:
- I presume ^ comes from Pascal.
- This new syntax uses := and = instead of = and == as in Pascal.
- I like -> to denote a function, but how to denote a delegate?


Another example, the declaration of set_new_handler in C++:

  void (*set_new_handler(void (*)(void)))(void);

That you can declare in two stages too (C++ again):

  typedef void (*new_handler)(void);
  new_handler set_new_handler(new_handler);

Their equivalents in this new syntax:

  func set_new_handler : (^(void->void) -> ^(void->void));

and:

  type new_handler : ^(void->void);
  func set_new_handler : (new_handler -> new_handler);

Again they seem more readable to me.
(This syntax has other differences too, that you can see in the article, but those ones are quite easy to spot and nice).

Bye,
bearophile



More information about the Digitalmars-d mailing list