| 1 | unit UPascalGenerator;
|
|---|
| 2 |
|
|---|
| 3 | {$mode Delphi}{$H+}
|
|---|
| 4 |
|
|---|
| 5 | interface
|
|---|
| 6 |
|
|---|
| 7 | uses
|
|---|
| 8 | Classes, SysUtils, UModel, StrUtils, UOutputGenerator;
|
|---|
| 9 |
|
|---|
| 10 | type
|
|---|
| 11 | TPascalGenerator = class(TOutputGenerator)
|
|---|
| 12 | private
|
|---|
| 13 | function GenerateVariableValue(Value: TVariableValue): string;
|
|---|
| 14 | public
|
|---|
| 15 | procedure Generate(Model: TModel); override;
|
|---|
| 16 | procedure GenerateModule(Module: TModule);
|
|---|
| 17 | end;
|
|---|
| 18 |
|
|---|
| 19 | implementation
|
|---|
| 20 |
|
|---|
| 21 | { TPascalGenerator }
|
|---|
| 22 |
|
|---|
| 23 | function TPascalGenerator.GenerateVariableValue(Value: TVariableValue): string;
|
|---|
| 24 | begin
|
|---|
| 25 | case Value.ValueType of
|
|---|
| 26 | vtVariable: if Assigned(Value.VariableDef) then Result := Value.VariableDef.Name
|
|---|
| 27 | else Result := '';
|
|---|
| 28 | vtFloat: Result := FloatToStr(Value.FloatConstant);
|
|---|
| 29 | vtNumber: Result := IntToStr(Value.NumberConstant);
|
|---|
| 30 | vtString: Result := '''' + StringReplace(Value.StringConstant, '''', '''''', [rfReplaceAll]) + '''';
|
|---|
| 31 | vtChar: Result := '''' + Value.CharConstant + '''';
|
|---|
| 32 | end;
|
|---|
| 33 | end;
|
|---|
| 34 |
|
|---|
| 35 | procedure TPascalGenerator.Generate(Model: TModel);
|
|---|
| 36 | begin
|
|---|
| 37 | inherited;
|
|---|
| 38 | GenerateModule(Model.Module);
|
|---|
| 39 | end;
|
|---|
| 40 |
|
|---|
| 41 | procedure TPascalGenerator.GenerateModule(Module: TModule);
|
|---|
| 42 | var
|
|---|
| 43 | I, P: Integer;
|
|---|
| 44 | ParameterText: string;
|
|---|
| 45 | Row: string;
|
|---|
| 46 | begin
|
|---|
| 47 | with Module do begin
|
|---|
| 48 | // Prepare output
|
|---|
| 49 | Output.Clear;
|
|---|
| 50 | Output.Add('program ' + Name + ';');
|
|---|
| 51 | Output.Add('{$APPTYPE CONSOLE}');
|
|---|
| 52 |
|
|---|
| 53 | // var section
|
|---|
| 54 | if Variables.Count > 0 then Output.Add('var');
|
|---|
| 55 | Inc(IndentCount);
|
|---|
| 56 | for I := 0 to Variables.Count - 1 do
|
|---|
| 57 | with Variables[I] do
|
|---|
| 58 | Output.Add(Indent + Name + ': ' + VarType.Name + ';');
|
|---|
| 59 | Dec(IndentCount);
|
|---|
| 60 |
|
|---|
| 61 | // Code block
|
|---|
| 62 | Output.Add('begin');
|
|---|
| 63 | Inc(IndentCount);
|
|---|
| 64 | for I := 0 to BeginEnd.Commands.Count - 1 do
|
|---|
| 65 | with BeginEnd.Commands[I] do begin
|
|---|
| 66 | if Name = 'Assignment' then Output.Add(Indent +
|
|---|
| 67 | Parameters[0].VariableDef.Name + ' := ' +
|
|---|
| 68 | GenerateVariableValue(TVariableValue(Parameters[1])) + ';')
|
|---|
| 69 | else begin
|
|---|
| 70 | Row := Name;
|
|---|
| 71 | if Parameters.Count > 0 then begin
|
|---|
| 72 | ParameterText := '';
|
|---|
| 73 | for P := 0 to Parameters.Count - 1 do
|
|---|
| 74 | ParameterText := ParameterText + GenerateVariableValue(Parameters[P]) + ', ';
|
|---|
| 75 | Row := Row + '(' + Copy(ParameterText, 1, Length(ParameterText) - 2) + ')';
|
|---|
| 76 | end;
|
|---|
| 77 | Output.Add(Indent + Row + ';');
|
|---|
| 78 | end;
|
|---|
| 79 | end;
|
|---|
| 80 | Dec(IndentCount);
|
|---|
| 81 | Output.Add('end.');
|
|---|
| 82 | end;
|
|---|
| 83 | end;
|
|---|
| 84 |
|
|---|
| 85 | end.
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|