File size: 2,322 Bytes
b7fb07f
 
 
 
 
 
 
 
 
 
 
 
 
 
feac656
b7fb07f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
Python Generator Agent - Uses LLM to generate equivalent Python code from Ada analysis.
"""

from smolagents import ToolCallingAgent


class PythonGeneratorAgent:
    """Agent responsible for generating Python code from Ada analysis using LLM."""
    
    def __init__(self, model):
        self.model = model
        self.agent = ToolCallingAgent(
            tools=[],
            model=model
        )
    
    def generate_python_code(self, ada_code: str, parsed_analysis: str, semantic_analysis: str) -> str:
        """
        Generate Python code equivalent to the Ada code using LLM.
        
        Args:
            ada_code: The original Ada source code
            parsed_analysis: Structural analysis from parser agent
            semantic_analysis: Semantic analysis from semantic agent
            
        Returns:
            Pure Python code string without markdown formatting
        """
        try:
            prompt = f"""
            Generate equivalent Python code for the given Ada code based on the analysis provided.
            
            Requirements:
            - Return ONLY pure Python code, no markdown or explanations
            - Include proper imports at the top
            - Use Python best practices and idiomatic code
            - Add type hints where appropriate
            - Include docstrings for functions and classes
            - Convert Ada types to appropriate Python equivalents:
              * Ada records β†’ Python dataclasses or classes
              * Ada arrays β†’ Python lists or numpy arrays
              * Ada enumerations β†’ Python Enum classes
              * Ada procedures β†’ Python functions returning None
              * Ada functions β†’ Python functions with return types
            
            Original Ada Code:
            ```ada
            {ada_code}
            ```
            
            Structural Analysis:
            {parsed_analysis}
            
            Semantic Analysis:
            {semantic_analysis}
            
            Generate the Python code:
            """
            
            result = self.agent.run(prompt)
            return result if isinstance(result, str) else str(result)
            
        except Exception as e:
            return f"# Error\n\nFailed to generate Python code: {str(e)}"