File: Expression.cc

package info (click to toggle)
fastdep 0.16-7
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 596 kB
  • ctags: 323
  • sloc: cpp: 2,144; ansic: 814; sh: 208; makefile: 124
file content (309 lines) | stat: -rw-r--r-- 5,098 bytes parent folder | download | duplicates (5)
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include "Expression.h"
#include "CompileState.h"

Expression::Expression(const std::string& anExpression, CompileState* aState)
: Text(anExpression), theState(aState), Pos(0)
{
}

int Expression::evaluate()
{
	Pos = 0;
	ws();
	int Result = boolean();
	if (Pos < Text.length())
		// TODO error
		;
	return Result;
}

int Expression::boolean() 
{
	int Left = equality();
	if (Pos == Text.length()) return Left;

	if (Text[Pos] == '|')
	{
		++Pos;
		if ( (Pos == Text.length()) || (Text[Pos] != '|') )
			//  TODO error
			return Left;
		++Pos;
		ws();
		int Right = equality();
		return Left || Right;
	}	
	if (Text[Pos] == '&')
	{
		++Pos;
		if ( (Pos == Text.length()) || (Text[Pos] != '&') )
			//  TODO error
			return Left;
		++Pos;
		ws();
		int Right = equality();
		return Left && Right;
	}
	return Left;
}

int Expression::equality()
{
	int Left = relational();
	if (Pos == Text.length()) return Left;
	if (Text[Pos] == '=')
	{
		++Pos;
		if ( (Pos == Text.length()) || (Text[Pos] != '=') )
			//  TODO error
			return Left;
		++Pos;
		ws();
		int Right = relational();
		return Left == Right;
	}	
	if (Text[Pos] == '!')
	{
		++Pos;
		if ( (Pos == Text.length()) || (Text[Pos] != '=') )
			//  TODO error
			return Left;
		++Pos;
		ws();
		int Right = relational();
		return Left != Right;
	}
	return Left;
}

int Expression::relational()
{
	int Left = additive();
	if (Pos == Text.length()) return Left;
	if (Text[Pos] == '<')
	{
		++Pos;
		if ( (Pos < Text.length()) && (Text[Pos] == '=') )
		{
			++Pos;
			ws();
			if (Pos == Text.length())
				return Left; // TODO error
			int Right = additive();
			return Left <= Right;
		}
		ws();
		if (Pos == Text.length())
			return Left; // TODO error
		int Right = additive();
		return Left < Right;
	}	
	if (Text[Pos] == '>')
	{
		++Pos;
		if ( (Pos < Text.length()) && (Text[Pos] == '=') )
		{
			++Pos;
			ws();
			if (Pos == Text.length())
				return Left; // TODO error
			int Right = additive();
			return Left >= Right;
		}
		ws();
		if (Pos == Text.length())
			return Left; // TODO error
		int Right = additive();
		return Left > Right;
	}
	return Left;
}

int Expression::additive()
{
	int Left = multiplicative();	
	while (Pos < Text.length())
		if (Text[Pos] == '+')
		{
			++Pos;
			ws();
			int Right = multiplicative();
			Left += Right;
		}
		else if (Text[Pos] == '-')
		{
			++Pos;
			ws();
			int Right = multiplicative();
			Left -= Right;
		}
		else
			break;
	return Left;
}

int Expression::multiplicative()
{
	int Left = factor();
	while (Pos < Text.length())
		if (Text[Pos] == '*')
		{
			++Pos;
			ws();
			int Right = factor();
			Left *= Right;
		}
		else if (Text[Pos] == '/')
		{
			++Pos;
			ws();
			int Right = factor();
			if (Right == 0)
				// TODO error
				Left = 0;
			else
				Left /= Right;
		}
		else
			break;
	return Left;
}

int Expression::factor()
{
	if (Pos == Text.length()) return 0;
	if (Text[Pos] == '!')
	{
		++Pos;
		ws();
		return !factor();
	}
	if (Text[Pos] == '-')
	{
		++Pos;
		ws();
		return -factor();
	}
	if (Text[Pos] == '(')
	{
		++Pos;
		ws();
		int Result = factor();
		if ( (Pos == Text.length()) || (Text[Pos] != ')') )
			// TODO error
			;
		else
		{
			++Pos;
			ws();
		}
		return Result;
	}
	if ( (Text[Pos] >= '0') && (Text[Pos] <= '9') )
	{
		int Result = Text[Pos]-'0';
		++Pos;
		while ( (Pos<Text.length()) && ((Text[Pos] >= '0') && (Text[Pos] <= '9')) )
			Result = Result*10 + Text[Pos++]-'0';
		ws();
		return Result;
	}
	if (token("defined"))
	{
		if ( (Pos == Text.length()) || (Text[Pos] != '('))
			// TODO error
			return 0;
		++Pos;
		ws();
		unsigned int To = Pos;
		while (To < Text.length())
		{
			char c=Text[To];
			if ( ((c >= 'a') && (c <= 'z')) ||
				  ((c >= 'A') && (c <= 'Z')) ||
				  ((c >= '0') && (c <= '9')) ||
				  c == '_')
				++To;
			else
				break;
		}
		int Result = 0;
		if (Pos < To)
		{
			std::string Macro(Text,Pos,To-Pos);
			if (theState->isDefined(Macro))
				Result = 1;
			Pos = To;
		}
		if ( (Pos == Text.length()) || (Text[Pos] != ')'))
			// TODO error
			;
		else
		{
			++Pos;
			ws();
		}
		return Result;
	}
	// TODO error
	return macro();
}

int Expression::macro()
{
	std::string  Name;
	while (Pos < Text.length())
	{
		char c = Text[Pos];
		if ( ((c >= 'a') && (c <= 'z')) ||
			  ((c >= 'A') && (c <= 'Z')) ||
			  ((c >= '0') && (c <= '9')) ||
			  c == '_')
		{
			Name += c;
			++Pos;
		}
		else
			break;
	}
	if (Name != "")
	{
		ws();
		Expression E(theState->getContent(Name),theState);
		return E.evaluate();	
	}
	else
		// TODO error
		return 0;
}

void Expression::ws()
{
	while ( (Pos < Text.length()) && (Text[Pos] == ' ') )
		++Pos;
}

bool Expression::token(const char* aToken)
{
	unsigned int OrigPos = Pos;
	for (int i=0; aToken[i]; ++i,++Pos)
		if ( (Pos == Text.length()) || (Text[Pos] != aToken[i]) )
		{
			Pos = OrigPos;
			return false;
		}
	if (Pos < Text.length())
	{
		char c=Text[Pos];
		if ( ((c >= 'a') && (c <= 'z')) ||
		     ((c >= 'A') && (c <= 'Z')) ||
		     ((c >= '0') && (c <= '9')) ||
			  c == '_')
		{
			Pos = OrigPos;
			return false;
		}
	}
	ws();
	return true;
}