This commit is contained in:
2026-01-13 22:52:55 +08:00
commit 211a837468
18 changed files with 2831 additions and 0 deletions

63
state.h Normal file
View File

@@ -0,0 +1,63 @@
#ifndef CAMELLYA_STATE_H
#define CAMELLYA_STATE_H
#include "lexer.h"
#include "parser.h"
#include "interpreter.h"
#include "value.h"
#include <string>
#include <memory>
namespace camellya {
// Main state class - similar to lua_State
class State {
public:
State();
~State() = default;
// 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:
std::unique_ptr<Interpreter> interpreter_;
std::vector<ValuePtr> stack_;
std::string last_error_;
ValuePtr get_stack_value(int index);
};
} // namespace camellya
#endif // CAMELLYA_STATE_H