pyfcstm.render.c_runtime

C runtime code-generation helpers for built-in native templates.

This module contains small, deterministic emitters used by the built-in c and c_poll templates. The helpers render lifecycle action, transition-effect, and guard bodies with explicit runtime diagnostics around DSL expression failures that would otherwise surface as native crashes or C compile errors.

The module contains:

The generated code remains C99-only and uses only helpers already emitted by machine.c; it does not add third-party runtime dependencies.

Example:

>>> from pyfcstm.dsl.node import Integer, OperationAssignment
>>> code = render_c_action_body(
...     [OperationAssignment("x", Integer("1"))],
...     {"x": "int"},
...     "DemoMachine",
...     "DEMO_MACHINE",
... )
>>> "scope->x = 1;" in code
True

OperationalNode

pyfcstm.render.c_runtime.OperationalNode

alias of OperationStatement | OperationalStatement

render_c_action_body

pyfcstm.render.c_runtime.render_c_action_body(statements: Iterable[OperationStatement | OperationalStatement], var_types: Mapping[str, Any], machine_class_name: str, machine_macro_name: str, indent: str = '    ') str[source]

Render a fallible generated C body for operation statements.

Parameters:
  • statements (Iterable[Union[pyfcstm.model.OperationStatement, pyfcstm.dsl.node.OperationalStatement]]) – Operation statements from an action or transition effect.

  • var_types (Mapping[str, Any]) – Persistent variable type mapping or model defines mapping.

  • machine_class_name (str) – Generated machine class name, such as "RootMachine".

  • machine_macro_name (str) – Generated macro prefix, such as "ROOT_MACHINE".

  • indent (str, optional) – Indentation unit used for generated C code, defaults to four spaces.

Returns:

C statements ending in a generated success or failure return.

Return type:

str

Example:

>>> from pyfcstm.dsl.node import Integer, OperationAssignment
>>> body = render_c_action_body(
...     [OperationAssignment("x", Integer("1"))], {"x": "int"}, "M", "M"
... )
>>> body.strip().endswith("return M_SUCCESS;")
True

render_c_reset_vars_body

pyfcstm.render.c_runtime.render_c_reset_vars_body(var_defines: Mapping[str, Any], machine_class_name: str, machine_macro_name: str, indent: str = '    ') str[source]

Render C statements for default persistent-variable initialization.

The emitted body evaluates def initializers in declaration order and applies the same persistent int writeback boundary used by operation blocks. Integer defaults therefore accept integer-valued floats but report a generated runtime error for non-integer floats instead of relying on C’s implicit narrowing conversion.

Parameters:
  • var_defines (Mapping[str, Any]) – Model variable definitions keyed by DSL variable name.

  • machine_class_name (str) – Generated machine class name.

  • machine_macro_name (str) – Generated macro prefix.

  • indent (str, optional) – Indentation unit used for generated C code, defaults to four spaces.

Returns:

C statements ending in a generated success return.

Return type:

str

Example:

>>> body = render_c_reset_vars_body({}, "Demo", "DEMO")
>>> body.strip().endswith("return DEMO_SUCCESS;")
True

render_c_condition_body

pyfcstm.render.c_runtime.render_c_condition_body(expr: Any, var_types: Mapping[str, Any], machine_class_name: str, machine_macro_name: str, usage: str, result_name: str = 'result', indent: str = '    ') str[source]

Render a fallible generated C body for a boolean condition.

Parameters:
  • expr (Any) – Guard or condition expression.

  • var_types (Mapping[str, Any]) – Persistent variable type mapping or model defines mapping.

  • machine_class_name (str) – Generated machine class name.

  • machine_macro_name (str) – Generated macro prefix.

  • usage (str) – Diagnostic usage prefix, for example "transition guard".

  • result_name (str, optional) – Pointer variable receiving the truth value, defaults to "result".

  • indent (str, optional) – Indentation unit used for generated C code, defaults to four spaces.

Returns:

C condition body ending in generated success or failure return.

Return type:

str

Example:

>>> from pyfcstm.dsl.node import Integer
>>> body = render_c_condition_body(Integer("1"), {}, "M", "M", "guard")
>>> "*result = !!(1);" in body
True