-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.js
157 lines (129 loc) · 3.92 KB
/
ast.js
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// contains the constructors to use for the relational algebra AST
"use strict";
module.exports = (function() {
const ast = {
Program : function(body){
this.body = Array.isArray(body) ? body : [body];
},
Assign : function(id, value) {
this.id = id;
this.value = value;
},
/* Binary Expressions */
Intersect : function(left, right){
this.left = left;
this.right = right;
},
Union : function(left, right){
this.left = left;
this.right = right;
},
Difference : function(left, right){
this.left = left;
this.right = right;
},
Product : function(left, right){
this.left = left;
this.right = right;
},
Divide : function(left, right){
this.left = left;
this.right = right;
},
NaturalJoin : function(left, right){
this.left = left;
this.right = right;
},
ThetaJoin : function(left, conditionList, right){
this.left = left;
this.conditionList = conditionList;
this.right = right;
},
LeftJoin : function(left, right){
this.left = left;
this.right = right;
},
RightJoin : function(left, right){
this.left = left;
this.right = right;
},
/* Unary Expressions */
Project : function(attributeList, relation){
this.attributeList = attributeList;
this.relation = relation;
},
Rename : function(target, newName, relation){
this.target = target;
this.newName = newName;
this.relation = relation;
},
Select : function(conditionList, relation){
this.conditionList = conditionList;
this.relation = relation;
},
/* Terminals */
Attribute : function(name, valueType) {
this.name = name;
this.valueType = valueType;
},
AttributeList : function(attribute){
this.attributes = Array.isArray(attribute) ? attribute : [attribute];
},
ConditionList : function(op, left, right){
this.op = op;
this.left = left;
this.right = right;
},
RelationReference : function(name) {
this.name = name;
},
Relation : function(attributes, data) {
this.attributes = attributes;
this.data = data;
},
Value : function(value){
this.value = value;
}
};
// Methods for certain nodes
ast.AttributeList.prototype.add = function(attribute){
this.attributes.push(attribute);
return this;
};
ast.Value.prototype.getType = function(){
return typeof this.value;
}
// Assign types to make node checking easier
ast.Program.prototype.type = 'Program';
ast.Assign.prototype.type = 'Assign';
ast.Intersect.prototype.type = 'Intersect';
ast.Union.prototype.type = 'Union';
ast.Difference.prototype.type = 'Difference';
ast.Product.prototype.type = 'Product';
ast.Divide.prototype.type = 'Divide';
ast.NaturalJoin.prototype.type = 'NaturalJoin';
ast.ThetaJoin.prototype.type = 'ThetaJoin';
ast.LeftJoin.prototype.type = 'LeftJoin';
ast.RightJoin.prototype.type = 'RightJoin';
ast.Project.prototype.type = 'Project';
ast.Rename.prototype.type = 'Rename';
ast.Select.prototype.type = 'Select';
ast.Attribute.prototype.type = 'Attribute';
ast.AttributeList.prototype.type = 'AttributeList';
ast.ConditionList.prototype.type = 'ConditionList';
ast.RelationReference.prototype.type = 'RelationReference';
ast.Relation.prototype.type = 'Relation';
ast.Value.prototype.type = 'Value';
// ast helper members
ast.binaryExprTypes = ['INTERSECT', 'UNION', 'DIFFERENCE', 'PRODUCT', 'DIVIDE', 'NATURALJOIN', 'THETAJOIN', 'LEFTJOIN', 'RIGHTJOIN'];
ast.unaryExprTypes = ['PROJECT', 'SELECT', 'RENAME'];
ast.metaTypes = ['PROGRAM', 'ASSIGN', 'RELATION', 'VALUE', 'RELATIONREFERENCE', 'ATTRIBUTELIST', 'CONDITIONLIST'];
ast.nodeTypes = ast.metaTypes.concat(ast.binaryExprTypes.concat(ast.unaryExprTypes));
ast.isBinaryExpr = (node) => {
return node.type.toUpperCase() in ast.binaryExprTypes;
}
ast.isUnaryExpr = (node) => {
return node.type.toUpperCase() in ast.unaryExprTypes;
}
return ast;
})();