Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
cogu committed Feb 5, 2024
1 parent 4705ba2 commit f5a7d21
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,15 @@ print(writer.write_str(code))
## Important update about declarations
Starting from version 0.3.1 you need to wrap functions, variables, structs and typedefs inside `C.declaraton` to actually declare them.
Before v0.3.1 these elements were implicitly declared when encountered in the code sequence.
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:
Not using `C.declaration` will only print the name when used on variables, functions or typedefs.
* without declaration: Will only forward declare the struct type.
* With declaration: Will fully declare the struct and its members.
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:
Expand Down Expand Up @@ -131,6 +133,35 @@ typedef struct mystruct {
} 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.

```python
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))
```

```C
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).
Expand Down

0 comments on commit f5a7d21

Please sign in to comment.