scope.cpp 3.02 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 50 51
      // printf("looking up %s\n", sym);
      // printf("is empty? %d\n", this->types_->size());
      *err = 0;
      return 0;
52 53
    }
  else
54
    return (*(this->types_))[temp];
55 56
}

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

// file scope
Type* FileScope::lookup_symbol(const char* sym, int* err)
{
69
  std::string temp = sym;
70
  // lookup here or root first?
71
  if(types_->find(temp) == types_->end())
72 73 74 75
    {
      // error
    }
  else
76
    return (*types_)[temp];
77 78 79 80 81
  
}

int FileScope::insert_symbol(const char* sym, Type* value)
{
82
  std::string temp = sym;
83
  // redefinition of something at root scope?
84 85
  std::pair<std::map<std::string,Type*>::iterator, bool> ret;
  ret = types_->insert(std::pair<std::string,Type*>(temp, value));
86 87 88 89 90 91 92 93
  
  return ret.second;
		      
}

FileScope::FileScope(RootScope* root)
{
  this->root_ = root;
94
  this->types_ = new std::map<std::string, Type*>();
95 96
}