33 {Token::DoubleAmperstand, 90, 2, DataHeader::AndLogical }, |
33 {Token::DoubleAmperstand, 90, 2, DataHeader::AndLogical }, |
34 {Token::DoubleBar, 100, 2, DataHeader::OrLogical }, |
34 {Token::DoubleBar, 100, 2, DataHeader::OrLogical }, |
35 {Token::QuestionMark, 110, 3, DataHeader::NumDataHeaders }, |
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() |
198 m_lexer->setPosition (pos); |
198 m_lexer->setPosition (pos); |
199 delete op; |
199 delete op; |
200 return null; |
200 return null; |
201 } |
201 } |
202 |
202 |
203 // ------------------------------------------------------------------------------------------------- |
203 // _________________________________________________________________________________________________ |
204 // |
204 // |
205 // The symbol parsing process only does token-based checking for operators. |
205 // The symbol parsing process only does token-based checking for operators. |
206 // Thus ALL minus operators are actually unary minuses simply because both |
206 // Thus ALL minus operators are actually unary minuses simply because both |
207 // have Token::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 |
208 // 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 |
222 if (op->id() == OPER_UnaryMinus and (*(it - 1))->type() == EXPRSYM_Value) |
222 if (op->id() == OPER_UnaryMinus and (*(it - 1))->type() == EXPRSYM_Value) |
223 op->setID (OPER_Subtraction); |
223 op->setID (OPER_Subtraction); |
224 } |
224 } |
225 } |
225 } |
226 |
226 |
227 // ------------------------------------------------------------------------------------------------- |
227 // _________________________________________________________________________________________________ |
228 // |
228 // |
229 // Verifies a single value. Helper function for Expression::verify. |
229 // Verifies a single value. Helper function for Expression::verify. |
230 // |
230 // |
231 void Expression::tryVerifyValue (List<bool>& verified, SymbolList::Iterator it) |
231 void Expression::tryVerifyValue (List<bool>& verified, SymbolList::Iterator it) |
232 { |
232 { |
245 error ("malformed expression (symbol #%1 is not a value)", i); |
245 error ("malformed expression (symbol #%1 is not a value)", i); |
246 |
246 |
247 verified[i] = true; |
247 verified[i] = true; |
248 } |
248 } |
249 |
249 |
250 // ------------------------------------------------------------------------------------------------- |
250 // _________________________________________________________________________________________________ |
251 // |
251 // |
252 // 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 |
253 // error if this is not the case. |
253 // error if this is not the case. |
254 // |
254 // |
255 void Expression::verify() |
255 void Expression::verify() |
362 error ("malformed expression: expr symbol #%1 is was left unverified", i); |
362 error ("malformed expression: expr symbol #%1 is was left unverified", i); |
363 } |
363 } |
364 } |
364 } |
365 |
365 |
366 |
366 |
367 // ------------------------------------------------------------------------------------------------- |
367 // _________________________________________________________________________________________________ |
368 // |
368 // |
369 // Which operator to evaluate? |
369 // Which operator to evaluate? |
370 // |
370 // |
371 Expression::SymbolList::Iterator Expression::findPrioritizedOperator() |
371 Expression::SymbolList::Iterator Expression::findPrioritizedOperator() |
372 { |
372 { |
389 } |
389 } |
390 |
390 |
391 return best; |
391 return best; |
392 } |
392 } |
393 |
393 |
394 // ------------------------------------------------------------------------------------------------- |
394 // _________________________________________________________________________________________________ |
395 // |
395 // |
396 // Process the given operator and values into a new value. |
396 // Process the given operator and values into a new value. |
397 // |
397 // |
398 ExpressionValue* Expression::evaluateOperator (const ExpressionOperator* op, |
398 ExpressionValue* Expression::evaluateOperator (const ExpressionOperator* op, |
399 const List<ExpressionValue*>& values) |
399 const List<ExpressionValue*>& values) |
593 ASSERT_EQ (m_symbols.first()->type(), EXPRSYM_Value) |
593 ASSERT_EQ (m_symbols.first()->type(), EXPRSYM_Value) |
594 ExpressionValue* val = static_cast<ExpressionValue*> (m_symbols.first()); |
594 ExpressionValue* val = static_cast<ExpressionValue*> (m_symbols.first()); |
595 return val; |
595 return val; |
596 } |
596 } |
597 |
597 |
598 // ------------------------------------------------------------------------------------------------- |
598 // _________________________________________________________________________________________________ |
599 // |
599 // |
600 ExpressionValue* Expression::getResult() |
600 ExpressionValue* Expression::getResult() |
601 { |
601 { |
602 return static_cast<ExpressionValue*> (m_symbols.first()); |
602 return static_cast<ExpressionValue*> (m_symbols.first()); |
603 } |
603 } |
604 |
604 |
605 // ------------------------------------------------------------------------------------------------- |
605 // _________________________________________________________________________________________________ |
606 // |
606 // |
607 String Expression::getTokenString() |
607 String Expression::getTokenString() |
608 { |
608 { |
609 return m_lexer->token()->text; |
609 return m_lexer->token()->text; |
610 } |
610 } |
611 |
611 |
612 // ------------------------------------------------------------------------------------------------- |
612 // _________________________________________________________________________________________________ |
613 // |
613 // |
614 ExpressionOperator::ExpressionOperator (ExpressionOperatorType id) : |
614 ExpressionOperator::ExpressionOperator (ExpressionOperatorType id) : |
615 ExpressionSymbol (EXPRSYM_Operator), |
615 ExpressionSymbol (EXPRSYM_Operator), |
616 m_id (id) {} |
616 m_id (id) {} |
617 |
617 |
618 // ------------------------------------------------------------------------------------------------- |
618 // _________________________________________________________________________________________________ |
619 // |
619 // |
620 ExpressionValue::ExpressionValue (DataType valuetype) : |
620 ExpressionValue::ExpressionValue (DataType valuetype) : |
621 ExpressionSymbol (EXPRSYM_Value), |
621 ExpressionSymbol (EXPRSYM_Value), |
622 m_buffer (null), |
622 m_buffer (null), |
623 m_valueType (valuetype) {} |
623 m_valueType (valuetype) {} |
624 |
624 |
625 // ------------------------------------------------------------------------------------------------- |
625 // _________________________________________________________________________________________________ |
626 // |
626 // |
627 ExpressionValue::~ExpressionValue() |
627 ExpressionValue::~ExpressionValue() |
628 { |
628 { |
629 delete m_buffer; |
629 delete m_buffer; |
630 } |
630 } |
631 |
631 |
632 // ------------------------------------------------------------------------------------------------- |
632 // _________________________________________________________________________________________________ |
633 // |
633 // |
634 void ExpressionValue::convertToBuffer() |
634 void ExpressionValue::convertToBuffer() |
635 { |
635 { |
636 if (isConstexpr() == false) |
636 if (isConstexpr() == false) |
637 return; |
637 return; |