-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode.h
More file actions
220 lines (193 loc) · 6.53 KB
/
node.h
File metadata and controls
220 lines (193 loc) · 6.53 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright (C) 2014, Daniel S. Fava
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//
#ifndef __NODE_H_
#define __NODE_H_
#include "scope.h"
#include "visitor.h"
#include <iostream>
#include <vector>
#include <llvm/IR/Value.h>
#include <llvm/IR/Type.h>
class NStatement;
class NExpression;
class NVariableDeclaration;
typedef std::vector<NStatement*> StatementList;
typedef std::vector<NExpression*> ExpressionList;
typedef std::vector<NVariableDeclaration*> VariableList;
class Visitor;
class Node {
public:
int lineno;
virtual ~Node() {}
virtual void accept(class Visitor &visitor) { }
};
class NExpression : public Node {
};
class NStatement : public Node {
};
class NBlock : public NExpression {
public:
StatementList statements;
NBlock() { }
virtual void accept(Visitor &visitor) {
visitor.visit(this, V_FLAG_ENTER);
StatementList::const_iterator it;
for (it = statements.begin(); it != statements.end(); it++) {
(**it).accept(visitor);
}
visitor.visit(this, V_FLAG_EXIT);
};
};
class NSkip : public NExpression {
public:
NSkip () { }
virtual void accept(Visitor &visitor) { visitor.visit(this, V_FLAG_NONE); };
};
class NInteger : public NExpression {
public:
long long value;
NInteger(long long value) : value(value) { }
virtual void accept(Visitor &visitor) { visitor.visit(this, V_FLAG_NONE); };
};
class NBool : public NExpression {
public:
std::string value;
NBool(std::string value) : value(value) { }
virtual void accept(Visitor &visitor) { visitor.visit(this, V_FLAG_NONE); };
};
class NDouble : public NExpression {
public:
double value;
NDouble(double value) : value(value) { }
virtual void accept(Visitor &visitor) { visitor.visit(this, V_FLAG_NONE); };
};
class NType : public NExpression {
public:
std::string name;
NType(const std::string& name) : name(name) { }
virtual void accept(Visitor &visitor) { visitor.visit(this, V_FLAG_NONE); };
};
class NSecurity : public NExpression {
public:
std::string name;
NSecurity(const std::string& name) : name(name) { }
virtual void accept(Visitor &visitor) { visitor.visit(this, V_FLAG_NONE); };
};
class NIdentifier : public NExpression {
public:
std::string name;
NIdentifier(const std::string& name) : name(name) { }
virtual void accept(Visitor &visitor) { visitor.visit(this, V_FLAG_NONE); };
};
class NIfExpression : public NExpression {
public:
NExpression& iguard;
NBlock & ithen;
NBlock & ielse;
NIfExpression(NExpression& iguard, NBlock& ithen, NBlock& ielse) :
iguard(iguard), ithen(ithen), ielse(ielse) { }
virtual void accept(Visitor &visitor) {
visitor.visit(this, V_FLAG_ENTER);
visitor.visit(this, V_FLAG_GUARD | V_FLAG_ENTER);
iguard.accept(visitor);
visitor.visit(this, V_FLAG_GUARD | V_FLAG_EXIT);
visitor.visit(this, V_FLAG_THEN | V_FLAG_ENTER);
ithen.accept(visitor);
visitor.visit(this, V_FLAG_THEN | V_FLAG_EXIT);
visitor.visit(this, V_FLAG_ELSE | V_FLAG_ENTER);
ielse.accept(visitor);
visitor.visit(this, V_FLAG_ELSE | V_FLAG_EXIT);
visitor.visit(this, V_FLAG_EXIT);
};
};
class NWhileExpression : public NExpression {
public:
NExpression& iguard;
NBlock & ithen;
NWhileExpression(NExpression& iguard, NBlock& ithen) :
iguard(iguard), ithen(ithen) { }
virtual void accept(Visitor &visitor) {
visitor.visit(this, V_FLAG_ENTER);
visitor.visit(this, V_FLAG_GUARD | V_FLAG_ENTER);
iguard.accept(visitor);
visitor.visit(this, V_FLAG_GUARD | V_FLAG_EXIT);
visitor.visit(this, V_FLAG_THEN | V_FLAG_ENTER);
ithen.accept(visitor);
visitor.visit(this, V_FLAG_THEN | V_FLAG_EXIT);
visitor.visit(this, V_FLAG_EXIT);
};
};
class NBinaryOperator : public NExpression {
public:
int op;
NExpression& lhs;
NExpression& rhs;
NBinaryOperator(NExpression& lhs, int op, NExpression& rhs) :
lhs(lhs), rhs(rhs), op(op) { }
virtual void accept(Visitor &visitor) {
lhs.accept(visitor);
rhs.accept(visitor);
visitor.visit(this, V_FLAG_NONE);
};
};
class NAssignment : public NExpression {
public:
NIdentifier& lhs;
NExpression& rhs;
NAssignment(NIdentifier& lhs, NExpression& rhs) :
lhs(lhs), rhs(rhs) { }
virtual void accept(Visitor &visitor) {
rhs.accept(visitor);
visitor.visit(this, V_FLAG_NONE);
};
};
class NExpressionStatement : public NStatement {
public:
NExpression& expression;
NExpressionStatement(NExpression& expression) :
expression(expression) { }
virtual void accept(Visitor &visitor) {
expression.accept(visitor);
visitor.visit(this, V_FLAG_NONE);
};
};
class NVariableDeclaration : public NStatement {
public:
const NType& type;
NSecurity& security;
NIdentifier& id;
NExpression *assignmentExpr;
NVariableDeclaration(const NType& type, NIdentifier& id, NSecurity& sec) :
type(type), id(id), security(sec) { }
NVariableDeclaration(const NType& type, NIdentifier& id, NExpression *assignmentExpr, NSecurity& sec) :
type(type), id(id), assignmentExpr(assignmentExpr), security(sec) { }
virtual void accept(Visitor &visitor) {
((NType&)type).accept(visitor);
security.accept(visitor);
visitor.visit(this, V_FLAG_NONE); // Must declare the variable before assign
if (assignmentExpr != NULL) {
NAssignment assign(id, *assignmentExpr);
assign.lineno = lineno;
assign.accept(visitor);
}
};
};
#endif // __NODE_H_