00001 #include "xtended.hh"
00002 #include "Text.hh"
00003 #include <math.h>
00004 #include "sigtyperules.hh"
00005
00006 class AbsPrim : public xtended
00007 {
00008
00009 public:
00010
00011 AbsPrim() : xtended("abs") {}
00012
00013 virtual unsigned int arity () { return 1; }
00014
00015 virtual bool needCache () { return true; }
00016
00017 virtual Type infereSigType (const vector<Type>& types)
00018 {
00019 assert (types.size() == arity());
00020 Type t = types[0];
00021 return castInterval(t, abs(t->getInterval()));
00022 return t;
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 args[0];
00031 }
00032
00033
00034 virtual Tree computeSigOutput (const vector<Tree>& args)
00035 {
00036 float f; int i;
00037
00038 assert (args.size() == arity());
00039
00040 if (isFloat(args[0]->node(),&f)) {
00041 return tree(fabsf(f));
00042
00043 } else if (isInt(args[0]->node(),&i)) {
00044 return tree(abs(i));
00045
00046 } else {
00047 return tree(symbol(), args[0]);
00048 }
00049 }
00050
00051 virtual string generateCode (Klass* klass, const vector<string>& args, const vector<Type>& types)
00052 {
00053 assert (args.size() == arity());
00054 assert (types.size() == arity());
00055
00056 Type t = infereSigType(types);
00057 if (t->nature() == kReal) {
00058 return subst("fabsf($0)", args[0]);
00059 } else {
00060 return subst("abs($0)", args[0]);
00061 }
00062 }
00063
00064 };
00065
00066
00067 xtended* gAbsPrim = new AbsPrim();
00068
00069