pyfcstm.model.expr

Expression handling module for mathematical expressions and operations.

This module provides a comprehensive set of classes for representing and evaluating mathematical expressions. It includes support for basic data types (integers, floats, booleans), various operators (unary, binary, conditional), mathematical functions, and variables.

The expression system allows for:

  • Building complex mathematical expressions

  • Evaluating expressions with variable substitution

  • Converting expressions to AST nodes

  • Analyzing expressions to extract variables

  • Handling operator precedence correctly

All expression classes inherit from the base Expr class and implement the AstExportable interface.

Expr

class pyfcstm.model.expr.Expr[source]

Base class for all expressions.

This abstract class defines the common interface for all expression types. It provides methods for traversing the expression tree, evaluating expressions, and converting expressions to AST nodes.

Return type:

Expr

list_variables()[source]

List all unique variables used in this expression.

Returns:

List of unique Variable objects

Return type:

list[Variable]

Integer

class pyfcstm.model.expr.Integer(value: int)[source]

Integer literal expression.

Parameters:

value (int) – Integer value

to_ast_node() Expr[source]

Convert to an Integer AST node.

Returns:

Integer AST node

Return type:

dsl_nodes.Integer

Float

class pyfcstm.model.expr.Float(value: float)[source]

Floating point literal expression.

Parameters:

value (float) – Float value

to_ast_node() Expr[source]

Convert to a Float AST node or a Constant node for special values.

Recognizes mathematical constants like pi, e, and tau.

Returns:

Float or Constant AST node

Return type:

Union[dsl_nodes.Float, dsl_nodes.Constant]

Boolean

class pyfcstm.model.expr.Boolean(value: bool)[source]

Boolean literal expression.

Parameters:

value (bool) – Boolean value

to_ast_node() Expr[source]

Convert to a Boolean AST node.

Returns:

Boolean AST node

Return type:

dsl_nodes.Boolean

Op

class pyfcstm.model.expr.Op[source]

Base class for all operator expressions.

This abstract class provides common functionality for operator expressions.

property op_mark

Get the operator mark for precedence lookup.

Returns:

Operator mark

Return type:

str

Raises:

NotImplementedError – Must be implemented by subclasses

BinaryOp

class pyfcstm.model.expr.BinaryOp(x: Expr, op: str, y: Expr)[source]

Binary operator expression.

Parameters:
  • x (Expr) – Left operand

  • op (str) – Operator symbol

  • y (Expr) – Right operand

property op_mark

Get the operator mark for precedence lookup.

Returns:

Operator mark

Return type:

str

to_ast_node() Expr[source]

Convert to a BinaryOp AST node.

Handles operator precedence by adding parentheses where needed.

Returns:

BinaryOp AST node

Return type:

dsl_nodes.BinaryOp

UnaryOp

class pyfcstm.model.expr.UnaryOp(op: str, x: Expr)[source]

Unary operator expression.

Parameters:
  • op (str) – Operator symbol

  • x (Expr) – Operand

property op_mark

Get the operator mark for precedence lookup.

Returns:

Operator mark

Return type:

str

to_ast_node() Expr[source]

Convert to a UnaryOp AST node.

Handles operator precedence by adding parentheses where needed.

Returns:

UnaryOp AST node

Return type:

dsl_nodes.UnaryOp

ConditionalOp

class pyfcstm.model.expr.ConditionalOp(cond: Expr, if_true: Expr, if_false: Expr)[source]

Conditional (ternary) operator expression.

Parameters:
  • cond (Expr) – Condition expression

  • if_true (Expr) – Expression to evaluate if condition is true

  • if_false (Expr) – Expression to evaluate if condition is false

property op_mark

Get the operator mark for precedence lookup.

Returns:

Operator mark

Return type:

str

to_ast_node() Expr[source]

Convert to a ConditionalOp AST node.

Handles operator precedence by adding parentheses where needed.

Returns:

ConditionalOp AST node

Return type:

dsl_nodes.ConditionalOp

UFunc

class pyfcstm.model.expr.UFunc(func: str, x: Expr)[source]

Mathematical function expression.

Represents calls to mathematical functions like sin, cos, sqrt, etc.

Parameters:
  • func (str) – Function name

  • x (Expr) – Function argument

to_ast_node() Expr[source]

Convert to a UFunc AST node.

Returns:

UFunc AST node

Return type:

dsl_nodes.UFunc

Variable

class pyfcstm.model.expr.Variable(name: str)[source]

Variable reference expression.

Parameters:

name (str) – Variable name

to_ast_node() Expr[source]

Convert to a Name AST node.

Returns:

Name AST node

Return type:

dsl_nodes.Name

parse_expr_node_to_expr

pyfcstm.model.expr.parse_expr_node_to_expr(node: Expr) Expr[source]

Parse an AST expression node into an Expr object.

This function converts DSL expression nodes into the corresponding expression objects.

Parameters:

node (dsl_nodes.Expr) – AST expression node

Returns:

Corresponding expression object

Return type:

Expr

Raises:

TypeError – If the node type is not recognized

Example:

>>> ast_node = dsl_nodes.Integer(raw="42")
>>> expr = parse_expr_node_to_expr(ast_node)
>>> isinstance(expr, Integer)
True
>>> expr.value
42