File: command.cc

package info (click to toggle)
littlewizard 1.2.2-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 3,400 kB
  • ctags: 1,127
  • sloc: sh: 9,388; cpp: 7,685; xml: 420; makefile: 384; awk: 40; ansic: 38
file content (278 lines) | stat: -rw-r--r-- 6,307 bytes parent folder | download | duplicates (4)
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */

 /* (C) Marcin Kwadrans <quarter@users.sourceforge.net> */

#include "include/support.h"
#include "include/command.h"
#include "include/message.h"

/*! \brief Konstruktor polecenia

	Wołany przez konstruktory poleceń dziedziczących z klasy
*/
LWCommand::LWCommand(): argc(0)
{
}

/*! \brief Deincjalizacja polecenia

	Metoda może być przeciążona w poleceniach dziedziczących z klasy.
*/
void LWCommand::reset1()
{
}

/*! \brief Deincjalizacja polecenia

	Deincjalizuje polecenie. Wewnętrznie woła metodę reset1.
*/
void LWCommand::reset()
{
	reset1();
	
	for (guint i=0; i < argc; i++)
		if (arg[i] != NULL)
			if (FALSE == arg[i]->isVariable()) {
				delete arg[i];
				arg[i] = NULL;
			}
		
	argc = 0;
}

/*! \brief Nadanie argumentu

	Nadaje wartość dla argumentu dla polecenia.
	\param n Numer argumentu numerowany od 0
	\param value Wartość argumentu
*/
void LWCommand::setArgument (guint n, LWValue *value)
{
	if (value != NULL)
		checkArgument (n, value);
	
	arg[n] = value;
}

/*! \brief Ustawianie ilości argumentów

	Ustawia ilość argumentów, które dostarczono do polecenia
	\param n Ilość argumentów
*/
void LWCommand::setArgc (guint n)
{
	checkArgc (n);
	
	argc = n;
}

/*! \brief Sprawdzenie poprawności argumentów

	Sprawdza poprawność argumentu. Metoda może być przeciążona
	przez polecenia dziedziczącące z klasy
	\param n Numer argumentu numerowany od 0
	\param value Wartość argumentu
*/
void LWCommand::checkArgument (guint n, LWValue *value)
{
	(void) value;
	(void) n;	
}

/*! \brief Sprawdzenie czy ilość argumentów jest poprawna

	Sprawdza czy ilość argumentów dostraczonych do polecenia
	jest dopuszczalna. Metoda może być przeciążona przez polecenia
	dziedziczącące z klasy. Nie przeciążona metoda zwraca wyjątek
	jeśli dostarczono jakiekolwiek argumenty.
	\param n Ilość argumentów
*/
void LWCommand::checkArgc (guint n)
{
	if (n != 0) 
		throw new LWMessage (LW_ERROR_WrongNumberOfArguments);
}	

/*! \brief Pobieranie argumentów

	Pobiera argumenty do tablicy, pobierając brakujące wartości
	ze stosu.
	\param args Tablica, w której znajdują się argumenty
	\param stack Stos
*/
void LWCommand::getArguments (LWValue *args[], GQueue *stack)
{
	g_return_if_fail (argc > 0);
	
	for (gint i= (gint) argc-1; i >= 0; i--)
		if (arg[i] == NULL) {
			args[i] = (LWValue *) g_queue_pop_head (stack);
		} else
			args[i] = arg[i];
}

/*! \brief Zwolnienie argumentów

	Zwalnia argumenty znajdujące się w tablicy
	\param args Tablica, w której znajdują się argumenty
*/
void LWCommand::freeArguments (LWValue *args[])
{
	for (guint i= 0; i < argc; i++)
		if (arg[i] == NULL)
			if (FALSE == args[i]->isVariable())
				delete args[i];
}


/*! \brief Zwolnienie argumentów

	Zwalnia argumenty znajdujące się w tablicy
	\param args Tablica, w której znajdują się argumenty
*/
void LWCommand::pushArguments (LWValue *args[], GQueue *stack)
{
	for (guint i= 0; i < argc; i++)
		if (arg[i] == NULL)
			g_queue_push_head (stack, (gpointer) args[i]);
}

/*! \brief Przypisanie wyniku

	Przekazuje wartość wyniku na stos
	\param value Wartość
	\param stack Stos
*/
void LWCommand::setReturn (LWValue *value, GQueue *stack)
{
	g_return_if_fail (hasReturn() == TRUE);
	
	g_queue_push_head (stack, (gpointer) value);
}

/*! \brief Czy polecenie zwraca wynik

	Tylko polecenia przeciążające tą metodę w taki sposób by
	zwracała prawdę, mogę zwracać wynik. Nie przeciążona
	metoda zwraca fałsz.
*/
gboolean LWCommand::hasReturn ()
{
	return FALSE;
}

/*! \brief Czy polecenie jest pętlą

	Tylko polecenia przeciążające tą metodę w taki sposób by
	zwracała prawdę, mogę implementować pętlę. Nie przeciążona
	metoda zwraca fałsz.
*/
gboolean LWCommand::isLoop ()
{
	return FALSE;	
}

/*! \brief Czy polecenie jest parzyste

	Czy polecenie składa się z dwóch części. Przykładami dwu członowych
	poleceń są nawiasy okrągłe oraz polecenia bloków logicznych.
	Nie przeciążona metoda zwraca fałsz.
*/
gboolean LWCommand::isSegment()
{
	return FALSE;	
}

/*! \brief Wykonanie polecenia

	Wykonuje polecenie. Wewnętrznie woła metodę execute1, która musi
	być zaimplementowana w poleceniu dziedziczącemu z tej klasy.
	\param context Kontekst polecenia
*/
void LWCommand::execute (LWContext *context, gboolean resume)
{
LWValue *r;
	
if (argc != 0) {
	LWValue *args[argc];
	GNode *lastip = context->instrPtr;
	getArguments (args, context->stack);

	try {
		r = (resume == FALSE) ? execute1 (context, argc, args) : resume1 (context, argc, args);
	} catch (LWMessage *msg) {

		/* Why it doesn't work with win32 ??? */
#ifndef _WIN32
		freeArguments (args);
#endif
		throw msg;
	}
	
	if (lastip != context->instrPtr || FALSE == context->resume)
		freeArguments (args);
	else
		pushArguments (args, context->stack);
} else
	r = (resume == FALSE) ? execute1 (context, argc, NULL) : resume1 (context, argc, NULL);

if (context->resume == FALSE && TRUE == hasReturn()) {
	g_return_if_fail (r != NULL);
	setReturn (r, context->stack);
} else
	g_return_if_fail (r == NULL);
}

/*! \brief Pobranie priorytetu

	Pobiera priorytet polecenia. Nie przeciążona metoda zwraca 
	priorytet 1 (domyślny dla poleceń dla czarodzieja)
*/
guint LWCommand::getPriority ()
{
	return 1;
}

gboolean LWCommand::canBeSkiped ()
{
	return FALSE;
}

/*! \brief Pobranie rodzaju wiązania

	Pobiera rodzaj wiązania jakie posiada polecenie.
*/
LWLink LWCommand::getLinkType () 
{
	return LW_LINK_LEFT;
}

gboolean LWCommand::isCommand ()
{
	return TRUE;
}

gboolean LWCommand::canClone ()
{
	return TRUE;
}

gboolean LWCommand::requiresLogicBlock ()
{
	return isLoop();
}

LWValue *LWCommand::resume1 (LWContext *context, guint argc, LWValue *args[])
{
	return execute1 (context, argc, args);
}