-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScope.h
More file actions
90 lines (80 loc) · 2.52 KB
/
Scope.h
File metadata and controls
90 lines (80 loc) · 2.52 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
#ifndef SCOPE_H
#define SCOPE_H
#include <map>
using std::map;
#include <utility>
using std::pair;
using std::make_pair;
#include <vector>
using std::vector;
#include <fstream>
using std::ostream;
using std::ofstream;
#include "Tree.h"
/*
GOD OBJECT STATUS
*/
typedef vector<int> TypeSignature;
/*
For inum, rnum: just one integer.
for arrays: ARRAY <lower_bound> <upper> <standard_type>
for functions/procedures: <FUNCTION|PROCEDURE> <type> .. <type> <inum|rnum> (last one: return type)
something is an argument when the last entry is ARGUMENT.
*/
typedef map<string, TypeSignature> SymbolTable;
typedef pair<vector<string>*,TypeSignature*> Decls;
class Scope {
public:
/* initializer: a name, a vector of variables/types,
a vector of children scopes, a tree for the code */
Scope(string, vector<Decls>*, vector<Scope*>*, Tree*);
// put new variables, all of the same type in.
void insert(vector<string>, TypeSignature);
// search for a variable.
TypeSignature search(string, string);
// attach a child scope to this one.
void scope_link(Scope*);
// attach many scopes to this one.
void scope_link(vector<Scope*>);
ostream& display(ostream&, int);
void semantic_check();
// ----------------------- CODE GENERATION -----------------------
void gencode(ofstream&); // stream for output s file.
void top_prologue(ofstream&);
private:
string scope_name;
SymbolTable syms;
Scope *parent;
vector<Scope*> children;
Tree *code_tree;
map<string,int> addr_tab;
vector<string> rstack;
// ----------------------- SEMANTIC CHECKING -----------------------
string display_type_sig(TypeSignature ts);
// check that the tree refers to accessible vars
void check_vars_valid(Tree*);
// computes types and makes sure they're consistent.
// assumes function/array ind are correct.
int compute_expr_types(Tree*);
// check that while condition is bool.
// check that for bound types are consistent.
void check_loop_if_conds(Tree*);
void check_index_args(Tree*);
bool check_function_returns(Tree*);
bool check_proc_returns(Tree*);
void check_subprog_calls(Tree*);
void check_proc_call(TypeSignature, Tree*);
bool is_local(string); // check if id is local.
void check_fcn_mutation(Tree*);
// ----------------------- CODE GENERATION -----------------------
void genasm(ofstream&, Tree*);
int compute_ershov_num(Tree*);
int compute_act_rec_sz();
void gen_expr(ofstream&, Tree*);
void swap_rs();
string pop_rs();
void push_rs(string);
string top_rs();
string op2asm(int);
};
#endif