A C code generator written in Python 3
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file cfile-0.4.0.tar.gz
.
File metadata
- Download URL: cfile-0.4.0.tar.gz
- Upload date:
- Size: 17.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 510696b7d37ea6b1522f74b950963cf72dfbf1ada7ce8b0c3831846ee97577c0 |
|
MD5 | 115f333af6a6c6b225920417ba96ff66 |
|
BLAKE2b-256 | 3ce72786addea24abca7c059b89681eb32d81f53c24d4559bbe2a05c1517da56 |
File details
Details for the file cfile-0.4.0-py3-none-any.whl
.
File metadata
- Download URL: cfile-0.4.0-py3-none-any.whl
- Upload date:
- Size: 14.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 20dbd8e1d2abc9fa06b815fdf873073a3bb9690a61143a8567315afce1e56277 |
|
MD5 | 10965b67de7a0dabb4061233361f7bce |
|
BLAKE2b-256 | 4dd7199b315d7b33d725fd98b04b5cba236e07fe245c34212adb949d1af82805 |