Changeset 16 for branches/Void/UOutputGenerator.pas
- Timestamp:
- Nov 10, 2009, 10:27:21 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Void/UOutputGenerator.pas
r14 r16 11 11 TOutputGenerator = class 12 12 private 13 public 14 Model: TModel; 13 15 IndentCount: Integer; 14 Model: TModel;16 Output: TStringList; 15 17 function Indent: string; 16 public17 Output: TStringList;18 18 procedure Generate(Model: TModel); virtual; 19 19 constructor Create; 20 20 destructor Destroy; override; 21 end;22 23 { TPascalGenerator }24 25 TPascalGenerator = class(TOutputGenerator)26 private27 function GenerateVariableValue(Value: TVariableValue): string;28 public29 procedure Generate(Model: TModel); override;30 procedure GenerateModule(Module: TModule);31 end;32 33 { TCGenerator }34 35 TCGenerator = class(TOutputGenerator)36 private37 function ConvertType(Name: string): string;38 function GenerateVariableValue(Value: TVariableValue): string;39 public40 procedure Generate(Model: TModel); override;41 procedure GenerateModule(Module: TModule);42 21 end; 43 22 … … 67 46 end; 68 47 69 { TPascalGenerator }70 71 function TPascalGenerator.GenerateVariableValue(Value: TVariableValue): string;72 begin73 case Value.ValueType of74 vtVariable: if Assigned(Value.VariableDef) then Result := Value.VariableDef.Name75 else Result := '';76 vtFloat: Result := FloatToStr(Value.FloatConstant);77 vtNumber: Result := IntToStr(Value.NumberConstant);78 vtString: Result := '''' + Value.StringConstant + '''';79 vtChar: Result := '''' + Value.CharConstant + '''';80 end;81 end;82 83 procedure TPascalGenerator.Generate(Model: TModel);84 begin85 inherited;86 GenerateModule(Model.Module);87 end;88 89 procedure TPascalGenerator.GenerateModule(Module: TModule);90 var91 I, P: Integer;92 ParameterText: string;93 Row: string;94 begin95 with Module do begin96 // Prepare output97 Output.Clear;98 Output.Add('program Test;');99 Output.Add('{$APPTYPE CONSOLE}');100 101 // var section102 if Variables.Count > 0 then Output.Add('var');103 Inc(IndentCount);104 for I := 0 to Variables.Count - 1 do105 with TVariable(Variables[I]) do106 Output.Add(Indent + Name + ': ' + VarType.Name + ';');107 Dec(IndentCount);108 109 // Code block110 Output.Add('begin');111 Inc(IndentCount);112 for I := 0 to BeginEnd.Commands.Count - 1 do113 with TCommand(BeginEnd.Commands[I]) do begin114 if Name = 'Assignment' then Output.Add(Indent +115 TVariableValue(Parameters[0]).VariableDef.Name + ' := ' +116 GenerateVariableValue(TVariableValue(Parameters[1])) + ';')117 else begin118 Row := Name;119 if Parameters.Count > 0 then begin120 ParameterText := '';121 for P := 0 to Parameters.Count - 1 do122 ParameterText := ParameterText + GenerateVariableValue(Parameters[P]) + ', ';123 Row := Row + '(' + Copy(ParameterText, 1, Length(ParameterText) - 2) + ')';124 end;125 Output.Add(Indent + Row + ';');126 end;127 end;128 Dec(IndentCount);129 Output.Add('end.');130 end;131 end;132 133 { TCGenerator }134 135 function TCGenerator.ConvertType(Name: string): string;136 begin137 if Name = 'String' then Result := 'char*'138 else if Name = 'Byte' then Result := 'unsigned char'139 else if Name = 'ShortInt' then Result := 'signed char'140 else if Name = 'Word' then Result := 'unsigned int'141 else if Name = 'Integer' then Result := 'int'142 else if Name = 'Real' then Result := 'float'143 else if Name = 'Double' then Result := 'double'144 else if Name = 'Char' then Result := 'char'145 else Result := Name;146 end;147 148 function TCGenerator.GenerateVariableValue(Value: TVariableValue): string;149 begin150 case Value.ValueType of151 vtVariable: if Assigned(Value.VariableDef) then Result := Value.VariableDef.Name152 else Result := '';153 vtFloat: Result := FloatToStr(Value.FloatConstant);154 vtNumber: Result := IntToStr(Value.NumberConstant);155 vtString: Result := '"' + Value.StringConstant + '"';156 vtChar: Result := '''' + Value.CharConstant + '''';157 end;158 end;159 160 procedure TCGenerator.Generate(Model: TModel);161 begin162 inherited;163 IndentCount := 0;164 GenerateModule(Model.Module);165 end;166 167 procedure TCGenerator.GenerateModule(Module: TModule);168 var169 I, P: Integer;170 Row: string;171 ParameterText: string;172 begin173 with Module do begin174 // Prepare output175 Output.Clear;176 177 Output.Add('int main()');178 Output.Add('{');179 Inc(IndentCount);180 181 // Variable section182 for I := 0 to Variables.Count - 1 do183 with TVariable(Variables[I]) do184 Output.Add(Indent + ConvertType(VarType.Name) + ' ' + Name + ';');185 if Variables.Count > 0 then Output.Add('');186 187 // Code block188 for I := 0 to BeginEnd.Commands.Count - 1 do189 with TCommand(BeginEnd.Commands[I]) do begin190 if Name = 'Assignment' then Output.Add(Indent +191 TVariableValue(Parameters[0]).VariableDef.Name + ' = ' +192 GenerateVariableValue(TVariableValue(Parameters[1])) + ';')193 else begin194 if Name = 'WriteLn' then Row := 'printf'195 else if Name = 'ReadLn' then Row := 'scanf'196 else if Name = 'Exit' then Row := 'exit';197 Row := Row + '(';198 if Parameters.Count > 0 then begin199 ParameterText := '';200 for P := 0 to Parameters.Count - 1 do201 ParameterText := ParameterText + GenerateVariableValue(Parameters[P]) + ', ';202 Row := Row + Copy(ParameterText, 1, Length(ParameterText) - 2);203 end;204 Output.Add(Indent + Row + ');');205 end;206 end;207 Dec(IndentCount);208 Output.Add('}');209 end;210 end;211 212 48 end. 213 49
Note:
See TracChangeset
for help on using the changeset viewer.