12 , m_current_mode(OperationMode::Direct)
14 LILA_DEBUG(Emitter::SYSTEM,
"Lila instance created");
20 LILA_DEBUG(Emitter::SYSTEM,
"Lila instance destroyed");
25 LILA_INFO(Emitter::SYSTEM,
"Initializing Lila");
26 m_current_mode = mode;
28 if (!initialize_interpreter()) {
29 LILA_ERROR(Emitter::SYSTEM,
"Failed to initialize interpreter");
33 if (mode == OperationMode::Server || mode == OperationMode::Both) {
34 if (!initialize_server(server_port)) {
35 LILA_ERROR(Emitter::SYSTEM,
"Failed to initialize server");
40 LILA_INFO(Emitter::SYSTEM,
"Lila initialized successfully");
46 LILA_DEBUG(Emitter::SYSTEM,
"Initializing interpreter subsystem");
47 return m_interpreter->initialize();
52 if (m_server && m_server->is_running()) {
53 LILA_WARN(Emitter::SYSTEM,
"Stopping existing server before starting new one");
58 std::string(
"Initializing server on port ") + std::to_string(port));
60 m_server = std::make_unique<Server>(port);
62 m_server->set_message_handler([
this](std::string_view message) {
63 return this->handle_server_message(message);
66 return m_server->start();
71 if (message.empty()) {
72 return R
"({"status":"error","message":"Empty message"})";
75 auto result = m_interpreter->eval(std::string(message));
78 if (m_success_callback) {
84 if (m_error_callback) {
85 m_error_callback(result.error);
88 return std::unexpected(escape_json(result.error));
94 LILA_ERROR(Emitter::SYSTEM,
"Cannot eval: interpreter not initialized");
98 auto result = m_interpreter->eval(code);
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);
106 return result.success;
111 if (!m_interpreter) {
112 LILA_ERROR(Emitter::SYSTEM,
"Cannot eval file: interpreter not initialized");
116 auto result = m_interpreter->eval_file(filepath);
117 return result.success;
123 std::string(
"Starting server on port ") + std::to_string(port));
125 initialize_server(port);
131 LILA_INFO(Emitter::SYSTEM,
"Stopping server");
139 return m_server && m_server->is_running();
144 return m_interpreter ? m_interpreter->get_symbol_address(name) :
nullptr;
149 return m_interpreter ? m_interpreter->get_defined_symbols() : std::vector<std::string> {};
155 m_interpreter->add_include_path(path);
162 m_interpreter->add_compile_flag(flag);
168 m_success_callback = std::move(callback);
169 LILA_DEBUG(Emitter::SYSTEM,
"Success callback registered");
174 m_error_callback = std::move(callback);
175 LILA_DEBUG(Emitter::SYSTEM,
"Error callback registered");
181 m_server->on_server_started(std::move(callback));
182 LILA_DEBUG(Emitter::SYSTEM,
"Server started callback registered");
189 m_server->on_client_connected(std::move(callback));
190 LILA_DEBUG(Emitter::SYSTEM,
"Client connected callback registered");
197 m_server->on_client_disconnected(std::move(callback));
198 LILA_DEBUG(Emitter::SYSTEM,
"Client disconnected callback registered");
204 return m_interpreter ? m_interpreter->get_last_error() :
"Interpreter not initialized";
209 return m_current_mode;
215 escaped.reserve(str.size());
Embedded Clang interpreter for live code evaluation in MayaFlux.
std::vector< std::string > get_defined_symbols()
Gets a list of all defined symbols.
static std::string escape_json(const std::string &str)
Escapes a string for safe JSON encoding.
bool eval(const std::string &code)
Evaluates a C++ code snippet directly.
void on_error(std::function< void(const std::string &)> callback)
Registers a callback for code evaluation errors.
std::expected< std::string, std::string > handle_server_message(std::string_view message)
void stop_server()
Stops the TCP server and disconnects all clients.
OperationMode get_current_mode() const
Gets the current operation mode.
void * get_symbol_address(const std::string &name)
Gets the address of a symbol defined in the interpreter.
bool initialize_interpreter()
void on_server_client_disconnected(std::function< void(const ClientInfo &)> callback)
Registers a callback for client disconnections (server mode)
bool initialize_server(int port)
bool initialize(OperationMode mode=OperationMode::Direct, int server_port=9090) noexcept
Initializes the live coding environment.
void on_server_client_connected(std::function< void(const ClientInfo &)> callback)
Registers a callback for new client connections (server mode)
void add_compile_flag(const std::string &flag)
Adds a compile flag for code evaluation.
void add_include_path(const std::string &path)
Adds an include path for code evaluation.
~Lila()
Destructor; cleans up interpreter and server resources.
void start_server(int port=9090)
Starts the TCP server for remote live coding.
bool eval_file(const std::string &filepath)
Evaluates a C++ code file directly.
std::string get_last_error() const
Gets the last error message.
bool is_server_running() const
Checks if the server is currently running.
Lila()
Constructs a Lila instance.
void on_success(std::function< void()> callback)
Registers a callback for successful code evaluation.
void on_server_started(std::function< void()> callback)
Registers a callback for server start events.
Information about a connected client.