source: branches/Void/Generators/UPascalGenerator.pas

Last change on this file was 18, checked in by george, 15 years ago
  • Upraveno: Nasazení generického TFPGObjectList pro použité seznamy typu TList.
  • Přidáno: Kostra tříd pro gramatickou analýzu.
File size: 2.4 KB
Line 
1unit UPascalGenerator;
2
3{$mode Delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, UModel, StrUtils, UOutputGenerator;
9
10type
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
19implementation
20
21{ TPascalGenerator }
22
23function TPascalGenerator.GenerateVariableValue(Value: TVariableValue): string;
24begin
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;
33end;
34
35procedure TPascalGenerator.Generate(Model: TModel);
36begin
37 inherited;
38 GenerateModule(Model.Module);
39end;
40
41procedure TPascalGenerator.GenerateModule(Module: TModule);
42var
43 I, P: Integer;
44 ParameterText: string;
45 Row: string;
46begin
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;
83end;
84
85end.
86
87
Note: See TracBrowser for help on using the repository browser.