Changeset 53 for branches/DelphiToC/Produce/UProducerPascal.pas
- Timestamp:
- Aug 10, 2010, 10:14:57 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DelphiToC/Produce/UProducerPascal.pas
r52 r53 15 15 TProducerPascal = class(TProducer) 16 16 private 17 procedure Emit( Text: string; NewLine: Boolean = True);17 procedure Emit(AText: string; NewLine: Boolean = True); 18 18 procedure GenerateUses(UsedModules: TUsedModuleList); 19 19 procedure GenerateModule(Module: TModule); 20 procedure GenerateType(AType: TType; AssignSymbol: Char = ':'); 21 procedure GenerateTypes(Types: TTypeList); 20 22 procedure GenerateCommonBlock(CommonBlock: TCommonBlock; 21 23 LabelPrefix: string); … … 27 29 procedure GenerateCommand(Command: TCommand); 28 30 procedure GenerateWhileDo(WhileDo: TWhileDo); 31 procedure GenerateForToDo(ForToDo: TForToDo); 29 32 procedure GenerateIfThenElse(IfThenElse: TIfThenElse); 30 33 procedure GenerateAssignment(Assignment: TAssignment); … … 56 59 end; 57 60 58 procedure TProducerPascal.Emit(Text: string; NewLine: Boolean = True); 59 begin 60 if NewLine then TextSource.Add(DupeString(' ', IndentationLength * Indetation) + Text) 61 else TextSource.Strings[TextSource.Count - 1] := TextSource.Strings[TextSource.Count - 1] + Text; 61 procedure TProducerPascal.Emit(AText: string; NewLine: Boolean = True); 62 begin 63 with TextSource do begin 64 if Count = 0 then Add(''); 65 if Strings[Count - 1] = '' then 66 Strings[Count - 1] := Strings[Count - 1] + DupeString(' ', IndentationLength * Indetation); 67 Strings[Count - 1] := Strings[Count - 1] + AText; 68 if NewLine then Add(''); 69 end; 62 70 end; 63 71 … … 83 91 GenerateCommonBlock(Module, ''); 84 92 Emit('.', False); 93 end; 94 95 procedure TProducerPascal.GenerateType(AType: TType; AssignSymbol: Char = ':'); 96 var 97 I: Integer; 98 begin 99 if AType is TTypeRecord then begin 100 Emit(AType.Name + ' ' + AssignSymbol + ' record'); 101 Inc(Indetation); 102 for I := 0 to TTypeRecord(AType).Items.Count - 1 do begin 103 GenerateType(TType(TTypeRecord(AType).Items[I])); 104 Emit(';'); 105 end; 106 Dec(Indetation); 107 Emit('end', False); 108 end else 109 if AType is TTypeArray then begin 110 Emit(AType.Name + ' ' + AssignSymbol + ' array ', False); 111 if Assigned(TTypeArray(AType).IndexType) then begin 112 Emit('[', False); 113 GenerateType(TTypeArray(AType).IndexType); 114 Emit(']', False); 115 end; 116 Emit(' of ', False); 117 if Assigned(TTypeArray(AType).ItemType) then 118 GenerateType(TTypeArray(AType).ItemType); 119 end else begin 120 Emit(AType.Name, False); 121 if Assigned(AType.UsedType) then begin 122 Emit(' ' + AssignSymbol + ' ', False); 123 GenerateType(AType.UsedType); 124 end; 125 end; 126 end; 127 128 procedure TProducerPascal.GenerateTypes(Types: TTypeList); 129 var 130 I: Integer; 131 begin 132 if Types.Count > 0 then begin 133 Emit('type'); 134 Inc(Indetation); 135 for I := 0 to Types.Count - 1 do 136 with TType(Types[I]) do 137 if (not System) then begin 138 GenerateType(TType(Types[I]), '='); 139 Emit(';'); 140 end; 141 Dec(Indetation); 142 Emit(''); 143 end; 85 144 end; 86 145 … … 127 186 Emit(Line + ';'); 128 187 GenerateBeginEnd(Code); 129 Emit(';' , False);188 Emit(';'); 130 189 Emit(''); 131 190 end; … … 136 195 I: Integer; 137 196 begin 138 Emit('const'); 139 Inc(Indetation); 140 for I := 0 to Constants.Count - 1 do 141 with TConstant(Constants[I]) do 142 Emit(Name + ': ' + ValueType.Name + ' = ' + Value + ';'); 143 Dec(Indetation); 144 Emit(''); 197 if Constants.Count > 0 then begin 198 Emit('const'); 199 Inc(Indetation); 200 for I := 0 to Constants.Count - 1 do 201 with TConstant(Constants[I]) do 202 if not System then begin 203 Emit(Name + ': ' + ValueType.Name + ' = ' + Value + ';'); 204 end; 205 Dec(Indetation); 206 Emit(''); 207 end; 145 208 end; 146 209 … … 154 217 for I := 0 to BeginEnd.Commands.Count - 1 do begin 155 218 GenerateCommand(TCommand(BeginEnd.Commands[I])); 156 Emit(';' , False);219 Emit(';'); 157 220 end; 158 221 159 222 Dec(Indetation); 160 Emit('end' );223 Emit('end', False); 161 224 end; 162 225 … … 178 241 if Command is TBeginEnd then GenerateBeginEnd(TBeginEnd(Command)) 179 242 else if Command is TWhileDo then GenerateWhileDo(TWhileDo(Command)) 243 else if Command is TForToDo then GenerateForToDo(TForToDo(Command)) 180 244 else if Command is TIfThenElse then GenerateIfThenElse(TIfThenElse(Command)) 181 245 else if Command is TAssignment then GenerateAssignment(TAssignment(Command)) … … 189 253 end; 190 254 255 procedure TProducerPascal.GenerateForToDo(ForToDo: TForToDo); 256 begin 257 with ForToDo do begin 258 Emit('for ' + ControlVariable.Name + ' := ' + 259 GenerateExpression(Start) + ' to ' + GenerateExpression(Stop) + ' do '); 260 GenerateCommand(Command); 261 end; 262 end; 263 191 264 procedure TProducerPascal.GenerateIfThenElse(IfThenElse: TIfThenElse); 192 265 begin … … 194 267 GenerateCommand(IfThenElse.Command); 195 268 if Assigned(IfThenElse.ElseCommand) then begin 196 Emit(' else ');269 Emit(' else '); 197 270 GenerateCommand(IfThenElse.ElseCommand); 198 271 end; … … 201 274 procedure TProducerPascal.GenerateAssignment(Assignment: TAssignment); 202 275 begin 203 Emit(Assignment.Target.Name + ' := ' + GenerateExpression(Assignment.Source) );276 Emit(Assignment.Target.Name + ' := ' + GenerateExpression(Assignment.Source), False); 204 277 end; 205 278 … … 220 293 end; 221 294 end; 222 Emit(Line );295 Emit(Line, False); 223 296 end; 224 297 … … 244 317 begin 245 318 with CommonBlock do begin 319 GenerateTypes(Types); 246 320 GenerateFunctions(Functions); 247 321 GenerateConstants(Constants);
Note:
See TracChangeset
for help on using the changeset viewer.