This server is intended for use for Academic Classwork related Git repositories only. Projects/repositories will generally be removed after 6 months following close of the semester. Inactive repositories from previous semester are now being archived when no activity for 365 days. They are renamed and marked as 'archived'. After 90 days in that state they will be removed from the system completely.

SymbolTableEntry.h 2.18 KB
Newer Older
Jonathan Evans's avatar
Jonathan Evans committed
1 2 3 4
#ifndef SYMBOL_TABLE_ENTRY_H
#define SYMBOL_TABLE_ENTRY_H

#include <string>
5 6
#include <ostream>
#include <stdexcept>
Jonathan Evans's avatar
Jonathan Evans committed
7 8
using namespace std;

jdev52's avatar
jdev52 committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22
#define UNDEFINED     -1   // Type codes 
#define FUNCTION   8 
#define INT    1 
#define STR    2 
#define INT_OR_STR   3 
#define BOOL    4
#define INT_OR_BOOL  5 
#define STR_OR_BOOL  6 
#define INT_OR_STR_OR_BOOL 7     
#define NOT_APPLICABLE   -1 

#define ARITHMETIC          10
#define LOGICAL             11
#define RELATIONAL          12
Jonathan Evans's avatar
Jonathan Evans committed
23

24 25 26 27 28 29 30
union TYPE_VALUE{
  char* text;
  bool boolean;
  int number;
};

struct TYPE_INFO 
31
{
32 33
  TYPE_VALUE value;

jdev52's avatar
jdev52 committed
34
	// one of the above type codes
35 36
	int type;

jdev52's avatar
jdev52 committed
37
	// numParams and returnType only applicable if type == FUNCTION   
38
  int numParams;  
39 40 41 42
	int returnType;
  
  // operatorType is only applicable if production is an operator 
  int operatorType;   
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

  friend ostream& operator<<(ostream& os, const TYPE_INFO& rhs){
    switch(rhs.type){
      case INT:
        os << rhs.value.number;
        break;
      case STR:
        os << rhs.value.text;
        break;
      case BOOL:
        os << rhs.value.boolean;
        break;
      default:
        throw std::invalid_argument("invalid type at print");
    }
    return os;
  }
}; 
jdev52's avatar
jdev52 committed
61

Jonathan Evans's avatar
Jonathan Evans committed
62 63 64 65 66
class SYMBOL_TABLE_ENTRY 
{
private:
  // Member variables
  string name;
jdev52's avatar
jdev52 committed
67
  TYPE_INFO typeInfo;  
Jonathan Evans's avatar
Jonathan Evans committed
68 69 70

public:
  // Constructors
jdev52's avatar
jdev52 committed
71 72 73 74 75 76
  SYMBOL_TABLE_ENTRY( ) { 
    name = "";
    typeInfo.type = UNDEFINED;
    typeInfo.numParams = UNDEFINED;
    typeInfo.returnType = UNDEFINED;
  }
Jonathan Evans's avatar
Jonathan Evans committed
77

jdev52's avatar
jdev52 committed
78
  SYMBOL_TABLE_ENTRY(const string theName, const TYPE_INFO theType) {
Jonathan Evans's avatar
Jonathan Evans committed
79
    name = theName;
jdev52's avatar
jdev52 committed
80 81 82
    typeInfo.type = theType.type;
    typeInfo.returnType = theType.returnType;
    typeInfo.numParams = theType.numParams;
Jonathan Evans's avatar
Jonathan Evans committed
83 84
  }

85 86 87 88 89 90 91 92
  SYMBOL_TABLE_ENTRY(const string theName, const int type = UNDEFINED,
       const int numParams = UNDEFINED, const int returnType = UNDEFINED){
    name = theName;
    typeInfo.type = type;
    typeInfo.returnType = numParams;
    typeInfo.numParams = returnType;  
  }

Jonathan Evans's avatar
Jonathan Evans committed
93 94
  // Accessors
  string getName() const { return name; }
95
  TYPE_INFO getTypeInfo() const { return typeInfo; }
Jonathan Evans's avatar
Jonathan Evans committed
96 97 98
};

#endif  // SYMBOL_TABLE_ENTRY_H