-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast_stmt.h
More file actions
140 lines (121 loc) · 3.57 KB
/
ast_stmt.h
File metadata and controls
140 lines (121 loc) · 3.57 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
/* File: ast_stmt.h
* ----------------
* The Stmt class and its subclasses are used to represent
* statements in the parse tree. For each statment in the
* language (for, if, return, etc.) there is a corresponding
* node class for that construct.
*
* pp3: You will need to extend the Stmt classes to implement
* semantic analysis for rules pertaining to statements.
*/
#ifndef _H_ast_stmt
#define _H_ast_stmt
#include "list.h"
#include "ast.h"
#include "hashtable.h"
#include "ast_decl.h"
#include "codegen.h"
class Decl;
class VarDecl;
class Expr;
class Program : public Node
{
protected:
List<Decl*> *decls;
Hashtable<Decl*> *globalST;
public:
Program(List<Decl*> *declList);
void Check();
void Emit();
};
class Stmt : public Node
{
public:
Stmt() : Node() {}
Stmt(yyltype loc) : Node(loc) {}
virtual void Check(List< Hashtable<Decl*> *> *ST){printf("stmt check\n");};
virtual void Emit(CodeGenerator* cg, List< Hashtable<Decl*> *> *ST){printf("Stmt::Emit\n");}
};
class StmtBlock : public Stmt
{
protected:
Hashtable<Decl*> *blockST;
List<VarDecl*> *decls;
List<Stmt*> *stmts;
public:
StmtBlock(List<VarDecl*> *variableDeclarations, List<Stmt*> *statements);
void Check(List< Hashtable<Decl*> *> *ST);
friend void FnDecl::Check(List< Hashtable<Decl*> *> *ST);
friend void FnDecl::Emit(CodeGenerator* cg, List< Hashtable<Decl*> *> *ST);
void Emit(CodeGenerator* cg, List< Hashtable<Decl*> *> *ST);
};
class ConditionalStmt : public Stmt
{
protected:
Expr *test;
Stmt *body;
public:
ConditionalStmt(Expr *testExpr, Stmt *body);
void Check(List< Hashtable<Decl*> *> *ST);
};
class LoopStmt : public ConditionalStmt
{
public:
char *outlabel=NULL;
LoopStmt(Expr *testExpr, Stmt *body)
: ConditionalStmt(testExpr, body) {}
};
class ForStmt : public LoopStmt
{
protected:
Expr *init, *step;
public:
ForStmt(Expr *init, Expr *test, Expr *step, Stmt *body);
void Check(List< Hashtable<Decl*> *> *ST);
void Emit(CodeGenerator* cg, List< Hashtable<Decl*> *> *ST);
};
class WhileStmt : public LoopStmt
{
public:
WhileStmt(Expr *test, Stmt *body) : LoopStmt(test, body) {}
void Emit(CodeGenerator* cg, List< Hashtable<Decl*> *> *ST);
};
class IfStmt : public ConditionalStmt
{
protected:
Stmt *elseBody;
public:
IfStmt(Expr *test, Stmt *thenBody, Stmt *elseBody);
void Check(List< Hashtable<Decl*> *> *ST);
void Emit(CodeGenerator* cg, List< Hashtable<Decl*> *> *ST);
};
class BreakStmt : public Stmt
{
public:
BreakStmt(yyltype loc) : Stmt(loc) {}
void Check(List< Hashtable<Decl*> *> *ST);
void Emit(CodeGenerator* cg, List< Hashtable<Decl*> *> *ST);
};
class ReturnStmt : public Stmt
{
protected:
Expr *expr;
public:
ReturnStmt(yyltype loc, Expr *expr);
void Check(List< Hashtable<Decl*> *> *ST) ;
void Emit(CodeGenerator* cg, List< Hashtable<Decl*> *> *ST);
};
class PrintStmt : public Stmt
{
protected:
List<Expr*> *args;
public:
PrintStmt(List<Expr*> *arguments);
void Check(List< Hashtable<Decl*> *> *ST) ;
void Emit(CodeGenerator* cg, List< Hashtable<Decl*> *> *ST);
};
void CheckList(Hashtable<Decl*> *currentST, List<Decl*> *declList, bool reportFlag=true);
void CheckList(Hashtable<Decl*> *currentST, List<VarDecl*> *declList);
void RemoveST(List< Hashtable<Decl*> *> *ST);
void print_ST(List< Hashtable<Decl*> *> *ST);
#endif