SourceForge.net Logo

Scramble

About

Scramble is a C preprocessor which allows to write C code which looks a bit like Python code with a full Python3 environment available for meta programming. That is, no semantics at all are changed - when using Scramble you still write C code. But it has a syntax more similar to Python.

Similar (and better) projects:

Download

Get latest scramble from here.

Source

Browse the source code.

Example

Scramble is used like this:

scramble.py -i input.py -c output.c -h output.h -n name

For example, if you have a file main.py, then you could run:

scramble.py -i src/main.py -c build/c/main.c -h build/h/main.h -n main

And if your src/main.c would look like to the left, the resulting files would look like to the right (The red color indicates what the -n options does):

src/main.py

import stdio, string, math

int def main(int argc, char **argv):
    if argc == 2:
        printf("%f\n", sin(strtod(argv[1]))
        return 0
    else:
        fprintf(stderr, "Need exactly one argument!\n")
        return 1

build/c/main.c

#include "main.h"

int main(int argc, char **argv)
{
    if (argc == 2) {
        printf("%f\n", sin(strtod(argv[1]));
        return 0;
    }
    else {
        fprintf(stderr, "Need exactly one argument!\n");
        return 1;
    }
}

build/h/main.h

#ifndef _MAIN_

#include "stdio.h"
#include "string.h"
#include "math.h"

extern int main(int argc, char **argv);

#endif

Syntax

usage: scramble [-?] [-i INPUT] [-c CFILE] [-C] [-h HFILE] [-n NAME]
                [-p PREFIX] [-N] [-s SFILE] [--noc99] [-j JOIN [JOIN ...]]
                [-o OUTPUT] [-t CTYPES]

optional arguments:
  -?, --help
  -i INPUT, --input INPUT
                        input file
  -c CFILE, --cfile CFILE
                        c output file
  -C, --comments        keep comments
  -h HFILE, --hfile HFILE
                        h output file
  -n NAME, --name NAME  module name
  -p PREFIX, --prefix PREFIX
                        header guard prefix
  -N, --no-lines        don't generate #line directives
  -s SFILE, --sfile SFILE
                        intermediate code output file
  --noc99               do not use C99
  -j JOIN [JOIN ...], --join JOIN [JOIN ...]
                        files to join
  -o OUTPUT, --output OUTPUT
                        source code output file
  -t CTYPES, --ctypes CTYPES
                        ctypes output file

Keywords

All C, C++ and Python keywords basically are also Scramble keywords. The following control flow constructs are used by scramble: while, switch...case, do...while, for X while Y with Z [for (X; Y; Z) in C], for...in, if...elif...else, label [: in C], goto.

And these declarations: class [struct in C], def, enum, global, import, macro [#define in C], static, struct, typedef, union.

These Python operators are used instead of the C++ ones: and [&& in C], max, min, not [! in C], or [|| in C].

And there's a few new constants: True, False, None.

Features

In general, here is what scramble will do:

More stuff (might change?)

Valid XHTML 1.0 Strict