CodeGen
 All Classes Namespaces Files Functions Variables Properties Pages
CppGenerator.cs
Go to the documentation of this file.
1 using System;
2 using System.Linq;
3 
4 namespace CodeGen.generators
5 {
10  public class CppGenerator : Generator
11  {
12  private const string ClassFormat = "class {0} {1}\n{{{2}{3}{4}}}";
13  private string Indent { get; } = GeneratorConf.GetIndent(UseTabs, 4);
14 
16  protected override string GenerateClass(Class @class)
17  {
18  var parent = "";
19  if (@class.Parent?.Length > 0)
20  {
21  parent = " : " + @class.Parent + " " + @class.Parent;
22  }
23 
24  var @public = GenerateSection("public", @class);
25  var @protected = GenerateSection("protected", @class);
26  var @private = GenerateSection("private", @class);
27  return string.Format(ClassFormat, @class.Name, parent, @public, @protected, @private);
28  }
29 
31  public override string GenerateField(Field field)
32  {
33  if (!new CppValidator().FieldIsValid(field))
34  {
35  throw new ArgumentNullException();
36  }
37 
38  var result = Indent;
39  if (field.Static)
40  {
41  result += "static ";
42  }
43 
44  if (field.Const)
45  {
46  result += "const ";
47  }
48 
49  result += field.Type + " " + field.Name;
50  if (field.Default != "")
51  {
52  result += " = " + field.Default;
53  }
54 
55  result += ";";
56  return result;
57  }
58 
60  public override string GenerateMethod(Method method)
61  {
62  var result = "";
63 
64  if (method.Static)
65  {
66  result += "static ";
67  }
68 
69  switch (method.Type)
70  {
71  case "":
72  result += "void ";
73  break;
74  default:
75  result += method.Type + " ";
76  break;
77  }
78 
79  result += method.Name + "(";
80  for (var i = 0; i < method.Parameters?.Length; i++)
81  {
82  var parameter = method.Parameters[i];
83  result += parameter.Type + " " + parameter.Name;
84  if (!string.IsNullOrWhiteSpace(parameter.Default))
85  {
86  result += " = " + parameter.Default;
87  }
88  if (i + 1 < method.Parameters?.Length)
89  {
90  result += ", ";
91  }
92  }
93 
94  result += ");";
95  return result;
96  }
97 
104  private string GenerateSection(string access, Class @class)
105  {
106  var result = @class.Fields?.Where(field => access == field.Access)
107  .Aggregate("", (current, field) => current + (GenerateField(field) + "\n"));
108 
109  result = @class.Methods?.Where(method => access == method.Access).Aggregate(result,
110  (current, method) => current + (GeneratorConf.ShiftCode(GenerateMethod(method), 1, Indent) + "\n"));
111  result = @class.Classes?.Where(clas => access == clas.Access).Aggregate(result,
112  (current, clas) => current + (GeneratorConf.ShiftCode(GenerateClass(clas), 1, Indent) + "\n"));
113  if (result != "")
114  {
115  result = "\n" + access + ":\n" + result;
116  }
117 
118  return result;
119  }
120  }
121 
122  public class CppValidator : Validator
123  {
124  }
125 }
bool Const
Denotes if field is constant or not. Type: boolean
Definition: Models.cs:133
string Type
Represents return type of the method. Type: string
Definition: Models.cs:178
The structure that describes class. Contains name, array of fields, methods and subclasses, parent class name, access specifier. Overrides ToString() method.
Definition: Models.cs:48
bool Static
Denotes if the mathod is static or not. Type: boolean
Definition: Models.cs:196
bool Static
Denotes if field is static or not. Type: boolean
Definition: Models.cs:139
Parameter[] Parameters
Represents parameters of the method. Type: array of type Parameter
Definition: Models.cs:202
override string GenerateClass(Class @class)
Class generator: generates class with fields, methods and subclasses from given class object Class ob...
Definition: CppGenerator.cs:16
override string GenerateField(Field field)
Field generator: generates field from given field object Field objectString of generated code of fiel...
Definition: CppGenerator.cs:31
string Default
Represents default value of the varibale. Type: string
Definition: Models.cs:114
Interface of language generator
override string GenerateMethod(Method method)
Method generator: generates method from given method object Method objectString of generated code of ...
Definition: CppGenerator.cs:60
The structure that describes method. Contains name, return type, access level, const and static prope...
Definition: Models.cs:166
The structure that describes field. Contains access, const and static properties. Inherits from Varia...
Definition: Models.cs:121