00001 #include "xtended.hh" 00002 #include "Text.hh" 00003 #include <math.h> 00004 #include "sigtyperules.hh" 00005 00006 #include "floats.hh" 00007 00008 class MinPrim : public xtended 00009 { 00010 00011 public: 00012 00013 MinPrim() : xtended("min") {} 00014 00015 virtual unsigned int arity () { return 2; } 00016 00017 virtual bool needCache () { return true; } 00018 00019 virtual Type infereSigType (const vector<Type>& types) 00020 { 00021 assert (types.size() == arity()); 00022 interval i = types[0]->getInterval(); 00023 interval j = types[1]->getInterval(); 00024 return castInterval(types[0]|types[1], min(i,j)); 00025 } 00026 00027 virtual void sigVisit (Tree sig, sigvisitor* visitor) {} 00028 00029 virtual int infereSigOrder (const vector<int>& args) 00030 { 00031 assert (args.size() == arity()); 00032 return max(args[0], args[1]); 00033 } 00034 00035 00036 virtual Tree computeSigOutput (const vector<Tree>& args) 00037 { 00038 double f,g; int i,j; 00039 00040 assert (args.size() == arity()); 00041 00042 if (isDouble(args[0]->node(),&f)) { 00043 00044 if (isDouble(args[1]->node(), &g)) { 00045 return tree(min(f, g)); 00046 } else if (isInt(args[1]->node(),&j)) { 00047 return tree(min(f, double(j))); 00048 } else { 00049 return tree(symbol(), args[0], args[1]); 00050 } 00051 00052 } else if (isInt(args[0]->node(),&i)) { 00053 00054 if (isDouble(args[1]->node(), &g)) { 00055 return tree(min(double(i), g)); 00056 } else if (isInt(args[1]->node(),&j)) { 00057 return tree(min(i, j)); 00058 } else { 00059 return tree(symbol(), args[0], args[1]); 00060 } 00061 00062 } else { 00063 00064 return tree(symbol(), args[0], args[1]); 00065 } 00066 } 00067 00068 virtual string generateCode (Klass* klass, const vector<string>& args, const vector<Type>& types) 00069 { 00070 assert (args.size() == arity()); 00071 assert (types.size() == arity()); 00072 00073 Type t = infereSigType(types); 00074 if (t->nature() == kReal) { 00075 return subst("min($0, $1)", args[0], args[1]); 00076 } else { 00077 return subst("min($0, $1)", args[0], args[1]); 00078 } 00079 } 00080 00081 virtual string generateLateq (Lateq* lateq, const vector<string>& args, const vector<Type>& types) 00082 { 00083 assert (args.size() == arity()); 00084 assert (types.size() == arity()); 00085 00086 Type t = infereSigType(types); 00087 return subst("\\min\\left( $0, $1 \\right)", args[0], args[1]); 00088 } 00089 00090 }; 00091 00092 00093 xtended* gMinPrim = new MinPrim(); 00094 00095