4 #include "sigtyperules.hh"
8 class MaxPrim
: public xtended
13 MaxPrim() : xtended("max") {}
15 virtual unsigned int arity () { return 2; }
17 virtual bool needCache () { return true; }
19 virtual Type
infereSigType (const vector
<Type
>& types
)
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
));
27 virtual void sigVisit (Tree sig
, sigvisitor
* visitor
) {}
29 virtual int infereSigOrder (const vector
<int>& args
)
31 assert (args
.size() == arity());
32 return max(args
[0], args
[1]);
36 virtual Tree
computeSigOutput (const vector
<Tree
>& args
)
40 assert (args
.size() == arity());
42 if (isDouble(args
[0]->node(),&f
)) {
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
)));
49 return tree(symbol(), args
[0], args
[1]);
52 } else if (isInt(args
[0]->node(),&i
)) {
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
));
59 return tree(symbol(), args
[0], args
[1]);
64 return tree(symbol(), args
[0], args
[1]);
68 virtual string
generateCode (Klass
* klass
, const vector
<string
>& args
, const vector
<Type
>& types
)
70 assert (args
.size() == arity());
71 assert (types
.size() == arity());
73 Type t
= infereSigType(types
);
74 if (t
->nature() == kReal
) {
75 return subst("max($0, $1)", args
[0], args
[1]);
77 return subst("max($0, $1)", args
[0], args
[1]);
81 virtual string
generateLateq (Lateq
* lateq
, const vector
<string
>& args
, const vector
<Type
>& types
)
83 assert (args
.size() == arity());
84 assert (types
.size() == arity());
86 Type t
= infereSigType(types
);
87 return subst("\\max\\left( $0, $1 \\right)", args
[0], args
[1]);
93 xtended
* gMaxPrim
= new MaxPrim();