dd56392a700199bae794032a450774de549462d0
[Faustine.git] / interpretor / faust-0.9.47mr3 / compiler / extended / minprim.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 MinPrim : public xtended
9 {
10
11 public:
12
13 MinPrim() : xtended("min") {}
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], min(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(min(f, g));
46 } else if (isInt(args[1]->node(),&j)) {
47 return tree(min(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(min(double(i), g));
56 } else if (isInt(args[1]->node(),&j)) {
57 return tree(min(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("min($0, $1)", args[0], args[1]);
76 } else {
77 return subst("min($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("\\min\\left( $0, $1 \\right)", args[0], args[1]);
88 }
89
90 };
91
92
93 xtended* gMinPrim = new MinPrim();
94
95