Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I first learned about the existence of syntactic sugar languages, like CoffeeScript and SASS while working in Rails. This got me thinking... why do we not have well-known (if any at all) syntactic sugar for languages like C, C++ and Java?

Imagine a language that reads like Python or Ruby that can be preprocessed to C, and then compiled to generate really fast code that also looks beautiful.

For example, instead of:

#include <stdio.h>

int divide(int a, int b)
{
    if(b != 0)
        return a / b;

    return 0;
}

int main()
{
    int a, b;

    scanf("%d", &a);
    scanf("%d", &b);

    printf("%d", divide(a, b));

    return 0;
}

Why not have:

include stdio.h

fn divide(int a, int b):
  a/b if b != 0
  ret 0

fn main:
  int a, b

  scanf '%d', a
  scanf '%d', b

  print '%d', divide(a,b)
  ret 0

And have this preprocess down to the same C code above?

Why stick to language implementations that are either fast or easy to read when you could have both?

share|improve this question
6  
C/C++ literally ship with a turing complete code preprocessor, and there are quite a few Java preprocessors. In Java's case I'd wager they aren't popular because it makes for nonportable and opaque code, and anybody who wants a nice language to write in has options like Scala which can interoperate near-perfectly with Java code. –  Phoshi Jan 16 at 14:33
4  
Because you couldn't have both. What gives C the ability to be fast is not the syntax, but the huge swaths of undefined behaviour and things that work exactly like the underlying microprocessor does. What made Fortran compilers efficient was not the uppercase keywords, but that fact that there was no aliasing at all, etc. –  Kilian Foth Jan 16 at 14:33
1  
I understand that the syntax has nothing to do with the fact that C is fast. I'm suggesting building a functionally complete modern "coat of paint over it" that is preprocessed down to normal C code. Updated my question with a quick example. –  peteykun Jan 16 at 15:17
7  
C already looks beautiful. –  mouviciel Jan 16 at 15:35
2  
I suppose if you really wanted this, you could write your own C preprocessor called "Cython" which would do exactly the transformation that you're suggesting. I'm guessing it hasn't happened already (or if it has, it's not very popular/well known) because there isn't enough demand for it, suggesting that most developers are content enough with the syntax to not bother writing such transformations. But if you really want it, you can create it. Who knows? Maybe it will catch on. –  FrustratedWithFormsDesigner Jan 16 at 16:15

3 Answers 3

up vote 8 down vote accepted

There are two well known syntactic sugar languages for C:

  • C++
  • Objective C

And two less well known, but more truly syntactic sugar as they are set on always compiling to C while the above switched to direct assembly generation:

Both C++ and Objective C started as translators to C. They switched to direct assembly generation because the code generation was getting complicated for some features. They were never designed to generate interfaces easily callable from C, but both embed complete C.

The other 2, GOB and Vala on the other hand are designed to generate interfaces easily callable from C. GOB only provides generating some boilerplate for object oriented programming while the function bodies are written in plain C, Vala is completely different language that maps to C.

All those languages provide features, not just simply different syntax. Because frankly, providing just different syntax has absolutely no benefit. It is not the syntax that governs how complicated it is to write anything in a language. It is the structural complexity of expressions and the amount of things the programmer has to keep track of. Saving a few keystrokes won't help. Programmers can generally type quite a bit faster than they think. So a different language can only help by:

  • Saving a lot of keystrokes. This is the case of GObject Builder and in part Vala. This is because boilerplate needed to create a GObject is rather large.
  • Providing actual features, like automatic reference counting in Vala.

Even then, of the four above mentioned languages, only C++ became universally popular and only because it added really powerful features. Objective C is only used on single platform and GOB and Vala are mainly niche languages for the Gnome project.

Because it is a lot of work to learn a new language. It's not enough if a programmer knows a language. Most projects are work of large teams these days and all members of the team have to know the language to use it on a project. And because people leave jobs, project leaders hesitate to use new language not only until they have enough programmers proficient in it, but also until they are sure they can hire more if needed. So new languages only catch up if they have really important new features. (Java while being rather weak language in itself has a huge advantage in comprehensive standard library. For productivity the standard library is even more important than features of the language, not to mention syntax).

share|improve this answer
1  
I wouldn't say only single platform for objective C, but rather "commonly used on a very small set of platforms" (iOS vs Mac OS are arguably two platforms... and then there's GNUstep also) - though thats a library/framework thing - nothing saying one can't use clang on BSD to compile a command line objective C app either. –  MichaelT Jan 16 at 17:09
    
There is also some "syntactic sugar" for C++: lazycplusplus.com –  Doc Brown Jan 30 at 7:09

Two points:

1/ The final goal is rerely to have C, it is to have machine language. In your examples, the final goal is to have JavaScript or CSS because those are somewhat the machine language of the browsers.

2/ Compiling to C was (is still) a popular choice for first implementation of compiled languages. That step is usually masked as it is just an artifact of the implementation and not a useful one. (In the same way, compilers which generate assembly instead of object files usually mask the fact they spawn an assembler to transform the assembly in object code). Later version may compile directly to assembly or machine language. (Nowadays, targeting existing VM like the Java and .NET one are also popular, and one may argue the availability of LLVM make targeting C often less pertinent.)

share|improve this answer
1  
There are two languages where the point is to have C. GOB and Vala. –  Jan Hudec Jan 16 at 16:26
    
You may want to compile specifically to C if you have to maintain an ABI compatibility with it as cheaply as possible. And if your target language is C++ this consideration is even more important. –  SK-logic Jan 16 at 21:28
    
Then it isn't the goal, it's a mean to achieve the goal. –  AProgrammer Jan 17 at 6:14

You mean things like Vala? https://wiki.gnome.org/Projects/Vala

Also note that the original C++ (C with Classes) was written purely with the C Pre-processor.

If you want to write something similarly with Python, knock yourself out.

share|improve this answer
1  
Early C++ was a preprocessor; it wasn't done with the C preprocessor. See this paper. –  Blrfl Jan 16 at 15:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.