Changeset 53
- Timestamp:
- Aug 10, 2010, 10:14:57 AM (14 years ago)
- Location:
- branches/DelphiToC
- Files:
-
- 3 added
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DelphiToC/Analyze/UParser.pas
r51 r53 65 65 procedure ParseConstantList(SourceCode: TConstantList); 66 66 procedure ParseTypeList(SourceCode: TTypeList); 67 function ParseType(TypeList: TTypeList): TType; 68 function ParseRecordItem(TypeList: TTypeList): TType; 67 function ParseType(TypeList: TTypeList; ExpectName: Boolean = True; AssignSymbol: string = '='): TType; 69 68 private 70 69 end; … … 999 998 begin 1000 999 Expect('type'); 1001 while IsIdentificator(FNextToken) do 1000 while IsIdentificator(FNextToken) do begin 1002 1001 NewType := ParseType(SourceCode); 1003 1002 if Assigned(NewType) then begin … … 1005 1004 Add(NewType); 1006 1005 end; 1006 Expect(';'); 1007 end; 1007 1008 end; 1008 1009 end; … … 1010 1011 { TParserType } 1011 1012 1012 function TPascalParser.ParseType(TypeList: TTypeList ): TType;1013 function TPascalParser.ParseType(TypeList: TTypeList; ExpectName: Boolean = True; AssignSymbol: string = '='): TType; 1013 1014 var 1014 1015 Name: string; 1015 UsedTypeName: string;1016 TypeName: string; 1016 1017 begin 1017 1018 //with SourceCode do 1018 1019 begin 1019 Name := ReadCode; 1020 Expect('='); 1020 if ExpectName then begin 1021 Name := ReadCode; 1022 Expect(AssignSymbol); 1023 end; 1021 1024 if FNextToken = 'record' then begin 1022 1025 Expect('record'); … … 1026 1029 while (FNextToken <> 'end') and (FNextTokenType <> ttEndOfFile) do 1027 1030 begin 1028 TTypeRecord(Result).Items.Add(ParseRecordItem(TypeList)); 1031 TTypeRecord(Result).Items.Add(ParseType(TypeList, True, ':')); 1032 Expect(';'); 1029 1033 end; 1030 1034 Expect('end'); 1035 end else 1036 if FNextToken = 'array' then begin 1037 Expect('array'); 1038 Result := TTypeArray.Create; 1039 TTypeArray(Result).Parent := TypeList; 1040 TType(Result).Name := Name; 1041 if FNextToken = '[' then begin 1042 Expect('['); 1043 TypeName := FNextToken; 1044 TTypeArray(Result).IndexType := ParseType(TypeList, False); 1045 if not Assigned(TTypeArray(Result).IndexType) then 1046 ErrorMessage(SUndefinedType, [TypeName]); 1047 Expect(']'); 1048 end; 1049 Expect('of'); 1050 TypeName := FNextToken; 1051 TTypeArray(Result).ItemType := ParseType(TypeList, False); 1052 if not Assigned(TTypeArray(Result).ItemType) then 1053 ErrorMessage(SUndefinedType, [TypeName]); 1031 1054 end else begin 1032 UsedTypeName := ReadCode; 1033 Result := TType.Create; 1034 TType(Result).Parent := TypeList; 1035 TType(Result).Name := Name; 1036 TType(Result).UsedType := TypeList.Search(UsedTypeName); 1037 if not Assigned(TType(Result).UsedType) then ErrorMessage(SUndefinedType, [UsedTypeName]); 1038 end; 1039 Expect(';'); 1040 end; 1041 end; 1042 1043 function TPascalParser.ParseRecordItem(TypeList: TTypeList): TType; 1044 var 1045 Name: string; 1046 UsedTypeName: string; 1047 begin 1048 //with SourceCode do 1049 begin 1050 Name := ReadCode; 1051 Expect(':'); 1052 if FNextToken = 'record' then begin 1053 Expect('record'); 1054 Result := TTypeRecord.Create; 1055 TTypeRecord(Result).Parent := TypeList; 1056 TTypeRecord(Result).Name := Name; 1057 while (FNextToken <> 'end') and (FNextTokenType <> ttEndOfFile) do 1058 begin 1059 TTypeRecord(Result).Items.Add(ParseRecordItem(TypeList)); 1060 end; 1061 Expect('end'); 1062 end else begin 1063 UsedTypeName := ReadCode; 1064 Result := TType.Create; 1065 TType(Result).Parent := TypeList; 1066 TType(Result).Name := Name; 1067 TType(Result).UsedType := TypeList.Search(UsedTypeName); 1068 if not Assigned(TType(Result).UsedType) then ErrorMessage(SUndefinedType, [UsedTypeName]); 1069 end; 1070 Expect(';'); 1055 TypeName := ReadCode; 1056 if ExpectName then begin 1057 Result := TType.Create; 1058 TType(Result).Parent := TypeList; 1059 TType(Result).Name := Name; 1060 TType(Result).UsedType := TypeList.Search(TypeName); 1061 if not Assigned(TType(Result).UsedType) then 1062 ErrorMessage(SUndefinedType, [TypeName]); 1063 end else begin 1064 TType(Result) := TypeList.Search(TypeName); 1065 if not Assigned(TType(Result)) then 1066 ErrorMessage(SUndefinedType, [TypeName]); 1067 end; 1068 end; 1071 1069 end; 1072 1070 end; -
branches/DelphiToC/DelphiToC.lpi
r52 r53 48 48 <TopLine Value="4"/> 49 49 <CursorPos X="1" Y="29"/> 50 <UsageCount Value="14 6"/>50 <UsageCount Value="148"/> 51 51 <Loaded Value="True"/> 52 52 </Unit0> … … 60 60 <EditorIndex Value="6"/> 61 61 <WindowIndex Value="0"/> 62 <TopLine Value=" 77"/>63 <CursorPos X=" 35" Y="87"/>64 <UsageCount Value="14 6"/>62 <TopLine Value="15"/> 63 <CursorPos X="46" Y="28"/> 64 <UsageCount Value="148"/> 65 65 <Loaded Value="True"/> 66 66 <LoadedDesigner Value="True"/> … … 73 73 <TopLine Value="1"/> 74 74 <CursorPos X="1" Y="1"/> 75 <UsageCount Value="14 6"/>75 <UsageCount Value="148"/> 76 76 </Unit2> 77 77 <Unit3> … … 81 81 <EditorIndex Value="5"/> 82 82 <WindowIndex Value="0"/> 83 <TopLine Value=" 637"/>84 <CursorPos X="1 3" Y="668"/>85 <UsageCount Value="14 6"/>83 <TopLine Value="168"/> 84 <CursorPos X="12" Y="181"/> 85 <UsageCount Value="148"/> 86 86 <Loaded Value="True"/> 87 87 </Unit3> … … 94 94 <TopLine Value="1"/> 95 95 <CursorPos X="40" Y="16"/> 96 <UsageCount Value="14 6"/>96 <UsageCount Value="148"/> 97 97 <Loaded Value="True"/> 98 98 </Unit4> … … 105 105 <TopLine Value="15"/> 106 106 <CursorPos X="19" Y="28"/> 107 <UsageCount Value="14 6"/>107 <UsageCount Value="148"/> 108 108 <Loaded Value="True"/> 109 109 </Unit5> … … 112 112 <IsPartOfProject Value="True"/> 113 113 <UnitName Value="UProducer"/> 114 <IsVisibleTab Value="True"/>115 114 <EditorIndex Value="1"/> 116 115 <WindowIndex Value="0"/> 117 116 <TopLine Value="1"/> 118 117 <CursorPos X="5" Y="13"/> 119 <UsageCount Value="14 6"/>118 <UsageCount Value="148"/> 120 119 <Loaded Value="True"/> 121 120 </Unit6> … … 128 127 <TopLine Value="2"/> 129 128 <CursorPos X="13" Y="15"/> 130 <UsageCount Value="14 6"/>129 <UsageCount Value="148"/> 131 130 <Loaded Value="True"/> 132 131 </Unit7> … … 137 136 <EditorIndex Value="0"/> 138 137 <WindowIndex Value="0"/> 139 <TopLine Value="10 21"/>140 <CursorPos X="1 " Y="1026"/>141 <UsageCount Value="14 6"/>138 <TopLine Value="1032"/> 139 <CursorPos X="16" Y="1059"/> 140 <UsageCount Value="148"/> 142 141 <Loaded Value="True"/> 143 142 </Unit8> … … 164 163 <TopLine Value="61"/> 165 164 <CursorPos X="7" Y="68"/> 166 <UsageCount Value="5 6"/>165 <UsageCount Value="57"/> 167 166 <Loaded Value="True"/> 168 167 </Unit11> … … 173 172 <TopLine Value="139"/> 174 173 <CursorPos X="16" Y="146"/> 175 <UsageCount Value="5 6"/>174 <UsageCount Value="57"/> 176 175 <Loaded Value="True"/> 177 176 </Unit12> … … 205 204 <TopLine Value="1"/> 206 205 <CursorPos X="20" Y="14"/> 207 <UsageCount Value="1 08"/>206 <UsageCount Value="110"/> 208 207 <Loaded Value="True"/> 209 208 </Unit16> … … 227 226 <IsPartOfProject Value="True"/> 228 227 <UnitName Value="UProducerPascal"/> 228 <IsVisibleTab Value="True"/> 229 229 <EditorIndex Value="7"/> 230 230 <WindowIndex Value="0"/> 231 <TopLine Value=" 1"/>232 <CursorPos X=" 23" Y="9"/>233 <UsageCount Value="6 2"/>231 <TopLine Value="88"/> 232 <CursorPos X="15" Y="99"/> 233 <UsageCount Value="64"/> 234 234 <Loaded Value="True"/> 235 235 </Unit19> 236 236 </Units> 237 <JumpHistory Count="30" HistoryIndex="2 9">237 <JumpHistory Count="30" HistoryIndex="28"> 238 238 <Position1> 239 <Filename Value=" Analyze\UParser.pas"/>240 <Caret Line=" 1001" Column="28" TopLine="988"/>239 <Filename Value="Forms\UMainForm.pas"/> 240 <Caret Line="86" Column="42" TopLine="66"/> 241 241 </Position1> 242 242 <Position2> 243 <Filename Value=" Analyze\UParser.pas"/>244 <Caret Line=" 1024" Column="57" TopLine="1011"/>243 <Filename Value="Forms\UMainForm.pas"/> 244 <Caret Line="9" Column="7" TopLine="1"/> 245 245 </Position2> 246 246 <Position3> 247 <Filename Value=" Analyze\UParser.pas"/>248 <Caret Line="1 022" Column="45" TopLine="1009"/>247 <Filename Value="Produce\UProducerTreeView.pas"/> 248 <Caret Line="14" Column="9" TopLine="7"/> 249 249 </Position3> 250 250 <Position4> 251 <Filename Value=" Analyze\UParser.pas"/>252 <Caret Line=" 1030" Column="39" TopLine="1017"/>251 <Filename Value="Forms\UMainForm.pas"/> 252 <Caret Line="84" Column="39" TopLine="71"/> 253 253 </Position4> 254 254 <Position5> 255 <Filename Value=" Analyze\UParser.pas"/>256 <Caret Line=" 1031" Column="41" TopLine="1018"/>255 <Filename Value="Forms\UMainForm.pas"/> 256 <Caret Line="88" Column="1" TopLine="81"/> 257 257 </Position5> 258 258 <Position6> 259 <Filename Value=" Analyze\UParser.pas"/>260 <Caret Line=" 1067" Column="63" TopLine="1051"/>259 <Filename Value="Forms\UMainForm.pas"/> 260 <Caret Line="86" Column="57" TopLine="73"/> 261 261 </Position6> 262 262 <Position7> 263 <Filename Value=" Analyze\UParser.pas"/>264 <Caret Line=" 1058" Column="54" TopLine="1045"/>263 <Filename Value="Forms\UMainForm.pas"/> 264 <Caret Line="85" Column="57" TopLine="72"/> 265 265 </Position7> 266 266 <Position8> 267 267 <Filename Value="Forms\UMainForm.pas"/> 268 <Caret Line=" 112" Column="60" TopLine="97"/>268 <Caret Line="99" Column="69" TopLine="75"/> 269 269 </Position8> 270 270 <Position9> 271 271 <Filename Value="Forms\UMainForm.pas"/> 272 <Caret Line=" 111" Column="60" TopLine="96"/>272 <Caret Line="65" Column="31" TopLine="52"/> 273 273 </Position9> 274 274 <Position10> 275 <Filename Value=" Produce\UProducer.pas"/>276 <Caret Line=" 11" Column="11" TopLine="1"/>275 <Filename Value="Forms\UMainForm.pas"/> 276 <Caret Line="76" Column="31" TopLine="63"/> 277 277 </Position10> 278 278 <Position11> 279 279 <Filename Value="Forms\UMainForm.pas"/> 280 <Caret Line=" 111" Column="60" TopLine="96"/>280 <Caret Line="77" Column="11" TopLine="64"/> 281 281 </Position11> 282 282 <Position12> 283 <Filename Value=" Produce\UProducerTreeView.pas"/>284 <Caret Line=" 36" Column="16" TopLine="17"/>283 <Filename Value="Forms\UMainForm.pas"/> 284 <Caret Line="87" Column="31" TopLine="74"/> 285 285 </Position12> 286 286 <Position13> 287 <Filename Value=" Produce\UProducerTreeView.pas"/>288 <Caret Line=" 35" Column="1" TopLine="23"/>287 <Filename Value="Forms\UMainForm.pas"/> 288 <Caret Line="98" Column="31" TopLine="85"/> 289 289 </Position13> 290 290 <Position14> 291 <Filename Value=" Forms\UMainForm.pas"/>292 <Caret Line=" 84" Column="35" TopLine="71"/>291 <Filename Value="Produce\UProducerC.pas"/> 292 <Caret Line="128" Column="17" TopLine="128"/> 293 293 </Position14> 294 294 <Position15> 295 <Filename Value=" Forms\UMainForm.pas"/>296 <Caret Line=" 86" Column="42" TopLine="66"/>295 <Filename Value="Produce\UProducerC.pas"/> 296 <Caret Line="15" Column="5" TopLine="2"/> 297 297 </Position15> 298 298 <Position16> 299 <Filename Value=" Forms\UMainForm.pas"/>300 <Caret Line=" 9" Column="7" TopLine="1"/>299 <Filename Value="Produce\UProducerAsm8051.pas"/> 300 <Caret Line="103" Column="22" TopLine="97"/> 301 301 </Position16> 302 302 <Position17> 303 <Filename Value=" Produce\UProducerTreeView.pas"/>304 <Caret Line=" 14" Column="9" TopLine="7"/>303 <Filename Value="Forms\UMainForm.pas"/> 304 <Caret Line="87" Column="35" TopLine="77"/> 305 305 </Position17> 306 306 <Position18> 307 307 <Filename Value="Forms\UMainForm.pas"/> 308 <Caret Line=" 84" Column="39" TopLine="71"/>308 <Caret Line="11" Column="27" TopLine="1"/> 309 309 </Position18> 310 310 <Position19> 311 <Filename Value=" Forms\UMainForm.pas"/>312 <Caret Line=" 88" Column="1" TopLine="81"/>311 <Filename Value="Analyze\UParser.pas"/> 312 <Caret Line="1012" Column="81" TopLine="1009"/> 313 313 </Position19> 314 314 <Position20> 315 <Filename Value=" Forms\UMainForm.pas"/>316 <Caret Line=" 86" Column="57" TopLine="73"/>315 <Filename Value="Analyze\UParser.pas"/> 316 <Caret Line="1027" Column="62" TopLine="1011"/> 317 317 </Position20> 318 318 <Position21> 319 <Filename Value=" Forms\UMainForm.pas"/>320 <Caret Line=" 85" Column="57" TopLine="72"/>319 <Filename Value="Analyze\UParser.pas"/> 320 <Caret Line="1022" Column="21" TopLine="1018"/> 321 321 </Position21> 322 322 <Position22> 323 <Filename Value=" Forms\UMainForm.pas"/>324 <Caret Line=" 99" Column="69" TopLine="75"/>323 <Filename Value="Analyze\UParser.pas"/> 324 <Caret Line="1011" Column="55" TopLine="1006"/> 325 325 </Position22> 326 326 <Position23> 327 <Filename Value=" Forms\UMainForm.pas"/>328 <Caret Line=" 65" Column="31" TopLine="52"/>327 <Filename Value="Analyze\UParser.pas"/> 328 <Caret Line="1014" Column="3" TopLine="1011"/> 329 329 </Position23> 330 330 <Position24> 331 <Filename Value=" Forms\UMainForm.pas"/>332 <Caret Line=" 76" Column="31" TopLine="63"/>331 <Filename Value="Analyze\UParser.pas"/> 332 <Caret Line="1027" Column="65" TopLine="1014"/> 333 333 </Position24> 334 334 <Position25> 335 <Filename Value=" Forms\UMainForm.pas"/>336 <Caret Line=" 77" Column="11" TopLine="64"/>335 <Filename Value="Produce\UProducerPascal.pas"/> 336 <Caret Line="9" Column="23" TopLine="1"/> 337 337 </Position25> 338 338 <Position26> 339 <Filename Value=" Forms\UMainForm.pas"/>340 <Caret Line=" 87" Column="31" TopLine="74"/>339 <Filename Value="Produce\UProducerPascal.pas"/> 340 <Caret Line="20" Column="15" TopLine="7"/> 341 341 </Position26> 342 342 <Position27> 343 <Filename Value=" Forms\UMainForm.pas"/>344 <Caret Line="9 8" Column="31" TopLine="85"/>343 <Filename Value="Produce\UProducerPascal.pas"/> 344 <Caret Line="97" Column="1" TopLine="88"/> 345 345 </Position27> 346 346 <Position28> 347 <Filename Value="Produce\UProducer C.pas"/>348 <Caret Line="1 28" Column="17" TopLine="128"/>347 <Filename Value="Produce\UProducerPascal.pas"/> 348 <Caret Line="160" Column="62" TopLine="151"/> 349 349 </Position28> 350 350 <Position29> 351 <Filename Value="Produce\UProducer C.pas"/>352 <Caret Line="15 " Column="5" TopLine="2"/>351 <Filename Value="Produce\UProducerPascal.pas"/> 352 <Caret Line="158" Column="14" TopLine="146"/> 353 353 </Position29> 354 354 <Position30> 355 <Filename Value=" Produce\UProducerAsm8051.pas"/>356 <Caret Line="1 03" Column="22" TopLine="97"/>355 <Filename Value="USourceCode.pas"/> 356 <Caret Line="181" Column="12" TopLine="168"/> 357 357 </Position30> 358 358 </JumpHistory> -
branches/DelphiToC/Example.pas
r51 r53 8 8 Y: Integer; 9 9 end; 10 11 TArrayOfByte = array[Byte] of Byte; 10 12 11 13 function Max(A, B: Byte): Byte; -
branches/DelphiToC/Forms/UMainForm.lfm
r52 r53 1 1 object MainForm: TMainForm 2 Left = 1 762 Left = 141 3 3 Height = 498 4 Top = 774 Top = 105 5 5 Width = 881 6 Caption = ' Pascal Compiler AVR'6 Caption = 'Transpascal ' 7 7 ClientHeight = 498 8 8 ClientWidth = 881 … … 15 15 Position = poDesktopCenter 16 16 LCLVersion = '0.9.29' 17 object Button 1: TButton18 Left = 80 017 object ButtonCompile: TButton 18 Left = 801 19 19 Height = 22 20 20 Top = 401 … … 22 22 Anchors = [akRight, akBottom] 23 23 Caption = 'Kompilovat' 24 OnClick = Button1Click 24 Font.Height = -11 25 Font.Name = 'Tahoma' 26 OnClick = ButtonCompileClick 27 ParentFont = False 25 28 TabOrder = 0 26 29 end -
branches/DelphiToC/Forms/UMainForm.pas
r52 r53 18 18 19 19 TMainForm = class(TForm) 20 Button 1: TButton;20 ButtonCompile: TButton; 21 21 ComboBox1: TComboBox; 22 22 ListBox1: TListBox; … … 27 27 procedure FormShow(Sender: TObject); 28 28 procedure FormClose(Sender: TObject; var Action: TCloseAction); 29 procedure Button 1Click(Sender: TObject);29 procedure ButtonCompileClick(Sender: TObject); 30 30 procedure FormCreate(Sender: TObject); 31 31 procedure FormDestroy(Sender: TObject); … … 48 48 { TMainForm } 49 49 50 procedure TMainForm.Button 1Click(Sender: TObject);50 procedure TMainForm.ButtonCompileClick(Sender: TObject); 51 51 var 52 52 I: Integer; … … 64 64 ProducerControl.Parent := Panel1; 65 65 ProducerControl.Align := alClient; 66 ProducerControl.Font.Name := 'Courier New'; 67 TMemo(ProducerControl).ScrollBars := ssAutoBoth; 66 68 with TProducerPascal(Compiler.Producer) do 67 69 TMemo(ProducerControl).Lines.Assign(TextSource); … … 75 77 ProducerControl.Parent := Panel1; 76 78 ProducerControl.Align := alClient; 79 ProducerControl.Font.Name := 'Courier New'; 80 TMemo(ProducerControl).ScrollBars := ssAutoBoth; 77 81 with TProducerC(Compiler.Producer) do 78 82 TMemo(ProducerControl).Lines.Assign(TextSource); … … 86 90 ProducerControl.Parent := Panel1; 87 91 ProducerControl.Align := alClient; 92 ProducerControl.Font.Name := 'Courier New'; 93 TMemo(ProducerControl).ScrollBars := ssAutoBoth; 88 94 with TProducerAsm8051(Compiler.Producer) do 89 95 for I := 0 to AssemblyCode.Count - 1 do … … 143 149 WindowState := wsMaximized; 144 150 SynEdit1.Lines.LoadFromFile(ExampleFileName); 145 Button 1Click(Self);151 ButtonCompileClick(Self); 146 152 end; 147 153 148 154 procedure TMainForm.ComboBox1Change(Sender: TObject); 149 155 begin 150 Button 1Click(Self);156 ButtonCompileClick(Self); 151 157 end; 152 158 -
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 -
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); -
branches/DelphiToC/Produce/UProducerTreeView.pas
r52 r53 22 22 procedure AddNodeTypeRecord(Node: TTreeNode; TypeRecord: TTypeRecord); 23 23 procedure AddNodeTypeList(Node: TTreeNode; Types: TTypeList); 24 procedure AddNodeType(Node: TTreeNode; AType: TType); 24 25 procedure AddNodeVariableList(Node: TTreeNode; Variables: TVariableList); 25 26 procedure AddNodeConstantList(Node: TTreeNode; Constants: TConstantList); … … 56 57 NewNode: TTreeNode; 57 58 NewNode2: TTreeNode; 58 NewNode3: TTreeNode;59 I: Integer;60 59 begin 61 60 NewNode := TreeView.Items.AddChild(Node, 'while'); … … 194 193 var 195 194 NewNode: TTreeNode; 196 NewNode2: TTreeNode;197 NewNode3: TTreeNode;198 195 I: Integer; 199 196 begin … … 202 199 for I := 0 to Types.Count - 1 do 203 200 with TType(Types[I]) do 204 if not System then begin 205 if TType(Types[I]) is TTypeRecord then begin 206 NewNode2 := TreeView.Items.AddChild(NewNode, Name + ' = record'); 207 AddNodeTypeRecord(NewNode2, TTypeRecord(Types[I])); 208 end else if Assigned(UsedType) then 209 NewNode2 := TreeView.Items.AddChild(NewNode, Name + ' = ' + UsedType.Name) 210 else NewNode2 := TreeView.Items.AddChild(NewNode, Name); 211 end; 201 if not System then AddNodeType(NewNode, TType(Types[I])); 202 end; 203 end; 204 205 procedure TProducerTreeView.AddNodeType(Node: TTreeNode; AType: TType); 206 var 207 NewNode: TTreeNode; 208 NewNode2: TTreeNode; 209 begin 210 with AType do begin 211 if AType is TTypeRecord then begin 212 NewNode := TreeView.Items.AddChild(Node, Name + ' = record'); 213 AddNodeTypeRecord(NewNode, TTypeRecord(AType)); 214 end else 215 if AType is TTypeArray then begin 216 NewNode := TreeView.Items.AddChild(Node, Name + ' = array'); 217 if Assigned(TTypeArray(AType).IndexType) then begin 218 NewNode2 := TreeView.Items.AddChild(NewNode, 'Index'); 219 AddNodeType(NewNode2, TTypeArray(AType).IndexType); 220 end; 221 if Assigned(TTypeArray(AType).ItemType) then begin 222 NewNode2 := TreeView.Items.AddChild(NewNode, 'ItemType'); 223 AddNodeType(NewNode2, TTypeArray(AType).ItemType); 224 end; 225 end else if Assigned(UsedType) then 226 NewNode := TreeView.Items.AddChild(Node, Name + ' = ' + UsedType.Name) 227 else NewNode := TreeView.Items.AddChild(Node, Name); 212 228 end; 213 229 end; … … 217 233 var 218 234 NewNode: TTreeNode; 219 NewNode2: TTreeNode;220 235 I: Integer; 221 236 begin … … 253 268 ); 254 269 var 255 NewNode: TTreeNode;256 NewNode2: TTreeNode;257 NewNode3: TTreeNode;258 270 I: Integer; 259 271 begin … … 261 273 for I := 0 to TypeRecord.Items.Count - 1 do 262 274 with TType(TypeRecord.Items[I]) do 263 if not System then begin 264 if TType(TypeRecord.Items[I]) is TTypeRecord then begin 265 NewNode2 := TreeView.Items.AddChild(NewNode, Name + ' = record'); 266 AddNodeTypeRecord(NewNode2, TTypeRecord(TypeRecord.Items[I])) 267 end else if Assigned(UsedType) then 268 NewNode2 := TreeView.Items.AddChild(NewNode, Name + ' = ' + UsedType.Name) 269 else NewNode2 := TreeView.Items.AddChild(NewNode, Name); 270 end; 275 if not System then 276 AddNodeType(Node, TType(TypeRecord.Items[I])); 271 277 end; 272 278 end; -
branches/DelphiToC/UCompiler.pas
r52 r53 92 92 with TType(Types[Types.Add(TType.Create)]) do begin 93 93 Name := 'Void'; 94 System := True; 94 95 Size := 0; 95 96 UsedType := nil; … … 97 98 with TType(Types[Types.Add(TType.Create)]) do begin 98 99 Name := 'Byte'; 100 System := True; 99 101 Size := 1; 100 102 UsedType := nil; -
branches/DelphiToC/USourceCode.pas
r51 r53 169 169 170 170 TTypeArray = class(TType) 171 //Range: TTypeRange;171 IndexType: TType; 172 172 ItemType: TType; 173 173 end; … … 180 180 181 181 TConstant = class 182 System: Boolean; 182 183 Name: string; 183 184 ValueType: TType;
Note:
See TracChangeset
for help on using the changeset viewer.