forked from wwoloszyn/expressions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integer_visitor.h
64 lines (59 loc) · 2.01 KB
/
integer_visitor.h
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
#pragma once
#include "expression_type.h"
struct AddVisitor {
auto operator()(int a, int b) const -> ExpressionType { return a + b; }
auto operator()(ExpressionType &a, ExpressionType &b) const
-> ExpressionType {
return std::visit(AddVisitor{}, a, b);
}
template <class T, class U> auto operator()(T, U) const -> ExpressionType {
return std::monostate();
}
};
struct MinusVisitor {
auto operator()(int a) const -> ExpressionType { return -a; }
auto operator()(ExpressionType &a) const -> ExpressionType {
return std::visit(MinusVisitor{}, a);
}
auto operator()(int a, int b) const -> ExpressionType { return a - b; }
auto operator()(ExpressionType &a, ExpressionType &b) const
-> ExpressionType {
return std::visit(MinusVisitor{}, a, b);
}
template <class T> auto operator()(T) const -> ExpressionType {
return std::monostate();
}
template <class T, class U> auto operator()(T, U) const -> ExpressionType {
return std::monostate();
}
};
struct MultiplyVisitor {
auto operator()(int a, int b) const -> ExpressionType { return a * b; }
auto operator()(ExpressionType &a, ExpressionType &b) const
-> ExpressionType {
return std::visit(MultiplyVisitor{}, a, b);
}
template <class T, class U> auto operator()(T, U) const -> ExpressionType {
return std::monostate();
}
};
struct DivideVisitor {
auto operator()(int a, int b) const -> ExpressionType { return a / b; }
auto operator()(ExpressionType &a, ExpressionType &b) const
-> ExpressionType {
return std::visit(DivideVisitor{}, a, b);
}
template <class T, class U> auto operator()(T, U) const -> ExpressionType {
return std::monostate();
}
};
struct EqualsVisitor {
auto operator()(int a, int b) const -> ExpressionType { return (a == b); }
auto operator()(ExpressionType &a, ExpressionType &b) const
-> ExpressionType {
return std::visit(EqualsVisitor{}, a, b);
}
template <class T, class U> auto operator()(T, U) const -> ExpressionType {
return std::monostate();
}
};