Ignore:
Timestamp:
Apr 22, 2020, 10:23:31 PM (5 years ago)
Author:
chronos
Message:
  • Added: Transformation of Result variable assignment into Return statement.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/interpreter2/UOptimizer.pas

    r211 r213  
    99
    1010type
     11  TOptimizeFeature = (ofReplaceRepeatUntilByWhileDo, ofReplaceResultByReturn);
     12  TOptimizeFeatures = set of TOptimizeFeature;
    1113
    1214  { TOptimizer }
     
    1820  public
    1921    Prog: TProgram;
     22    Features: TOptimizeFeatures;
    2023    procedure Optimize;
    2124  end;
     
    2932var
    3033  I: Integer;
     34  TempNewNode: TSourceNode;
    3135begin
    3236  for I := 0 to SourceNodes.Count - 1 do begin
    3337    if SourceNodes[I] is TSourceNode then begin
    34       OptimizeNode(TSourceNode(SourceNodes[I]), NewNode);
    35       if Assigned(NewNode) and (NewNode <> TSourceNode(SourceNodes[I])) then begin
    36         SourceNodes[I] := NewNode;
     38      OptimizeNode(TSourceNode(SourceNodes[I]), TempNewNode);
     39      if Assigned(TempNewNode) and (TempNewNode <> TSourceNode(SourceNodes[I])) then begin
     40        SourceNodes[I] := TempNewNode;
    3741      end;
    3842    end else raise Exception.Create('Unsupported node type');
     
    4650  WhileDo: TWhileDo;
    4751  Condition: TIfThenElse;
     52  Return: TReturn;
    4853  Field: TField;
    4954  Obj: TObject;
     55  TempNewNode: TSourceNode;
    5056begin
    5157  NewNode := nil;
     
    5561    OptimizeNodes(TSourceNodes(SourceNode), NewNode)
    5662  end else
    57   if SourceNode is TRepeatUntil then begin
     63  if (ofReplaceRepeatUntilByWhileDo in Features) and (SourceNode is TRepeatUntil) then begin
    5864    WhileDo := TWhileDo.Create;
    5965    WhileDo.Command := TBeginEnd.Create;
     
    7985    NewNode := WhileDo;
    8086  end else
     87  if (ofReplaceResultByReturn in Features) and (SourceNode is TAssignment) then begin
     88    if TAssignment(SourceNode).Variable.Name = 'Result' then begin
     89      Return := TReturn.Create;
     90      Return.Parent := TAssignment(SourceNode).Parent;
     91      Return.Expression.Free;
     92      Return.Expression := TAssignment(SourceNode).Expression;
     93      Return.Expression.Parent := Return;
     94      TAssignment(SourceNode).Expression := TExpression.Create;
     95      NewNode := Return;
     96    end;
     97  end else
    8198  if SourceNode is TSourceNode then begin
    8299    for I := 0 to SourceNode.FieldsCount - 1 do begin
     
    85102        SourceNode.GetValue(I, Obj);
    86103        if Obj is TSourceNode then begin
    87           OptimizeNode(TSourceNode(Obj), NewNode);
    88           if Assigned(NewNode) and (NewNode <> TSourceNode(Obj)) then begin
    89             SourceNode.SetValueObject(I, NewNode);
     104          OptimizeNode(TSourceNode(Obj), TempNewNode);
     105          if Assigned(TempNewNode) and (TempNewNode <> TSourceNode(Obj)) then begin
     106            SourceNode.SetValueObject(I, TempNewNode);
    90107          end;
    91108        end;
Note: See TracChangeset for help on using the changeset viewer.