This repository was archived by the owner on Apr 13, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram_stucture.h
More file actions
349 lines (277 loc) · 8.64 KB
/
program_stucture.h
File metadata and controls
349 lines (277 loc) · 8.64 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#pragma once
#include "program_tree.h"
#include <vector>
#include <string>
#include <list>
#include <iosfwd>
#ifndef NDEBUG
#include <iostream>
#endif
#include <boost/optional.hpp>
#include <boost/variant.hpp>
namespace sap
{
struct Triad;
struct ProgramCode
{
using Set = std::set< Triad >;
using Line = Set::iterator;
Set code_;
Line addTriad( const std::string& i_op, const std::string& i_lhs = "", const std::string& i_rhs = "" );
void addComment( const std::string& i_comment1, const std::string& i_comment2 );
static ProgramCode& instance();
uint size() const;
void reset();
private:
ProgramCode() = default;
ProgramCode( const ProgramCode& ) = delete;
ProgramCode( ProgramCode&& ) = delete;
ProgramCode& operator=( const ProgramCode& ) = delete;
ProgramCode& operator=( ProgramCode&& ) = delete;
};
std::ostream& operator<<( std::ostream& out, const ProgramCode& lhs );
struct Triad
{
static uint getNextCounter();
const uint no_;
std::string op_;
mutable std::string lhs_;
mutable std::string rhs_;
Triad( const std::string& i_op, const std::string& i_lhs, const std::string& i_rhs );
};
std::ostream& operator<<( std::ostream& out, const Triad& lhs );
bool operator<( const Triad& lhs, const Triad& rhs_ );
struct ClassScope;
using Identifiers = std::list< std::string >;
using Identifier = Identifiers::const_iterator;
struct GlobalScope
{
using Classes = std::map< std::string, ClassScope >;
Classes classes_;
bool nameIsFree( const std::string& i_name) const;
ClassScope& addClass( const std::string& i_name );
Identifiers getBlockedNames() const;
};
std::ostream& operator<<( std::ostream& out, const GlobalScope& scope );
struct MethodScope;
struct ClassScope
{
using Methods = std::map< std::string, MethodScope >;
const Identifiers blocked_;
Methods methods_;
bool nameIsFree( const std::string& i_name ) const;
MethodScope& addMethod( const std::string& i_name);
ClassScope( Identifiers&& i_blocked );
Identifiers getBlockedNames() const;
};
std::ostream& operator<<( std::ostream& out, const ClassScope& scope );
struct Scope;
struct Scope
{
using Scopes = std::list< Scope >;
const Identifiers inherited_;
const Identifiers blocked_;
Identifiers local_;
Scopes inner_;
Scope() = default;
Scope( Identifiers&& i_blocked, Identifiers&& i_inherited );
boost::optional< Identifier > getVariable_o( const std::string& i_name ) const;
Identifier getVariable( const std::string& i_name ) const;
Identifier addVariable( const std::string& i_name );
Scope& addScope();
Identifiers getBlockedNames() const;
Identifiers getInherited() const;
};
std::ostream& operator<<( std::ostream& out, const Scope& scope );
struct MethodScope
{
const Identifiers blocked_;
Identifiers parameters_;
std::shared_ptr< Scope > local_;
MethodScope( Identifiers&& i_blocked );
// MethodScope( const MethodScope& other ) : blocked_{ other.blocked_}, parameters_{ other.parameters_ }, local_( other.local_ ) { }
bool nameIsFree( const std::string& i_name ) const;
Identifier addParameter( const std::string& i_name );
Identifiers getBlockedNames() const;
Identifiers getParameters() const;
Scope& scope();
};
std::ostream& operator<<( std::ostream& out, const MethodScope& scope );
template < lex::rule Rule >
struct ProgramElement
{
ProgramElement( const node& i_node )
{
#ifndef NDEBUG
std::cout << "Parsing node with " << Rule << ';'
<< "Actual " << i_node.rule_ << '\n';
#endif
assert( Rule == i_node.rule_ );
}
};
struct OperandDouble : ProgramElement< lex::rule::OPERAND_INT >
{
using Value = boost::variant< Identifier, double >;
Value value_;
OperandDouble( const node& i_node, const Scope& i_symbolTable );
uint operator()() const;
};
struct OperandBool : ProgramElement< lex::rule::OPERAND_BOOL >
{
using Value = boost::variant< Identifier, bool >;
Value value_;
OperandBool( const node& i_node, const Scope& i_symbolTable );
uint operator()() const;
};
struct OperandString : ProgramElement< lex::rule::OPERAND_STR >
{
using Value = boost::variant< Identifier, std::string >;
Value value_;
OperandString( const node& i_node, const Scope& i_symbolTable );
uint operator()() const;
};
struct ExprDouble : ProgramElement< lex::rule::EXPR_INT >
{
OperandDouble lhs_;
enum class Op { PLUS, MINUS, MULT, DIV };
boost::optional< Op > op_;
boost::optional< OperandDouble > rhs_;
ExprDouble( const node& i_node, const Scope& i_symbolTable );
uint operator()() const;
};
enum class Cmp { EQ, NE, LE, GE };
struct LogicBool : ProgramElement< lex::rule::LOGIC_BOOL >
{
OperandBool lhs_;
boost::optional< Cmp > cmp_;
boost::optional< OperandBool > rhs_;
LogicBool( const node& i_node, const Scope& i_symbolTable );
uint operator()() const;
};
struct LogicDouble : ProgramElement< lex::rule::LOGIC_INT >
{
OperandDouble lhs_;
Cmp cmp_;
OperandDouble rhs_;
LogicDouble( const node& i_node, const Scope& i_symbolTable );
uint operator()() const;
};
struct LogicString : ProgramElement< lex::rule::LOGIC_STR >
{
OperandString lhs_;
Cmp cmp_;
OperandString rhs_;
LogicString( const node& i_node, const Scope& i_symbolTable );
uint operator()() const;
};
using Logic = boost::variant< LogicBool, LogicDouble, LogicString >;
struct Constructor : private ProgramElement< lex::rule::FCALL >
{
std::string name_;
Constructor( const node& i_node, const Scope& i_scope );
uint operator()() const;
};
struct MethodCall;
struct Input;
using Rightside = boost::variant< Logic, ExprDouble, Constructor, MethodCall, Input, std::string >;
using Parameters = std::vector< Rightside >;
struct Applicable : ProgramElement< lex::rule::APPLICABLE >
{
boost::optional< Identifier > object_;
boost::optional< Constructor > class_;
Applicable( const node& i_node, const Scope& i_symbolTable );
uint operator()() const;
};
struct MethodCall : ProgramElement< lex::rule::MCALL >
{
Applicable lhs_;
std::string method_;
Parameters params_;
MethodCall( const node& i_node, const Scope& i_symbolTable );
uint operator()() const;
};
struct Input : ProgramElement< lex::rule::INPUT >
{
Input( const node& i_node );
uint operator()() const;
};
struct NewVariable : ProgramElement< lex::rule::NEW_IDENTIFIER >
{
Identifier this_;
NewVariable( const node& i_node, Scope& i_symbolTable );
};
struct Assignment : ProgramElement< lex::rule::ASSIGNMENT >
{
NewVariable lhs_;
Rightside rhs_;
Assignment( const node& i_node, Scope& i_symbolTable );
void operator()() const;
};
struct Break : ProgramElement< lex::rule::BREAKLINE >
{
Break( const node& i_node );
ProgramCode::Line operator()( bool insideWhile ) const;
};
struct Return : ProgramElement< lex::rule::RETURNLINE >
{
Rightside rhs_;
Return( const node& i_node, const Scope& i_symbolTable );
void operator()() const;
};
struct Print : ProgramElement< lex::rule::PRINTLINE >
{
Rightside rhs_;
Print( const node& i_node, const Scope& i_symbolTable );
void operator()() const;
};
struct If;
struct While;
using Sline = boost::variant< Assignment, MethodCall, Break, Return, Print, If, While >;
using Slines = std::vector< Sline >;
struct If : ProgramElement< lex::rule::IFLINE >
{
Logic logic_;
Scope& thenTable_;
Slines then_;
Scope& elseTable_;
Slines else_;
If( const node& i_node, Scope& i_symbolTable );
void operator()() const;
};
struct While : ProgramElement< lex::rule::WHILELINE >
{
Logic logic_;
Scope& localTable_;
Slines body_;
While( const node& i_node, Scope& i_symbolTable );
void operator()() const;
};
using MethodParameters = std::vector< Identifier >;
struct MethodDecl : ProgramElement< lex::rule::METHOD_DECL >
{
const std::string name_;
MethodScope& methodScope_;
MethodParameters params_;
Slines body_;
MethodDecl( const node& i_node, ClassScope& i_symbolTable );
void operator()() const;
};
using MethodDecls = std::vector< MethodDecl >;
struct ClassDecl : ProgramElement< lex::rule::CLASS_DECL >
{
const std::string name_;
ClassScope& classScope_;
MethodDecls methods_;
ClassDecl( const node& i_node, GlobalScope& i_symbolTable );
void operator()() const;
};
using ClassDecls = std::vector< ClassDecl >;
struct Program : ProgramElement< lex::rule::START >
{
GlobalScope scope_;
ClassDecls classes_;
MethodCall run_;
Program( const node& i_node );
void operator()() const;
};
} // namespace sap