Пример #1
0
bool ast_fun_call_expr::is_valid(symbol_table& sym)
{
  bool res = true;

  do
  {
    ptr<ast_fun_def> _fd = sym.get_fun_def(_id);
    if (_fd == nullptr)
    {
      cerr << MYLANGA_PARSE_ERROR(_ln) << " | " << \
        "La función \'" << *_id << "\' no se encuentra definida." << endl;
      res = false; break;
    }

    if (_fd->_ids->size() != _exs->size())
    {
      cerr << MYLANGA_PARSE_ERROR(_ln) << " | " << \
        "La función " << (*(_fd->_id)) << " recibe " << to_string(_fd->_ids->size()) << \
        " parámetro(s), pero es invocada con " <<  to_string(_exs->size()) << \
        " argumento(s)." << endl;
      res = false; break;
    }
  } while (false);
  
  for (auto _ex : *_exs)
    res = _ex->is_valid(sym) and res;

  return res;
}
Пример #2
0
bool ast_fun_def::is_valid(symbol_table& sym)
{
  bool res = true;

  if (sym.get_fun_def(_id) != nullptr)
  {
    cerr << MYLANGA_PARSE_ERROR(_ln) << " | " << \
      "La función \'" << *_id << "\' ya está definida." << endl;
    res = false;
  }

  if (has_repeated_elements(_ids))
  {
    cerr << MYLANGA_PARSE_ERROR(_ln) << " | " << \
      "La función \'" << *_id << "\' contiene parámetros repetidos en su definición." << endl;
    res = false;
  }

  // para soportar recursión
  sym.define_fun(shared_from_this());
  sym.open_scope();

  for (auto _id : *_ids)
    sym.declare_var(_id);

  res = _bl->is_valid(sym) and res;

  sym.close_scope();
  sym.undefine_fun(shared_from_this());

  return res;
}
Пример #3
0
fp_t ast_fun_call_expr::eval(symbol_table& sym)
{
  list<fp_t> args;
  for (auto _ex : *_exs)
    args.push_back(_ex->eval(sym));

  sym.open_scope();

  ptr<ast_fun_def> _fd = sym.get_fun_def(_id);
  
  auto arg_it = args.begin();
  for (auto _param_id : *(_fd->_ids))
    sym.set_var(_param_id, *arg_it++);

  maybe_fp_t ret = _fd->_bl->exec(sym);

  sym.close_scope();

  if (not ret.is_valid)
  {
    // runtime error
    cerr << MYLANGA_RUNTIME_ERROR << " | " << \
      "La función \'" << *_id << "\' se ejecutó sin retornar un valor." << endl;
    MYLANGA_END_ABRUPTLY();
  }

  return ret.value;
}