82 lines
2.1 KiB
C++
82 lines
2.1 KiB
C++
#ifndef CAMELLYA_STATE_H
|
|
#define CAMELLYA_STATE_H
|
|
|
|
#include "lexer.h"
|
|
#include "parser.h"
|
|
#include "interpreter.h"
|
|
#include "vm.h"
|
|
#include "compiler.h"
|
|
#include "value.h"
|
|
#include <string>
|
|
#include <memory>
|
|
|
|
namespace camellya {
|
|
|
|
// Execution mode
|
|
enum class ExecutionMode {
|
|
INTERPRETER, // Tree-walking interpreter
|
|
VM // Bytecode VM
|
|
};
|
|
|
|
// Main state class - similar to lua_State
|
|
class State {
|
|
public:
|
|
State(ExecutionMode mode = ExecutionMode::VM);
|
|
~State() = default;
|
|
|
|
// Set execution mode
|
|
void set_execution_mode(ExecutionMode mode) { execution_mode_ = mode; }
|
|
ExecutionMode get_execution_mode() const { return execution_mode_; }
|
|
|
|
// Execute script from string
|
|
bool do_string(const std::string& script);
|
|
|
|
// Execute script from file
|
|
bool do_file(const std::string& filename);
|
|
|
|
// Register native function
|
|
void register_function(const std::string& name, NativeFunction func);
|
|
|
|
// Get global variable
|
|
ValuePtr get_global(const std::string& name);
|
|
|
|
// Set global variable
|
|
void set_global(const std::string& name, ValuePtr value);
|
|
|
|
// Stack operations (Lua-like API)
|
|
void push_number(double value);
|
|
void push_string(const std::string& value);
|
|
void push_bool(bool value);
|
|
void push_nil();
|
|
void push_value(ValuePtr value);
|
|
|
|
double to_number(int index);
|
|
std::string to_string(int index);
|
|
bool to_bool(int index);
|
|
ValuePtr to_value(int index);
|
|
|
|
int get_top() const { return static_cast<int>(stack_.size()); }
|
|
void set_top(int index);
|
|
void pop(int n = 1);
|
|
|
|
// Error handling
|
|
const std::string& get_error() const { return last_error_; }
|
|
|
|
private:
|
|
ExecutionMode execution_mode_;
|
|
std::unique_ptr<Interpreter> interpreter_;
|
|
std::unique_ptr<VM> vm_;
|
|
std::unique_ptr<Compiler> compiler_;
|
|
std::vector<ValuePtr> stack_;
|
|
std::string last_error_;
|
|
|
|
ValuePtr get_stack_value(int index);
|
|
|
|
// Helper for execution
|
|
bool execute_program(const Program& program);
|
|
};
|
|
|
|
} // namespace camellya
|
|
|
|
#endif // CAMELLYA_STATE_H
|