-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStmt.hpp
More file actions
130 lines (117 loc) · 4.32 KB
/
Stmt.hpp
File metadata and controls
130 lines (117 loc) · 4.32 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
#ifndef Stmt_h
#define Stmt_h
#include "FwdTypes.hpp"
#include "Expr.hpp"
#include <string>
#include <vector>
class Stmt : public SType {
public:
virtual void accept(const StmtVisitor&) const = 0;
virtual ~Stmt() {}
};
class StmtFunction;
class StmtBlock : public Stmt {
std::vector<std::shared_ptr<Stmt>> statements;
public:
StmtBlock() = default;
StmtBlock(const std::vector<std::shared_ptr<Stmt>>& statements) : statements{ statements } {}
void append(std::shared_ptr<Stmt> statement) {
statements.push_back(statement);
}
const auto& get() const { return statements; }
void accept(const StmtVisitor&) const override;
};
class StmtClass : public Stmt {
std::string name;
std::shared_ptr<ExprVariable> superclass;
std::vector<std::shared_ptr<StmtFunction>> methods;
public:
StmtClass() = default;
StmtClass(const std::string& name,
std::shared_ptr<ExprVariable> superclass,
const std::vector<std::shared_ptr<StmtFunction>>& methods)
: name{ name }, superclass{ superclass }, methods{ methods } {}
void setName(const std::string& n) { name = n; }
void setSuper(std::shared_ptr<ExprVariable> s) { superclass = s; }
void addMethod(std::shared_ptr<StmtFunction> m) { methods.push_back(m); }
const auto& getName() const { return name; }
const auto& getSuper() const { return superclass; }
const auto& getMethods() const { return methods; }
void accept(const StmtVisitor&) const override;
};
class StmtExpression : public Stmt {
std::shared_ptr<Expr> expression;
public:
StmtExpression(std::shared_ptr<Expr> expression) : expression{ expression } {}
const auto get() const { return expression; }
void accept(const StmtVisitor&) const override;
};
class StmtFunction : public Stmt {
std::string name;
std::vector<std::string> params;
std::shared_ptr<StmtBlock> body;
public:
StmtFunction() = default;
StmtFunction(const std::string& name,
const std::vector<std::string>& params,
std::shared_ptr<StmtBlock> body)
: name{ name }, params{ params }, body{ body } {}
void setName(const std::string& n) { name = n; }
void addParam(const std::string& p) { params.push_back(p); }
void setBody(std::shared_ptr<StmtBlock> b) { body = b; }
const auto& getName() const { return name; }
const auto& getParams() const { return params; }
const auto& getBody() const { return body; }
void accept(const StmtVisitor&) const override;
};
class StmtIf : public Stmt {
std::shared_ptr<Expr> condition;
std::shared_ptr<Stmt> thenBranch;
std::shared_ptr<Stmt> elseBranch;
public:
StmtIf(std::shared_ptr<Expr> condition,
std::shared_ptr<Stmt> thenBranch,
std::shared_ptr<Stmt> elseBranch)
: condition{ condition }, thenBranch{ thenBranch }, elseBranch{ elseBranch } {}
const auto getCond() const { return condition; }
const auto getThen() const { return thenBranch; }
const auto getElse() const { return elseBranch; }
void accept(const StmtVisitor&) const override;
};
class StmtPrint : public Stmt {
std::shared_ptr<Expr> expression;
public:
StmtPrint(std::shared_ptr<Expr> expression) : expression{ expression } {}
const auto get() const { return expression; }
void accept(const StmtVisitor&) const override;
};
class StmtReturn : public Stmt {
std::shared_ptr<Expr> value;
public:
StmtReturn(std::shared_ptr<Expr> value) : value{ value } {}
const auto get() const { return value; }
void accept(const StmtVisitor&) const override;
};
class StmtVariable : public Stmt {
std::string name;
std::shared_ptr<Expr> initializer;
public:
StmtVariable(const std::string& name,
std::shared_ptr<Expr> initializer)
: name{ name }, initializer{ initializer } {}
const auto& getName() const { return name; }
const auto getInit() const { return initializer; }
void accept(const StmtVisitor&) const override;
};
class StmtWhile : public Stmt {
std::shared_ptr<Expr> condition;
std::shared_ptr<Stmt> body;
public:
StmtWhile(std::shared_ptr<Expr> condition,
std::shared_ptr<Stmt> body)
: condition{ condition }, body{ body } {}
const auto getCond() const { return condition; }
const auto getBody() const { return body; }
void accept(const StmtVisitor&) const override;
};
#endif // Stmt_h