// Rascal, the Advanced Scientific CALculator
// Copyright (C) 2001, Sebastian Ritterbusch (Rascal@Ritterbusch.de)
//
// This program 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
// of the License, or (at your option) any later version.
//
// This program 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 this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//

#include <string>
#include <string.h>
#include <stdio.h>
#include <iostream.h>
#include <stdlib.h>

int showlicence(void)
{
   cout << " Rascal, the Advanced Scientific CALculator" << endl;
   cout << " Copyright (C) 2001, Sebastian Ritterbusch (Rascal@Ritterbusch.de)" << endl;
   cout << "" << endl;
   cout << " This program is free software; you can redistribute it and/or" << endl;
   cout << " modify it under the terms of the GNU General Public License" << endl;
   cout << " as published by the Free Software Foundation; either version 2" << endl;
   cout << " of the License, or (at your option) any later version." << endl;
   cout << "" << endl;
   cout << " This program is distributed in the hope that it will be useful," << endl;
   cout << " but WITHOUT ANY WARRANTY; without even the implied warranty of" << endl;
   cout << " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the" << endl;
   cout << " GNU General Public License for more details." << endl;
   cout << "" << endl;
   cout << " You should have received a copy of the GNU General Public License" << endl;
   cout << " along with this program; if not, write to the Free Software" << endl;
   cout << " Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA." << endl;
   cout << endl;
   return 0;
}

string   showversion(void)
{
   string b;
#ifdef VERSION
   #ifdef VALUE_CONFIGURATION
            if(VALUE_CONFIGURATION!=string("full"))
               b=string("Rascal-")+string(VALUE_CONFIGURATION)+string("-")+string(VERSION);
            else
               b=string("Rascal-")+string(VERSION);
   #else
            b=string("Rascal-")+string(VERSION);
   #endif
#else
            b=string("Rascal-undefined");
#endif            
   return b;
}

string   showconfiguration(void)
{
#ifdef VALUE_CONFIGURATION
   return string(VALUE_CONFIGURATION);
#else
   return string("custom");
#endif
}

#ifndef STANDALONE_VALUE
#ifndef PARSERvariable
#include "../y.tab.h"
#endif
#include "../symtab.h"
#endif

int showvariables(void)
{
#ifdef STANDALONE_VALUE
   cout << "sorry, show variables not supported in standalone version." << endl;
   return 1;
#else
   symrec * current;
   current=sym_table;

   while(current)
   {
      char *p=current->name;
      int  type=current->type;
      if(type==PARSERvariable && current->var!=value(0))
         cout << p << "=" << current->var << endl;
      current=current->next;
   }
   return 0;
#endif
}

int showfunctions(void)
{
#ifdef STANDALONE_VALUE
   cout << "sorry, show functions not supported in standalone version." << endl;
   return 1;
#else
   symrec * current;
   current=sym_table;

   while(current)
   {
      char *p=current->name;
      int  type=current->type;
      if(type==PARSERuserfunction)
         cout << p << current->ivalue.body << endl;
      else if(type==PARSERprocedure)
         cout << p << "\t(builtin)" << endl;
      else if(type==PARSERfunction)
         cout << p << "(.)\t(builtin)" << endl;
      else if(type==PARSERfunction2)
         cout << p << "(.,.)\t(builtin)" << endl;
      current=current->next;
   }
   return 0;
#endif
}

int showmodules(void)
{
   int i;
   for(i=0;rascal_module_indices[i];i++)
   {
      cout << rascal_module_indices[i] << endl;
   }
   cout << endl << "Enter 'help <module>' for more information." << endl;
   return 0;
}

int quit_program(void)
{
   exit(0);
   return 0;
}

string output(const valuematrix &a)
{
   int i,j;
   string out("[");
   for(i=0;i<a.N;i++)
   {
      for(j=0;j<a.M;j++)
      {
         value o(output(a(i,j)));
         if(o.isSTRING())
            out+=o.asSTRING();
         else
            out+="Error";
         if(j<a.M-1)
            out+=" ";
      }
      if(i<a.N-1)
         out+=";";
   }
   out+="]";
   return out;
}

string output(const int &a)
{
   char buffer[1024];
   sprintf(buffer,"%d",a);
   return string(buffer);

}

int precision1(1),precision2(10);

string output(const double & a)
{
   char buffer[1024];
   char prec[40];
   if(precision1>=0 && precision2>=0)
   {
      sprintf(prec,"%%%d.%dlg",precision1,precision2);
      sprintf(buffer,prec,a);
   } else
   {
      int s1=precision1;
      int s2=precision2;

      precision2=0;

      sprintf(prec,"%%%d.%dlg",precision1,precision2);
      sprintf(buffer,prec,a);
      
      string beststring=string(buffer);
      double bestdistance=fabs(a-stringtodouble(beststring).asDOUBLE());
            
      for(precision2=1;precision2<=-s2;precision2++)
      {
         sprintf(prec,"%%%d.%dlg",precision1,precision2);
         sprintf(buffer,prec,a);
      
         double now=fabs(a-stringtodouble(buffer).asDOUBLE());
         
         if(now<bestdistance)
         {
            beststring=string(buffer);
            bestdistance=now;
         }
         if(now==0)
            break;
      }
      
      precision1=s1;
      precision2=s2;
      
      return beststring;
   }
   return string(buffer);
}

int precision(int a,int b)
{
   precision1=a;
   precision2=b;
   return 0;
}

int showprecision(void)
{
   cout << "Precision(" << precision1 << "," << precision2 << ")" << endl;
   return 0;
}

int fac(int a)
{
   int r=1,i;
#ifndef RESPECT_RASCAL_USER_STOP_VARIABLE
   for(i=2;i<=a;i++)
#else
   for(i=2;i<=a && !rascal_user_stop;i++)
#endif
      r*=i;
   return r;
}

value stringtoint(string a)
{
   int d;
   sscanf(a.c_str(),"%ld",&d);
   return value(d);
}

value stringtodouble(string a)
{
   double d;
   sscanf(a.c_str(),"%lf",&d);
   return value(d);   
}

value matrixmatrixcell(const valuematrix & a,const valuematrix & b)
{
   int s(size(b).asINTEGER());
   if(s==0)
      return value();
   else if(s==1)
   {
      return cell(a,cell(b,1));
   }
   if((cell(b,1)<=a.N&&cell(b,1)>0)&&(cell(b,2)<=a.M&&cell(b,2)>0))
   {
      if(size(b)<=2)
         return a(Integer(cell(b,1)).asINTEGER()-1,Integer(cell(b,2)).asINTEGER()-1);

      valuematrix c(1,size(b).asINTEGER()-2);
      
      int i;
      for(i=1;i<=size(b).asINTEGER()-2;i++)
         c(0,i-1)=cell(b,i+2);
      
      return cell(a(Integer(cell(b,1)).asINTEGER()-1,Integer(cell(b,2)).asINTEGER()-1),c);
   } else
      return value();
}

valuematrix randommatrix(int i,int j)
{
   int k,l;
   valuematrix a(i,j);
   for(k=0;k<i;k++)
      for(l=0;l<j;l++)
         a(k,l)=(random()&65535)/65535.;
   return a;
}


syntax highlighted by Code2HTML, v. 0.9.1