-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpr.h
108 lines (93 loc) · 2.86 KB
/
expr.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#ifndef EXPR_H
#define EXPR_H
#include "dberror.h"
#include "tables.h"
// datatype for arguments of expressions used in conditions
typedef enum ExprType {
EXPR_OP,
EXPR_CONST,
EXPR_ATTRREF
} ExprType;
typedef struct Expr {
ExprType type;
union expr {
Value *cons;
int attrRef;
struct Operator *op;
} expr;
} Expr;
// comparison operators
typedef enum OpType {
OP_BOOL_AND,
OP_BOOL_OR,
OP_BOOL_NOT,
OP_COMP_EQUAL,
OP_COMP_SMALLER
} OpType;
typedef struct Operator {
OpType type;
Expr **args;
} Operator;
// expression evaluation methods
extern RC valueEquals (Value *left, Value *right, Value *result);
extern RC valueSmaller (Value *left, Value *right, Value *result);
extern RC boolNot (Value *input, Value *result);
extern RC boolAnd (Value *left, Value *right, Value *result);
extern RC boolOr (Value *left, Value *right, Value *result);
extern RC evalExpr (Record *record, Schema *schema, Expr *expr, Value **result);
extern RC freeExpr (Expr *expr);
extern void freeVal(Value *val);
#define CPVAL(_result,_input) \
do { \
(_result)->dt = _input->dt; \
switch(_input->dt) \
{ \
case DT_INT: \
(_result)->v.intV = _input->v.intV; \
break; \
case DT_STRING: \
(_result)->v.stringV = (char *) malloc(strlen(_input->v.stringV)); \
strcpy((_result)->v.stringV, _input->v.stringV); \
break; \
case DT_FLOAT: \
(_result)->v.floatV = _input->v.floatV; \
break; \
case DT_BOOL: \
(_result)->v.boolV = _input->v.boolV; \
break; \
} \
} while(0)
#define MAKE_BINOP_EXPR(_result,_left,_right,_optype) \
do { \
Operator *_op = (Operator *) malloc(sizeof(Operator)); \
_result = (Expr *) malloc(sizeof(Expr)); \
_result->type = EXPR_OP; \
_result->expr.op = _op; \
_op->type = _optype; \
_op->args = (Expr **) malloc(2 * sizeof(Expr*)); \
_op->args[0] = _left; \
_op->args[1] = _right; \
} while (0)
#define MAKE_UNOP_EXPR(_result,_input,_optype) \
do { \
Operator *_op = (Operator *) malloc(sizeof(Operator)); \
_result = (Expr *) malloc(sizeof(Expr)); \
_result->type = EXPR_OP; \
_result->expr.op = _op; \
_op->type = _optype; \
_op->args = (Expr **) malloc(sizeof(Expr*)); \
_op->args[0] = _input; \
} while (0)
#define MAKE_ATTRREF(_result,_attr) \
do { \
_result = (Expr *) malloc(sizeof(Expr)); \
_result->type = EXPR_ATTRREF; \
_result->expr.attrRef = _attr; \
} while(0)
#define MAKE_CONS(_result,_value) \
do { \
_result = (Expr *) malloc(sizeof(Expr)); \
_result->type = EXPR_CONST; \
_result->expr.cons = _value; \
} while(0)
#endif // EXPR