Scope module finished. Types module included &done

This commit is contained in:
2021-05-03 17:07:31 +02:00
parent 6923bec289
commit d20663ac7e
5 changed files with 166 additions and 34 deletions

105
scope.cpp
View File

@@ -1,50 +1,93 @@
#include "scope.h"
#include "variables.h"
#include "types.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;
};
#include <stack>
struct t_scope {
std::vector<t_variable> variables;
t_scope* parent_scope;
int total_size;
};
static t_scope scope_global;
static t_scope* scope_current = &scope_global;
static t_scope global;
static t_scope local;
static std::stack<int> blocks;
static bool are_we_inside_local = false;
static t_variable *current_var = NULL;
void scope_init() {
scope_current = &scope_global;
scope_global.variables.clear();
global.variables.clear();
global.total_size = 0;
local.variables.clear();
local.total_size = 0;
while (!blocks.empty()) blocks.pop();
are_we_inside_local = false;
}
void scope_open_local() {
t_scope* new_scope = new t_scope;
new_scope->parent_scope = scope_current;
scope_current = new_scope;
are_we_inside_local = true;
}
void scope_close_local() {
t_scope* closing_scope = scope_current;
scope_current = scope_current->parent_scope;
delete closing_scope;
const int scope_close_local() {
if (!blocks.empty()) {
// Malament, no havem tancat algún block. No se si es detectarà abans per altra part. Per ara anem a deixar este comentari.
}
const int return_value = local.total_size;
local.variables.clear();
local.total_size = 0;
are_we_inside_local = false;
return return_value;
}
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();
void scope_open_block() {
blocks.push(local.variables.size());
}
void scope_close_block() {
const int size_before_block = blocks.top();
blocks.pop();
while (local.variables.size() > size_before_block) local.variables.pop_back();
}
const uint32_t scope_declare_variable(const std::string name, const int type, const int length) {
t_scope& scope = (are_we_inside_local ? local : global);
if (are_we_inside_local) {
for (int i = blocks.top(); i < scope.variables.size(); i++ ) if (scope.variables[i].name == name) return -1;
} else {
for (t_variable v : scope.variables) if (v.name == name) return -1;
}
const int address = scope.total_size;
const t_variable var {name, type, length, address};
scope_current->variables.push_back(var);
scope.variables.push_back(var);
scope.total_size += types_get_length(type) + (length == 0 ? 1 : length);
return address;
}
const bool scope_variable_exists(const std::string name) {
t_scope& scope = (are_we_inside_local ? local : global);
for (t_variable v : scope.variables) if (v.name == name) {
current_var = &v;
return true;
}
}
const uint32_t scope_get_variable_address() {
if (current_var == NULL) {
// ERROR INTERN
}
return current_var->address;
}
const int scope_get_variable_size() {
if (current_var == NULL) {
// ERROR INTERN
}
return current_var->length;
}
const int scope_get_variable_type() {
if (current_var == NULL) {
// ERROR INTERN
}
return current_var->type;
}