MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
ClangInterpreter.hpp
Go to the documentation of this file.
1#pragma once
2
3namespace clang {
4class Interpreter;
5class CompilerInstance;
6}
7
8namespace Lila {
9
10/**
11 * @class ClangInterpreter
12 * @brief Embedded Clang interpreter for live code evaluation in MayaFlux
13 *
14 * The ClangInterpreter class provides an interface for compiling and executing C++ code snippets
15 * at runtime using Clang's incremental interpreter. It is the core engine behind Lila's live coding
16 * capabilities, enabling real-time code evaluation, symbol lookup, and dynamic integration of user code.
17 *
18 * ## Core Responsibilities
19 * - Initialize and manage the Clang interpreter instance
20 * - Evaluate code snippets and files, returning results and errors
21 * - Track and expose defined symbols for runtime introspection
22 * - Allow configuration of include paths, compile flags, and target triple
23 * - Provide error reporting and reset functionality
24 *
25 * ## Usage Flow
26 * 1. Construct a ClangInterpreter instance
27 * 2. Call initialize() to set up the interpreter
28 * 3. Use eval() or eval_file() to execute code
29 * 4. Query symbols or errors as needed
30 * 5. Optionally configure include paths, flags, or target triple before initialization
31 * 6. Call shutdown() or reset() to clean up
32 *
33 * This class is intended for internal use by Lila and is not exposed to end users directly.
34 */
35class LILA_API ClangInterpreter {
36public:
37 /**
38 * @brief Constructs a ClangInterpreter instance
39 */
41
42 /**
43 * @brief Destructor; shuts down the interpreter if active
44 */
46
50 ClangInterpreter& operator=(ClangInterpreter&&) noexcept;
51
52 /**
53 * @brief Initializes the Clang interpreter and prepares for code evaluation
54 * @return True if initialization succeeded, false otherwise
55 */
56 bool initialize();
57
58 /**
59 * @brief Shuts down the interpreter and releases resources
60 */
61 void shutdown();
62
63 /**
64 * @struct EvalResult
65 * @brief Result of code evaluation
66 *
67 * Contains success status, output, error message, and optional symbol address.
68 */
69 struct EvalResult {
70 bool success = false; ///< True if evaluation succeeded
71 std::string output; ///< Output from code execution
72 std::string error; ///< Error message if evaluation failed
73 void* symbol_address = nullptr; ///< Address of defined symbol (if applicable)
74 };
75
76 /**
77 * @brief Evaluates a code snippet
78 * @param code C++ code to execute
79 * @return EvalResult containing success, output, and error info
80 */
81 EvalResult eval(const std::string& code);
82
83 /**
84 * @brief Evaluates a code file by including it
85 * @param filepath Path to the file to execute
86 * @return EvalResult containing success, output, and error info
87 */
88 EvalResult eval_file(const std::string& filepath);
89
90 /**
91 * @brief Gets the address of a symbol defined in the interpreter
92 * @param name Symbol name
93 * @return Pointer to symbol, or nullptr if not found
94 */
95 void* get_symbol_address(const std::string& name);
96
97 /**
98 * @brief Gets a list of all defined symbols
99 * @return Vector of symbol names
100 */
101 std::vector<std::string> get_defined_symbols();
102
103 /**
104 * @brief Adds an include path for code evaluation
105 * @param path Directory to add to the include search path
106 */
107 void add_include_path(const std::string& path);
108
109 /**
110 * @brief Adds a library path (not yet implemented)
111 * @param path Directory to add to the library search path
112 */
113 void add_library_path(const std::string& path);
114
115 /**
116 * @brief Adds a compile flag for code evaluation
117 * @param flag Compiler flag to add
118 */
119 void add_compile_flag(const std::string& flag);
120
121 /**
122 * @brief Sets the target triple for code generation
123 * @param triple Target triple string
124 */
125 void set_target_triple(const std::string& triple);
126
127 /**
128 * @brief Resets the interpreter, clearing all state
129 */
130 void reset();
131
132 /**
133 * @brief Gets the last error message
134 * @return String containing the last error
135 */
136 [[nodiscard]] std::string get_last_error() const { return m_last_error; }
137
138private:
139 struct Impl; ///< Internal implementation details
140 std::unique_ptr<Impl> m_impl;
141 std::string m_last_error;
142
145 std::string preprocess_code(const std::string& code);
146 void extract_symbols_from_code(const std::string& code);
148};
149
150} // namespace Lila
void extract_symbols_from_code(const std::string &code)
std::string get_last_error() const
Gets the last error message.
ClangInterpreter(ClangInterpreter &&) noexcept
ClangInterpreter(const ClangInterpreter &)=delete
std::unique_ptr< Impl > m_impl
Internal implementation details.
std::string preprocess_code(const std::string &code)
ClangInterpreter & operator=(const ClangInterpreter &)=delete
Embedded Clang interpreter for live code evaluation in MayaFlux.
void initialize()
Definition main.cpp:11
std::string error
Error message if evaluation failed.
std::string output
Output from code execution.