Files
jailbasic/scope.cpp

51 lines
1.2 KiB
C++

#include "scope.h"
#include <vector>
#include "heap.h"
struct t_variable {
//char name[MAX_IDENTIFIER_LENGTH];
std::string name;
int type;
int length;
int address;
};
struct t_struct {
//char name[MAX_IDENTIFIER_LENGTH];
std::string name;
std::vector<t_variable> members;
};
struct t_scope {
std::vector<t_variable> variables;
t_scope* parent_scope;
};
static t_scope scope_global;
static t_scope* scope_current = &scope_global;
void scope_init() {
scope_current = &scope_global;
scope_global.variables.clear();
}
void scope_open_local() {
t_scope* new_scope = new t_scope;
new_scope->parent_scope = scope_current;
scope_current = new_scope;
}
void scope_close_local() {
t_scope* closing_scope = scope_current;
scope_current = scope_current->parent_scope;
delete closing_scope;
}
const int scope_declare_variable(const std::string name, const int type, const int length) {
for (t_variable v : scope_current->variables) { if (v.name == name) return -1; }
const int address = heap_get_address();
const t_variable var {name, type, length, address};
scope_current->variables.push_back(var);
return address;
}