initial commit

This commit is contained in:
Quentin Roussel
2024-12-05 18:39:07 +08:00
commit 9a400b14ca
6 changed files with 307 additions and 0 deletions

69
preprocessing.py Normal file
View File

@@ -0,0 +1,69 @@
import ast
import inspect
import z3
import brahma
def list_function_inputs(func):
"""
Lists the input parameters of a given Python function.
Args:
func (function): The Python function to inspect.
Returns:
list: A list of strings representing the input parameter names.
"""
if not callable(func):
raise ValueError("The provided argument is not a function.")
# Get the signature of the function
signature = inspect.signature(func)
# Extract the parameter names
inputs = [param.name for param in signature.parameters.values()
if param.kind in (param.POSITIONAL_OR_KEYWORD, param.KEYWORD_ONLY)]
return inputs
def get_oldest_references_from_lambda(func):
"""
Extracts the oldest values referenced for each variable from a lambda function.
Args:
func (callable): A lambda function, such as (lambda I1_1, I1_2: (I1_1 & I1_2) - 1).
Returns:
dict: A dictionary where keys are variable names (e.g., "I1") and values are the oldest indices referenced.
"""
# Get the source code of the lambda function
source = inspect.getsource(func).strip()
# Parse the source code into an AST
tree = ast.parse(source)
# Extract the lambda function node (should be the first node in the body)
lambda_node = tree.body[0].value
# Dictionary to store the oldest reference for each variable
oldest_references = {}
class ReferenceVisitor(ast.NodeVisitor):
def visit_Name(self, node):
# Check if the variable matches the naming pattern (e.g., I1_1, I1_2, etc.)
if "_" in node.id:
name, index = node.id.rsplit("_", 1)
if index.isdigit():
index = int(index)
if name not in oldest_references or index > oldest_references[name]:
oldest_references[name] = index
else:
if node.id not in oldest_references:
oldest_references[node.id] = 0
# Visit the AST nodes to extract variable references
ReferenceVisitor().visit(lambda_node)
return oldest_references