aboutsummaryrefslogtreecommitdiffstats
path: root/c_compiler/src/bindings.cpp
blob: 7deda13de4b39b8e23fe9a5ccf9835b9bf5d0f77 (plain)
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
#include "bindings.hpp"

std::vector<std::string> Bindings::string_literals;

// Bindings definition

Bindings::Bindings()
    : break_label_(""), continue_label_(""), stack_counter_(0), expression_stack_(16)
{}

void Bindings::insertBinding(const std::string &id,const TypePtr &type, const int &stack_position)
{
    auto binding = bindings_.find(id);

    if(binding == bindings_.end())
    {
	DeclarationData decl_data;
	decl_data.type = type;
	decl_data.stack_position = stack_position;
	bindings_.insert(std::make_pair(id, decl_data));
    }
    else
    {
	(*binding).second.stack_position = stack_position;
	(*binding).second.type = type;
    }
}

void Bindings::insertBinding(const std::string &id, const TypePtr &type, const int &stack_position, const std::vector<int> array_sizes)
{
    auto binding = bindings_.find(id);

    if(binding == bindings_.end())
    {
	DeclarationData decl_data;
	decl_data.type = type;
	decl_data.stack_position = stack_position;
	decl_data.array_sizes = array_sizes;
	bindings_.insert(std::make_pair(id, decl_data));
    }
    else
    {
	(*binding).second.stack_position = stack_position;
	(*binding).second.type = type;
	(*binding).second.array_sizes = array_sizes;
    }
}

int Bindings::insertStringLiteral(const std::string &string_literal)
{
    string_literals.push_back(string_literal);
    return (int)string_literals.size()-1;
}

void Bindings::increaseStackPosition()
{
    stack_counter_ += 4;
}

void Bindings::increaseStackPosition(const int &position)
{
    stack_counter_ += position;
}

void Bindings::setStackPosition(const int &stack_counter)
{
    stack_counter_ = stack_counter;
}

void Bindings::nextExpressionStackPosition()
{
    expression_stack_ += 4;
}

void Bindings::setExpressionStackPosition(const int &stack_counter)
{
    expression_stack_ = stack_counter;
}

TypePtr Bindings::getType(const std::string &id) const
{
    auto binding = bindings_.find(id);
    return (*binding).second.type;
}

const std::vector<int> &Bindings::getArraySizes(const std::string &id) const
{
    auto binding = bindings_.find(id);
    return (*binding).second.array_sizes;
}

std::string Bindings::breakLabel()
{
    return break_label_;
}

std::string Bindings::breakLabel(const std::string &label)
{
    break_label_ = label;
    return break_label_;
}

std::string Bindings::continueLabel()
{
    return continue_label_;
}

std::string Bindings::continueLabel(const std::string &label)
{
    continue_label_ = label;
    return continue_label_;
}

int Bindings::currentStackPosition() const
{
    return stack_counter_;
}

int Bindings::stackPosition(const std::string &id) const
{
    auto binding = bindings_.find(id);

    if(binding != bindings_.end())
	return (*binding).second.stack_position;

    else return 0;
}

int Bindings::currentExpressionStackPosition() const
{
    return expression_stack_;
}

std::pair<std::vector<std::string>::const_iterator, std::vector<std::string>::const_iterator>
Bindings::getStringLiteralIterator() const
{
    return std::make_pair(string_literals.begin(), string_literals.end());
}

bool Bindings::bindingExists(const std::string &id) const
{
    auto binding = bindings_.find(id);

    if(binding == bindings_.end())
	return false;

    else
	return true;
}