scope.cpp 2.75 KB
Newer Older
1
#include "../include/lcd_ast.h"
2

3
RootScope::RootScope()
4
{
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
   // init builtin int types
  this->types_ = new std::map<const char*, Type*>();

  // instert for each builtin in type, add size to type if not done alreayd
  this->types_->insert( std::pair<const char*,Type*>("char"
					       , new IntegerType("char", false, sizeof("char"))));
  this->types_->insert( std::pair<const char*,Type*>("unsigned char"
					       , new IntegerType("unsigned char", true, sizeof("char"))));
  this->types_->insert( std::pair<const char*,Type*>("short"
					      , new IntegerType("short", false, sizeof("short"))));
  this->types_->insert( std::pair<const char*,Type*>("unsigned short"
					      , new IntegerType("unsigned short", true, sizeof("short"))));
  this->types_->insert( std::pair<const char*,Type*>("int"
					      , new IntegerType("int", false, sizeof("int"))));
  this->types_->insert( std::pair<const char*,Type*>("unsigned int"
					      , new IntegerType("unsigned int", true, sizeof("int"))));
  this->types_->insert( std::pair<const char*,Type*>("long"
					      , new IntegerType("long", false, sizeof("long"))));
  this->types_->insert( std::pair<const char*,Type*>("unsigned long"
					      , new IntegerType("unsigned long", true, sizeof("long"))));
  this->types_->insert( std::pair<const char*,Type*>("long long"
					      , new IntegerType("long long", false, sizeof("long long"))));
  this->types_->insert( std::pair<const char*,Type*>("unsigned long long"
					      , new IntegerType("unsigned long long", true, sizeof("long long"))));
  this->types_->insert( std::pair<const char*,Type*>("capability"
					       , new IntegerType("capability_t", false, sizeof("int"))));
}

RootScope* RootScope::instance()
{
  if(!RootScope::instance_)
    RootScope::instance_ = new RootScope();
37 38 39
  return instance_;
}

40
Type * RootScope::lookup_symbol(const char * sym, int* err)
41
{
42
  if(types_->find(sym) == types_->end())
43 44 45 46
    {
      // error
    }
  else
47
    return (*types_)[sym];
48 49
}

50
int RootScope::insert_symbol(const char* sym, Type * value)
51
{
52 53
  std::pair<std::map<const char*,Type*>::iterator,bool> ret;
  ret = types_->insert(std::pair<const char*, Type*>(sym, value));
54 55 56
  
  return ret.second;
}
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

// file scope
Type* FileScope::lookup_symbol(const char* sym, int* err)
{
  // lookup here or root first?
  if(types_->find(sym) == types_->end())
    {
      // error
    }
  else
    return (*types_)[sym];
  
}

int FileScope::insert_symbol(const char* sym, Type* value)
{
  // redefinition of something at root scope?
  std::pair<std::map<const char*,Type*>::iterator, bool> ret;
  ret = types_->insert(std::pair<const char*,Type*>(sym, value));
  
  return ret.second;
		      
}

FileScope::FileScope(RootScope* root)
{
  this->root_ = root;
  this->types_ = new std::map<const char*, Type*>();
}