MayaFlux 0.4.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 Initialize the Clang interpreter.
54 *
55 * @param skip_host_library_load If true, do not call LoadDynamicLibrary
56 * on MayaFluxLib. Pass true when the host process has already
57 * loaded MayaFluxLib (attach scenario). Pass false for
58 * standalone use (lila_server).
59 * @return true on success.
60 */
61 bool initialize(bool skip_host_library_load = false);
62
63 /**
64 * @brief Shuts down the interpreter and releases resources
65 */
66 void shutdown();
67
68 /**
69 * @struct EvalResult
70 * @brief Result of code evaluation
71 *
72 * Contains success status, output, error message, and optional symbol address.
73 */
74 struct EvalResult {
75 bool success = false; ///< True if evaluation succeeded
76 std::string output; ///< Output from code execution
77 std::string error; ///< Error message if evaluation failed
78 void* symbol_address = nullptr; ///< Address of defined symbol (if applicable)
79 };
80
81 /**
82 * @brief Evaluates a code snippet
83 * @param code C++ code to execute
84 * @return EvalResult containing success, output, and error info
85 */
86 EvalResult eval(const std::string& code);
87
88 /**
89 * @brief Evaluates a code file by including it
90 * @param filepath Path to the file to execute
91 * @return EvalResult containing success, output, and error info
92 */
93 EvalResult eval_file(const std::string& filepath);
94
95 /**
96 * @brief Gets the address of a symbol defined in the interpreter
97 * @param name Symbol name
98 * @return Pointer to symbol, or nullptr if not found
99 */
100 void* get_symbol_address(const std::string& name);
101
102 /**
103 * @brief Gets a list of all defined symbols
104 * @return Vector of symbol names
105 */
106 std::vector<std::string> get_defined_symbols();
107
108 /**
109 * @brief Adds an include path for code evaluation
110 * @param path Directory to add to the include search path
111 */
112 void add_include_path(const std::string& path);
113
114 /**
115 * @brief Adds a library path (not yet implemented)
116 * @param path Directory to add to the library search path
117 */
118 void add_library_path(const std::string& path);
119
120 /**
121 * @brief Adds a compile flag for code evaluation
122 * @param flag Compiler flag to add
123 */
124 void add_compile_flag(const std::string& flag);
125
126 /**
127 * @brief Sets the target triple for code generation
128 * @param triple Target triple string
129 */
130 void set_target_triple(const std::string& triple);
131
132 /**
133 * @brief Resets the interpreter, clearing all state
134 */
135 void reset();
136
137 /**
138 * @brief Gets the last error message
139 * @return String containing the last error
140 */
141 [[nodiscard]] std::string get_last_error() const { return m_last_error; }
142
143private:
144 struct Impl; ///< Internal implementation details
145 std::unique_ptr<Impl> m_impl;
146 std::string m_last_error;
147
150 std::string preprocess_code(const std::string& code);
151 void extract_symbols_from_code(const std::string& code);
153};
154
155} // 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.