Tool for braceless, whitespace sensitive C syntax

后端 未结 8 2435
灰色年华
灰色年华 2021-01-04 17:57

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

#include 

int main(void)
          


        
相关标签:
8条回答
  • 2021-01-04 18:15

    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.

    0 讨论(0)
  • 2021-01-04 18:15

    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
    
    0 讨论(0)
  • 2021-01-04 18:17

    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.

    0 讨论(0)
  • 2021-01-04 18:22

    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)); 
    } 
    
    0 讨论(0)
  • 2021-01-04 18:23

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

    0 讨论(0)
  • 2021-01-04 18:24

    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.

    0 讨论(0)
提交回复
热议问题