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