src/expression.cpp

changeset 134
eca2fc0acaa2
parent 132
4d45b1383246
child 135
8b9132fea327
equal deleted inserted replaced
133:dbbdb870c835 134:eca2fc0acaa2
2 #include "dataBuffer.h" 2 #include "dataBuffer.h"
3 #include "lexer.h" 3 #include "lexer.h"
4 4
5 struct OperatorInfo 5 struct OperatorInfo
6 { 6 {
7 ETokenType token; 7 Token token;
8 int priority; 8 int priority;
9 int numoperands; 9 int numoperands;
10 DataHeader header; 10 DataHeader header;
11 }; 11 };
12 12
13 static const OperatorInfo g_Operators[] = 13 static const OperatorInfo g_Operators[] =
14 { 14 {
15 {TK_ExclamationMark, 0, 1, DH_NegateLogical, }, 15 {Token::ExclamationMark, 0, 1, DataHeader::NegateLogical, },
16 {TK_Minus, 0, 1, DH_UnaryMinus, }, 16 {Token::Minus, 0, 1, DataHeader::UnaryMinus, },
17 {TK_Multiply, 10, 2, DH_Multiply, }, 17 {Token::Multiply, 10, 2, DataHeader::Multiply, },
18 {TK_Divide, 10, 2, DH_Divide, }, 18 {Token::Divide, 10, 2, DataHeader::Divide, },
19 {TK_Modulus, 10, 2, DH_Modulus, }, 19 {Token::Modulus, 10, 2, DataHeader::Modulus, },
20 {TK_Plus, 20, 2, DH_Add, }, 20 {Token::Plus, 20, 2, DataHeader::Add, },
21 {TK_Minus, 20, 2, DH_Subtract, }, 21 {Token::Minus, 20, 2, DataHeader::Subtract, },
22 {TK_LeftShift, 30, 2, DH_LeftShift, }, 22 {Token::LeftShift, 30, 2, DataHeader::LeftShift, },
23 {TK_RightShift, 30, 2, DH_RightShift, }, 23 {Token::RightShift, 30, 2, DataHeader::RightShift, },
24 {TK_Lesser, 40, 2, DH_LessThan, }, 24 {Token::Lesser, 40, 2, DataHeader::LessThan, },
25 {TK_Greater, 40, 2, DH_GreaterThan, }, 25 {Token::Greater, 40, 2, DataHeader::GreaterThan, },
26 {TK_AtLeast, 40, 2, DH_AtLeast, }, 26 {Token::AtLeast, 40, 2, DataHeader::AtLeast, },
27 {TK_AtMost, 40, 2, DH_AtMost, }, 27 {Token::AtMost, 40, 2, DataHeader::AtMost, },
28 {TK_Equals, 50, 2, DH_Equals }, 28 {Token::Equals, 50, 2, DataHeader::Equals },
29 {TK_NotEquals, 50, 2, DH_NotEquals }, 29 {Token::NotEquals, 50, 2, DataHeader::NotEquals },
30 {TK_Amperstand, 60, 2, DH_AndBitwise }, 30 {Token::Amperstand, 60, 2, DataHeader::AndBitwise },
31 {TK_Caret, 70, 2, DH_EorBitwise }, 31 {Token::Caret, 70, 2, DataHeader::EorBitwise },
32 {TK_Bar, 80, 2, DH_OrBitwise }, 32 {Token::Bar, 80, 2, DataHeader::OrBitwise },
33 {TK_DoubleAmperstand, 90, 2, DH_AndLogical }, 33 {Token::DoubleAmperstand, 90, 2, DataHeader::AndLogical },
34 {TK_DoubleBar, 100, 2, DH_OrLogical }, 34 {Token::DoubleBar, 100, 2, DataHeader::OrLogical },
35 {TK_QuestionMark, 110, 3, (DataHeader) 0 }, 35 {Token::QuestionMark, 110, 3, DataHeader::NumDataHeaders },
36 }; 36 };
37 37
38 // ============================================================================= 38 // -------------------------------------------------------------------------------------------------
39 // 39 //
40 Expression::Expression (BotscriptParser* parser, Lexer* lx, DataType reqtype) : 40 Expression::Expression (BotscriptParser* parser, Lexer* lx, DataType reqtype) :
41 m_parser (parser), 41 m_parser (parser),
42 m_lexer (lx), 42 m_lexer (lx),
43 m_type (reqtype) 43 m_type (reqtype)
56 adjustOperators(); 56 adjustOperators();
57 verify(); 57 verify();
58 evaluate(); 58 evaluate();
59 } 59 }
60 60
61 // ============================================================================= 61 // -------------------------------------------------------------------------------------------------
62 // 62 //
63 Expression::~Expression() 63 Expression::~Expression()
64 { 64 {
65 for (ExpressionSymbol* sym : m_symbols) 65 for (ExpressionSymbol* sym : m_symbols)
66 delete sym; 66 delete sym;
67 } 67 }
68 68
69 // ============================================================================= 69 // -------------------------------------------------------------------------------------------------
70 // 70 //
71 // Try to parse an expression symbol (i.e. an operator or operand or a colon) 71 // Try to parse an expression symbol (i.e. an operator or operand or a colon)
72 // from the lexer. 72 // from the lexer.
73 // 73 //
74 ExpressionSymbol* Expression::parseSymbol() 74 ExpressionSymbol* Expression::parseSymbol()
75 { 75 {
76 int pos = m_lexer->position(); 76 int pos = m_lexer->position();
77 ExpressionValue* op = null; 77 ExpressionValue* op = null;
78 78
79 if (m_lexer->next (TK_Colon)) 79 if (m_lexer->next (Token::Colon))
80 return new ExpressionColon; 80 return new ExpressionColon;
81 81
82 // Check for operator 82 // Check for operator
83 for (const OperatorInfo& op : g_Operators) 83 for (const OperatorInfo& op : g_Operators)
84 {
84 if (m_lexer->next (op.token)) 85 if (m_lexer->next (op.token))
85 return new ExpressionOperator ((ExpressionOperatorType) (&op - &g_Operators[0])); 86 return new ExpressionOperator ((ExpressionOperatorType) (&op - &g_Operators[0]));
87 }
86 88
87 // Check sub-expression 89 // Check sub-expression
88 if (m_lexer->next (TK_ParenStart)) 90 if (m_lexer->next (Token::ParenStart))
89 { 91 {
90 Expression expr (m_parser, m_lexer, m_type); 92 Expression expr (m_parser, m_lexer, m_type);
91 m_lexer->mustGetNext (TK_ParenEnd); 93 m_lexer->mustGetNext (Token::ParenEnd);
92 return expr.getResult()->clone(); 94 return expr.getResult()->clone();
93 } 95 }
94 96
95 op = new ExpressionValue (m_type); 97 op = new ExpressionValue (m_type);
96 98
97 // Check function 99 // Check function
98 if (CommandInfo* comm = findCommandByName (m_lexer->peekNextString())) 100 if (CommandInfo* comm = findCommandByName (m_lexer->peekNextString()))
99 { 101 {
100 m_lexer->skip(); 102 m_lexer->skip();
101 103
102 if (m_type != TYPE_Unknown && comm->returnvalue != m_type) 104 if (m_type != TYPE_Unknown and comm->returnvalue != m_type)
103 error ("%1 returns an incompatible data type", comm->name); 105 error ("%1 returns an incompatible data type", comm->name);
104 106
105 op->setBuffer (m_parser->parseCommand (comm)); 107 op->setBuffer (m_parser->parseCommand (comm));
106 return op; 108 return op;
107 } 109 }
108 110
109 // Check for variables 111 // Check for variables
110 if (m_lexer->next (TK_DollarSign)) 112 if (m_lexer->next (Token::DollarSign))
111 { 113 {
112 m_lexer->mustGetNext (TK_Symbol); 114 m_lexer->mustGetNext (Token::Symbol);
113 Variable* var = m_parser->findVariable (getTokenString()); 115 Variable* var = m_parser->findVariable (getTokenString());
114 116
115 if (var == null) 117 if (var == null)
116 error ("unknown variable %1", getTokenString()); 118 error ("unknown variable %1", getTokenString());
117 119
118 if (var->type != m_type) 120 if (var->type != m_type)
121 {
119 error ("expression requires %1, variable $%2 is of type %3", 122 error ("expression requires %1, variable $%2 is of type %3",
120 dataTypeName (m_type), var->name, dataTypeName (var->type)); 123 dataTypeName (m_type), var->name, dataTypeName (var->type));
124 }
121 125
122 if (var->isarray) 126 if (var->isarray)
123 { 127 {
124 m_lexer->mustGetNext (TK_BracketStart); 128 m_lexer->mustGetNext (Token::BracketStart);
125 Expression expr (m_parser, m_lexer, TYPE_Int); 129 Expression expr (m_parser, m_lexer, TYPE_Int);
126 expr.getResult()->convertToBuffer(); 130 expr.getResult()->convertToBuffer();
127 DataBuffer* buf = expr.getResult()->buffer()->clone(); 131 DataBuffer* buf = expr.getResult()->buffer()->clone();
128 buf->writeDWord (DH_PushGlobalArray); 132 buf->writeDWord (DataHeader::PushGlobalArray);
129 buf->writeDWord (var->index); 133 buf->writeDWord (var->index);
130 op->setBuffer (buf); 134 op->setBuffer (buf);
131 m_lexer->mustGetNext (TK_BracketEnd); 135 m_lexer->mustGetNext (Token::BracketEnd);
132 } 136 }
133 elif (var->writelevel == WRITE_Constexpr) 137 elif (var->writelevel == WRITE_Constexpr)
138 {
134 op->setValue (var->value); 139 op->setValue (var->value);
140 }
135 else 141 else
136 { 142 {
137 DataBuffer* buf = new DataBuffer (8); 143 DataBuffer* buf = new DataBuffer (8);
138 144
139 if (var->IsGlobal()) 145 if (var->isGlobal())
140 buf->writeDWord (DH_PushGlobalVar); 146 buf->writeDWord (DataHeader::PushGlobalVar);
141 else 147 else
142 buf->writeDWord (DH_PushLocalVar); 148 buf->writeDWord (DataHeader::PushLocalVar);
143 149
144 buf->writeDWord (var->index); 150 buf->writeDWord (var->index);
145 op->setBuffer (buf); 151 op->setBuffer (buf);
146 } 152 }
147 153
152 switch (m_type) 158 switch (m_type)
153 { 159 {
154 case TYPE_Void: 160 case TYPE_Void:
155 case TYPE_Unknown: 161 case TYPE_Unknown:
156 { 162 {
157 error ("unknown identifier `%1` (expected keyword, function or variable)", getTokenString()); 163 error ("unknown identifier `%1` (expected keyword, function or variable)",
164 getTokenString());
158 break; 165 break;
159 } 166 }
160 167
161 case TYPE_Bool: 168 case TYPE_Bool:
162 { 169 {
163 if (m_lexer->next (TK_True) || m_lexer->next (TK_False)) 170 if (m_lexer->next (Token::True) or m_lexer->next (Token::False))
164 { 171 {
165 ETokenType tt = m_lexer->tokenType(); 172 Token tt = m_lexer->tokenType();
166 op->setValue (tt == TK_True ? 1 : 0); 173 op->setValue (tt == Token::True ? 1 : 0);
167 return op; 174 return op;
168 } 175 }
169 } 176 }
170 177
171 case TYPE_Int: 178 case TYPE_Int:
172 { 179 {
173 if (m_lexer->next (TK_Number)) 180 if (m_lexer->next (Token::Number))
174 { 181 {
175 op->setValue (getTokenString().toLong()); 182 op->setValue (getTokenString().toLong());
176 return op; 183 return op;
177 } 184 }
178 } 185 }
179 186
180 case TYPE_String: 187 case TYPE_String:
181 { 188 {
182 if (m_lexer->next (TK_String)) 189 if (m_lexer->next (Token::String))
183 { 190 {
184 op->setValue (getStringTableIndex (getTokenString())); 191 op->setValue (getStringTableIndex (getTokenString()));
185 return op; 192 return op;
186 } 193 }
187 } 194 }
191 m_lexer->setPosition (pos); 198 m_lexer->setPosition (pos);
192 delete op; 199 delete op;
193 return null; 200 return null;
194 } 201 }
195 202
196 // ============================================================================= 203 // -------------------------------------------------------------------------------------------------
197 // 204 //
198 // The symbol parsing process only does token-based checking for operators. 205 // The symbol parsing process only does token-based checking for operators.
199 // Thus ALL minus operators are actually unary minuses simply because both 206 // Thus ALL minus operators are actually unary minuses simply because both
200 // have TK_Minus as their token and the unary minus is prior to the binary minus 207 // have Token::Minus as their token and the unary minus is prior to the binary minus
201 // in the operator table. Now that we have all symbols present, we can 208 // in the operator table. Now that we have all symbols present, we can
202 // correct cases like this. 209 // correct cases like this.
203 // 210 //
204 void Expression::adjustOperators() 211 void Expression::adjustOperators()
205 { 212 {
210 217
211 ExpressionOperator* op = static_cast<ExpressionOperator*> (*it); 218 ExpressionOperator* op = static_cast<ExpressionOperator*> (*it);
212 219
213 // Unary minus with a value as the previous symbol cannot really be 220 // Unary minus with a value as the previous symbol cannot really be
214 // unary; replace with binary minus. 221 // unary; replace with binary minus.
215 if (op->id() == OPER_UnaryMinus && (*(it - 1))->type() == EXPRSYM_Value) 222 if (op->id() == OPER_UnaryMinus and (*(it - 1))->type() == EXPRSYM_Value)
216 op->setID (OPER_Subtraction); 223 op->setID (OPER_Subtraction);
217 } 224 }
218 } 225 }
219 226
220 // ============================================================================= 227 // -------------------------------------------------------------------------------------------------
221 // 228 //
222 // Verifies a single value. Helper function for Expression::verify. 229 // Verifies a single value. Helper function for Expression::verify.
223 // 230 //
224 void Expression::tryVerifyValue (bool* verified, SymbolList::Iterator it) 231 void Expression::tryVerifyValue (bool* verified, SymbolList::Iterator it)
225 { 232 {
226 // If it's an unary operator we skip to its value. The actual operator will 233 // If it's an unary operator we skip to its value. The actual operator will
227 // be verified separately. 234 // be verified separately.
228 if ((*it)->type() == EXPRSYM_Operator && 235 if ((*it)->type() == EXPRSYM_Operator and
229 g_Operators[static_cast<ExpressionOperator*> (*it)->id()].numoperands == 1) 236 g_Operators[static_cast<ExpressionOperator*> (*it)->id()].numoperands == 1)
230 { 237 {
231 ++it; 238 ++it;
232 } 239 }
233 240
238 error ("malformed expression (symbol #%1 is not a value)", i); 245 error ("malformed expression (symbol #%1 is not a value)", i);
239 246
240 verified[i] = true; 247 verified[i] = true;
241 } 248 }
242 249
243 // ============================================================================= 250 // -------------------------------------------------------------------------------------------------
244 // 251 //
245 // Ensures the expression is valid and well-formed and not OMGWTFBBQ. Throws an 252 // Ensures the expression is valid and well-formed and not OMGWTFBBQ. Throws an
246 // error if this is not the case. 253 // error if this is not the case.
247 // 254 //
248 void Expression::verify() 255 void Expression::verify()
281 // - unary operator is not the last symbol 288 // - unary operator is not the last symbol
282 // - unary operator is succeeded by a value symbol 289 // - unary operator is succeeded by a value symbol
283 // - neither symbol overlaps with something already verified 290 // - neither symbol overlaps with something already verified
284 tryVerifyValue (verified, it + 1); 291 tryVerifyValue (verified, it + 1);
285 292
286 if (it == last || verified[i] == true) 293 if (it == last or verified[i] == true)
287 error ("malformed expression"); 294 error ("ill-formed expression");
288 295
289 verified[i] = true; 296 verified[i] = true;
290 break; 297 break;
291 } 298 }
292 299
296 // - binary operator is not the first or last symbol 303 // - binary operator is not the first or last symbol
297 // - is preceded and succeeded by values 304 // - is preceded and succeeded by values
298 // - none of the three tokens are already verified 305 // - none of the three tokens are already verified
299 // 306 //
300 // Basically similar logic as above. 307 // Basically similar logic as above.
301 if (it == first || it == last || verified[i] == true) 308 if (it == first or it == last or verified[i] == true)
302 error ("malformed expression"); 309 error ("ill-formed expression");
303 310
304 tryVerifyValue (verified, it + 1); 311 tryVerifyValue (verified, it + 1);
305 tryVerifyValue (verified, it - 1); 312 tryVerifyValue (verified, it - 1);
306 verified[i] = true; 313 verified[i] = true;
307 break; 314 break;
325 // 332 //
326 tryVerifyValue (verified, it - 1); 333 tryVerifyValue (verified, it - 1);
327 tryVerifyValue (verified, it + 1); 334 tryVerifyValue (verified, it + 1);
328 tryVerifyValue (verified, it + 3); 335 tryVerifyValue (verified, it + 3);
329 336
330 if (it == first || 337 if (it == first
331 it >= m_symbols.end() - 3 || 338 or it >= m_symbols.end() - 3
332 verified[i] == true || 339 or verified[i] == true
333 verified[i + 2] == true || 340 or verified[i + 2] == true
334 (*(it + 2))->type() != EXPRSYM_Colon) 341 or (*(it + 2))->type() != EXPRSYM_Colon)
335 { 342 {
336 error ("malformed expression"); 343 error ("ill-formed expression");
337 } 344 }
338 345
339 verified[i] = true; 346 verified[i] = true;
340 verified[i + 2] = true; 347 verified[i + 2] = true;
341 break; 348 break;
345 error ("WTF operator with %1 operands", numoperands); 352 error ("WTF operator with %1 operands", numoperands);
346 } 353 }
347 } 354 }
348 355
349 for (int i = 0; i < m_symbols.size(); ++i) 356 for (int i = 0; i < m_symbols.size(); ++i)
357 {
350 if (verified[i] == false) 358 if (verified[i] == false)
351 error ("malformed expression: expr symbol #%1 is was left unverified", i); 359 error ("malformed expression: expr symbol #%1 is was left unverified", i);
360 }
352 361
353 delete verified; 362 delete verified;
354 } 363 }
355 364
356 365
357 // ============================================================================= 366 // -------------------------------------------------------------------------------------------------
358 // 367 //
359 // Which operator to evaluate? 368 // Which operator to evaluate?
360 // 369 //
361 Expression::SymbolList::Iterator Expression::findPrioritizedOperator() 370 Expression::SymbolList::Iterator Expression::findPrioritizedOperator()
362 { 371 {
379 } 388 }
380 389
381 return best; 390 return best;
382 } 391 }
383 392
384 // ============================================================================= 393 // -------------------------------------------------------------------------------------------------
385 // 394 //
386 // Process the given operator and values into a new value. 395 // Process the given operator and values into a new value.
387 // 396 //
388 ExpressionValue* Expression::evaluateOperator (const ExpressionOperator* op, 397 ExpressionValue* Expression::evaluateOperator (const ExpressionOperator* op,
389 const List<ExpressionValue*>& values) 398 const List<ExpressionValue*>& values)
390 { 399 {
391 const OperatorInfo* info = &g_Operators[op->id()]; 400 const OperatorInfo* info = &g_Operators[op->id()];
392 bool isconstexpr = true; 401 bool isconstexpr = true;
393 ASSERT_EQ (values.size(), info->numoperands) 402 ASSERT_EQ (values.size(), info->numoperands)
394 403
404 // See whether the values are constexpr
395 for (ExpressionValue* val : values) 405 for (ExpressionValue* val : values)
396 { 406 {
397 if (val->isConstexpr() == false) 407 if (not val->isConstexpr())
398 { 408 {
399 isconstexpr = false; 409 isconstexpr = false;
400 break; 410 break;
401 } 411 }
402 } 412 }
403 413
404 // If not all of the values are constant expressions, none of them shall be. 414 // If not all of the values are constexpr, none of them shall be.
405 if (isconstexpr == false) 415 if (not isconstexpr)
416 {
406 for (ExpressionValue* val : values) 417 for (ExpressionValue* val : values)
407 val->convertToBuffer(); 418 val->convertToBuffer();
419 }
408 420
409 ExpressionValue* newval = new ExpressionValue (m_type); 421 ExpressionValue* newval = new ExpressionValue (m_type);
410 422
411 if (isconstexpr == false) 423 if (isconstexpr == false)
412 { 424 {
415 // until Zandronum processes it at run-time. 427 // until Zandronum processes it at run-time.
416 newval->setBuffer (new DataBuffer); 428 newval->setBuffer (new DataBuffer);
417 429
418 if (op->id() == OPER_Ternary) 430 if (op->id() == OPER_Ternary)
419 { 431 {
420 // There isn't a dataheader for ternary operator. Instead, we use DH_IfNotGoto 432 // There isn't a dataheader for ternary operator. Instead, we use DataHeader::IfNotGoto
421 // to create an "if-block" inside an expression. 433 // to create an "if-block" inside an expression. Behold, big block of writing madness!
422 // Behold, big block of writing madness! :P
423 //
424 DataBuffer* buf = newval->buffer(); 434 DataBuffer* buf = newval->buffer();
425 DataBuffer* b0 = values[0]->buffer(); 435 DataBuffer* b0 = values[0]->buffer();
426 DataBuffer* b1 = values[1]->buffer(); 436 DataBuffer* b1 = values[1]->buffer();
427 DataBuffer* b2 = values[2]->buffer(); 437 DataBuffer* b2 = values[2]->buffer();
428 ByteMark* mark1 = buf->addMark (""); // start of "else" case 438 ByteMark* mark1 = buf->addMark (""); // start of "else" case
429 ByteMark* mark2 = buf->addMark (""); // end of expression 439 ByteMark* mark2 = buf->addMark (""); // end of expression
430 buf->mergeAndDestroy (b0); 440 buf->mergeAndDestroy (b0);
431 buf->writeDWord (DH_IfNotGoto); // if the first operand (condition) 441 buf->writeDWord (DataHeader::IfNotGoto); // if the first operand (condition)
432 buf->addReference (mark1); // didn't eval true, jump into mark1 442 buf->addReference (mark1); // didn't eval true, jump into mark1
433 buf->mergeAndDestroy (b1); // otherwise, perform second operand (true case) 443 buf->mergeAndDestroy (b1); // otherwise, perform second operand (true case)
434 buf->writeDWord (DH_Goto); // afterwards, jump to the end, which is 444 buf->writeDWord (DataHeader::Goto); // afterwards, jump to the end, which is
435 buf->addReference (mark2); // marked by mark2. 445 buf->addReference (mark2); // marked by mark2.
436 buf->adjustMark (mark1); // move mark1 at the end of the true case 446 buf->adjustMark (mark1); // move mark1 at the end of the true case
437 buf->mergeAndDestroy (b2); // perform third operand (false case) 447 buf->mergeAndDestroy (b2); // perform third operand (false case)
438 buf->adjustMark (mark2); // move the ending mark2 here 448 buf->adjustMark (mark2); // move the ending mark2 here
439 449
440 for (int i = 0; i < 3; ++i) 450 for (int i = 0; i < 3; ++i)
441 values[i]->setBuffer (null); 451 values[i]->setBuffer (null);
442 } 452 }
443 else 453 else
444 { 454 {
455 ASSERT_NE (info->header, DataHeader::NumDataHeaders);
456
445 // Generic case: write all arguments and apply the operator's 457 // Generic case: write all arguments and apply the operator's
446 // data header. 458 // data header.
447 for (ExpressionValue* val : values) 459 for (ExpressionValue* val : values)
448 { 460 {
449 newval->buffer()->mergeAndDestroy (val->buffer()); 461 newval->buffer()->mergeAndDestroy (val->buffer());
482 case OPER_CompareEquals: a = (nums[0] == nums[1]) ? 1 : 0; break; 494 case OPER_CompareEquals: a = (nums[0] == nums[1]) ? 1 : 0; break;
483 case OPER_CompareNotEquals: a = (nums[0] != nums[1]) ? 1 : 0; break; 495 case OPER_CompareNotEquals: a = (nums[0] != nums[1]) ? 1 : 0; break;
484 case OPER_BitwiseAnd: a = nums[0] & nums[1]; break; 496 case OPER_BitwiseAnd: a = nums[0] & nums[1]; break;
485 case OPER_BitwiseOr: a = nums[0] | nums[1]; break; 497 case OPER_BitwiseOr: a = nums[0] | nums[1]; break;
486 case OPER_BitwiseXOr: a = nums[0] ^ nums[1]; break; 498 case OPER_BitwiseXOr: a = nums[0] ^ nums[1]; break;
487 case OPER_LogicalAnd: a = (nums[0] && nums[1]) ? 1 : 0; break; 499 case OPER_LogicalAnd: a = (nums[0] and nums[1]) ? 1 : 0; break;
488 case OPER_LogicalOr: a = (nums[0] || nums[1]) ? 1 : 0; break; 500 case OPER_LogicalOr: a = (nums[0] or nums[1]) ? 1 : 0; break;
489 case OPER_Ternary: a = (nums[0] != 0) ? nums[1] : nums[2]; break; 501 case OPER_Ternary: a = (nums[0] != 0) ? nums[1] : nums[2]; break;
490 502
491 case OPER_Division: 503 case OPER_Division:
492 { 504 {
493 if (nums[1] == 0) 505 if (nums[1] == 0)
516 528
517 delete op; 529 delete op;
518 return newval; 530 return newval;
519 } 531 }
520 532
521 // ============================================================================= 533 // -------------------------------------------------------------------------------------------------
522 // 534 //
523 ExpressionValue* Expression::evaluate() 535 ExpressionValue* Expression::evaluate()
524 { 536 {
525 SymbolList::Iterator it; 537 SymbolList::Iterator it;
526 538
583 ASSERT_EQ (m_symbols.first()->type(), EXPRSYM_Value) 595 ASSERT_EQ (m_symbols.first()->type(), EXPRSYM_Value)
584 ExpressionValue* val = static_cast<ExpressionValue*> (m_symbols.first()); 596 ExpressionValue* val = static_cast<ExpressionValue*> (m_symbols.first());
585 return val; 597 return val;
586 } 598 }
587 599
588 // ============================================================================= 600 // -------------------------------------------------------------------------------------------------
589 // 601 //
590 ExpressionValue* Expression::getResult() 602 ExpressionValue* Expression::getResult()
591 { 603 {
592 return static_cast<ExpressionValue*> (m_symbols.first()); 604 return static_cast<ExpressionValue*> (m_symbols.first());
593 } 605 }
594 606
595 // ============================================================================= 607 // -------------------------------------------------------------------------------------------------
596 // 608 //
597 String Expression::getTokenString() 609 String Expression::getTokenString()
598 { 610 {
599 return m_lexer->token()->text; 611 return m_lexer->token()->text;
600 } 612 }
601 613
602 // ============================================================================= 614 // -------------------------------------------------------------------------------------------------
603 // 615 //
604 ExpressionOperator::ExpressionOperator (ExpressionOperatorType id) : 616 ExpressionOperator::ExpressionOperator (ExpressionOperatorType id) :
605 ExpressionSymbol (EXPRSYM_Operator), 617 ExpressionSymbol (EXPRSYM_Operator),
606 m_id (id) {} 618 m_id (id) {}
607 619
608 // ============================================================================= 620 // -------------------------------------------------------------------------------------------------
609 // 621 //
610 ExpressionValue::ExpressionValue (DataType valuetype) : 622 ExpressionValue::ExpressionValue (DataType valuetype) :
611 ExpressionSymbol (EXPRSYM_Value), 623 ExpressionSymbol (EXPRSYM_Value),
612 m_buffer (null), 624 m_buffer (null),
613 m_valueType (valuetype) {} 625 m_valueType (valuetype) {}
614 626
615 // ============================================================================= 627 // -------------------------------------------------------------------------------------------------
616 // 628 //
617 ExpressionValue::~ExpressionValue() 629 ExpressionValue::~ExpressionValue()
618 { 630 {
619 delete m_buffer; 631 delete m_buffer;
620 } 632 }
621 633
622 // ============================================================================= 634 // -------------------------------------------------------------------------------------------------
623 // 635 //
624 void ExpressionValue::convertToBuffer() 636 void ExpressionValue::convertToBuffer()
625 { 637 {
626 if (isConstexpr() == false) 638 if (isConstexpr() == false)
627 return; 639 return;
630 642
631 switch (m_valueType) 643 switch (m_valueType)
632 { 644 {
633 case TYPE_Bool: 645 case TYPE_Bool:
634 case TYPE_Int: 646 case TYPE_Int:
635 buffer()->writeDWord (DH_PushNumber); 647 buffer()->writeDWord (DataHeader::PushNumber);
636 buffer()->writeDWord (abs (value())); 648 buffer()->writeDWord (abs (value()));
637 649
638 if (value() < 0) 650 if (value() < 0)
639 buffer()->writeDWord (DH_UnaryMinus); 651 buffer()->writeDWord (DataHeader::UnaryMinus);
640 break; 652 break;
641 653
642 case TYPE_String: 654 case TYPE_String:
643 buffer()->writeDWord (DH_PushStringIndex); 655 buffer()->writeDWord (DataHeader::PushStringIndex);
644 buffer()->writeDWord (value()); 656 buffer()->writeDWord (value());
645 break; 657 break;
646 658
647 case TYPE_Void: 659 case TYPE_Void:
648 case TYPE_Unknown: 660 case TYPE_Unknown:

mercurial