|
#ifndef RUNTIME_H
|
|
#define RUNTIME_H
|
|
|
|
#include <pthread.h>
|
|
#include <stddef.h>
|
|
|
|
typedef struct {
|
|
int type;
|
|
long val;
|
|
double dval;
|
|
char *text;
|
|
int line;
|
|
int column;
|
|
const char *filename;
|
|
} Token;
|
|
|
|
typedef struct {
|
|
char *name;
|
|
int type;
|
|
int addr;
|
|
int is_array;
|
|
} Symbol;
|
|
|
|
typedef struct {
|
|
char *name;
|
|
int entry_point;
|
|
int param_count;
|
|
int params_start;
|
|
int is_async;
|
|
} Func;
|
|
|
|
typedef long (*NativeFunc)(long*, int);
|
|
|
|
typedef struct {
|
|
char *name;
|
|
NativeFunc func;
|
|
} NativeFuncDef;
|
|
|
|
typedef struct {
|
|
char *name;
|
|
int type;
|
|
long default_value;
|
|
int offset;
|
|
} ClassField;
|
|
|
|
typedef struct {
|
|
char *name;
|
|
int func_idx;
|
|
int is_static;
|
|
int is_constructor;
|
|
int is_destructor;
|
|
} ClassMethod;
|
|
|
|
typedef struct {
|
|
char *name;
|
|
int parent_class_idx;
|
|
ClassField *fields;
|
|
int field_count;
|
|
int field_capacity;
|
|
ClassMethod *methods;
|
|
int method_count;
|
|
int method_capacity;
|
|
int size;
|
|
int token_start;
|
|
} ClassDef;
|
|
|
|
typedef struct {
|
|
int class_idx;
|
|
long *field_data;
|
|
int ref_count;
|
|
int marked_for_deletion;
|
|
} Object;
|
|
|
|
typedef struct Coroutine {
|
|
int active;
|
|
int complete;
|
|
long result;
|
|
void *thread;
|
|
int func_idx;
|
|
long *args;
|
|
int argc;
|
|
int pc_saved;
|
|
int sp_saved;
|
|
int bp_saved;
|
|
int loc_cnt_saved;
|
|
Symbol *locals_saved;
|
|
long *memory_saved;
|
|
int locals_capacity;
|
|
int memory_capacity;
|
|
} Coroutine;
|
|
|
|
typedef struct {
|
|
const char *function_name;
|
|
const char *filename;
|
|
int line;
|
|
int is_native;
|
|
} CallStackFrame;
|
|
|
|
typedef struct {
|
|
CallStackFrame *frames;
|
|
int depth;
|
|
int capacity;
|
|
} CallStack;
|
|
|
|
typedef struct {
|
|
Token *tokens;
|
|
int count;
|
|
int capacity;
|
|
pthread_rwlock_t lock;
|
|
} TokenArray;
|
|
|
|
typedef struct {
|
|
long *data;
|
|
int size;
|
|
int capacity;
|
|
pthread_mutex_t lock;
|
|
} Memory;
|
|
|
|
typedef struct {
|
|
Symbol *data;
|
|
int count;
|
|
int capacity;
|
|
pthread_mutex_t lock;
|
|
} SymbolTable;
|
|
|
|
typedef struct {
|
|
Func *data;
|
|
int count;
|
|
int capacity;
|
|
pthread_rwlock_t lock;
|
|
} FuncTable;
|
|
|
|
typedef struct {
|
|
NativeFuncDef *data;
|
|
int count;
|
|
int capacity;
|
|
pthread_rwlock_t lock;
|
|
} NativeFuncTable;
|
|
|
|
typedef struct {
|
|
ClassDef *data;
|
|
int count;
|
|
int capacity;
|
|
pthread_rwlock_t lock;
|
|
} ClassTable;
|
|
|
|
typedef struct {
|
|
Object *data;
|
|
int count;
|
|
int capacity;
|
|
pthread_mutex_t lock;
|
|
} ObjectTable;
|
|
|
|
typedef struct {
|
|
Coroutine *data;
|
|
int count;
|
|
int capacity;
|
|
pthread_mutex_t lock;
|
|
} CoroutineTable;
|
|
|
|
typedef struct {
|
|
char *data;
|
|
size_t size;
|
|
size_t capacity;
|
|
pthread_mutex_t lock;
|
|
} StringPool;
|
|
|
|
typedef struct {
|
|
TokenArray tokens;
|
|
Memory memory;
|
|
SymbolTable symbols;
|
|
FuncTable funcs;
|
|
NativeFuncTable native_funcs;
|
|
ClassTable classes;
|
|
ObjectTable objects;
|
|
CoroutineTable coroutines;
|
|
StringPool str_pool;
|
|
CallStack call_stack;
|
|
|
|
int pc;
|
|
int sp;
|
|
int bp;
|
|
long ax;
|
|
int return_flag;
|
|
|
|
pthread_mutex_t state_lock;
|
|
} Runtime;
|
|
|
|
extern Runtime *global_runtime;
|
|
|
|
Runtime* runtime_create();
|
|
void runtime_destroy(Runtime *rt);
|
|
|
|
int token_array_init(TokenArray *arr);
|
|
void token_array_destroy(TokenArray *arr);
|
|
int token_array_ensure_capacity(TokenArray *arr, int min_capacity);
|
|
int token_array_add(TokenArray *arr, Token token);
|
|
|
|
int memory_init(Memory *mem);
|
|
void memory_destroy(Memory *mem);
|
|
int memory_ensure_capacity(Memory *mem, int min_size);
|
|
long memory_get(Memory *mem, int addr);
|
|
void memory_set(Memory *mem, int addr, long value);
|
|
|
|
int symbol_table_init(SymbolTable *tbl);
|
|
void symbol_table_destroy(SymbolTable *tbl);
|
|
int symbol_table_ensure_capacity(SymbolTable *tbl, int min_capacity);
|
|
int symbol_table_add(SymbolTable *tbl, Symbol symbol);
|
|
|
|
int func_table_init(FuncTable *tbl);
|
|
void func_table_destroy(FuncTable *tbl);
|
|
int func_table_ensure_capacity(FuncTable *tbl, int min_capacity);
|
|
int func_table_add(FuncTable *tbl, Func func);
|
|
int func_table_find(FuncTable *tbl, const char *name, int len);
|
|
|
|
int native_func_table_init(NativeFuncTable *tbl);
|
|
void native_func_table_destroy(NativeFuncTable *tbl);
|
|
int native_func_table_add(NativeFuncTable *tbl, const char *name, NativeFunc func);
|
|
int native_func_table_find(NativeFuncTable *tbl, const char *name, int len);
|
|
|
|
int class_table_init(ClassTable *tbl);
|
|
void class_table_destroy(ClassTable *tbl);
|
|
int class_table_add(ClassTable *tbl, ClassDef class_def);
|
|
int class_table_find(ClassTable *tbl, const char *name, int len);
|
|
|
|
int object_table_init(ObjectTable *tbl);
|
|
void object_table_destroy(ObjectTable *tbl);
|
|
int object_table_add(ObjectTable *tbl, Object obj);
|
|
|
|
int coroutine_table_init(CoroutineTable *tbl);
|
|
void coroutine_table_destroy(CoroutineTable *tbl);
|
|
int coroutine_table_alloc(CoroutineTable *tbl);
|
|
|
|
int string_pool_init(StringPool *pool);
|
|
void string_pool_destroy(StringPool *pool);
|
|
char* string_pool_add(StringPool *pool, const char *str, size_t len);
|
|
|
|
int call_stack_init(CallStack *stack);
|
|
void call_stack_destroy(CallStack *stack);
|
|
int call_stack_push(CallStack *stack, const char *func_name, const char *filename, int line, int is_native);
|
|
void call_stack_pop(CallStack *stack);
|
|
|
|
#endif
|