yapcad.dsl.transforms package

Submodules

yapcad.dsl.transforms.base module

AST transformation framework for DSL optimization.

Provides a base class for AST transformations that can be applied before interpretation. This allows for future optimizations like: - Constant folding - Dead code elimination - Pattern unrolling - Common subexpression caching

class yapcad.dsl.transforms.base.AstTransform[source]

Bases: ABC

Base class for AST transformations.

Transforms are applied to a Module and return a (potentially modified) Module. Transforms can be composed in a pipeline.

abstract property name: str

Name of this transform for debugging/logging.

abstractmethod transform(module: Module) Module[source]

Apply this transform to a module.

Parameters:

module – The input module AST

Returns:

The transformed module (may be the same object or a copy)

class yapcad.dsl.transforms.base.IdentityTransform[source]

Bases: AstTransform

Identity transform - returns module unchanged.

property name: str

Name of this transform for debugging/logging.

transform(module: Module) Module[source]

Apply this transform to a module.

Parameters:

module – The input module AST

Returns:

The transformed module (may be the same object or a copy)

class yapcad.dsl.transforms.base.TransformPipeline(transforms: List[AstTransform] = None)[source]

Bases: object

A pipeline of AST transforms to apply in sequence.

add(transform: AstTransform) TransformPipeline[source]

Add a transform to the pipeline.

apply(module: Module) Module[source]

Apply all transforms in sequence.

class yapcad.dsl.transforms.base.TreeTransform[source]

Bases: AstTransform

A transform that walks the tree and can modify nodes.

Subclasses override visit_* methods to transform specific node types. By default, nodes are copied unchanged.

transform(module: Module) Module[source]

Transform a module by visiting all nodes.

visit_assignment(node: AssignmentStatement) Statement[source]

Visit an assignment statement.

visit_binary_op(node: BinaryOp) Expression[source]
visit_block(node: Block) Statement[source]

Visit a block.

visit_command(node: FunctionDef) FunctionDef[source]

Visit a command node.

visit_dict_literal(node: DictLiteral) Expression[source]
visit_emit(node: EmitStatement) Statement[source]

Visit an emit statement.

visit_expr_statement(node: ExpressionStatement) Statement[source]

Visit an expression statement.

visit_expression(node: Expression) Expression[source]

Visit an expression node.

visit_for(node: ForStatement) Statement[source]

Visit a for statement.

visit_function_call(node: FunctionCall) Expression[source]
visit_identifier(node: Identifier) Expression[source]
visit_if_expr(node: IfExpr) Expression[source]
visit_index_access(node: IndexAccess) Expression[source]
visit_lambda(node: LambdaExpr) Expression[source]
visit_let(node: VarDecl) Statement[source]

Visit a let statement.

visit_list_comprehension(node: ListComprehension) Expression[source]
visit_list_literal(node: ListLiteral) Expression[source]
visit_literal(node: Literal) Expression[source]
visit_match_expr(node: MatchExpr) Expression[source]
visit_member_access(node: MemberAccess) Expression[source]
visit_method_call(node: MethodCall) Expression[source]
visit_module(node: Module) Module[source]

Visit a module node.

visit_python_block(node: PythonBlock) Statement[source]

Visit a Python block (no transformation by default).

visit_python_expr(node: PythonExpr) Expression[source]
visit_range(node: RangeExpr) Expression[source]
visit_require(node: AssertStatement) Statement[source]

Visit a require statement.

visit_return(node: ReturnStatement) Statement[source]

Visit a return statement.

visit_statement(node: Statement) Statement[source]

Visit a statement node.

visit_unary_op(node: UnaryOp) Expression[source]

Module contents

DSL AST Transformation Framework.

Provides a framework for AST transformations that can be applied before interpretation. This allows for optimizations like: - Constant folding - Dead code elimination - Pattern unrolling - Common subexpression caching

Usage:

from yapcad.dsl.transforms import TransformPipeline, ConstantFoldTransform

pipeline = TransformPipeline() pipeline.add(ConstantFoldTransform())

optimized_module = pipeline.apply(module)

class yapcad.dsl.transforms.AstTransform[source]

Bases: ABC

Base class for AST transformations.

Transforms are applied to a Module and return a (potentially modified) Module. Transforms can be composed in a pipeline.

abstract property name: str

Name of this transform for debugging/logging.

abstractmethod transform(module: Module) Module[source]

Apply this transform to a module.

Parameters:

module – The input module AST

Returns:

The transformed module (may be the same object or a copy)

class yapcad.dsl.transforms.IdentityTransform[source]

Bases: AstTransform

Identity transform - returns module unchanged.

property name: str

Name of this transform for debugging/logging.

transform(module: Module) Module[source]

Apply this transform to a module.

Parameters:

module – The input module AST

Returns:

The transformed module (may be the same object or a copy)

class yapcad.dsl.transforms.TransformPipeline(transforms: List[AstTransform] = None)[source]

Bases: object

A pipeline of AST transforms to apply in sequence.

add(transform: AstTransform) TransformPipeline[source]

Add a transform to the pipeline.

apply(module: Module) Module[source]

Apply all transforms in sequence.

class yapcad.dsl.transforms.TreeTransform[source]

Bases: AstTransform

A transform that walks the tree and can modify nodes.

Subclasses override visit_* methods to transform specific node types. By default, nodes are copied unchanged.

transform(module: Module) Module[source]

Transform a module by visiting all nodes.

visit_assignment(node: AssignmentStatement) Statement[source]

Visit an assignment statement.

visit_binary_op(node: BinaryOp) Expression[source]
visit_block(node: Block) Statement[source]

Visit a block.

visit_command(node: FunctionDef) FunctionDef[source]

Visit a command node.

visit_dict_literal(node: DictLiteral) Expression[source]
visit_emit(node: EmitStatement) Statement[source]

Visit an emit statement.

visit_expr_statement(node: ExpressionStatement) Statement[source]

Visit an expression statement.

visit_expression(node: Expression) Expression[source]

Visit an expression node.

visit_for(node: ForStatement) Statement[source]

Visit a for statement.

visit_function_call(node: FunctionCall) Expression[source]
visit_identifier(node: Identifier) Expression[source]
visit_if_expr(node: IfExpr) Expression[source]
visit_index_access(node: IndexAccess) Expression[source]
visit_lambda(node: LambdaExpr) Expression[source]
visit_let(node: VarDecl) Statement[source]

Visit a let statement.

visit_list_comprehension(node: ListComprehension) Expression[source]
visit_list_literal(node: ListLiteral) Expression[source]
visit_literal(node: Literal) Expression[source]
visit_match_expr(node: MatchExpr) Expression[source]
visit_member_access(node: MemberAccess) Expression[source]
visit_method_call(node: MethodCall) Expression[source]
visit_module(node: Module) Module[source]

Visit a module node.

visit_python_block(node: PythonBlock) Statement[source]

Visit a Python block (no transformation by default).

visit_python_expr(node: PythonExpr) Expression[source]
visit_range(node: RangeExpr) Expression[source]
visit_require(node: AssertStatement) Statement[source]

Visit a require statement.

visit_return(node: ReturnStatement) Statement[source]

Visit a return statement.

visit_statement(node: Statement) Statement[source]

Visit a statement node.

visit_unary_op(node: UnaryOp) Expression[source]