-
Notifications
You must be signed in to change notification settings - Fork 0
/
astblock_old.cpp
146 lines (122 loc) · 3.83 KB
/
astblock_old.cpp
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
#include "astblock_old.h"
#include "astident.h"
#include "astflow.h"
#include "closure.h"
#include "stream.h"
/*
AstBlock::BindFlag operator|(AstFunction::BindFlag a, AstFunction::BindFlag b)
{ return static_cast<AstFunction::BindFlag>(static_cast<int>(a) | static_cast<int>(b)); }
AstBlock::BindFlag operator&(AstFunction::BindFlag a, AstFunction::BindFlag b)
{ return static_cast<AstFunction::BindFlag>(static_cast<int>(a) & static_cast<int>(b)); }
AstBlock::BindFlag operator^(AstFunction::BindFlag a, AstFunction::BindFlag b)
{ return static_cast<AstFunction::BindFlag>(static_cast<int>(a) ^ static_cast<int>(b)); }
AstBlock::BindFlag operator~(AstFunction::BindFlag a)
{ return static_cast<AstFunction::BindFlag>(~static_cast<int>(a)); }
AstBlock::BindFlag operator|=(AstFunction::BindFlag &a, AstFunction::BindFlag b)
{ return a = static_cast<AstFunction::BindFlag>(static_cast<int>(a) | static_cast<int>(b)); }
AstBlock::BindFlag operator&=(AstFunction::BindFlag &a, AstFunction::BindFlag b)
{ return a = static_cast<AstFunction::BindFlag>(static_cast<int>(a) & static_cast<int>(b)); }
AstBlock::BindFlag operator^=(AstFunction::BindFlag &a, AstFunction::BindFlag b)
{ return a = static_cast<AstFunction::BindFlag>(static_cast<int>(a) ^ static_cast<int>(b)); }
*/
void AstBlock::set_bind(const Flags flags)
{
bind |= flags;
}
void AstBlock::apply_bind(AstBlock *scope)
{
parent_scope = scope;
scope = this;
if (bind & BindInOut)
{
declared_symbols.push_back("in");
declared_symbols.push_back("out");
}
if (bind & BindImplicitRel)
{
declared_symbols.push_back(AstIdent::ImplicitIn);
declared_symbols.push_back(AstIdent::ImplicitOut);
}
if (bind & BindAutoOut)
{
declared_symbols.push_back(AstIdent::AutoOut);
std::vector<AstExpr*>::iterator i = exprs.begin();
while (i != exprs.end())
{
*i = new AstFlow(new AstIdent(AstIdent::AutoOut), *i);
i++;
}
}
bind_ident_decl = bind & BindIdentDecl;
std::vector<AstExpr*>::iterator i = exprs.begin();
while (i != exprs.end())
{
(*i)->apply_bind(scope);
i++;
}
}
void AstBlock::set_stack_start(unsigned int stack_size)
{
stack_start = stack_size;
stack_size += declared_symbols.size();
}
unsigned int AstBlock::hoist_ident(std::string symbol, bool declare)
{
unsigned int index = static_cast<unsigned int>(-1);
std::vector<std::string>::iterator i = declared_symbols.begin();
while (i != declared_symbols.end())
{
if (*i == symbol)
{
index = i - declared_symbols.begin();
break;
}
i++;
}
if (declare)
{
if (bind_ident_decl)
{
if (index == -1)
{
index = declared_symbols.size();
declared_symbols.push_back(symbol);
}
else
{
std::cerr << "Identifier \"" << symbol << "\" is declared twice in this scope" << std::endl;
}
}
else
{
if (index != -1)
{
std::cerr << "Identifier \"" << symbol << "\" is shadowed by another declaration of the same name" << std::endl;
index = -1;
}
}
}
if (index != -1)
{
return stack_start + index;
}
else
{
if (parent_scope)
{
return parent_scope->hoist_ident(symbol, declare);
}
else
{
std::cerr << "Identifier \"" << symbol << "\" is not declared in this scope" << std::endl;
return 0;
}
}
}
Stream *AstBlock::execute(Context *context)
{
Closure *closure = new Closure(exprs, declared_symbols.size(), context);
Stream *res = new Stream();
res->flow_from(closure);
return res;
}