Skip to main content

cfile

A C code generator written in Python 3.

Usage

import cfile

C = cfile.CFactory()
code = C.sequence()
code.append(C.sysinclude("stdio.h"))
code.append(C.blank())
char_ptr_type = C.type("char", pointer=True)
code.append(C.declaration(C.function("main", "int",
                                     params=[C.variable("argc", "int"),
                                             C.variable("argv", char_ptr_type, pointer=True)
                                             ])))
main_body = C.block()
main_body.append(C.statement(C.func_call("printf", C.str_literal(r"Hello World\n"))))
main_body.append(C.statement(C.func_return(0)))
code.append(main_body)
writer = cfile.Writer(cfile.StyleOptions())
print(writer.write_str(code))

Output

   #include <stdio.h>

   int main(int argc, char** argv)
   {
      printf("Hello World!\n");
      return 0;
   }

Here's the same example again but this time we change the formatting style of the output:

  • Opening brace on same line
  • Pointer alignment to the right
import cfile

C = cfile.CFactory()
code = C.sequence()
code.append(C.sysinclude("stdio.h"))
code.append(C.blank())
char_ptr_type = C.type("char", pointer=True)
code.append(C.declaration(C.function("main", "int",
                                     params=[C.variable("argc", "int"),
                                             C.variable("argv", char_ptr_type, pointer=True)
                                             ])))
main_body = C.block()
main_body.append(C.statement(C.func_call("printf", C.str_literal(r"Hello World\n"))))
main_body.append(C.statement(C.func_return(0)))
code.append(main_body)
style = cfile.StyleOptions(break_before_braces=cfile.BreakBeforeBraces.ATTACH,
                           pointer_alignment=cfile.Alignment.RIGHT)
writer = cfile.Writer(style)
print(writer.write_str(code))

Output

   #include <stdio.h>

   int main(int argc, char **argv) {
      printf("Hello World!\n");
      return 0;
   }

Important update about declarations

Starting from version 0.3.2 you need to wrap functions, variables, structs and typedefs inside C.declaraton to actually declare them. Before v0.3.2 these elements were implicitly declared when encountered in the code sequence.

Not using C.declaration will only print the name when used on variables, functions or typedefs.

For structs it will have the following meaning:

  • without declaration: Will only forward declare the struct type.
  • With declaration: Will fully declare the struct and its members.

Example

import cfile

C = cfile.CFactory()

code = C.sequence()
mystruct = C.struct("mystruct",
                    members=[C.struct_member("field_1", "int"),
                             C.struct_member("field_2", "int")])
code.append(C.statement(mystruct))  # Forward declaration
code.append(C.blank())
code.append(C.statement(C.declaration(mystruct)))  # Struct declaration
writer = cfile.Writer(cfile.StyleOptions())
print(writer.write_str(code))

Output

struct mystruct;

struct mystruct
{
    int field_1;
    int field_2;
};

When declaring typedefs of structs you can wrap the struct declaration inside the declaration of the typedef.

Example

import cfile

C = cfile.CFactory()

code = C.sequence()
mystruct = C.struct("mystruct",
                    members=[C.struct_member("field_1", "int"),
                             C.struct_member("field_2", "int")])
code.append(C.statement(C.declaration(C.typedef("mystruct_t", C.declaration(mystruct)))))
writer = cfile.Writer(cfile.StyleOptions(break_before_braces=cfile.BreakBeforeBraces.ATTACH))
print(writer.write_str(code))

Output

typedef struct mystruct {
    int field_1;
    int field_2;
} mystruct_t;

There's some basic support for struct initializers. By rearranging the code from the example above we can declare a new struct variable named instance with initializer.

import cfile

C = cfile.CFactory()
code = C.sequence()
struct = C.struct("mystruct",
                  members=[C.struct_member("field_1", "int"),
                           C.struct_member("field_2", "int")])
struct_type = C.typedef("my_struct_t", C.declaration(struct))
code.append(C.statement(C.declaration(struct_type)))
code.append(C.blank())
code.append(C.statement(C.declaration(C.variable("instance", struct_type), [0, 0])))

writer = cfile.Writer(cfile.StyleOptions(break_before_braces=cfile.BreakBeforeBraces.ATTACH))
print(writer.write_str(code))

Output

typedef struct mystruct {
    int field_1;
    int field_2;
} my_struct_t;

my_struct_t instance = {0, 0};

Requires

Python 3.10+ (Needed for modern type hinting support).

Documentation

Documentation will be written at a later date.

For currently supported style options see class StyleOptions in cfile.style module.

Changelog

See Changelog document.

About versions

v0.2: No longer maintained.

v0.4: Stable version (bug-fixes only).

v0.5: Development track.

Metadata

Release files for cfile 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cfile 0.4.0
File Size Uploaded
cfile-0.4.0.tar.gz 17.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cfile 0.4.0
File Interpreter ABI Platform
cfile-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 31.5 kB

Release files / cfile-0.4.0.tar.gz

Download URL cfile-0.4.0.tar.gz
Size 17.3 kB
Tags Source
SHA-256 checksum
How to use checksums
510696b7d37ea6b1522f74b950963cf72dfbf1ada7ce8b0c3831846ee97577c0
BLAKE2b-256 checksum
How to use checksums
3ce72786addea24abca7c059b89681eb32d81f53c24d4559bbe2a05c1517da56
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.2

Release files / cfile-0.4.0-py3-none-any.whl

Download URL cfile-0.4.0-py3-none-any.whl
Size 14.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
20dbd8e1d2abc9fa06b815fdf873073a3bb9690a61143a8567315afce1e56277
BLAKE2b-256 checksum
How to use checksums
4dd7199b315d7b33d725fd98b04b5cba236e07fe245c34212adb949d1af82805
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.2

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.2.1

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page