Changeset 53 for branches/DelphiToC/Produce/UProducerC.pas
- Timestamp:
- Aug 10, 2010, 10:14:57 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DelphiToC/Produce/UProducerC.pas
r52 r53 17 17 function TranslateType(Name: string): string; 18 18 function TranslateOperator(Name: string): string; 19 procedure Emit( Text: string);19 procedure Emit(AText: string; NewLine: Boolean = True); 20 20 procedure GenerateUses(UsedModules: TUsedModuleList); 21 21 procedure GenerateModule(Module: TModule); 22 22 procedure GenerateCommonBlock(CommonBlock: TCommonBlock; 23 23 LabelPrefix: string); 24 procedure GenerateType(AType: TType); 25 procedure GenerateTypes(Types: TTypeList); 24 26 procedure GenerateProgram(ProgramBlock: TProgram); 25 27 procedure GenerateFunctions(Functions: TFunctionList); … … 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); … … 64 67 else if Name = 'Cardinal' then Result := 'uint32' 65 68 else if Name = 'Integer' then Result := 'int32' 66 else if Name = 'Void' then Result := 'void'; 69 else if Name = 'Void' then Result := 'void' 70 else Result := Name; 67 71 end; 68 72 … … 82 86 end; 83 87 84 procedure TProducerC.Emit(Text: string); 85 begin 86 TextSource.Add(DupeString(' ', IndentationLength * Indetation) + Text); 88 procedure TProducerC.Emit(AText: string; NewLine: Boolean = True); 89 begin 90 with TextSource do begin 91 if Count = 0 then Add(''); 92 if Strings[Count - 1] = '' then 93 Strings[Count - 1] := Strings[Count - 1] + DupeString(' ', IndentationLength * Indetation); 94 Strings[Count - 1] := Strings[Count - 1] + AText; 95 if NewLine then Add(''); 96 end; 87 97 end; 88 98 … … 180 190 if Command is TBeginEnd then GenerateBeginEnd(TBeginEnd(Command)) 181 191 else if Command is TWhileDo then GenerateWhileDo(TWhileDo(Command)) 192 else if Command is TForToDo then GenerateForToDo(TForToDo(Command)) 182 193 else if Command is TIfThenElse then GenerateIfThenElse(TIfThenElse(Command)) 183 194 else if Command is TAssignment then GenerateAssignment(TAssignment(Command)) … … 189 200 Emit('while (' + GenerateExpression(WhileDo.Condition) + ')'); 190 201 GenerateCommand(WhileDo.Command); 202 end; 203 204 procedure TProducerC.GenerateForToDo(ForToDo: TForToDo); 205 begin 206 with ForToDo do begin 207 Emit('for(' + ControlVariable.Name + ' = ' + 208 GenerateExpression(Start) + '; ' + ControlVariable.Name + ' < ' + 209 GenerateExpression(Stop) + '; ' + ControlVariable.Name + '++)'); 210 GenerateCommand(Command); 211 end; 191 212 end; 192 213 … … 246 267 begin 247 268 with CommonBlock do begin 269 GenerateTypes(Types); 248 270 GenerateFunctions(Functions); 249 271 Emit('void ' + Name + '()'); … … 252 274 end; 253 275 276 procedure TProducerC.GenerateType(AType: TType); 277 var 278 I: Integer; 279 begin 280 if AType is TTypeRecord then begin 281 Emit('typedef struct'); 282 Emit('{'); 283 Inc(Indetation); 284 for I := 0 to TTypeRecord(AType).Items.Count - 1 do begin 285 GenerateType(TType(TTypeRecord(AType).Items[I])); 286 Emit(';'); 287 end; 288 Dec(Indetation); 289 Emit('} ' + TranslateType(AType.Name), False); 290 end else 291 if AType is TTypeArray then begin 292 GenerateType(TTypeArray(AType).ItemType); 293 Emit('* ', False); 294 295 (* if Assigned(TTypeArray(AType).IndexType) then begin 296 Emit(AType.Name + '[', False); 297 Emit('[', False); 298 GenerateType(TTypeArray(AType).IndexType); 299 Emit(']', False); 300 end; 301 Emit(' of ', False); 302 if Assigned(TTypeArray(AType).ItemType) then*) 303 Emit(TranslateType(AType.Name), False); 304 end else begin 305 if Assigned(AType.UsedType) then begin 306 GenerateType(AType.UsedType); 307 Emit(' ', False); 308 end; 309 Emit(TranslateType(AType.Name), False); 310 end; 311 end; 312 313 procedure TProducerC.GenerateTypes(Types: TTypeList); 314 var 315 I: Integer; 316 begin 317 if Types.Count > 0 then begin 318 Inc(Indetation); 319 for I := 0 to Types.Count - 1 do 320 with TType(Types[I]) do 321 if (not System) then begin 322 GenerateType(TType(Types[I])); 323 Emit(';'); 324 end; 325 Dec(Indetation); 326 Emit(''); 327 end; 328 end; 329 254 330 255 331
Note:
See TracChangeset
for help on using the changeset viewer.