Changeset 213 for branches/interpreter2/UOptimizer.pas
- Timestamp:
- Apr 22, 2020, 10:23:31 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/interpreter2/UOptimizer.pas
r211 r213 9 9 10 10 type 11 TOptimizeFeature = (ofReplaceRepeatUntilByWhileDo, ofReplaceResultByReturn); 12 TOptimizeFeatures = set of TOptimizeFeature; 11 13 12 14 { TOptimizer } … … 18 20 public 19 21 Prog: TProgram; 22 Features: TOptimizeFeatures; 20 23 procedure Optimize; 21 24 end; … … 29 32 var 30 33 I: Integer; 34 TempNewNode: TSourceNode; 31 35 begin 32 36 for I := 0 to SourceNodes.Count - 1 do begin 33 37 if SourceNodes[I] is TSourceNode then begin 34 OptimizeNode(TSourceNode(SourceNodes[I]), NewNode);35 if Assigned( NewNode) and (NewNode <> TSourceNode(SourceNodes[I])) then begin36 SourceNodes[I] := NewNode;38 OptimizeNode(TSourceNode(SourceNodes[I]), TempNewNode); 39 if Assigned(TempNewNode) and (TempNewNode <> TSourceNode(SourceNodes[I])) then begin 40 SourceNodes[I] := TempNewNode; 37 41 end; 38 42 end else raise Exception.Create('Unsupported node type'); … … 46 50 WhileDo: TWhileDo; 47 51 Condition: TIfThenElse; 52 Return: TReturn; 48 53 Field: TField; 49 54 Obj: TObject; 55 TempNewNode: TSourceNode; 50 56 begin 51 57 NewNode := nil; … … 55 61 OptimizeNodes(TSourceNodes(SourceNode), NewNode) 56 62 end else 57 if SourceNode is TRepeatUntilthen begin63 if (ofReplaceRepeatUntilByWhileDo in Features) and (SourceNode is TRepeatUntil) then begin 58 64 WhileDo := TWhileDo.Create; 59 65 WhileDo.Command := TBeginEnd.Create; … … 79 85 NewNode := WhileDo; 80 86 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 81 98 if SourceNode is TSourceNode then begin 82 99 for I := 0 to SourceNode.FieldsCount - 1 do begin … … 85 102 SourceNode.GetValue(I, Obj); 86 103 if Obj is TSourceNode then begin 87 OptimizeNode(TSourceNode(Obj), NewNode);88 if Assigned( NewNode) and (NewNode <> TSourceNode(Obj)) then begin89 SourceNode.SetValueObject(I, NewNode);104 OptimizeNode(TSourceNode(Obj), TempNewNode); 105 if Assigned(TempNewNode) and (TempNewNode <> TSourceNode(Obj)) then begin 106 SourceNode.SetValueObject(I, TempNewNode); 90 107 end; 91 108 end;
Note:
See TracChangeset
for help on using the changeset viewer.