531 // ============================================================================ |
531 // ============================================================================ |
532 // Parses an expression, potentially recursively |
532 // Parses an expression, potentially recursively |
533 DataBuffer* ScriptReader::ParseExpression (int reqtype) { |
533 DataBuffer* ScriptReader::ParseExpression (int reqtype) { |
534 DataBuffer* retbuf = new DataBuffer (64); |
534 DataBuffer* retbuf = new DataBuffer (64); |
535 |
535 |
536 DataBuffer* lb = NULL; |
536 // Parse first operand |
537 |
537 retbuf->Merge (ParseExprValue (reqtype)); |
538 lb = ParseExprValue (reqtype); |
538 |
539 |
539 // Parse any and all operators we get |
540 // Get an operator |
540 int oper; |
541 int oper = ParseOperator (true); |
541 while ((oper = ParseOperator (true)) != -1) { |
542 |
542 // We peeked the operator, move forward now |
543 // No operator found - stop here. |
543 MustNext(); |
544 if (oper == -1) { |
544 |
545 retbuf->Merge (lb); |
545 // Can't be an assignement operator, those belong in assignments. |
546 return retbuf; |
546 if (IsAssignmentOperator (oper)) |
547 } |
547 ParserError ("assignment operator inside expressions"); |
548 |
548 |
549 // We peeked the operator, move forward now |
549 // Parse the right operand, |
550 MustNext(); |
550 MustNext (); |
551 |
551 DataBuffer* rb = ParseExprValue (reqtype); |
552 // Can't be an assignement operator, those belong in assignments. |
552 |
553 if (IsAssignmentOperator (oper)) |
553 // Write to buffer |
554 ParserError ("assignment operator inside expressions"); |
554 retbuf->Merge (rb); |
555 |
555 long dh = DataHeaderByOperator (NULL, oper); |
556 // Parse the right operand, |
556 retbuf->Write<word> (dh); |
557 MustNext (); |
557 } |
558 DataBuffer* rb = ParseExprValue (reqtype); |
558 |
559 |
|
560 retbuf->Merge (lb); |
|
561 retbuf->Merge (rb); |
|
562 |
|
563 long dh = DataHeaderByOperator (NULL, oper); |
|
564 retbuf->Write<word> (dh); |
|
565 return retbuf; |
559 return retbuf; |
566 } |
560 } |
567 |
561 |
568 // ============================================================================ |
562 // ============================================================================ |
569 // `arses an operator string. Returns the operator number code. |
563 // Parses an operator string. Returns the operator number code. |
570 int ScriptReader::ParseOperator (bool peek) { |
564 int ScriptReader::ParseOperator (bool peek) { |
571 str oper; |
565 str oper; |
572 if (peek) |
566 if (peek) |
573 oper += PeekNext (); |
567 oper += PeekNext (); |
574 else |
568 else |
683 if (g_CurMode == MODE_TOPLEVEL) // TODO: lift this restriction |
677 if (g_CurMode == MODE_TOPLEVEL) // TODO: lift this restriction |
684 ParserError ("can't alter variables at top level"); |
678 ParserError ("can't alter variables at top level"); |
685 |
679 |
686 // Parse the right operand, |
680 // Parse the right operand, |
687 MustNext (); |
681 MustNext (); |
688 DataBuffer* retbuf = ParseExprValue (TYPE_INT); |
682 DataBuffer* retbuf = ParseExpression (TYPE_INT); |
689 |
683 |
690 long dh = DataHeaderByOperator (var, oper); |
684 long dh = DataHeaderByOperator (var, oper); |
691 retbuf->Write<word> (dh); |
685 retbuf->Write<word> (dh); |
692 retbuf->Write<word> (var->index); |
686 retbuf->Write<word> (var->index); |
693 |
687 |