pyfcstm.render.expr
Expression rendering module for converting DSL nodes to different language formats.
This module provides functionality to render expression nodes in various language styles including DSL, C/C++, and Python. It uses Jinja2 templating to transform abstract syntax tree nodes into string representations according to the specified language style.
The module contains predefined templates for different node types and operators, and allows for custom template extensions.
fn_expr_render
- pyfcstm.render.expr.fn_expr_render(node: float | int | dict | Expr | Any, templates: Dict[str, str], env: Environment)[source]
Render an expression node using the provided templates and Jinja2 environment.
This function handles different types of expression nodes and selects the appropriate template for rendering based on the node type and available templates.
- Parameters:
node (Union[float, int, dict, dsl_nodes.Expr, Any]) – The expression node to render, can be a DSL node or a primitive value
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
Example:
>>> env = create_env() >>> templates = _DSL_STYLE >>> fn_expr_render(Integer(42).to_ast_node(), templates, env) '42'
create_expr_render_template
- pyfcstm.render.expr.create_expr_render_template(lang_style: str = 'dsl', ext_configs: Dict[str, str] | None = None)[source]
Create a template dictionary for expression rendering based on the specified language style.
This function combines the predefined templates for the specified language style with any additional custom templates provided in ext_configs.
- Parameters:
lang_style (str) – The language style to use (‘dsl’, ‘c’, ‘cpp’, ‘python’)
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]
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)[source]
Render an expression node to a string representation in the specified language style.
This is a high-level function that sets up the environment and renders the expression in one step. It’s a convenient wrapper around add_expr_render_to_env and fn_expr_render.
- 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’)
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
Example:
>>> from pyfcstm.dsl import Integer >>> render_expr_node(Integer('42'), lang_style='python') '42' >>> render_expr_node(Integer('42'), lang_style='c') '42'