feat: add scope-based memory management with push/pop and variable isolation
Introduce a new scope system that replaces manual stack frame management with structured scope_push/scope_pop calls. The implementation includes a Scope struct with dedicated memory and local symbol arrays, along with helper functions for variable lookup, memory access, and state synchronization. This change touches context.c, main.c, oop.c, parser.c, and async_stdlib.c to integrate scope initialization, cleanup, and frame transitions during function calls and destructor execution.
This commit is contained in:
+54
@@ -0,0 +1,54 @@
|
||||
#ifndef SCOPE_H
|
||||
#define SCOPE_H
|
||||
|
||||
#include "types.h"
|
||||
|
||||
#define SCOPE_MEM_SIZE 10000
|
||||
#define SCOPE_VAR_MAX 500
|
||||
|
||||
typedef struct Scope {
|
||||
struct Scope *parent;
|
||||
long *memory;
|
||||
int mem_size;
|
||||
Symbol *locals;
|
||||
int loc_capacity;
|
||||
int sp;
|
||||
int bp;
|
||||
int loc_cnt;
|
||||
int depth;
|
||||
} Scope;
|
||||
|
||||
extern Scope *current_scope;
|
||||
extern Scope *root_scope;
|
||||
|
||||
Scope* scope_create(Scope *parent);
|
||||
void scope_destroy(Scope *scope);
|
||||
Scope* scope_push(void);
|
||||
Scope* scope_pop(void);
|
||||
|
||||
int scope_find_local(const char *name, int len);
|
||||
int scope_find_local_current_only(const char *name, int len);
|
||||
Symbol* scope_get_local(int idx);
|
||||
Symbol* scope_add_local(const char *name, int len, int type, int addr, int is_array);
|
||||
|
||||
long scope_mem_read(int addr);
|
||||
void scope_mem_write(int addr, long value);
|
||||
int scope_mem_alloc(int size);
|
||||
int scope_check_sp(int required);
|
||||
int scope_check_addr(int addr);
|
||||
|
||||
void scope_init(void);
|
||||
void scope_cleanup(void);
|
||||
|
||||
void scope_save_state(int *out_sp, int *out_bp, int *out_loc_cnt);
|
||||
void scope_restore_state(int saved_sp, int saved_bp, int saved_loc_cnt);
|
||||
|
||||
void scope_sync_to_globals(void);
|
||||
void scope_sync_from_globals(void);
|
||||
|
||||
Symbol* scope_find_local_symbol(const char *name, int len);
|
||||
int scope_find_local_with_scope(const char *name, int len, Scope **out_scope);
|
||||
long scope_read_var(const char *name, int len);
|
||||
void scope_write_var(const char *name, int len, long value);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user