1 /**********************************************************
2  * Version $Id$
3  *********************************************************/
4 //#include <..\stdafx.h>
5 
6 #include "foreach.h"
7 #include "bedingung.h"
8 
9 
10 using namespace std;
11 
BBAnweisung()12 BBAnweisung::BBAnweisung()
13 {
14 	typ = Zuweisung;
15 	memset(&AnweisungVar, 0, sizeof(T_AnweisungVar));
16 }
17 
~BBAnweisung()18 BBAnweisung::~BBAnweisung()
19 {
20 	switch(typ)
21 	{
22 	case ForEach:
23 		if (AnweisungVar.For != NULL)
24 			delete AnweisungVar.For;
25 		break;
26 	case IF:
27 		if (AnweisungVar.IF != NULL)
28 			delete AnweisungVar.IF;
29 		break;
30 	case Zuweisung:
31 		if (AnweisungVar.Zu != NULL)
32 			delete AnweisungVar.Zu;
33 		break;
34 	case Funktion:
35 		if (AnweisungVar.Fkt != NULL)
36 			delete AnweisungVar.Fkt;
37 		break;
38 	}
39 	memset(&AnweisungVar, 0, sizeof(T_AnweisungVar));
40 }
41 
BBForEach()42 BBForEach::BBForEach()
43 {
44 	M = 0;
45 	P = 0;
46 	N = 0;
47 	type = Point;
48 }
49 
~BBForEach()50 BBForEach::~BBForEach()
51 {
52 //	if (M != NULL)
53 //		delete M;
54 //	if (P != NULL)
55 //		delete P;
56 //	if (N != NULL)
57 //		delete N;
58 //	M = 0;
59 //	P = 0;
60 //	N = 0;
61 	DeleteAnweisungList(z);
62 }
63 
DeleteAnweisungList(T_AnweisungList & a)64 void DeleteAnweisungList(T_AnweisungList& a)
65 {
66 	T_AnweisungList::iterator it;
67 	for (it = a.begin(); it != a.end(); it++)
68 	{
69 		if (*it != NULL)
70 			delete *it;
71 	}
72 	a.clear();
73 }
74 
getNextToken(const string & ss,int & pos,string & erg)75 bool getNextToken(const string& ss, int& pos, string& erg)
76 {
77 	if (pos >= ss.size())
78 		return false;
79 
80 	string s = ss;
81 	erg = ss.substr(pos);
82 
83 	WhiteSpace(erg, pos);
84 	WhiteSpace(erg, pos, false);
85 	pos += erg.size();
86 	return true;
87 }
88 
getNextChar(const string & ss,int & pos,char & c)89 bool getNextChar(const string& ss, int& pos, char& c)
90 {
91 	string s = ss.substr(pos);
92 	WhiteSpace(s, pos);
93 	pos++;
94 	c = s[0];
95 	return true;
96 }
97 
getStringBetweenKlammer(const string & s,int & pos)98 bool getStringBetweenKlammer(const string& s, int& pos)
99 {
100 
101 	if (pos >= s.size())
102 		return false;
103 
104 	int klammer_ebene = 1;
105 
106 	for (int i=pos; i<s.size(); i++)
107 	{
108 		if (s[i] == '{')
109 			klammer_ebene++;
110 		if (s[i] == '}')
111 			klammer_ebene--;
112 		if (klammer_ebene == 0)
113 		{
114 			pos = i;
115 			return true;
116 		}
117 	}
118 	return false;
119 }
120 
isForEach(const string & ins,int & pos,BBForEach * & f,string & anweisungen)121 bool isForEach(const string& ins, int& pos, BBForEach *& f, string& anweisungen)
122 {
123 	// pos wird auf  { gesetzt
124 	//
125 	//
126 	// Syntax:			foreach p in M do { ... }
127 	//					foreachn n of p in M do { ... }
128 	string s;
129 	BBPoint *p1, *p2;
130 	BBMatrix *m;
131 	bool isPoint;
132 
133 	if (!getNextToken(ins, pos, s))
134 		return false;
135 	trim(s);
136 	// foreach
137 	if (s == "foreach")
138 		isPoint = true;
139 	else if (s == "foreachn")
140 		isPoint = false;
141 	else
142 		return false;
143 
144 	if (!getNextToken(ins, pos, s))
145 		return false;
146 	trim(s);
147 	BBTyp *bt = isVar(s);
148 	if (bt == NULL)
149 		return false;
150 	// p/n
151 	if (isPVar(s, bt))
152 		p1 = getVarP(bt);
153 	else
154 		return false;
155 
156 	if (!getNextToken(ins, pos, s))
157 		return false;
158 	trim(s);
159 	// of
160 	if (s == "of")
161 	{
162 		if (isPoint)
163 			return false;
164 		if (!getNextToken(ins, pos, s))
165 			return false;
166 		trim(s);
167 		BBTyp *bt1 = isVar(s);
168 		if (bt1 == NULL)
169 			return false;
170 		// p
171 		if (isPVar(s, bt1))
172 			p2 = getVarP(bt1);
173 		else
174 			return false;
175 
176 		if (!getNextToken(ins, pos, s)) // n�chstes "of" holen
177 			return false;
178 		trim(s);
179 	}
180 	else if (!isPoint)  // "foreachn" ohne "of" ist Fehler
181 		return false;
182 
183 	// in
184 	if (s != "in")
185 		return false;
186 
187 	if (!getNextToken(ins, pos, s))
188 		return false;
189 
190 	// M
191 	bt = isVar(s);
192 	if (bt == NULL)
193 		return false;
194 	if (isMVar(s, bt))
195 		m = getVarM(bt);
196 	else
197 		return false;
198 
199 	char c;
200 	// do
201 	getNextChar(ins, pos, c);
202 	if (c != 'd')
203 		return false;
204 	getNextChar(ins, pos, c);
205 	if (c != 'o')
206 		return false;
207 
208 	// Klammer-Paar finden { .. }
209 	getNextChar(ins, pos, c);
210 	if (c != '{')
211 		return false;
212 	int p = pos;
213 	if (!getStringBetweenKlammer(ins, p))
214 		return false;
215 
216 	// alles OK
217 
218 	anweisungen = ins.substr(pos, p-pos);
219 	f = new BBForEach;
220 	f->type = (isPoint ? BBForEach::Point : BBForEach::Nachbar);
221 	f->M = m;
222 	f->P = p1;
223 	if (!isPoint)
224 	{
225 		f->P = p2;
226 		f->N = p1;
227 	}
228 	return true;
229 }
230 
231 
232