1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
#include "xtended.hh"
#include "Text.hh"
#include <math.h>
#include "sigtyperules.hh"
#include "floats.hh"
class AbsPrim : public xtended
{
public:
AbsPrim() : xtended("abs") {}
virtual unsigned int arity () { return 1; }
virtual bool needCache () { return true; }
virtual Type infereSigType (const vector<Type>& types)
{
assert (types.size() == arity());
Type t = types[0];
return castInterval(t, abs(t->getInterval()));
return t;
}
virtual void sigVisit (Tree sig, sigvisitor* visitor) {}
virtual int infereSigOrder (const vector<int>& args)
{
assert (args.size() == arity());
return args[0];
}
virtual Tree computeSigOutput (const vector<Tree>& args)
{
double f; int i;
assert (args.size() == arity());
if (isDouble(args[0]->node(),&f)) {
return tree(fabs(f));
} else if (isInt(args[0]->node(),&i)) {
return tree(abs(i));
} else {
return tree(symbol(), args[0]);
}
}
virtual string generateCode (Klass* klass, const vector<string>& args, const vector<Type>& types)
{
assert (args.size() == arity());
assert (types.size() == arity());
Type t = infereSigType(types);
if (t->nature() == kReal) {
return subst("fabs$1($0)", args[0], isuffix());
} else {
return subst("abs($0)", args[0]);
}
}
virtual string generateLateq (Lateq* lateq, const vector<string>& args, const vector<Type>& types)
{
assert (args.size() == arity());
assert (types.size() == arity());
Type t = infereSigType(types);
return subst("\\left\\lvert{$0}\\right\\rvert", args[0]);
}
};
xtended* gAbsPrim = new AbsPrim();
|