1 | unit UCGenerator;
|
---|
2 |
|
---|
3 | {$mode Delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, UModel, StrUtils, UOutputGenerator;
|
---|
9 |
|
---|
10 | type
|
---|
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 |
|
---|
20 | implementation
|
---|
21 |
|
---|
22 | { TCGenerator }
|
---|
23 |
|
---|
24 | function TCGenerator.ConvertType(Name: string): string;
|
---|
25 | begin
|
---|
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;
|
---|
35 | end;
|
---|
36 |
|
---|
37 | function TCGenerator.GenerateVariableValue(Value: TVariableValue): string;
|
---|
38 | begin
|
---|
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;
|
---|
47 | end;
|
---|
48 |
|
---|
49 | procedure TCGenerator.Generate(Model: TModel);
|
---|
50 | begin
|
---|
51 | inherited;
|
---|
52 | IndentCount := 0;
|
---|
53 | GenerateModule(Model.Module);
|
---|
54 | end;
|
---|
55 |
|
---|
56 | procedure TCGenerator.GenerateModule(Module: TModule);
|
---|
57 | var
|
---|
58 | I, P: Integer;
|
---|
59 | Row: string;
|
---|
60 | ParameterText: string;
|
---|
61 | begin
|
---|
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;
|
---|
99 | end;
|
---|
100 |
|
---|
101 | end.
|
---|
102 |
|
---|
103 |
|
---|