src/expression.cpp

changeset 135
8b9132fea327
parent 134
eca2fc0acaa2
child 138
a426c1039655
equal deleted inserted replaced
134:eca2fc0acaa2 135:8b9132fea327
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 Token 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
127 { 127 {
128 m_lexer->mustGetNext (Token::BracketStart); 128 m_lexer->mustGetNext (Token::BracketStart);
129 Expression expr (m_parser, m_lexer, TYPE_Int); 129 Expression expr (m_parser, m_lexer, TYPE_Int);
130 expr.getResult()->convertToBuffer(); 130 expr.getResult()->convertToBuffer();
131 DataBuffer* buf = expr.getResult()->buffer()->clone(); 131 DataBuffer* buf = expr.getResult()->buffer()->clone();
132 buf->writeDWord (DataHeader::PushGlobalArray); 132 buf->writeHeader (DataHeader::PushGlobalArray);
133 buf->writeDWord (var->index); 133 buf->writeDWord (var->index);
134 op->setBuffer (buf); 134 op->setBuffer (buf);
135 m_lexer->mustGetNext (Token::BracketEnd); 135 m_lexer->mustGetNext (Token::BracketEnd);
136 } 136 }
137 elif (var->writelevel == WRITE_Constexpr) 137 elif (var->writelevel == WRITE_Constexpr)
141 else 141 else
142 { 142 {
143 DataBuffer* buf = new DataBuffer (8); 143 DataBuffer* buf = new DataBuffer (8);
144 144
145 if (var->isGlobal()) 145 if (var->isGlobal())
146 buf->writeDWord (DataHeader::PushGlobalVar); 146 buf->writeHeader (DataHeader::PushGlobalVar);
147 else 147 else
148 buf->writeDWord (DataHeader::PushLocalVar); 148 buf->writeHeader (DataHeader::PushLocalVar);
149 149
150 buf->writeDWord (var->index); 150 buf->writeDWord (var->index);
151 op->setBuffer (buf); 151 op->setBuffer (buf);
152 } 152 }
153 153
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 (bool* verified, SymbolList::Iterator it) 231 void Expression::tryVerifyValue (List<bool>& verified, SymbolList::Iterator it)
232 { 232 {
233 // 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
234 // be verified separately. 234 // be verified separately.
235 if ((*it)->type() == EXPRSYM_Operator and 235 if ((*it)->type() == EXPRSYM_Operator and
236 g_Operators[static_cast<ExpressionOperator*> (*it)->id()].numoperands == 1) 236 g_Operators[static_cast<ExpressionOperator*> (*it)->id()].numoperands == 1)
263 } 263 }
264 264
265 if (m_type == TYPE_String) 265 if (m_type == TYPE_String)
266 error ("Cannot perform operations on strings"); 266 error ("Cannot perform operations on strings");
267 267
268 bool* verified = new bool[m_symbols.size()]; 268 List<bool> verified (m_symbols.size());
269 memset (verified, 0, m_symbols.size() * sizeof (decltype (*verified))); 269
270 for (bool& a : verified)
271 a = false;
272
270 const auto last = m_symbols.end() - 1; 273 const auto last = m_symbols.end() - 1;
271 const auto first = m_symbols.begin(); 274 const auto first = m_symbols.begin();
272 275
273 for (auto it = m_symbols.begin(); it != m_symbols.end(); ++it) 276 for (auto it = m_symbols.begin(); it != m_symbols.end(); ++it)
274 { 277 {
356 for (int i = 0; i < m_symbols.size(); ++i) 359 for (int i = 0; i < m_symbols.size(); ++i)
357 { 360 {
358 if (verified[i] == false) 361 if (verified[i] == false)
359 error ("malformed expression: expr symbol #%1 is was left unverified", i); 362 error ("malformed expression: expr symbol #%1 is was left unverified", i);
360 } 363 }
361
362 delete verified;
363 } 364 }
364 365
365 366
366 // ------------------------------------------------------------------------------------------------- 367 // -------------------------------------------------------------------------------------------------
367 // 368 //
436 DataBuffer* b1 = values[1]->buffer(); 437 DataBuffer* b1 = values[1]->buffer();
437 DataBuffer* b2 = values[2]->buffer(); 438 DataBuffer* b2 = values[2]->buffer();
438 ByteMark* mark1 = buf->addMark (""); // start of "else" case 439 ByteMark* mark1 = buf->addMark (""); // start of "else" case
439 ByteMark* mark2 = buf->addMark (""); // end of expression 440 ByteMark* mark2 = buf->addMark (""); // end of expression
440 buf->mergeAndDestroy (b0); 441 buf->mergeAndDestroy (b0);
441 buf->writeDWord (DataHeader::IfNotGoto); // if the first operand (condition) 442 buf->writeHeader (DataHeader::IfNotGoto); // if the first operand (condition)
442 buf->addReference (mark1); // didn't eval true, jump into mark1 443 buf->addReference (mark1); // didn't eval true, jump into mark1
443 buf->mergeAndDestroy (b1); // otherwise, perform second operand (true case) 444 buf->mergeAndDestroy (b1); // otherwise, perform second operand (true case)
444 buf->writeDWord (DataHeader::Goto); // afterwards, jump to the end, which is 445 buf->writeHeader (DataHeader::Goto); // afterwards, jump to the end, which is
445 buf->addReference (mark2); // marked by mark2. 446 buf->addReference (mark2); // marked by mark2.
446 buf->adjustMark (mark1); // move mark1 at the end of the true case 447 buf->adjustMark (mark1); // move mark1 at the end of the true case
447 buf->mergeAndDestroy (b2); // perform third operand (false case) 448 buf->mergeAndDestroy (b2); // perform third operand (false case)
448 buf->adjustMark (mark2); // move the ending mark2 here 449 buf->adjustMark (mark2); // move the ending mark2 here
449 450
463 // Null the pointer out so that the value's destructor will not 464 // Null the pointer out so that the value's destructor will not
464 // attempt to double-free it. 465 // attempt to double-free it.
465 val->setBuffer (null); 466 val->setBuffer (null);
466 } 467 }
467 468
468 newval->buffer()->writeDWord (info->header); 469 newval->buffer()->writeHeader (info->header);
469 } 470 }
470 } 471 }
471 else 472 else
472 { 473 {
473 // We have a constant expression. We know all the values involved and 474 // We have a constant expression. We know all the values involved and
474 // can thus compute the result of this expression on compile-time. 475 // can thus compute the result of this expression on compile-time.
475 List<int> nums; 476 List<int> nums;
476 int a; 477 int a = 0;
477 478
478 for (ExpressionValue* val : values) 479 for (ExpressionValue* val : values)
479 nums << val->value(); 480 nums << val->value();
480 481
481 switch (op->id()) 482 switch (op->id())
540 { 541 {
541 int i = it - m_symbols.begin(); 542 int i = it - m_symbols.begin();
542 List<SymbolList::Iterator> operands; 543 List<SymbolList::Iterator> operands;
543 ExpressionOperator* op = static_cast<ExpressionOperator*> (*it); 544 ExpressionOperator* op = static_cast<ExpressionOperator*> (*it);
544 const OperatorInfo* info = &g_Operators[op->id()]; 545 const OperatorInfo* info = &g_Operators[op->id()];
545 int lower, upper; // Boundaries of area to replace 546
547 // Boundaries of area to replace
548 int lower = 0;
549 int upper = 0;
546 550
547 switch (info->numoperands) 551 switch (info->numoperands)
548 { 552 {
549 case 1: 553 case 1:
550 {
551 lower = i; 554 lower = i;
552 upper = i + 1; 555 upper = i + 1;
553 operands << it + 1; 556 operands << it + 1;
554 break; 557 break;
555 }
556 558
557 case 2: 559 case 2:
558 {
559 lower = i - 1; 560 lower = i - 1;
560 upper = i + 1; 561 upper = i + 1;
561 operands << it - 1 562 operands << it - 1
562 << it + 1; 563 << it + 1;
563 break; 564 break;
564 }
565 565
566 case 3: 566 case 3:
567 {
568 lower = i - 1; 567 lower = i - 1;
569 upper = i + 3; 568 upper = i + 3;
570 operands << it - 1 569 operands << it - 1
571 << it + 1 570 << it + 1
572 << it + 3; 571 << it + 3;
573 break; 572 break;
574 }
575 573
576 default: 574 default:
577 error ("WTF bad expression with %1 operands", info->numoperands); 575 error ("WTF bad expression with %1 operands", info->numoperands);
578 } 576 }
579 577
642 640
643 switch (m_valueType) 641 switch (m_valueType)
644 { 642 {
645 case TYPE_Bool: 643 case TYPE_Bool:
646 case TYPE_Int: 644 case TYPE_Int:
647 buffer()->writeDWord (DataHeader::PushNumber); 645 buffer()->writeHeader (DataHeader::PushNumber);
648 buffer()->writeDWord (abs (value())); 646 buffer()->writeDWord (abs (value()));
649 647
650 if (value() < 0) 648 if (value() < 0)
651 buffer()->writeDWord (DataHeader::UnaryMinus); 649 buffer()->writeHeader (DataHeader::UnaryMinus);
652 break; 650 break;
653 651
654 case TYPE_String: 652 case TYPE_String:
655 buffer()->writeDWord (DataHeader::PushStringIndex); 653 buffer()->writeHeader (DataHeader::PushStringIndex);
656 buffer()->writeDWord (value()); 654 buffer()->writeDWord (value());
657 break; 655 break;
658 656
659 case TYPE_Void: 657 case TYPE_Void:
660 case TYPE_Unknown: 658 case TYPE_Unknown:

mercurial