MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Lila.cpp
Go to the documentation of this file.
1#include "Lila.hpp"
2
4#include "Server.hpp"
5
6#include "Commentator.hpp"
7
8namespace Lila {
9
11 : m_interpreter(std::make_unique<ClangInterpreter>())
12 , m_current_mode(OperationMode::Direct)
13{
14 LILA_DEBUG(Emitter::SYSTEM, "Lila instance created");
15}
16
18{
19 stop_server();
20 LILA_DEBUG(Emitter::SYSTEM, "Lila instance destroyed");
21}
22
23bool Lila::initialize(OperationMode mode, int server_port) noexcept
24{
25 LILA_INFO(Emitter::SYSTEM, "Initializing Lila");
26 m_current_mode = mode;
27
28 if (!initialize_interpreter()) {
29 LILA_ERROR(Emitter::SYSTEM, "Failed to initialize interpreter");
30 return false;
31 }
32
33 if (mode == OperationMode::Server || mode == OperationMode::Both) {
34 if (!initialize_server(server_port)) {
35 LILA_ERROR(Emitter::SYSTEM, "Failed to initialize server");
36 return false;
37 }
38 }
39
40 LILA_INFO(Emitter::SYSTEM, "Lila initialized successfully");
41 return true;
42}
43
45{
46 LILA_DEBUG(Emitter::SYSTEM, "Initializing interpreter subsystem");
47 return m_interpreter->initialize();
48}
49
51{
52 if (m_server && m_server->is_running()) {
53 LILA_WARN(Emitter::SYSTEM, "Stopping existing server before starting new one");
54 stop_server();
55 }
56
57 LILA_DEBUG(Emitter::SYSTEM,
58 std::string("Initializing server on port ") + std::to_string(port));
59
60 m_server = std::make_unique<Server>(port);
61
62 m_server->set_message_handler([this](std::string_view message) {
63 return this->handle_server_message(message);
64 });
65
66 return m_server->start();
67}
68
69std::expected<std::string, std::string> Lila::handle_server_message(std::string_view message)
70{
71 if (message.empty()) {
72 return R"({"status":"error","message":"Empty message"})";
73 }
74
75 auto result = m_interpreter->eval(std::string(message));
76
77 if (result.success) {
78 if (m_success_callback) {
79 m_success_callback();
80 }
81 return "";
82 }
83
84 if (m_error_callback) {
85 m_error_callback(result.error);
86 }
87
88 return std::unexpected(escape_json(result.error));
89}
90
91bool Lila::eval(const std::string& code)
92{
93 if (!m_interpreter) {
94 LILA_ERROR(Emitter::SYSTEM, "Cannot eval: interpreter not initialized");
95 return false;
96 }
97
98 auto result = m_interpreter->eval(code);
99
100 if (result.success && m_success_callback) {
101 m_success_callback();
102 } else if (!result.success && m_error_callback) {
103 m_error_callback(result.error);
104 }
105
106 return result.success;
107}
108
109bool Lila::eval_file(const std::string& filepath)
110{
111 if (!m_interpreter) {
112 LILA_ERROR(Emitter::SYSTEM, "Cannot eval file: interpreter not initialized");
113 return false;
114 }
115
116 auto result = m_interpreter->eval_file(filepath);
117 return result.success;
118}
119
120void Lila::start_server(int port)
121{
122 LILA_INFO(Emitter::SYSTEM,
123 std::string("Starting server on port ") + std::to_string(port));
124
125 initialize_server(port);
126}
127
129{
130 if (m_server) {
131 LILA_INFO(Emitter::SYSTEM, "Stopping server");
132 m_server->stop();
133 m_server.reset();
134 }
135}
136
138{
139 return m_server && m_server->is_running();
140}
141
142void* Lila::get_symbol_address(const std::string& name)
143{
144 return m_interpreter ? m_interpreter->get_symbol_address(name) : nullptr;
145}
146
147std::vector<std::string> Lila::get_defined_symbols()
148{
149 return m_interpreter ? m_interpreter->get_defined_symbols() : std::vector<std::string> {};
150}
151
152void Lila::add_include_path(const std::string& path)
153{
154 if (m_interpreter) {
155 m_interpreter->add_include_path(path);
156 }
157}
158
159void Lila::add_compile_flag(const std::string& flag)
160{
161 if (m_interpreter) {
162 m_interpreter->add_compile_flag(flag);
163 }
164}
165
166void Lila::on_success(std::function<void()> callback)
167{
168 m_success_callback = std::move(callback);
169 LILA_DEBUG(Emitter::SYSTEM, "Success callback registered");
170}
171
172void Lila::on_error(std::function<void(const std::string&)> callback)
173{
174 m_error_callback = std::move(callback);
175 LILA_DEBUG(Emitter::SYSTEM, "Error callback registered");
176}
177
178void Lila::on_server_started(std::function<void()> callback)
179{
180 if (m_server) {
181 m_server->on_server_started(std::move(callback));
182 LILA_DEBUG(Emitter::SYSTEM, "Server started callback registered");
183 }
184}
185
186void Lila::on_server_client_connected(std::function<void(const ClientInfo&)> callback)
187{
188 if (m_server) {
189 m_server->on_client_connected(std::move(callback));
190 LILA_DEBUG(Emitter::SYSTEM, "Client connected callback registered");
191 }
192}
193
194void Lila::on_server_client_disconnected(std::function<void(const ClientInfo&)> callback)
195{
196 if (m_server) {
197 m_server->on_client_disconnected(std::move(callback));
198 LILA_DEBUG(Emitter::SYSTEM, "Client disconnected callback registered");
199 }
200}
201
202std::string Lila::get_last_error() const
203{
204 return m_interpreter ? m_interpreter->get_last_error() : "Interpreter not initialized";
205}
206
207OperationMode Lila::get_current_mode() const
208{
209 return m_current_mode;
210}
211
212std::string Lila::escape_json(const std::string& str)
213{
214 std::string escaped;
215 escaped.reserve(str.size());
216
217 for (char c : str) {
218 switch (c) {
219 case '"':
220 escaped += "\\\"";
221 break;
222 case '\\':
223 escaped += "\\\\";
224 break;
225 case '\n':
226 escaped += "\\n";
227 break;
228 case '\r':
229 escaped += "\\r";
230 break;
231 case '\t':
232 escaped += "\\t";
233 break;
234 default:
235 if (c >= 0x20) {
236 escaped += c;
237 }
238 }
239 }
240 return escaped;
241}
242
243} // namespace Lila
#define LILA_WARN(emitter, msg)
#define LILA_ERROR(emitter, msg)
#define LILA_DEBUG(emitter, msg)
#define LILA_INFO(emitter, msg)
Embedded Clang interpreter for live code evaluation in MayaFlux.
std::vector< std::string > get_defined_symbols()
Gets a list of all defined symbols.
Definition Lila.cpp:147
static std::string escape_json(const std::string &str)
Escapes a string for safe JSON encoding.
Definition Lila.cpp:212
bool eval(const std::string &code)
Evaluates a C++ code snippet directly.
Definition Lila.cpp:91
void on_error(std::function< void(const std::string &)> callback)
Registers a callback for code evaluation errors.
Definition Lila.cpp:172
std::expected< std::string, std::string > handle_server_message(std::string_view message)
Definition Lila.cpp:69
void stop_server()
Stops the TCP server and disconnects all clients.
Definition Lila.cpp:128
OperationMode get_current_mode() const
Gets the current operation mode.
Definition Lila.cpp:207
void * get_symbol_address(const std::string &name)
Gets the address of a symbol defined in the interpreter.
Definition Lila.cpp:142
bool initialize_interpreter()
Definition Lila.cpp:44
void on_server_client_disconnected(std::function< void(const ClientInfo &)> callback)
Registers a callback for client disconnections (server mode)
Definition Lila.cpp:194
bool initialize_server(int port)
Definition Lila.cpp:50
bool initialize(OperationMode mode=OperationMode::Direct, int server_port=9090) noexcept
Initializes the live coding environment.
Definition Lila.cpp:23
void on_server_client_connected(std::function< void(const ClientInfo &)> callback)
Registers a callback for new client connections (server mode)
Definition Lila.cpp:186
void add_compile_flag(const std::string &flag)
Adds a compile flag for code evaluation.
Definition Lila.cpp:159
void add_include_path(const std::string &path)
Adds an include path for code evaluation.
Definition Lila.cpp:152
~Lila()
Destructor; cleans up interpreter and server resources.
Definition Lila.cpp:17
void start_server(int port=9090)
Starts the TCP server for remote live coding.
Definition Lila.cpp:120
bool eval_file(const std::string &filepath)
Evaluates a C++ code file directly.
Definition Lila.cpp:109
std::string get_last_error() const
Gets the last error message.
Definition Lila.cpp:202
bool is_server_running() const
Checks if the server is currently running.
Definition Lila.cpp:137
Lila()
Constructs a Lila instance.
Definition Lila.cpp:10
void on_success(std::function< void()> callback)
Registers a callback for successful code evaluation.
Definition Lila.cpp:166
void on_server_started(std::function< void()> callback)
Registers a callback for server start events.
Definition Lila.cpp:178
Information about a connected client.
Definition EventBus.hpp:58