Changeset 48 for branches/DelphiToC/Produce/UProducerPascal.pas
- Timestamp:
- Aug 9, 2010, 1:53:33 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DelphiToC/Produce/UProducerPascal.pas
r47 r48 15 15 TProducerPascal = class(TCodeProducer) 16 16 private 17 procedure Emit(Text: string );17 procedure Emit(Text: string; NewLine: Boolean = True); 18 18 procedure GenerateUses(UsedModules: TUsedModuleList); 19 19 procedure GenerateModule(Module: TModule); … … 22 22 procedure GenerateProgram(ProgramBlock: TProgram); 23 23 procedure GenerateFunctions(Functions: TFunctionList); 24 procedure GenerateConstants(Constants: TConstantList); 24 25 procedure GenerateBeginEnd(BeginEnd: TBeginEnd); 25 26 procedure GenerateVariableList(Variables: TVariableList); … … 28 29 procedure GenerateIfThenElse(IfThenElse: TIfThenElse); 29 30 procedure GenerateAssignment(Assignment: TAssignment); 30 procedure Generate MethodCall(MethodCall: TFunctionCall);31 procedure GenerateFunctionCall(ConstantCall: TFunctionCall); 31 32 function GenerateExpression(Expression: TExpression): string; 32 33 public … … 55 56 end; 56 57 57 procedure TProducerPascal.Emit(Text: string); 58 begin 59 TextSource.Add(DupeString(' ', IndentationLength * Indetation) + Text); 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; 60 62 end; 61 63 … … 68 70 for I := 0 to UsedModules.Count - 1 do begin 69 71 Line := Line + TUsedModule(UsedModules[I]).Name; 70 if I < UsedModules.Count then Line := Line + ', ';71 end; 72 Emit(Line );72 if I < UsedModules.Count - 1 then Line := Line + ', '; 73 end; 74 Emit(Line + ';'); 73 75 Emit(''); 74 76 end; … … 78 80 GenerateUses(Module.UsedModules); 79 81 GenerateCommonBlock(Module, ''); 82 Emit(';', False); 80 83 end; 81 84 … … 125 128 end; 126 129 130 procedure TProducerPascal.GenerateConstants(Constants: TConstantList); 131 var 132 I: Integer; 133 begin 134 Emit('const'); 135 Inc(Indetation); 136 for I := 0 to Constants.Count - 1 do 137 with TConstant(Constants[I]) do 138 Emit(Name + ': ' + ValueType.Name + ' = ' + Value + ';'); 139 Dec(Indetation); 140 Emit(''); 141 end; 142 127 143 procedure TProducerPascal.GenerateBeginEnd(BeginEnd: TBeginEnd); 128 144 var … … 132 148 Inc(Indetation); 133 149 // Commands 134 for I := 0 to BeginEnd.Commands.Count - 1 do 150 for I := 0 to BeginEnd.Commands.Count - 1 do begin 135 151 GenerateCommand(TCommand(BeginEnd.Commands[I])); 152 Emit(';', False); 153 end; 136 154 137 155 Dec(Indetation); 138 Emit('end ;');156 Emit('end'); 139 157 end; 140 158 … … 144 162 begin 145 163 Emit('var'); 164 Inc(Indetation); 146 165 for I := 0 to Variables.Count - 1 do 147 166 with TVariable(Variables[I]) do 148 167 Emit(Name + ': ' + ValueType.Name + ';'); 168 Dec(Indetation); 149 169 Emit(''); 150 170 end; … … 156 176 else if Command is TIfThenElse then GenerateIfThenElse(TIfThenElse(Command)) 157 177 else if Command is TAssignment then GenerateAssignment(TAssignment(Command)) 158 else if Command is TFunctionCall then Generate MethodCall(TFunctionCall(Command));178 else if Command is TFunctionCall then GenerateFunctionCall(TFunctionCall(Command)); 159 179 end; 160 180 … … 177 197 procedure TProducerPascal.GenerateAssignment(Assignment: TAssignment); 178 198 begin 179 Emit(Assignment.Target.Name + ' := ' + GenerateExpression(Assignment.Source) + ';'); 180 end; 181 182 procedure TProducerPascal.GenerateMethodCall(MethodCall: TFunctionCall); 183 begin 184 Emit(MethodCall.FunctionRef.Name + ';'); 199 Emit(Assignment.Target.Name + ' := ' + GenerateExpression(Assignment.Source)); 200 end; 201 202 procedure TProducerPascal.GenerateFunctionCall(ConstantCall: TFunctionCall); 203 var 204 Line: string; 205 I: Integer; 206 begin 207 with ConstantCall do begin 208 Line := FunctionRef.Name; 209 if ParameterExpression.Count > 0 then begin 210 Line := Line + '('; 211 for I := 0 to ParameterExpression.Count - 1 do begin 212 Line := Line + GenerateExpression(TExpression(ParameterExpression[I])); 213 if I < ParameterExpression.Count - 1 then Line := Line + ', '; 214 end; 215 Line := Line + ')'; 216 end; 217 end; 218 Emit(Line); 185 219 end; 186 220 … … 206 240 begin 207 241 with CommonBlock do begin 208 GenerateFunctions(Methods); 209 Emit('procedure ' + Name + ''); 242 GenerateFunctions(Functions); 243 GenerateConstants(Constants); 244 GenerateVariableList(Variables); 210 245 GenerateBeginEnd(Code); 211 246 end;
Note:
See TracChangeset
for help on using the changeset viewer.