-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathexpr.h
More file actions
73 lines (56 loc) · 1.43 KB
/
expr.h
File metadata and controls
73 lines (56 loc) · 1.43 KB
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
#ifndef EXPR_H_
#define EXPR_H_
#include "variant.h"
#include "table.h"
class Expr {
public:
virtual ~Expr() {};
virtual Variant *Evaluate(Row *row) = 0;
virtual void Print(int indent = 0) = 0;
};
class BinaryExpr : public Expr{
public:
BinaryExpr(int op = 0, Expr *left = NULL, Expr *right = NULL);
~BinaryExpr() { delete left_; delete right_;}
Variant *Evaluate(Row *row);
void Print(int indent = 0);
private:
int op_;
Expr *left_;
Expr *right_;
};
class UnaryExpr : public Expr {
public:
UnaryExpr(int op = 0, Expr *right = NULL);
~UnaryExpr() { delete right_; }
Variant *Evaluate(Row *row);
void Print(int indent = 0) {
for (int i = 0; i < indent; i++) printf(" ");
printf("(%d,\n", op_);
right_->Print(indent + 2);
}
private:
int op_;
Expr *right_;
};
class Value : public Expr {
public:
Value(Variant *value = NULL, int id = 0, bool is_attr = false)
: value_(value), id_(id), is_attr_(is_attr) {}
~Value() { delete value_; }
void set_is_attr(bool is_attr = true) { is_attr_ = is_attr; }
int id() { return id_; }
Variant *Evaluate(Row *row) {
if (is_attr_) return row->get(value_->c_str())->Clone();
else return value_->Clone();
}
void Print(int indent = 0) {
for (int i = 0; i < indent; i++) printf(" ");
printf("%s[%s]", value_->c_str(), (is_attr_? "ATTR" : "VAL"));
}
private:
Variant *value_;
int id_;
bool is_attr_;
};
#endif // EXPR_H_