Browse Source

* Patch from Anton fixing complex const expressions. Mantis 19372

git-svn-id: trunk@17517 -
marco 14 years ago
parent
commit
32d12843d3
2 changed files with 42 additions and 6 deletions
  1. 36 5
      packages/fcl-passrc/src/pastree.pp
  2. 6 1
      packages/fcl-passrc/src/pparser.pp

+ 36 - 5
packages/fcl-passrc/src/pastree.pp

@@ -125,7 +125,7 @@ type
 
 
   TExprOpCode = (eopNone,
   TExprOpCode = (eopNone,
                  eopAdd,eopSubtract,eopMultiply,eopDivide, eopDiv,eopMod, eopPower,// arithmetic
                  eopAdd,eopSubtract,eopMultiply,eopDivide, eopDiv,eopMod, eopPower,// arithmetic
-                 eopShr,eopSHl, // bit operations
+                 eopShr,eopShl, // bit operations
                  eopNot,eopAnd,eopOr,eopXor, // logical/bit
                  eopNot,eopAnd,eopOr,eopXor, // logical/bit
                  eopEqual, eopNotEqual,  // Logical
                  eopEqual, eopNotEqual,  // Logical
                  eopLessThan,eopGreaterThan, eopLessthanEqual,eopGreaterThanEqual, // ordering
                  eopLessThan,eopGreaterThan, eopLessthanEqual,eopGreaterThanEqual, // ordering
@@ -2038,6 +2038,8 @@ Const
  Seps : Array[Boolean] of Char = ('=',':');
  Seps : Array[Boolean] of Char = ('=',':');
 
 
 begin
 begin
+  if (Value = '') and Assigned(Expr) then
+    Value := Expr.GetDeclaration(full);
   If Assigned(VarType) then
   If Assigned(VarType) then
     begin
     begin
     If VarType.Name='' then
     If VarType.Name='' then
@@ -2506,7 +2508,7 @@ end;
 
 
 constructor TPasExpr.Create(AParent : TPasElement; AKind: TPasExprKind; AOpCode: TexprOpcode);
 constructor TPasExpr.Create(AParent : TPasElement; AKind: TPasExprKind; AOpCode: TexprOpcode);
 begin
 begin
-  Create(ClassName, AParent);
+  inherited Create(ClassName, AParent);
   Kind:=AKind;
   Kind:=AKind;
   OpCode:=AOpCode;
   OpCode:=AOpCode;
 end;
 end;
@@ -2567,16 +2569,45 @@ end;
 { TBinaryExpr }
 { TBinaryExpr }
 
 
 function TBinaryExpr.GetDeclaration(Full : Boolean):AnsiString;
 function TBinaryExpr.GetDeclaration(Full : Boolean):AnsiString;
-
+  function OpLevel(op: TPasExpr): Integer;
+  begin
+    case op.OpCode of
+      eopNot,eopAddress:
+        Result := 4;
+      eopMultiply, eopDivide, eopDiv, eopMod, eopAnd, eopShl,
+      eopShr, eopAs, eopPower:
+        Result := 3;
+      eopAdd, eopSubtract, eopOr, eopXor:
+        Result := 2;
+      eopEqual, eopNotEqual, eopLessThan, eopLessthanEqual, eopGreaterThan,
+      eopGreaterThanEqual, eopIn, eopIs:
+        Result := 1;
+    else
+      Result := 5; // Numbers and Identifiers
+    end;
+  end;
+var op: string;
 begin
 begin
   If Kind=pekRange then
   If Kind=pekRange then
     Result:='..'
     Result:='..'
   else
   else
     Result:=' '+OpcodeStrings[Opcode]+' ';
     Result:=' '+OpcodeStrings[Opcode]+' ';
   If Assigned(Left) then
   If Assigned(Left) then
-    Result:=Left.GetDeclaration(Full)+Result;
+  begin
+    op := Left.GetDeclaration(Full);
+    if OpLevel(Left) < OpLevel(Self) then
+      Result := '(' + op + ')' + Result
+    else
+      Result := op + Result;
+  end;
   If Assigned(Right) then
   If Assigned(Right) then
-    Result:=Result +Right.GetDeclaration(Full);
+  begin
+    op := Right.GetDeclaration(Full);
+    if (OpLevel(Right) < 5) and (OpLevel(Right) >= OpLevel(Self)) then
+      Result := Result + '(' + op + ')'
+    else
+      Result := Result + op;
+  end;
 end;
 end;
 
 
 
 

+ 6 - 1
packages/fcl-passrc/src/pparser.pp

@@ -951,7 +951,7 @@ begin
         // CEP assumes that it's array or record, because the expression
         // CEP assumes that it's array or record, because the expression
         // starts with "(". After the first part is parsed, the CEP meets "-"
         // starts with "(". After the first part is parsed, the CEP meets "-"
         // that assures, it's not an array expression. The CEP should give the
         // that assures, it's not an array expression. The CEP should give the
-        // first partback to the expression parser, to get the correct
+        // first part back to the expression parser, to get the correct
         // token tree according to the operations priority.
         // token tree according to the operations priority.
         //
         //
         // quite ugly. type information is required for CEP to work clean
         // quite ugly. type information is required for CEP to work clean
@@ -1151,6 +1151,11 @@ begin
     else
     else
       // Binary expression!  ((128 div sizeof(longint)) - 3);       ;
       // Binary expression!  ((128 div sizeof(longint)) - 3);       ;
       Result:=DoParseExpression(AParent,x);
       Result:=DoParseExpression(AParent,x);
+      if CurToken<>tkBraceClose then ParseExc(SParserExpectedCommaRBracket);
+      NextToken;
+      if CurToken <> tkSemicolon then // the continue of expresion
+        Result:=DoParseExpression(AParent,Result);
+      Exit;
     end;
     end;
     if CurToken<>tkBraceClose then ParseExc(SParserExpectedCommaRBracket);
     if CurToken<>tkBraceClose then ParseExc(SParserExpectedCommaRBracket);
     NextToken;
     NextToken;