tags:

views:

503

answers:

8

I'm writing some C at the moment and because I like whitespace sensitive syntax, I'd like to write it like this:

#include <stdio.h>

int main(void)
  printf("Hello, world!")
  return 0

Instead of this:

#include <stdio.h>

int main(void) {
  printf("Hello, world!");
  return 0; 
}

Does anybody know of a tool that will convert the former into the latter?

Edit: I've really no interest in arguing with those that think this is a bad idea. By all means continue to think that, you have your reasons. But at least know this: I'm aware Python is a whitespace sensitive language but I have not used it. Why would I? I know Ruby already. Also know: I am not just learning C for the first time and I have used PHP and JavaScript for more than four years, so I am not requesting this out of some personal difficulty, lack of familiarity with block syntax, or dogmatic affiliation. I am also aware of what would be involved in writing one of these and that's not beyond my ability but I don't want this enough to justify spending the time writing one.

+1  A: 

Yea, there's one I really like. They call it Python.

jcm
Belongs on smartassoverflow.com
Robert Harvey
@Robert ass overflow? ewwwww.
rascher
I'm torn in two. I want to vote you up for trolling Python, and I want to vote you down for slamming C, so no votes it is :D
Matt Joiner
I am in no way slamming C. Just the guy that wants C to be Python.
jcm
A: 

I don't believe such a tool exists. Tools exist to clean formatting, but the code must already be C formatted.

I really think you should use the language as designed and learn to use braces.

gbrandt
+2  A: 

No tool, but pseudocode:

last_spc_count = 0
For all lines in input file check number of trailing spaces spc_count
  Print old line
  If spc_count > last_spc_count print "{\n" (last_spc_count-spc_count)/2 times
  Else If spc_count < last_spc_count print "}\n" (last_spc_count-spc_count)/2 times
  Else print "\n"
  last_spc_count = spc_count
print "}\n" last_spc_count/2 times
schnaader
Thank you for actually trying to help. I appreciate it.
Ollie Saunders
+25  A: 

Even if there was such a tool, I would strongly encourage you to reconsider this idea. Here are just a few problems I think you'll find with doing this:

  1. Your code will no longer be standard C.
  2. That means that you'll have a problem with other programmers reading your code.
  3. You'll also be unable to use any code-analysis tools, since they won't understand your syntax.
  4. If you've got some kind of tool that will convert on, say, every compile, that still means you'll be writing different code than you'll be reading. I would hate to use a tool that changes my code for me all the time.

This really seems to be a case where fitting your habits to everybody else is the smarter approach.

Hope this causes you to reconsider.

Edan Maor
The existing tool doesn't remove the braces it creates special indentation blocks instead. I'm just experiencing some trouble finding it :(
Li0liQ
I'd upvote this twice if I could. I hade to deal with a coworker once who had a header file where he defined latin versions of all keywords, so he could write his C code using latin keywords. He also replaced braces with corresponding latin words. I still dream nightmares about that code.
JesperE
@Jesper right, but for this question there's no evidence that there are any coworkers. Or that it'll be production anything.
rascher
-1. (1) and (2): OP doesn't mention anything about other programmers. Seems like a personal project. (3) OP doesn't mention wanting to do any analysis. (4) Um, compilers change code *all the time*. They're supposed to. gcc converts code to an intermediate, then optimizes, and finally creates binary, which "looks" nothing like your code. There is no evidence that brace-syntax is any "smarter" than whitespace syntax. Disagree!
rascher
@rascher: Just to clarify what I meant: 3) By code analysis, I'm including everything an IDE can usually do with code like auto-completion (obviously this isn't just about IDEs. Any tagging program is probably going to have the same trouble understanding the code). As for point 4), I may have been unclear. I was talking about a case where the conversion is done on the source file itself (not producing a new file), every time you compile.
Edan Maor
As much as I disagree with the idea in the question, I also disagree with this answer. Any decent build process can automate the "code generation", and the output of the "code generation" is standard C. So any time a tool (or other coders) need to look at standard C, you just point them to the tool output. Also, this is a rare case of a code generator that should (in principle) be able to generate perfectly good source from edited object code - ie to pretty-print while stripping the braces back out. As for "your code will no longer be standard C" - neither is my C++, so what?
Steve314
Why do I disagree with the idea in the question then? While I'm perfectly OK with the idea of standard C source code as object code (that's what lex, yacc and the rest do after all), this is a case of a domain specific language where the domain is "pointless timewasting".
Steve314
+3  A: 

Python-style indentation for C.

Looks like it is what you are looking for.

Li0liQ
Although, still cannot find the tool I saw some time ago :(
Li0liQ
It looks like that has not been implemented yet - but it is the same exercise.
Jonathan Leffler
A: 

http://www.cython.org/

But that's a different language... it's basically a Python dialect with C performance properties.

Don't roll your own language, use something standard.

Andrew McGregor
+11  A: 

If you really want to do this, it is not going to be possible without implementing a language parser, and even then, I am not sure how the coding convention will be for some of the cases in your "new language that looks like C but has no braces". For example, take the following C code:

struct a {
    int i;
};

int main(void) {
    ...
}

You can write it as

struct a
    int i

int main(void)
    ...

But it has to be converted to the original code, not:

struct a {
    int i;
} /* Note the missing semicolon! */

int main(void) {
    ...
}

Also, given the snippets below:

/* declare b of type struct a */
struct a {
    int i;
} b;

/* a struct typedef */
typedef struct a {
    int i;
} b;

How are you going to specify these in your language?

You seem to not want to use semicolons in your language either. This restricts your code quite a bit, and makes the conversion tool complicated as well, because you can't have continuation lines without extra effort:

i = j +
k;

is legal C, but

i = j + ;
k;

is not.

So first, you need to define the grammar of your "braceless C" more precisely. As others have said, this sort of thing is fraught with peril.

Alok
I would appreciate a reason for downvoting.
Alok
You may already know this but you don't need a complete parser. You need a partial lexer and some insertion logic. You might have meant all that when you said "parser" but dumbed down the answer for my benefit.
Ollie Saunders
Reading some of your later points: An implementation of this in a scripting language could forgo a formal grammar. I'm aware that making C whitespace sensitive will mean that things can't be spaced out on multiple lines (that's true by definition) and I'm also aware that not all blocks requite semi-colons. These are details of the issue but not reasons why an implementation isn't feasible. They do, however, identify why writing something like this would take a little time.
Ollie Saunders
OK, I see your points. I didn't "dumb down" my answer for you, it was loose terminology on my part. If you want to implement a subset of the language, and restrict your formatting, writing such a tool is easier. I still think it's not such a good idea, though.
Alok
Interesting analysis: see also the Go language - which has undergone some changes. It does away with the parentheses around conditions, but requires braces around blocks of code.
Jonathan Leffler
@Jonathan: thanks. I have been meaning to check Go out. I am not sure if I like the "parentheses sometimes required" approach of Go, but everything takes getting used to anyway.
Alok
+8  A: 

PythoidC is a braceless C language http://pythoidc.googlecode.com

c.include(c.h.stdio) 
c.include(c.h.stdlib) 

int fib(int n): 
    if (n<=2): 
        return 1 
    else:
        return fib(n-1) + fib(n-2) 

int main(int argc, char **argv): 
    int n //C style annotation 
    n=c.stdlib.atoi(argv[1]) 
    c.stdio.printf('fibonacci(%d)=%d\n', n, fib(n))

PythoidC automatically generates the following C code:

int fib(int n){ 
    if (n<=2){ 
        return 1;} 
    else{ 
        return fib(n-1) + fib(n-2);}} 

int main(int argc, char **argv){ 
    int n ;//C style annotation 
    n=atoi(argv[1]); 
    printf("fibonacci(%d)=%d\n", n, fib(n)); 
} 
ChenGuang
Presumably, it also generates `#include <stdio.h>` and `#include <stdlib.h>`; you probably left them out because they looked like enormous headers (and the '`#`' was missing). Learn to indent code by 4 spaces and the majority of the problems go away. Also note that when you're editing, there is a button with binary 101010 sequence in it; that indents code for you.
Jonathan Leffler
What does a button with a binary sequence in it mean? Do you mean a toggle button?
Ollie Saunders
This is exactly what one would look for, but the tool seems pretty awful. The source code is also fairly primitive and messy. :(
Matt Joiner
+1 for answering the question, though I still think the whole idea is pointless.
Steve314
afriza