5. C code generation utilities

emlearn.cgen.array_declare(name, size=None, dtype='float', modifiers='static const', values=None, end='', indent='')[source]

Declare and optionally initialize an array.

>>> from emlearn import cgen
>>> cgen.array_declare("declareonly", 10)
"static const float declareonly[10];"

Also intialize it.

>>> from emlearn import cgen
>>> cgen.array_declare("initialized", 3, dtype='int', modifiers='const')
"const int initialized[3] = { 1, 2, 3 };"
emlearn.cgen.array_declare_fixedpoint(name, fixedpoint: FixedPointFormat | None = None, values=None, **kwargs)[source]

Declare and optionally initialize an fixed-point array.

Handles conversion of floating-point values to fixed-point.

emlearn.cgen.assert_valid_identifier(s)[source]

Check whether a identifier is a valid in C

Raises:

ValueError – In case this is not a valid C string

emlearn.cgen.constant(val, dtype='float')[source]

A literal value

>>> from emlearn import cgen
>>> cgen.constant(3.14)
"3.14f"
emlearn.cgen.constant_declare(name, val, dtype='int')[source]

Declaration and initialization of a constant value

>>> from emlearn import cgen
>>> cgen.constant_declare('myvariable', 3)
'static const int myvariable = 3; '

Floating point instead

>>> from emlearn import cgen
>>> cgen.constant_declare('myfloat', 3.14, dtype='float')
'static const float myfloat = 3.140000f; '
emlearn.cgen.identifier_is_reserved(s)[source]

Check whether identifier is a reserved keyword in C

>>> from emlearn import cgen
>>> cgen.identifier_is_reserved("for")
True
emlearn.cgen.identifier_is_valid(s)[source]

Check whether identifier consists only of valid characters for C

>>> from emlearn import cgen
>>> cgen.identifier_is_reserved("_++")
True
emlearn.cgen.struct_declare(name, type_name, values=[], modifiers='static const')[source]

Declaration and initialization of a (typedef) struct

>>> from emlearn import cgen
>>> cgen.struct_declare('myinstance', 'SomeStruct', [2, 'foo_array'])
'static const SomeStruct myinstance = { 2, foo_array };'
emlearn.cgen.struct_init(*args)[source]

Struct initializer

>>> from emlearn import cgen
>>> cgen.struct_init([ 1, 2, 3 ])
"{ 1, 2, 3 }"