Changeset 76 for branches/Transpascal/Compiler/Produce/UProducerC.pas
- Timestamp:
- Oct 21, 2010, 1:20:57 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Transpascal/Compiler/Produce/UProducerC.pas
r75 r76 19 19 function TranslateType(Name: string): string; 20 20 function TranslateOperator(Name: string): string; 21 procedure Emit(AText: string; NewLine: Boolean = True); 21 procedure Emit(AText: string); 22 procedure EmitLn(AText: string = ''); 22 23 procedure GenerateUses(UsedModules: TUsedModuleList); 23 24 procedure GenerateModule(Module: TModule); … … 27 28 procedure GenerateTypes(Types: TTypeList); 28 29 procedure GenerateProgram(ProgramBlock: TProgram); 29 procedure GenerateFunctions(Functions: TFunctionList); 30 procedure GenerateFunctions(Functions: TFunctionList; 31 Prefix: string = ''); 30 32 procedure GenerateBeginEnd(BeginEnd: TBeginEnd); 31 33 procedure GenerateVariableList(VariableList: TVariableList); … … 94 96 end; 95 97 96 procedure TProducerC.Emit(AText: string; NewLine: Boolean = True); 98 procedure TProducerC.EmitLn(AText: string = ''); 99 begin 100 Emit(AText); 101 TextSource.Add(''); 102 end; 103 104 procedure TProducerC.Emit(AText: string); 97 105 begin 98 106 with TextSource do begin … … 101 109 Strings[Count - 1] := Strings[Count - 1] + DupeString(' ', IndentationLength * Indetation); 102 110 Strings[Count - 1] := Strings[Count - 1] + AText; 103 if NewLine then Add('');104 111 end; 105 112 end; … … 111 118 for I := 0 to UsedModules.Count - 1 do 112 119 if Dialect = pdDynamicC then 113 Emit ('#use "' + TUsedModule(UsedModules[I]).Name + '.lib"')114 else Emit ('#include "' + TUsedModule(UsedModules[I]).Name + '.h"');115 Emit ('');120 EmitLn('#use "' + TUsedModule(UsedModules[I]).Name + '.lib"') 121 else EmitLn('#include "' + TUsedModule(UsedModules[I]).Name + '.h"'); 122 EmitLn; 116 123 end; 117 124 118 125 procedure TProducerC.GenerateModule(Module: TModule); 119 126 begin 120 if Dialect = pdDynamicC then Emit ('#use "platform.lib"')121 else Emit ('#include "platform.h"');122 Emit ('');127 if Dialect = pdDynamicC then EmitLn('#use "platform.lib"') 128 else EmitLn('#include "platform.h"'); 129 EmitLn; 123 130 if Module is TModuleProgram then begin 124 131 TModuleProgram(Module).Body.Name := 'main'; … … 149 156 end; 150 157 151 procedure TProducerC.GenerateFunctions(Functions: TFunctionList); 158 procedure TProducerC.GenerateFunctions(Functions: TFunctionList; 159 Prefix: string = ''); 152 160 var 153 161 I: Integer; … … 157 165 for I := 0 to Functions.Count - 1 do 158 166 with TFunction(Functions[I]) do 159 if not System then 160 begin 161 if HaveResult then Line := TranslateType(ResultType.Name) + ' ' 167 if not System then begin 168 if HaveResult and Assigned(ResultType) then Line := TranslateType(ResultType.Name) + ' ' 162 169 else Line := 'void '; 163 Line := Line + Name + '(';170 Line := Line + Prefix + Name + '('; 164 171 if Parameters.Count > 0 then 165 172 for J := 0 to Parameters.Count - 1 do begin … … 169 176 end; 170 177 Line := Line + ')'; 171 Emit (Line);178 EmitLn(Line); 172 179 GenerateBeginEnd(Code); 173 Emit ('');180 EmitLn; 174 181 end; 175 182 end; … … 179 186 I: Integer; 180 187 begin 181 Emit ('{');188 EmitLn('{'); 182 189 Inc(Indetation); 183 190 … … 192 199 193 200 Dec(Indetation); 194 Emit ('}');201 EmitLn('}'); 195 202 end; 196 203 … … 201 208 for I := 0 to VariableList.Count - 1 do 202 209 GenerateVariable(TVariable(VariableList[I])); 203 // Emit ('');210 // EmitLn; 204 211 end; 205 212 … … 207 214 begin 208 215 with Variable do 209 Emit (TranslateType(ValueType.Name) + ' ' + Name + ';');216 EmitLn(TranslateType(ValueType.Name) + ' ' + Name + ';'); 210 217 end; 211 218 … … 222 229 procedure TProducerC.GenerateWhileDo(WhileDo: TWhileDo); 223 230 begin 224 Emit ('while (' + GenerateExpression(WhileDo.Condition) + ')');225 GenerateCommand(WhileDo.Command);231 EmitLn('while (' + GenerateExpression(WhileDo.Condition) + ')'); 232 if Assigned(WhileDo.Command) then GenerateCommand(WhileDo.Command); 226 233 end; 227 234 … … 229 236 begin 230 237 with ForToDo do begin 231 Emit('for(' + ControlVariable.Name + ' = ' + 238 if Assigned(ControlVariable) then 239 EmitLn('for(' + ControlVariable.Name + ' = ' + 232 240 GenerateExpression(Start) + '; ' + ControlVariable.Name + ' < ' + 233 241 GenerateExpression(Stop) + '; ' + ControlVariable.Name + '++)'); … … 238 246 procedure TProducerC.GenerateIfThenElse(IfThenElse: TIfThenElse); 239 247 begin 240 Emit ('if(' + GenerateExpression(IfThenElse.Condition) + ')');248 EmitLn('if(' + GenerateExpression(IfThenElse.Condition) + ')'); 241 249 GenerateCommand(IfThenElse.Command); 242 250 if Assigned(IfThenElse.ElseCommand) then begin 243 Emit ('else ');251 EmitLn('else '); 244 252 GenerateCommand(IfThenElse.ElseCommand); 245 253 end; … … 248 256 procedure TProducerC.GenerateAssignment(Assignment: TAssignment); 249 257 begin 250 if Assignment.Target.Name = 'Result' then Emit ('return(' + GenerateExpression(Assignment.Source) + ');')251 else Emit (Assignment.Target.Name + ' = ' + GenerateExpression(Assignment.Source) + ';');258 if Assignment.Target.Name = 'Result' then EmitLn('return(' + GenerateExpression(Assignment.Source) + ');') 259 else EmitLn(Assignment.Target.Name + ' = ' + GenerateExpression(Assignment.Source) + ';'); 252 260 end; 253 261 … … 266 274 end; 267 275 Line := Line + ');'; 268 Emit (Line);276 EmitLn(Line); 269 277 end; 270 278 end; … … 302 310 GenerateTypes(Types); 303 311 GenerateFunctions(Functions); 304 Emit ('void ' + Name + '()');312 EmitLn('void ' + Name + '()'); 305 313 GenerateBeginEnd(Code); 306 314 end; … … 313 321 if Assigned(AType) then begin 314 322 if AType is TTypeRecord then begin 315 Emit ('struct');316 Emit ('{');323 EmitLn('struct'); 324 EmitLn('{'); 317 325 Inc(Indetation); 318 326 GenerateVariableList(TTypeRecord(AType).CommonBlock.Variables); 319 327 Dec(Indetation); 320 Emit('} ' + TranslateType(AType.Name), False); 328 EmitLn('} ' + TranslateType(AType.Name) + ';'); 329 EmitLn; 330 GenerateFunctions(TTypeRecord(AType).CommonBlock.Functions, AType.Name + '_'); 321 331 end else 322 332 if AType is TTypeArray then begin 323 333 GenerateType(TTypeArray(AType).ItemType); 324 Emit ('* ', False);334 EmitLn('* '); 325 335 326 336 (* if Assigned(TTypeArray(AType).IndexType) then begin 327 Emit(AType.Name + '[' , False);328 Emit('[' , False);337 Emit(AType.Name + '['); 338 Emit('['); 329 339 GenerateType(TTypeArray(AType).IndexType); 330 Emit(']' , False);331 end; 332 Emit(' of ' , False);340 Emit(']'); 341 end; 342 Emit(' of '); 333 343 if Assigned(TTypeArray(AType).ItemType) then*) 334 Emit(TranslateType(AType.Name), False); 344 Emit(TranslateType(AType.Name)); 345 end else 346 if AType is TTypePointer then begin 347 if Assigned(AType.UsedType) then begin 348 Emit(AType.UsedType.Name); 349 Emit(' *'); 350 end; 351 Emit(TranslateType(AType.Name)); 335 352 end else begin 336 353 if Assigned(AType.UsedType) then begin 337 354 //GenerateType(AType.UsedType); 338 Emit(AType.UsedType.Name , False);339 Emit(' ' , False);340 end; 341 Emit(TranslateType(AType.Name) , False);355 Emit(AType.UsedType.Name); 356 Emit(' '); 357 end; 358 Emit(TranslateType(AType.Name)); 342 359 end; 343 360 end; … … 353 370 with TType(Types[I]) do 354 371 if (not System) then begin 355 Emit('typedef ' , False);372 Emit('typedef '); 356 373 GenerateType(TType(Types[I])); 357 Emit (';');374 EmitLn(';'); 358 375 end; 359 376 Dec(Indetation); 360 Emit(''); 361 end; 362 end; 363 364 377 EmitLn(''); 378 end; 379 end; 365 380 366 381 end.
Note:
See TracChangeset
for help on using the changeset viewer.