First commit, porting from PaCO

This commit is contained in:
2021-04-27 19:02:20 +02:00
commit f1f590735b
11 changed files with 1283 additions and 0 deletions

50
scope.cpp Normal file
View File

@@ -0,0 +1,50 @@
#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;
}