Changeset 13 for branches/Void/UOutputGenerator.pas
- Timestamp:
- Nov 10, 2009, 7:14:14 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Void/UOutputGenerator.pas
r11 r13 6 6 7 7 uses 8 Classes, SysUtils, UModel ;8 Classes, SysUtils, UModel, StrUtils; 9 9 10 10 type 11 12 { TOutputGenerator }13 14 11 TOutputGenerator = class 15 12 private 13 IndentCount: Integer; 16 14 Model: TModel; 15 function Indent: string; 17 16 public 18 17 Output: TStringList; … … 22 21 end; 23 22 24 { TPascalGenerator }25 26 23 TPascalGenerator = class(TOutputGenerator) 27 24 procedure Generate(Model: TModel); override; … … 29 26 end; 30 27 31 { TCGenerator }32 33 28 TCGenerator = class(TOutputGenerator) 29 private 30 function ConvertType(Name: string): string; 31 public 34 32 procedure Generate(Model: TModel); override; 35 33 procedure GenerateModule(Module: TModule); … … 39 37 40 38 { TOutputGenerator } 39 40 function TOutputGenerator.Indent: string; 41 begin 42 Result := DupeString(' ', IndentCount); 43 end; 41 44 42 45 procedure TOutputGenerator.Generate(Model: TModel); … … 78 81 // var section 79 82 if Variables.Count > 0 then Output.Add('var'); 83 Inc(IndentCount); 80 84 for I := 0 to Variables.Count - 1 do 81 85 with TVariable(Variables[I]) do 82 Output.Add(' ' + Name + ': ' + VarType + ';'); 86 Output.Add(Indent + Name + ': ' + VarType.Name + ';'); 87 Dec(IndentCount); 83 88 84 89 // Code block 85 90 Output.Add('begin'); 91 Inc(IndentCount); 86 92 for I := 0 to BeginEnd.Commands.Count - 1 do 87 93 with TCommand(BeginEnd.Commands[I]) do begin 88 if Name = 'Assignment' then Output.Add( Parameters[0] + ' := ' + Parameters[1] + ';')94 if Name = 'Assignment' then Output.Add(Indent + Parameters[0] + ' := ' + Parameters[1] + ';') 89 95 else begin 90 96 Row := Name; … … 95 101 Row := Row + '(' + Copy(ParameterText, 1, Length(ParameterText) - 2) + ')'; 96 102 end; 97 Output.Add( Row + ';');103 Output.Add(Indent + Row + ';'); 98 104 end; 99 105 end; 106 Dec(IndentCount); 100 107 Output.Add('end.'); 101 108 end; … … 104 111 { TCGenerator } 105 112 113 function TCGenerator.ConvertType(Name: string): string; 114 begin 115 if Name = 'String' then Result := 'char*' 116 else if Name = 'Byte' then Result := 'unsigned char' 117 else if Name = 'ShortInt' then Result := 'signed char' 118 else if Name = 'Word' then Result := 'unsigned int' 119 else if Name = 'Integer' then Result := 'int' 120 else if Name = 'Real' then Result := 'float' 121 else if Name = 'Double' then Result := 'double' 122 else if Name = 'Char' then Result := 'char' 123 else Result := Name; 124 end; 125 106 126 procedure TCGenerator.Generate(Model: TModel); 107 127 begin 108 128 inherited; 129 IndentCount := 0; 109 130 GenerateModule(Model.Module); 110 131 end; … … 122 143 Output.Add('int main()'); 123 144 Output.Add('{'); 145 Inc(IndentCount); 124 146 125 147 // variable section 126 148 for I := 0 to Variables.Count - 1 do 127 149 with TVariable(Variables[I]) do 128 Output.Add(' ' + VarType + ' ' + Name + ';'); 150 Output.Add(Indent + ' ' + ConvertType(VarType.Name) + ' ' + Name + ';'); 151 if Variables.Count > 0 then Output.Add(''); 129 152 130 153 // Code block 131 154 for I := 0 to BeginEnd.Commands.Count - 1 do 132 155 with TCommand(BeginEnd.Commands[I]) do begin 133 if Name = 'Assignment' then Output.Add( Parameters[0] + ' = ' + Parameters[1] + ';')156 if Name = 'Assignment' then Output.Add(Indent + Parameters[0] + ' = ' + Parameters[1] + ';') 134 157 else begin 135 158 if Name = 'WriteLn' then Row := 'printf' … … 143 166 Row := Row + Copy(ParameterText, 1, Length(ParameterText) - 2); 144 167 end; 145 Output.Add( Row + ');');168 Output.Add(Indent + Row + ');'); 146 169 end; 147 170 end; 171 Dec(IndentCount); 148 172 Output.Add('}'); 149 173 end;
Note:
See TracChangeset
for help on using the changeset viewer.