orga-comp/scope.cpp

78 lines
1.6 KiB
C++

#include "orga-comp.h"
#include <cstddef>
#include <cstdlib>
#include <iostream>
#include <string>
scope::scope(){
head = nullptr;
}
void
scope::add(std::string key, symbol_type type){
if (head == nullptr){
head = new node;
head->key = key;
head->type = type;
head->pos = _initial_position + _var_in_use;
head->next = nullptr;
}else{
struct node *tmp = head;
head = new node;
head->key = key;
head->type = type;
head->pos = _initial_position + _var_in_use;
head->next = tmp;
}
_var_in_use++;
}
void
scope::print_scope(){
std::cout << "SCOPE AT : " << this << "\n";
struct node *tmp = head;
while(tmp != nullptr){
std::cout << tmp->key << " @ " << tmp->pos << "\n";
tmp = tmp->next;
}
}
scope*
scope::new_scope(){
scope *tmp = new scope();
tmp->head = head;
tmp->_var_in_use = _var_in_use;
return tmp;
}
size_t
scope::look_pos(std::string key){
struct node *tmp = head;
while(tmp != nullptr){
if(tmp->key == key) return tmp->pos;
tmp = tmp->next;
}
add(key, symbol_type::VALUE);
return head->pos;
}
size_t
scope::look_at_pos(std::string key){
struct node *tmp = head;
while(tmp != nullptr){
if(tmp->key == key) return tmp->pos;
tmp = tmp->next;
}
std::cout << "\n--ERROR--\n";
std::cout << "ID " << key << " NOT DEFINED\n";
std::exit(-1);
}
scope::symbol_type
scope::look_type(std::string key){
struct node *tmp = head;
while(tmp != nullptr){
if(tmp->key == key) return tmp->type;
tmp = tmp->next;
}
return NOT_FOUND;
}