/*

Copyright (C) 1996, 1997 John W. Eaton

This file is part of Octave.

Octave is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

Octave is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with Octave; see the file COPYING.  If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

#if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION)
#pragma implementation
#endif

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "Cell.h"
#include "error.h"
#include "oct-map.h"
#include "oct-obj.h"
#include "oct-lvalue.h"
#include "ov.h"
#include "pager.h"
#include "pt-arg-list.h"
#include "pt-bp.h"
#include "pt-idx.h"
#include "pt-walk.h"
#include "utils.h"
#include "variables.h"

// Index expressions.

tree_index_expression::tree_index_expression (tree_expression *e,
					      tree_argument_list *lst,
					      int l, int c, char t)
  : tree_expression (l, c), expr (e), args (), type (),
    arg_nm (), dyn_field ()
{
  append (lst, t);
}

tree_index_expression::tree_index_expression (tree_expression *e,
					      const std::string& n,
					      int l, int c)
  : tree_expression (l, c), expr (e), args (), type (),
    arg_nm (), dyn_field ()
{
  append (n);
}

tree_index_expression::tree_index_expression (tree_expression *e,
					      tree_expression *df,
					      int l, int c)
  : tree_expression (l, c), expr (e), args (), type (),
    arg_nm (), dyn_field ()
{
  append (df);
}

void
tree_index_expression::append (tree_argument_list *lst, char t)
{
  args.push_back (lst);
  type.append (1, t);
  arg_nm.push_back (lst ? lst->get_arg_names () : string_vector ());
  dyn_field.push_back (static_cast<tree_expression *> (0));
}

void
tree_index_expression::append (const std::string& n)
{
  args.push_back (static_cast<tree_argument_list *> (0));
  type.append (".");
  arg_nm.push_back (n);
  dyn_field.push_back (static_cast<tree_expression *> (0));
}

void
tree_index_expression::append (tree_expression *df)
{
  args.push_back (static_cast<tree_argument_list *> (0));
  type.append (".");
  arg_nm.push_back ("");
  dyn_field.push_back (df);
}

tree_index_expression::~tree_index_expression (void)
{
  delete expr;

  while (! args.empty ())
    {
      std::list<tree_argument_list *>::iterator p = args.begin ();
      delete *p;
      args.erase (p);
    }
}

// This is useful for printing the name of the variable in an indexed
// assignment.

std::string
tree_index_expression::name (void) const
{
  return expr->name ();
}

static Cell
make_subs_cell (tree_argument_list *args, const string_vector& arg_nm)
{
  Cell retval;

  octave_value_list arg_values;

  if (args)
    arg_values = args->convert_to_const_vector ();

  if (! error_state)
    {
      int n = arg_values.length ();

      if (n > 0)
	{
	  arg_values.stash_name_tags (arg_nm);

	  retval.resize (1, n);

	  for (int i = 0; i < n; i++)
	    retval(0,i) = arg_values(i);
	}
    }

  return retval;
}

static inline octave_value_list
make_value_list (tree_argument_list *args, const string_vector& arg_nm,
		 const octave_value *object)
{
  octave_value_list retval;

  if (args)
    retval = args->convert_to_const_vector (object);

  if (! error_state)
    {
      int n = retval.length ();

      if (n > 0)
	retval.stash_name_tags (arg_nm);
    }

  return retval;
}

std::string
tree_index_expression::get_struct_index
  (std::list<string_vector>::const_iterator p_arg_nm,
   std::list<tree_expression *>::const_iterator p_dyn_field) const
{
  std::string fn = (*p_arg_nm)(0);

  if (fn.empty ())
    {
      tree_expression *df = *p_dyn_field;

      if (df)
	{
	  octave_value t = df->rvalue ();

	  if (! error_state)
	    {
	      fn = t.string_value ();

	      if (! valid_identifier (fn))
		::error ("invalid structure field name");
	    }
	}
      else
	panic_impossible ();
    }

  return fn;
}

Octave_map
tree_index_expression::make_arg_struct (void) const
{
  int n = args.size ();

  octave_value_list subs_list (n, octave_value ());
  octave_value_list type_list (n, octave_value ());

  std::list<tree_argument_list *>::const_iterator p_args = args.begin ();
  std::list<string_vector>::const_iterator p_arg_nm = arg_nm.begin ();
  std::list<tree_expression *>::const_iterator p_dyn_field = dyn_field.begin ();

  Octave_map m;

  for (int i = 0; i < n; i++)
    {
      switch (type[i])
	{
	case '(':
	  subs_list(i) = make_subs_cell (*p_args, *p_arg_nm);
	  break;

	case '{':
	  subs_list(i) = make_subs_cell (*p_args, *p_arg_nm);
	  break;

	case '.':
	  {
	    subs_list(i) = get_struct_index (p_arg_nm, p_dyn_field);

	    if (error_state)
	      eval_error ();
	  }
	  break;

	default:
	  panic_impossible ();
	}

      if (error_state)
	return m;

      p_args++;
      p_arg_nm++;
      p_dyn_field++;
    }

  m ["subs"] = subs_list;
  m ["type"] = type_list;

  return m;
}

octave_value_list
tree_index_expression::rvalue (int nargout)
{
  octave_value_list retval;

  if (error_state)
    return retval;

  octave_value tmp = expr->rvalue ();

  if (! error_state)
    {
      std::list<octave_value_list> idx;

      int n = args.size ();

      std::list<tree_argument_list *>::iterator p_args = args.begin ();
      std::list<string_vector>::iterator p_arg_nm = arg_nm.begin ();
      std::list<tree_expression *>::iterator p_dyn_field = dyn_field.begin ();

      for (int i = 0; i < n; i++)
	{
	  switch (type[i])
	    {
	    case '(':
	      idx.push_back (make_value_list (*p_args, *p_arg_nm, &tmp));
	      break;

	    case '{':
	      idx.push_back (make_value_list (*p_args, *p_arg_nm, &tmp));
	      break;

	    case '.':
	      {
		idx.push_back (octave_value (get_struct_index (p_arg_nm, p_dyn_field)));

		if (error_state)
		  eval_error ();
	      }
	      break;

	    default:
	      panic_impossible ();
	    }

	  if (error_state)
	    break;

	  p_args++;
	  p_arg_nm++;
	  p_dyn_field++;
	}

      if (! error_state)
	retval = tmp.subsref (type, idx, nargout);
    }

  return retval;
}

octave_value
tree_index_expression::rvalue (void)
{
  octave_value retval;

  octave_value_list tmp = rvalue (1);

  if (! tmp.empty ())
    retval = tmp(0);

  return retval;
}

octave_lvalue
tree_index_expression::lvalue (void)
{
  octave_lvalue retval;

  std::list<octave_value_list> idx;

  int n = args.size ();

  std::list<tree_argument_list *>::iterator p_args = args.begin ();
  std::list<string_vector>::iterator p_arg_nm = arg_nm.begin ();
  std::list<tree_expression *>::iterator p_dyn_field = dyn_field.begin ();

  retval = expr->lvalue ();

  if (! error_state)
    {
      const octave_value *tmp = retval.object ();

      for (int i = 0; i < n; i++)
	{
	  switch (type[i])
	    {
	    case '(':
	      idx.push_back (make_value_list (*p_args, *p_arg_nm, tmp));
	      break;

	    case '{':
	      idx.push_back (make_value_list (*p_args, *p_arg_nm, tmp));
	      break;

	    case '.':
	      {
		idx.push_back (octave_value (get_struct_index (p_arg_nm, p_dyn_field)));

		if (error_state)
		  eval_error ();
	      }
	      break;

	    default:
	      panic_impossible ();
	    }

	  if (error_state)
	    break;

	  p_args++;
	  p_arg_nm++;
	  p_dyn_field++;
	}

      if (! error_state)
	retval.set_index (type, idx);
    }

  return retval;
}

void
tree_index_expression::eval_error (void) const
{
  int l = line ();
  int c = column ();

  const char *type_str;

  if (type[0] == '.')
    type_str = "structure reference operator";
  else if (args.front ())
    type_str = "index expression";
  else
    type_str = "expression";

  if (l != -1 && c != -1)
    ::error ("evaluating %s near line %d, column %d", type_str, l, c);
  else
    ::error ("evaluating %s", type_str);
}

void
tree_index_expression::accept (tree_walker& tw)
{
  tw.visit_index_expression (*this);
}

/*
;;; Local Variables: ***
;;; mode: C++ ***
;;; End: ***
*/
