Tool for braceless, whitespace sensitive C syntax

2020-03-01 16:56发布

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.

标签: c syntax
8条回答
Evening l夕情丶
2楼-- · 2020-03-01 17:53

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)); 
} 
查看更多
手持菜刀,她持情操
3楼-- · 2020-03-01 17:54

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
查看更多
登录 后发表回答