// Rascal, the Advanced Scientific CALculator // Copyright (C) 2001, 2002, 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 #include #include #include #define MAX_TYPES 256 #define MAX_HELP 1024 #define MAX_LENGTH 1024 #define MAX_FUNCTION 256 #define MAX_OVERLOAD 256 int typean=0; char *atypename[MAX_TYPES]; char *ctypename[MAX_TYPES]; int constructor[MAX_TYPES]; int functionan=0; char *function[MAX_FUNCTION]; int overloadan[MAX_FUNCTION]; int nr_ary[MAX_FUNCTION]; // -1 means implicit cast char *overload[MAX_FUNCTION][MAX_OVERLOAD][4]; int overload_done[MAX_FUNCTION][MAX_OVERLOAD]; int implicit_order[MAX_FUNCTION][MAX_OVERLOAD]; int implicit_an=0; int CPPan=0; char *CPPs[MAX_TYPES]; int HPPan=0; char *HPPs[MAX_TYPES]; int TEXan=0; char *TEXs[MAX_TYPES]; int MAKan=0; char *MAKs[MAX_TYPES]; int HELPan=0; char *HELPname[MAX_HELP]; char *HELPfile[MAX_HELP]; int MODULEan=0; char *MODULEname[MAX_TYPES]; char *configuration="custom"; int level=0; int datatype_count=0; int function_count=0; int function2_count=0; int cast_count=0; void show_statistics(void) { // cerr << configuration << ": " << typean << "+2 data types " << cast_count << " implicit casts "<< functionan << " functions " << function_count << "+" << function2_count << " overloads\r"; } void show_final_statistics(void) { cerr << configuration << ": " << typean << "+2 data types " << cast_count << " implicit casts "<< functionan << " functions " << function_count << "+" << function2_count << " overloads" << endl; } void findcast(int fromtype,int & i1,int & i2, int fintype, double & length) { level++; #ifdef DEBUG cout << length << endl; #endif if(length<0) return; if(fromtype==fintype || fintype==typean) { length=0; i1=-2; i2=-2; level--; #ifdef DEBUG cout << "*" << atypename[fromtype] << " to " << atypename[fintype] << " costs " << length << endl; #endif return; } int i,j; int found=0; for(i=0;i0;i++) if(nr_ary[i]==-1) for(j=0;j1)) { int k; double currentcost=((overload[i][j][1][0]=='*'?4.0:1)+double(implicit_an-implicit_order[i][j]-1)/implicit_an); if(currentcost<=length) { for(k=0;k2 || (overload[i][j][1][0]!='*' && length>1)) { double newlength=length-currentcost; int j1=-1,j2=-1; if(level overload[i][newoverloadan][1]=atypename[fromtype]; // evil =:> overload[i][newoverloadan][2]=new char[2+strlen(function[i])+2+8+strlen(overload[castindex1][castindex2][0])+1+7]; strcpy(overload[i][newoverloadan][2],function[i]); strcat(overload[i][newoverloadan][2],"(value(va,value::"); strcat(overload[i][newoverloadan][2],overload[castindex1][castindex2][0]); strcat(overload[i][newoverloadan][2],"))"); newoverloadan++; function2_count++;show_statistics(); } } } overloadan[i]=newoverloadan; } else if(nr_ary[i]==2) { int k1,k2; int newoverloadan=overloadan[i]; for(k1=0;k1 overload[i][newoverloadan][1]=atypename[fromtype1]; // evil =:> overload[i][newoverloadan][2]=atypename[fromtype2]; // evil =:> if(castindex11==-2) overload[i][newoverloadan][3]=new char[ +2 +strlen(function[i]) +strlen(overload[castindex21][castindex22][0]) +1 +10+11+2+14]; else if(castindex21==-2) overload[i][newoverloadan][3]=new char[ strlen(overload[castindex11][castindex12][0]) +strlen(function[i]) +2 +1 +10+11+2+14]; else overload[i][newoverloadan][3]=new char[ strlen(overload[castindex11][castindex12][0]) +strlen(function[i]) +strlen(overload[castindex21][castindex22][0]) +1 +10+11+2+14]; strcpy(overload[i][newoverloadan][3],function[i]); strcat(overload[i][newoverloadan][3],"("); if(castindex11==-2) strcat(overload[i][newoverloadan][3],"va"); else { strcat(overload[i][newoverloadan][3],"value(va,value::"); // 10 strcat(overload[i][newoverloadan][3],overload[castindex11][castindex12][0]); strcat(overload[i][newoverloadan][3],")"); } strcat(overload[i][newoverloadan][3],","); if(castindex21==-2) strcat(overload[i][newoverloadan][3],"vb"); else { strcat(overload[i][newoverloadan][3],"value(vb,value::");// 11 strcat(overload[i][newoverloadan][3],overload[castindex21][castindex22][0]); strcat(overload[i][newoverloadan][3],")"); } strcat(overload[i][newoverloadan][3],")"); // 2 newoverloadan++; function2_count++;show_statistics(); } } } overloadan[i]=newoverloadan; } } int existoverload(int i,char *p1,char *p2,int jfrom=0) { int j; for(j=jfrom;j> command) { if(strlen(command)) { if(strcmp(command,"INCLUDE")==0) { fin >> command; ifstream fin2(command); readFromStream(fin2); } else if(strcmp(command,"CPP")==0) { fin >> command; CPPs[CPPan]=new char[strlen(command)+1]; strcpy(CPPs[CPPan++],command); } else if(strcmp(command,"HPP")==0) { fin >> command; HPPs[HPPan]=new char[strlen(command)+1]; strcpy(HPPs[HPPan++],command); } else if(strcmp(command,"TEX")==0) { fin >> command; TEXs[TEXan]=new char[strlen(command)+1]; strcpy(TEXs[TEXan++],command); } else if(strcmp(command,"MAK")==0) { fin >> command; MAKs[MAKan]=new char[strlen(command)+1]; strcpy(MAKs[MAKan++],command); } else if(strcmp(command,"CONFIGURATION")==0) { fin >> command; configuration=new char[strlen(command)+1]; strcpy(configuration,command); } else if(strcmp(command,"HELP")==0) { fin >> command; HELPname[HELPan]=new char[strlen(command)+1]; strcpy(HELPname[HELPan],command); fin >> command; HELPfile[HELPan]=new char[strlen(command)+1]; strcpy(HELPfile[HELPan++],command); } else if(strcmp(command,"MODULE")==0) { fin >> command; HELPname[HELPan]=new char[strlen(command)+1]; strcpy(HELPname[HELPan],command); MODULEname[MODULEan]=new char[strlen(command)+1]; strcpy(MODULEname[MODULEan++],command); fin >> command; HELPfile[HELPan]=new char[strlen(command)+1]; strcpy(HELPfile[HELPan++],command); } else if(strcmp(command,"TYPES")==0) { while((fin >> command) && strcmp(command,"END")) { atypename[typean]=new char[strlen(command)+1]; strcpy(atypename[typean],command); fin >> command; ctypename[typean]=new char[strlen(command)+1]; strcpy(ctypename[typean],command); constructor[typean]=0; typean++; datatype_count++;show_statistics(); } } else if(strcmp(command,"BINARYFUNCTIONS")==0) { while((fin >> command) && strcmp(command,"END")) { int i; for(i=0;i> command; overload[i][overloadan[i]][j]=new char[strlen(command)+1]; strcpy(overload[i][overloadan[i]][j],command); if((j==1 || j==2) && command[0]!='*' && strcmp(command,"VALUE")!=0) { int k; for(k=0;k> command) && strcmp(command,"END")) { int i; for(i=0;i> command; overload[i][overloadan[i]][j]=new char[strlen(command)+1]; strcpy(overload[i][overloadan[i]][j],command); if(j==1 && command[0]!='*' && strcmp(command,"VALUE")!=0) { int k; for(k=0;k> command) && strcmp(command,"END")) { int i; for(i=0;i> command; overload[i][overloadan[i]][j]=new char[strlen(command)+1]; strcpy(overload[i][overloadan[i]][j],command); if(j==0 && strcmp(command,"VALUE")!=0) { int k; for(k=0;k> command) && strcmp(command,"END")) { int i; for(i=0;i> command; already done overload[i][overloadan[i]][j]=new char[strlen(command)+1]; strcpy(overload[i][overloadan[i]][j],command); if(j!=2 && command[0]!='*' && strcmp(command,"VALUE")!=0) { int k; for(k=0;k> command; } overload_done[i][overloadan[i]]=0; overloadan[i]++; cast_count++;show_statistics(); } } else cerr << "unexpected " << command << endl; } } } int main(void) { char command[MAX_LENGTH]; ifstream fin("value.in"); readFromStream(fin); doImplicitCastExtension(); int i; ofstream fout("value.out"); ofstream fout_c("value.cpp"); ofstream fout2("value.fn"); ofstream fout3("value.fn2"); ofstream fout4("value.fn0"); fout << "// Rascal, the Advanced Scientific CALculator" << endl; fout << "// Copyright (C) 2001, 2002, Sebastian Ritterbusch (Rascal@Ritterbusch.de)" << endl; fout << "//" << endl; fout << "// This program is free software; you can redistribute it and/or" << endl; fout << "// modify it under the terms of the GNU General Public License" << endl; fout << "// as published by the Free Software Foundation; either version 2" << endl; fout << "// of the License, or (at your option) any later version." << endl; fout << "//" << endl; fout << "// This program is distributed in the hope that it will be useful," << endl; fout << "// but WITHOUT ANY WARRANTY; without even the implied warranty of" << endl; fout << "// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the" << endl; fout << "// GNU General Public License for more details." << endl; fout << "//" << endl; fout << "// You should have received a copy of the GNU General Public License" << endl; fout << "// along with this program; if not, write to the Free Software" << endl; fout << "// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA." << endl; fout << "//" << endl; fout << "// This is an automatically generated file. Do not edit!" << endl; fout << endl; fout_c << "// Rascal, the Advanced Scientific CALculator" << endl; fout_c << "// Copyright (C) 2001, 2002, Sebastian Ritterbusch (Rascal@Ritterbusch.de)" << endl; fout_c << "//" << endl; fout_c << "// This program is free software; you can redistribute it and/or" << endl; fout_c << "// modify it under the terms of the GNU General Public License" << endl; fout_c << "// as published by the Free Software Foundation; either version 2" << endl; fout_c << "// of the License, or (at your option) any later version." << endl; fout_c << "//" << endl; fout_c << "// This program is distributed in the hope that it will be useful," << endl; fout_c << "// but WITHOUT ANY WARRANTY; without even the implied warranty of" << endl; fout_c << "// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the" << endl; fout_c << "// GNU General Public License for more details." << endl; fout_c << "//" << endl; fout_c << "// You should have received a copy of the GNU General Public License" << endl; fout_c << "// along with this program; if not, write to the Free Software" << endl; fout_c << "// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA." << endl; fout_c << "//" << endl; fout_c << "// This is an automatically generated file. Do not edit!" << endl; fout_c << endl; fout_c << "#include \"value.hpp\"" << endl << endl; fout << "#ifndef VALUE_OUT_INCLUDED" << endl; fout << "#define VALUE_OUT_INCLUDED" << endl; fout << endl; fout << "#define VALUE_CONFIGURATION \"" << configuration << "\"" << endl; fout << endl; for(i=0;i0) fout << atypename[i] << ','; else fout << atypename[i] << "=1,"; fout << "VALUE,VALUEERROR};" << endl << endl; fout << " TYPE type;" << endl <=overloadan[i]) { if(overload[i][j][1][0]!='*') fout_c << " case " << overload[i][j][1] << ":" << endl; else { if(defaultdone) cerr << "Error! Multiple defaults for cast to " << function[i] << endl; else { defaultdone=1; fout_c << " default:" << endl; } } fout_c << " {" << endl; for(k=0;k(const value &va, const value &vb) { value temp(greater(va,vb)); if(temp.isINTEGER()) return temp.asINTEGER(); else return 0; }" << endl; fout << " friend int operator >=(const value &va, const value &vb){ value temp(greatereq(va,vb)); if(temp.isINTEGER()) return temp.asINTEGER(); else return 0; }" << endl; fout << " friend int operator ==(const value &va, const value &vb){ value temp(eq(va,vb)); if(temp.isINTEGER()) return temp.asINTEGER(); else return 0; }" << endl; fout << " friend int operator !=(const value &va, const value &vb){ value temp(neq(va,vb)); if(temp.isINTEGER()) return temp.asINTEGER(); else return 1; }" << endl; fout << " friend value operator &(const value &va, const value &vb) { return And(va,vb); }" << endl; fout << " friend value operator |(const value &va, const value &vb) { return Or(va,vb); }" << endl; fout << " friend value operator +(const value &va, const value &vb) { return add(va,vb); }" << endl; fout << " friend value operator -(const value &va, const value &vb) { return sub(va,vb); }" << endl; fout << " friend value operator -(const value &va) { return neg(va); }" << endl; fout << " friend value operator *(const value &va, const value &vb) { return mul(va,vb); }" << endl; fout << " friend value operator /(const value &va, const value &vb) { return div(va,vb); }" << endl; fout << " friend value operator %(const value &va, const value &vb) { return mod(va,vb); }" << endl; fout << " friend int operator !(const value &va) { value temp(Not(va)); if(temp.asINTEGER()) return temp.asINTEGER(); else return 1; }" << endl; fout << " friend value & operator +=(value &va, const value &vb) { return va=add(va,vb); }" << endl; fout << " friend value & operator -=(value &va, const value &vb) { return va=sub(va,vb); }" << endl; fout << " friend value & operator *=(value &va, const value &vb) { return va=mul(va,vb); }" << endl; fout << " friend value & operator /=(value &va, const value &vb) { return va=div(va,vb); }" << endl; fout << " friend value & operator &=(value &va, const value &vb) { return va=And(va,vb); }" << endl; fout << " friend value & operator |=(value &va, const value &vb) { return va=Or(va,vb); }" << endl; fout << " friend ostream & operator <<(ostream &o, const value &vb) { value out(output(vb));return out.isSTRING()?(o << out.asSTRING()):(o << \"OutputError:\"+vb.Typename()); }" << endl; fout << endl << " // binary functions" << endl; fout_c << endl << "// binary functions" << endl; for(i=0;i> c; if(c=='\n') fout_c << "\\n"; else if(c=='\t') fout_c << "\\t"; else if(c=='\"') fout_c << "\\\""; else if(c!=0) fout_c << c; } fout_c << "\"," << endl; } fout_c << " NULL" << endl; fout_c << "};" << endl << endl; show_final_statistics(); return 0; }