aboutsummaryrefslogtreecommitdiffstats
path: root/function.cpp
blob: e666cffc376b00cae2bd4b776600c4ea33489c16 (plain) (blame)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// function.cpp
// Revision 10-jul-2004

#include "function.h"
#include "error.h"

#include <vector>
#include <map>

class ParameterList::Internal {
public:
	static Internal * getnew ();
	void addref ();
	void delref ();
	void push_back (const std::string & name);
	size_t size () const;
	//const std::string & get (size_t n) const;
	std::string get (size_t n) const;
private:
	Internal ();
	Internal (const Internal &); // Forbidden
	~Internal () { }
	void operator= (const Internal &); // Forbidden
	size_t counter;
	std::vector <std::string> element;
};

ParameterList::Internal::Internal () :
	counter (1)
{
}

void ParameterList::Internal::addref ()
{
	++counter;
}

void ParameterList::Internal::delref ()
{
	if (--counter == 0)
		delete this;
}

ParameterList::Internal * ParameterList::Internal::getnew ()
{
	return new Internal;
}

void ParameterList::Internal::push_back (const std::string & name)
{
	element.push_back (name);
}

size_t ParameterList::Internal::size () const
{
	return element.size ();
}

//const std::string & ParameterList::Internal::get (size_t n) const
std::string ParameterList::Internal::get (size_t n) const
{
	if (n >= element.size () )
		throw ErrBlassicInternal;
	return element [n];
}

ParameterList::ParameterList () :
	pin (Internal::getnew () )
{
}

ParameterList::ParameterList (const ParameterList & pl) :
	pin (pl.pin)
{
	pin->addref ();
}

ParameterList::~ParameterList ()
{
	pin->delref ();
}

ParameterList & ParameterList::operator= (const ParameterList & pl)
{
	pl.pin->addref ();
	pin->delref ();
	pin= pl.pin;
	return * this;
}

void ParameterList::push_back (const std::string & name)
{
	pin->push_back (name);
}

size_t ParameterList::size () const
{
	return pin->size ();
}

//const std::string & ParameterList::operator [] (size_t n) const
std::string ParameterList::operator [] (size_t n) const
{
	return pin->get (n);
}

class Function::Internal {
public:
	void addref ();
	void delref ();
	static Internal * getnew
		(const std::string & strdef, const ParameterList & param)
	{
		return new Internal (strdef, param);
	}
	static Internal * getnew
		(ProgramPos posfn, const ParameterList & param)
	{
		return new Internal (posfn, param);
	}
	DefType getdeftype () const { return deftype; }
	CodeLine & getcode () { return code; }
	ProgramPos getpos () const { return pos; }
	const ParameterList & getparam () { return param; }
protected:
	// Protected to avoid a warning on certain versions of gcc.
	Internal (const std::string & strdef, const ParameterList & param);
	~Internal () { }
private:
	Internal (ProgramPos posfn, const ParameterList & param);
	Internal (const Internal &); // Forbidden
	void operator = (const Internal &); // Forbidden
	size_t counter;
	DefType deftype;
	CodeLine code;
	ProgramPos pos;
	ParameterList param;
};

Function::Internal::Internal
		(const std::string & strdef, const ParameterList & param) :
	counter (1),
	deftype (DefSingle),
	param (param)
{
	code.scan (strdef);
}

Function::Internal::Internal
		(ProgramPos posfn, const ParameterList & param) :
	counter (1),
	deftype (DefMulti),
	pos (posfn),
	param (param)
{
}

void Function::Internal::addref ()
{
	++counter;
}

void Function::Internal::delref ()
{
	if (--counter == 0)
		delete this;
}

Function::Function (const std::string & strdef, const ParameterList & param) :
	pin (Internal::getnew (strdef, param) )
{
}

Function::Function (ProgramPos posfn, const ParameterList & param) :
	pin (Internal::getnew (posfn, param) )
{
}

Function::Function (const Function & f) :
	pin (f.pin)
{
	pin->addref ();
}

Function::~Function ()
{
	pin->delref ();
}

Function & Function::operator= (const Function & f)
{
	f.pin->addref ();
	pin->delref ();
	pin= f.pin;
	return * this;
}

Function::DefType Function::getdeftype () const
{
	return pin->getdeftype ();
}

CodeLine & Function::getcode ()
{
	return pin->getcode ();
}

ProgramPos Function::getpos () const
{
	return pin->getpos ();
}

const ParameterList & Function::getparam ()
{
	return pin->getparam ();
}

namespace {

typedef std::map <std::string, Function> mapfunction_t;
mapfunction_t mapfunction;

} // namespace

void Function::clear ()
{
	mapfunction.clear ();
}

void Function::insert (const std::string & name)
{
	mapfunction_t::iterator it=
		mapfunction.find (name);
	if (it != mapfunction.end () )
		it->second= * this;
	else
		mapfunction.insert (std::make_pair (name, * this) );
}

Function & Function::get (const std::string & name)
{
	mapfunction_t::iterator it=
		mapfunction.find (name);
	if (it != mapfunction.end () )
		return it->second;
	throw ErrFunctionNoDefined;
}

// Fin de function.cpp
Un proyecto texto-plano.xyz