views:

457

answers:

9

I am constantly trying to optimize my time. Writing a C code takes a lot of time and requires much more keyboard touches than say writing a Python program.

However, in order to speed up the time required to create a C program, one can automatize many things. I'd like to write my programs using smth. like Python but with C semantics. It means, all keywords are C keywords, but syntax is optimized.

For example, this C code:

#include "dsplib.h"
#include "coeffs.h"

#define MODULENAME "dsplib"
#define NUM_SAMPLES 320

typedef float t_Vec; 
typedef struct s_Inter
{
    char *pc_Name;
    struct s_Inter *px_Next;
}t_Inter; 
typedef struct s_DspLibControl
{ 
    t_Vec f_Y; 
}t_DspLibControl;

void v_DspLibName(void)
{ 
    printf("Module: %s", MODULENAME); printf("\n");
} 

int v_DspLibInitInterControl(t_DspLibControl *px_Con)
{ 
    int y; 
    px_Con->f_Y             = 0.0; 
    for(int i=0;i<10;i++) 
    { 
        y += i * i; 
    } 
    return y;
}

in optimized pythonized version can look like:

include dsplib, coeffs
define MODULENAME="dsplib", NUM_SAMPLES=320

typedef float t_Vec 
typedef struct s_Inter:
        char *pc_Name
        struct s_Inter *px_Next 
t_Inter 
typedef struct s_DspLibControl:
    t_Vec f_Y 
t_DspLibControl 

v_DspLibName(): 
    printf("Module: %s", MODULENAME); printf("\n") 

int v_DspLibInitInterControl(t_DspLibControl *px_Con): 
    int y 
    px_Con->f_Y             = 0.0 
    for int i=0;i<10;i++: 
        y += i * i
    return y

My question is: Do you know any VIM script, which allows to translate an original pythonized C code into a standard C code? For example, one is writing a C code but uses pythonized syntax, once she decides to translate pythonized blocks into standard C, she selects such blocks and press some key. And she doesn't save such pythonized code of course, VIM translates it into standard C.

+1  A: 

Cython can help you. The usual way to use cython is to speed up modules by having part of them compiled to C. However it is perfectly possible to use cython to output pure C files from pythonlike syntax if you are careful.

This also has an advantage of letting you write unittests in Python if you choose.

gnibbler
your link doesn't work
psihodelia
Jonas
of course the link doesn't work if the site it down, sheesh. maybe try again later?
gnibbler
+7  A: 

Cython is designed to write python extensions, not full-fledged programs. The same is true for Pyrex.

Even though it's quite different from your example, PyPy might be what you're looking for. It uses a Python subset (called RPython, a kind of more statical python) to generate code to different backends - including C. It won't let you a fine-grained control over data structures as you may want, but give it a try.

What are you asking for is really a different and somewhat easier C dialect - while it might not be a bad idea by itself, there're already plenty of different programming languages around the world, and it would be quite an issue if everybody invented a new dialect for each app that should be written.

If you think C is too verbose or too lowlevel for your needs, try this approach:

  • write your program in python
  • profile it and find what parts really need speed
  • isolate such parts in well-defined modules
  • rewrite such parts through Pyrex or Cython

and you'll probably get great readability, maintainability AND speed.

Alan Franzoni
I personally feel a lot of syntactical things in C is outdated. Unfoirtunately, almost any low-level system application is written in C and there is no alternative language for this application area.
psihodelia
while that might be true - or not, depending on the context - yours is not the best solution. Try limiting C - true C - to the context where it's really needed, and use other languages to 'glue' it around.
Alan Franzoni
A: 

Have a look at "Go"....

http://golang.org/

AJ
Go's syntax isn't much simpler than C, and it isn't a VIM script
Pete Kirkham
Go is based on C, and has influences from Python. No, it's not a VIM script, it's an imperative programming language. Why use some text editor hackery to side-step convention when an entire language has been designed with a more simple syntax in mind?
AJ
+1  A: 

If you want to type less C, you don't need syntax tricks.

You need a library of better, higher-level constructs so you actually type less C, not fewer characters to create pseudo-C.

If you had a library of better, higher-level constructs, you could write properly object-oriented programs in a short, easy-to-read syntax.

Taking this to the logical extreme, you can create nice library bundles that are callable from Python. Then you can switch to Python, type less and get the same amount of work done.

S.Lott
+4  A: 

Have you profiled what you actually spend time on --- you really want to trade maintainability for saving a few keystrokes?

When I write code, I spend most my time reading code.

Perhaps you're just a very slow typer... ;-)

Alex Brasetvik
The OP should investigate the *Personal Software Process* from Carnegie Mellon. One of the attributes is how to quantify your development time, whether it be designing, coding or testing. Profiling at this level is what the OP needs.
Thomas Matthews
A: 

I agree with what Alan Franzoni said "What are you asking for is really a different and somewhat easier C dialect" and since you are already prepared to use such a different dialect as your proposed, might I suggest the computer language D? It solves many of the problems with C, while still allowing tight control over memory and structures. It is also link compatible with C.

Another approach might be Objective C.

That said, it shouldn't be too hard to write a parser that converts from your syntax to C syntax, but I suspect as others did here, that someone will hate you for it, when time comes for maintaining the program. ;-)

Amigable Clark Kant
A: 

I've written a few Vim scripts for various things. I also had to write a lexical analyzer, parser, and interpreter from scratch for a class. This is a very bad idea.

  1. Writing a vim script to translate you syntax to C syntax is not a trivial thing. Ever written a lexical analyzer? a parser? Because that's what your Vim script would be. A lexical analyzer/parser/compiler/interpreter. Which would be a nightmare to do in Vim script.

  2. It would take a long time to write enough of your modified-syntax code to recoup the time it would take to write and test any interpreter (not just a Vim script one, although that would probably be an upper bound) that would do the kind of translation you're talking about.

  3. Even if you were successful in creating your syntax-interpreter, unless you had it save the file in normal C syntax after you wrote it, anyone who ever had to read that code would want to kill you. And if you had your interpreter save the file in the normal C cyntax it was outputting, next time you had to maintain/modify the file, you'd be straight back to your notion of C code taking too much time for you to write.

  4. If you want to be adventurous with Vim scripting, you could always write some Vim scripts or mappings/abbreviations to make the tedium of curly braces and semicolons a lot less painful. That would be worth doing.

alesplin
A: 

It sounds like you need to devote some time to perfecting usage of snippets, abbreviations, macros, autocompletes for C. You will gain speed with hard work and practise.

The closest sane analogy to what you are suggesting is perhaps writing HAML to generate HTML. But that is for a declarative language not an imperative one.

michael
A: 

If C++ is OK for you, unlike Cython and Pyrex, Shed Skin is used to convert a subset of Python to C++ and compile it with make/g++. The "subset of Python" means that everything you code is still Python, but you can not use all of the modules of Python(actually you can use only a few of them) and you can not use some too dynamic stuff. Since the code is converted to C++ and compiled with g++, it is fair enough.

For example:

# Actually, in Python, this is one line:
matrix = [[1, 2, 3], [4, 5, 6]]
zipped = zip(*matrix)
# But, Shed Skin has some restrictions for this, so you have:
matrix = [[1, 2, 3], [4, 5, 6]]
zipped = zip(matrix[0], matrix[1])

Shed Skin uses type inference, so,

x = 4.0 # OK
x = 5.0 # OK
x = "foo" # OK in Python, but Shed Skin will not compile this

I must warn you that it is in experimental stage even though the development is active.

Caglar Toklu