pyfcstm.render.expr
Expression rendering utilities for converting DSL AST nodes into language-specific text.
This module provides functionality to render expression nodes from the DSL abstract syntax tree into string representations for multiple language styles, including DSL, C, C++, Python, Java, JavaScript, TypeScript, Rust, and Go. Rendering is performed through Jinja2 templating with a small set of default templates and an optional extension mechanism to override or extend the template set.
The module exposes the following public functions:
fn_expr_render()- Render a single expression node with a provided template setcreate_expr_render_template()- Build template mappings for a given language stylerender_expr_node()- High-level rendering convenience function
Note
Templates must include a default key if you intend to render custom nodes
that are not matched by the predefined entries.
Example:
>>> from pyfcstm.dsl import Integer
>>> from pyfcstm.render.expr import render_expr_node
>>> render_expr_node(Integer("42"), lang_style='python')
'42'
>>> render_expr_node(Integer("42"), lang_style='c')
'42'
fn_expr_render
- pyfcstm.render.expr.fn_expr_render(node: float | int | dict | Expr | Any, templates: Dict[str, str], env: Environment) str[source]
Render an expression node using the provided templates and Jinja2 environment.
This function detects the node type and selects the most specific template available. For operator nodes, it tries operator-specific templates such as
UnaryOp(!)orBinaryOp(**)before falling back to the generic template. If the node is not a DSL expression, primitive Python types are converted into their corresponding DSL literal nodes. Any other object is rendered viarepr().- Parameters:
node (Union[float, int, dict, dsl_nodes.Expr, Any]) – The expression node to render, which may be a DSL expression, a primitive value, or any Python object
templates (Dict[str, str]) – Dictionary mapping node types to Jinja2 template strings
env (jinja2.Environment) – Jinja2 environment for template rendering
- Returns:
The rendered string representation of the expression node
- Return type:
str
- Raises:
KeyError – If no matching template is found and
defaultis absent
Example:
>>> env = create_env() >>> templates = _DSL_STYLE >>> fn_expr_render(Integer(42).to_ast_node(), templates, env) '42' >>> fn_expr_render(True, templates, env) 'True'
create_expr_render_template
- pyfcstm.render.expr.create_expr_render_template(lang_style: str = 'dsl', ext_configs: Dict[str, str] | None = None) Dict[str, str][source]
Create a template dictionary for expression rendering based on the specified language style.
This function merges predefined templates for the requested language style with optional custom template entries. Custom entries override defaults with the same key.
- Parameters:
lang_style (str) – The language style to use (
'dsl','c','cpp','python','java','js','ts','rust','go')ext_configs (Optional[Dict[str, str]]) – Optional additional template configurations to extend or override defaults
- Returns:
A dictionary of templates for the specified language style
- Return type:
Dict[str, str]
- Raises:
KeyError – If
lang_styleis not recognized
Example:
>>> templates = create_expr_render_template('python', {'CustomNode': '{{ node.custom_value }}'}) >>> 'UFunc' in templates and 'CustomNode' in templates True
render_expr_node
- pyfcstm.render.expr.render_expr_node(expr: float | int | dict | Expr | Any, lang_style: str = 'dsl', ext_configs: Dict[str, str] | None = None, env: Environment | None = None) str[source]
Render an expression node to a string representation in the specified language style.
This is a high-level convenience wrapper that prepares a Jinja2 environment, registers the
expr_renderfilter and global function, and renders the provided expression node.- Parameters:
expr (Union[float, int, dict, dsl_nodes.Expr, Any]) – The expression to render
lang_style (str) – The language style to use (
'dsl','c','cpp','python','java','js','ts','rust','go')ext_configs (Optional[Dict[str, str]]) – Optional additional template configurations
env (Optional[jinja2.Environment]) – Optional pre-configured Jinja2 environment
- Returns:
The rendered string representation of the expression
- Return type:
str
- Raises:
KeyError – If
lang_styleis not recognized
Example:
>>> from pyfcstm.dsl import Integer >>> render_expr_node(Integer('42'), lang_style='python') '42' >>> render_expr_node(Integer('42'), lang_style='c') '42'