/**
 * \brief Implements column() function for tables.
 *
 * \arg \c columnPath Path to the column to read data from. See
 * resolveColumnPath().
 *
 * The row to read from is determined by the muParser variable "i" set during
 * iteration of a column
 * formula. For explicitly specifying the row, use cell() instead.
 *
 * \sa tableCellFunction()
 */
double MuParserScript::tableColumnFunction(const char *columnPath) {
  Column *column =
      s_currentInstance->resolveColumnPath(QString::fromUtf8(columnPath));
  if (!column) return NAN;  // failsafe, shouldn't happen
  int row = qRound(s_currentInstance->m_variables["i"]) - 1;
  if (column->isInvalid(row)) throw new EmptySourceError();
  return column->valueAt(row);
}
/**
 * \brief Implements cell_() function for tables.
 *
 * \arg \c columnIndex 1-based index of column to read data from.
 * \arg \c rowIndex 1-based index of row to read data from.
 *
 * It is preferable to use cell() instead of this function where possible, because referring to
 * columns via index silently breaks when moving columns.
 *
 * \sa tableColumn_Function()
 */
double MuParserScript::tableCell_Function(double columnIndex, double rowIndex) {
	Table *thisTable = qobject_cast<Table*>(s_currentInstance->Context);
	if (!thisTable)
		throw mu::Parser::exception_type(qPrintable(tr("cell() works only on tables and matrices!")));
	Column *column = thisTable->d_future_table->column(qRound(columnIndex) - 1);
	if (!column)
		throw mu::Parser::exception_type(qPrintable(tr("There's no column %1 in table %2!")
					.arg(qRound(columnIndex)).arg(thisTable->objectName())));
	int row = qRound(rowIndex) - 1;
	if (column->isInvalid(row))
		throw new EmptySourceError();
	return column->valueAt(row);
}
/**
 * \brief Implements column_() function for tables.
 *
 * \arg \c columnIndex 1-based index of the column to read data from.
 *
 * It is preferable to use column() instead of this function where possible, because referring to
 * columns via index silently breaks when moving columns.
 *
 * The row to read from is determined by the muParser variable "i" set during iteration of a column
 * formula. For explicitly specifying the row, use cell_() instead.
 *
 * \sa tableCell_Function()
 */
double MuParserScript::tableColumn_Function(double columnIndex) {
	Table *thisTable = qobject_cast<Table*>(s_currentInstance->Context);
	if (!thisTable)
		// improving the error message would break translations
		// TODO: change col() to column() for next minor release
		throw mu::Parser::exception_type(qPrintable(tr("col() works only on tables!")));
	Column *column = thisTable->d_future_table->column(qRound(columnIndex) - 1);
	if (!column)
		throw mu::Parser::exception_type(qPrintable(tr("There's no column %1 in table %2!")
					.arg(qRound(columnIndex)).arg(thisTable->objectName())));
	int row = qRound(s_currentInstance->m_variables["i"]) - 1;
	if (column->isInvalid(row))
		throw new EmptySourceError();
	return column->valueAt(row);
}