9c58ca7bd1d494437c406aca04321997ff210388
[Faustine.git] / interpretor / faust-0.9.47mr3 / compiler / extended / maxprim.cpp
1 #include "xtended.hh"
2 #include "Text.hh"
3 #include <math.h>
4 #include "sigtyperules.hh"
5
6 #include "floats.hh"
7
8 class MaxPrim : public xtended
9 {
10
11 public:
12
13 MaxPrim() : xtended("max") {}
14
15 virtual unsigned int arity () { return 2; }
16
17 virtual bool needCache () { return true; }
18
19 virtual Type infereSigType (const vector<Type>& types)
20 {
21 assert (types.size() == arity());
22 interval i = types[0]->getInterval();
23 interval j = types[1]->getInterval();
24 return castInterval(types[0]|types[1], max(i,j));
25 }
26
27 virtual void sigVisit (Tree sig, sigvisitor* visitor) {}
28
29 virtual int infereSigOrder (const vector<int>& args)
30 {
31 assert (args.size() == arity());
32 return max(args[0], args[1]);
33 }
34
35
36 virtual Tree computeSigOutput (const vector<Tree>& args)
37 {
38 double f,g; int i,j;
39
40 assert (args.size() == arity());
41
42 if (isDouble(args[0]->node(),&f)) {
43
44 if (isDouble(args[1]->node(), &g)) {
45 return tree(max(f, g));
46 } else if (isInt(args[1]->node(),&j)) {
47 return tree(max(f, double(j)));
48 } else {
49 return tree(symbol(), args[0], args[1]);
50 }
51
52 } else if (isInt(args[0]->node(),&i)) {
53
54 if (isDouble(args[1]->node(), &g)) {
55 return tree(max(double(i), g));
56 } else if (isInt(args[1]->node(),&j)) {
57 return tree(max(i, j));
58 } else {
59 return tree(symbol(), args[0], args[1]);
60 }
61
62 } else {
63
64 return tree(symbol(), args[0], args[1]);
65 }
66 }
67
68 virtual string generateCode (Klass* klass, const vector<string>& args, const vector<Type>& types)
69 {
70 assert (args.size() == arity());
71 assert (types.size() == arity());
72
73 Type t = infereSigType(types);
74 if (t->nature() == kReal) {
75 return subst("max($0, $1)", args[0], args[1]);
76 } else {
77 return subst("max($0, $1)", args[0], args[1]);
78 }
79 }
80
81 virtual string generateLateq (Lateq* lateq, const vector<string>& args, const vector<Type>& types)
82 {
83 assert (args.size() == arity());
84 assert (types.size() == arity());
85
86 Type t = infereSigType(types);
87 return subst("\\max\\left( $0, $1 \\right)", args[0], args[1]);
88 }
89
90 };
91
92
93 xtended* gMaxPrim = new MaxPrim();
94
95