scope.cpp 3.03 KB
Newer Older
1 2
#include "lcd_ast.h"
#include <stdio.h>
3

4 5
RootScope* RootScope::instance_ = 0;

6
RootScope::RootScope()
7
{
8
   // init builtin int types
9
  this->types_ = new std::map<std::string, Type*>();
10 11

  // instert for each builtin in type, add size to type if not done alreayd
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
  this->types_->insert( std::pair<std::string,Type*>("char"
					       , new IntegerType("char", false, sizeof(char))));
  this->types_->insert( std::pair<std::string,Type*>("unsigned char"
					       , new IntegerType("unsigned char", true, sizeof(char))));
  this->types_->insert( std::pair<std::string,Type*>("short"
					      , new IntegerType("short", false, sizeof(short))));
  this->types_->insert( std::pair<std::string,Type*>("unsigned short"
					      , new IntegerType("unsigned short", true, sizeof(short))));
  this->types_->insert( std::pair<std::string,Type*>("int"
					      , new IntegerType("int", false, sizeof(int))));
  this->types_->insert( std::pair<std::string,Type*>("unsigned int"
					      , new IntegerType("unsigned int", true, sizeof(int))));
  this->types_->insert( std::pair<std::string,Type*>("long"
					      , new IntegerType("long", false, sizeof(long))));
  this->types_->insert( std::pair<std::string,Type*>("unsigned long"
					      , new IntegerType("unsigned long", true, sizeof(long))));
  this->types_->insert( std::pair<std::string,Type*>("long long"
					      , new IntegerType("long long", false, sizeof(long long))));
  this->types_->insert( std::pair<std::string,Type*>("unsigned long long"
					      , new IntegerType("unsigned long long", true, sizeof(long long))));
  this->types_->insert( std::pair<std::string,Type*>("capability"
					       , new IntegerType("capability_t", false, sizeof(int))));
34 35 36 37 38 39
}

RootScope* RootScope::instance()
{
  if(!RootScope::instance_)
    RootScope::instance_ = new RootScope();
40 41 42
  return instance_;
}

43
Type * RootScope::lookup_symbol(const char * sym, int* err)
44
{
45 46
  std::string temp = sym;
  if(this->types_->find(temp) == this->types_->end())
47
    {
48 49
      *err = 0;
      return 0;
50 51
    }
  else
Sarah Spall's avatar
Sarah Spall committed
52 53 54 55
    {
      *err = 1;
      return (*(this->types_))[temp];
    }
56 57
}

58
int RootScope::insert_symbol(const char* sym, Type * value)
59
{
60 61 62
  std::string temp = sym;
  std::pair<std::map<std::string,Type*>::iterator,bool> ret;
  ret = types_->insert(std::pair<std::string, Type*>(temp, value));
63 64 65
  
  return ret.second;
}
66 67 68 69

// file scope
Type* FileScope::lookup_symbol(const char* sym, int* err)
{
70
  std::string temp = sym;
Sarah Spall's avatar
Sarah Spall committed
71 72
  
  if(this->types_->find(temp) == this->types_->end())
73
    {
Sarah Spall's avatar
Sarah Spall committed
74
      return this->root_->lookup_symbol(sym, err); 
75 76
    }
  else
Sarah Spall's avatar
Sarah Spall committed
77 78 79 80
    {
      *err = 1;
      return (*types_)[temp];
    }
81 82 83 84 85
  
}

int FileScope::insert_symbol(const char* sym, Type* value)
{
86
  std::string temp = sym;
87
  // redefinition of something at root scope?
88
  std::pair<std::map<std::string,Type*>::iterator, bool> ret;
Sarah Spall's avatar
Sarah Spall committed
89 90 91
  // filescope map not rootscope
  ret = this->types_->insert(std::pair<std::string,Type*>(temp, value));
  return ret.second; 
92 93 94 95 96
}

FileScope::FileScope(RootScope* root)
{
  this->root_ = root;
97
  this->types_ = new std::map<std::string, Type*>();
98 99
}