source: branches/Void/Generators/UCGenerator.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: 3.0 KB
Line 
1unit UCGenerator;
2
3{$mode Delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, UModel, StrUtils, UOutputGenerator;
9
10type
11 TCGenerator = class(TOutputGenerator)
12 private
13 function ConvertType(Name: string): string;
14 function GenerateVariableValue(Value: TVariableValue): string;
15 public
16 procedure Generate(Model: TModel); override;
17 procedure GenerateModule(Module: TModule);
18 end;
19
20implementation
21
22{ TCGenerator }
23
24function TCGenerator.ConvertType(Name: string): string;
25begin
26 if Name = 'String' then Result := 'char*'
27 else if Name = 'Byte' then Result := 'unsigned char'
28 else if Name = 'ShortInt' then Result := 'signed char'
29 else if Name = 'Word' then Result := 'unsigned int'
30 else if Name = 'Integer' then Result := 'int'
31 else if Name = 'Real' then Result := 'float'
32 else if Name = 'Double' then Result := 'double'
33 else if Name = 'Char' then Result := 'char'
34 else Result := Name;
35end;
36
37function TCGenerator.GenerateVariableValue(Value: TVariableValue): string;
38begin
39 case Value.ValueType of
40 vtVariable: if Assigned(Value.VariableDef) then Result := Value.VariableDef.Name
41 else Result := '';
42 vtFloat: Result := FloatToStr(Value.FloatConstant);
43 vtNumber: Result := IntToStr(Value.NumberConstant);
44 vtString: Result := '"' + StringReplace(Value.StringConstant, '"', '\"', [rfReplaceAll]) + '"';
45 vtChar: Result := '''' + Value.CharConstant + '''';
46 end;
47end;
48
49procedure TCGenerator.Generate(Model: TModel);
50begin
51 inherited;
52 IndentCount := 0;
53 GenerateModule(Model.Module);
54end;
55
56procedure TCGenerator.GenerateModule(Module: TModule);
57var
58 I, P: Integer;
59 Row: string;
60 ParameterText: string;
61begin
62 with Module do begin
63 // Prepare output
64 Output.Clear;
65
66 Output.Add('int main()');
67 Output.Add('{');
68 Inc(IndentCount);
69
70 // Variable section
71 for I := 0 to Variables.Count - 1 do
72 with Variables[I] do
73 Output.Add(Indent + ConvertType(VarType.Name) + ' ' + Name + ';');
74 if Variables.Count > 0 then Output.Add('');
75
76 // Code block
77 for I := 0 to BeginEnd.Commands.Count - 1 do
78 with BeginEnd.Commands[I] do begin
79 if Name = 'Assignment' then Output.Add(Indent +
80 Parameters[0].VariableDef.Name + ' = ' +
81 GenerateVariableValue(TVariableValue(Parameters[1])) + ';')
82 else begin
83 if Name = 'WriteLn' then Row := 'printf'
84 else if Name = 'ReadLn' then Row := 'scanf'
85 else if Name = 'Exit' then Row := 'exit';
86 Row := Row + '(';
87 if Parameters.Count > 0 then begin
88 ParameterText := '';
89 for P := 0 to Parameters.Count - 1 do
90 ParameterText := ParameterText + GenerateVariableValue(Parameters[P]) + ', ';
91 Row := Row + Copy(ParameterText, 1, Length(ParameterText) - 2);
92 end;
93 Output.Add(Indent + Row + ');');
94 end;
95 end;
96 Dec(IndentCount);
97 Output.Add('}');
98 end;
99end;
100
101end.
102
103
Note: See TracBrowser for help on using the repository browser.