bl-compiler

compiler for the bl programming language.
Log | Files | Refs | README

symbol.cpp (3120B)


      1 #include "symbol.hpp"
      2 
      3 Symbol::Symbol(std::string name, SymbolType type, std::string data_type, int scope) : symbol_name(name), symbol_type(type), data_type(data_type), scope_level(scope_level) {}
      4 
      5 std::string Symbool::getName() {
      6     return name;
      7 }
      8 
      9 SymbolType Symbol::getSymbolType() {
     10     return symbol_type;
     11 }
     12 
     13 std::string Symbol::getDataType() {
     14     return symbol_data_type;
     15 }
     16 
     17 int Symbol::getSymbolLevel() {
     18     return scope_level;
     19 }
     20 
     21 bool Symbol::isInitialized() {
     22     return is_initialized;
     23 }
     24 
     25 void Symbol::setInitialized(bool init) {
     26     is_initialized = init;
     27 }
     28 
     29 bool Symbol::isParameter() {
     30     return is_parameter();
     31 }
     32 
     33 void Symbol::setParameter(bool is_param) {
     34     is_parameter = is_param;
     35 }
     36 
     37 std::vector<std::string> Symbol::getParameterTypes() {
     38     return paremeter_types;
     39 }
     40 
     41 void Symbol::setParameterTypes(std::vector<std::string> types) {
     42     parameter_types = types;
     43 }
     44 
     45 std::string Symbol::getReturnType() {
     46     return return_type;
     47 }
     48 
     49 void Symbol::setReturnType(std::string type) {
     50     return_type = type;
     51 }
     52 
     53 int Symbol::getLineDeclared() {
     54     return line_declared;
     55 }
     56 
     57 void Symbol::setLineDeclared(int line) {
     58     line_declared = line;
     59 }
     60 
     61 std::string Symbol::toString() const {
     62     std::string result = "";
     63     
     64     // Symbol name and basic info
     65     result += "Symbol: " + name + "\n";
     66     
     67     // Symbol type (VARIABLE, FUNCTION, PARAMETER)
     68     result += "  Type: ";
     69     switch (symbol_type) {
     70         case SymbolType::VARIABLE:
     71             result += "VARIABLE\n";
     72             break;
     73         case SymbolType::FUNCTION:
     74             result += "FUNCTION\n";
     75             break;
     76         case SymbolType::PARAMETER:
     77             result += "PARAMETER\n";
     78             break;
     79         default:
     80             result += "UNKNOWN\n";
     81             break;
     82     }
     83     
     84     // Data type
     85     result += "  Data Type: " + data_type + "\n";
     86     
     87     // Scope level
     88     result += "  Scope Level: " + std::to_string(scope_level) + "\n";
     89     
     90     // Initialization status (only relevant for variables)
     91     if (symbol_type == SymbolType::VARIABLE || symbol_type == SymbolType::PARAMETER) {
     92         result += "  Initialized: " + std::string(is_initialized ? "true" : "false") + "\n";
     93     }
     94     
     95     // Parameter flag
     96     if (is_parameter) {
     97         result += "  Is Parameter: true\n";
     98     }
     99     
    100     // Function-specific information
    101     if (symbol_type == SymbolType::FUNCTION) {
    102         result += "  Return Type: " + return_type + "\n";
    103         
    104         result += "  Parameters: [";
    105         for (size_t i = 0; i < parameter_types.size(); i++) {
    106             result += parameter_types[i];
    107             if (i < parameter_types.size() - 1) {
    108                 result += ", ";
    109             }
    110         }
    111         result += "]\n";
    112         
    113         result += "  Parameter Count: " + std::to_string(parameter_types.size()) + "\n";
    114     }
    115     
    116     // Declaration location
    117     if (line_declared > 0) {
    118         result += "  Declared at: line " + std::to_string(line_declared);
    119         if (column_declared > 0) {
    120             result += ", column " + std::to_string(column_declared);
    121         }
    122         result += "\n";
    123     }
    124     
    125     return result;
    126 }