Cell* search_symbol(string s, bool expected)
{
  //stack<symbol_map> temp_AR_stack;
  symbol_map curr_map;
  symbol_map::iterator curr_map_it;
  bool exist = false;
  Cell* linked_symbol = NULL;

/*  while (!(AR_stack.empty()))
  {
    curr_map = AR_stack.top();
    curr_map_it = curr_map.find(s);
    // Check if the current record (map) has the mapped value
    if (curr_map_it != curr_map.end())
    {
      linked_symbol = curr_map_it->second;
      exist = true;
      break;
    }
    else
    {
      temp_AR_stack.push(curr_map);
      AR_stack.pop();
    }
  }
*/
  for (int i = AR_vector.size()-1; i>=0; i--)
  {
    curr_map = AR_vector[i];
    curr_map_it = curr_map.find(s);
    //Check if the current record (map) contains the mapped value
   if (curr_map_it != curr_map.end())
   {
     linked_symbol = curr_map_it->second;
     exist = true;
     break;
   }
  }
  /*
   * after searching the symbol map, if it is still not found,
   * check the global variable record
   */
  if (!exist)
  {
    SMap_it = SMap.find(s);
    if ((SMap_it == SMap.end())&&expected)
    {
      //if (!Symbol_operator_finder(s))
        throw runtime_error("attempt to reference an undefined symbol \"" + s
        +"\"");
      //return make_symbol(s.c_str());
    }
    else if (SMap_it == SMap.end())
    	linked_symbol = NULL;
    else
        linked_symbol = SMap_it->second;
  }

  return linked_symbol;
}
void insert_element(string s, Cell* map_cell)
{

  //check if there is any local symbol table
  if (!(AR_vector.empty()))
  {
    symbol_map curr_map = AR_vector.back();
    AR_vector.pop_back();
    symbol_map::iterator curr_map_it = curr_map.find(s);

    //check if it is defined before
    if (curr_map_it != curr_map.end())
    {
      AR_vector.push_back(curr_map);
      throw runtime_error(s + " cannot be redefined.");
    }
    curr_map[s] = map_cell;
    AR_vector.push_back(curr_map);
  }
  else
  {
    SMap_it = SMap.find(s);

    //check if it is defined before
    if (SMap_it != SMap.end())
      throw runtime_error(s + " cannot be redefined.");
    SMap.insert( pair< string, Cell*>(s,map_cell));
  }
}
Esempio n. 3
0
void StdLib::Initialize(symbol_map& symbols) {
  {
    auto_ptr<Object> object(new Object());
    symbols.insert(OBJECT, object);
  }
  symbol_iter object = symbols.find(OBJECT);

  {
    auto_ptr<Object> function(new Object(*object->second));
    symbols.insert(FUNCTION, function);
  }
  symbol_iter function = symbols.find(FUNCTION);

  {
    auto_ptr<Object> int_(new Object(*object->second));
    symbols.insert(INTEGER, int_);
  }
  //symbol_iter int_ = symbols.find(INTEGER);

  {
    auto_ptr<Object> str(new Object(*object->second));
    symbols.insert(STRING, str);
  }
  //symbol_iter str = symbols.find(STRING);

  {
    auto_ptr<Object> print(new Object(*function->second));
    symbols.insert("print", print);
  } {
    symbol_mod_iter print = symbols.find("print");
    auto_ptr<function_vec> function(new function_vec());
    Cmd c;
    Variable arg(".arg1");
    c.exps.push_back(arg);
    c.cmd = "echo {}";
    function->push_back(c);
    print->second->insertFunction(function);
  }
}
Esempio n. 4
0
void parse_template(template_files_vector::iterator i, std::ifstream &input, std::ofstream &output, const symbol_map &symbols)
{
    std::string line;
    parser_vector parsers;
    parsers.push_back(parser_object_ptr(new symbol_parser(symbols, parsers, input, output)));

    input.open(i->first.c_str(), std::ios::in);
    string_pair &out_file = i->second;
    output.open(std::string(out_file.first + symbols.find("NAME_LOWER")->second + out_file.second).c_str(),
        std::ios::out | std::ios::trunc);

    if( !input.is_open() )
        throw std::runtime_error("Failed to open template file");
    if( !output.is_open() )
        throw std::runtime_error("Failed to open output file");

    std::string buff;

    // read and write line by line
    while(true)
    {
        symbol_parser::m_should_output = true;

        std::getline(input, line);
        buff = line;

        if( input.eof() )
            break;

        if( !input.good() || !output.good() )
            throw std::runtime_error("Input or Output error!");

        for( parser_vector::reverse_iterator i = parsers.rbegin(), end = parsers.rend(); i != end; ++i )
            if( (**i)(buff) )
                break;
    }

    input.close();
    output.close();
    input.clear();
    output.clear();
}