CodeGen
 All Classes Namespaces Files Functions Variables Properties Pages
Python.cs
Go to the documentation of this file.
1 using System.Collections.Generic;
2 using System.Linq;
3 
4 namespace CodeGen.generators
5 {
10  public class PythonGenerator : Generator
11  {
12  private const string ClassFormat = "class {0}{1}:\n{2}{3}{4}";
13  private string Indent { get; set; } = GeneratorConf.GetIndent(true, 4);
14 
16  protected override string GenerateClass(Class @class)
17  {
18  string fields = "", inherits = "", methods = "", classes = "";
19 
20  if (!string.IsNullOrWhiteSpace(@class.Parent))
21  {
22  inherits = $"({@class.Parent})";
23  }
24 
25  if (@class.Fields?.Length > 0)
26  {
27  fields = GeneratorConf.ShiftCode(GenerateInit(@class), 1, Indent);
28  }
29 
30  methods = @class.Methods?.Aggregate('\n' + methods,
31  (current, method) => $"{current}{GeneratorConf.ShiftCode(GenerateMethod(method), 1, Indent)}\n");
32 
33  classes = @class.Classes?.Aggregate('\n' + classes,
34  (current, cls) => current + GeneratorConf.ShiftCode(GenerateClass(cls), 1, Indent));
35 
36  var result = string.Format(ClassFormat, @class.Name, inherits, fields, methods, classes);
37 
38  if (result[result.Length - 2] == ':' && result[result.Length - 1] == '\n')
39  {
40  result += Indent + "pass";
41  }
42 
43  return result;
44  }
45 
47  public override string GenerateField(Field field)
48  {
49  return "self." + field.Name;
50  }
51 
53  public override string GenerateMethod(Method method)
54  {
55  return GenerateMethodWithBody(method, "pass");
56  }
57 
58  private string GenerateMethodWithBody(Method method, string body)
59  {
60  var result = "def ";
61 
62  switch (method.Access)
63  {
64  case "private":
65  method.Name = "__" + method.Name;
66  break;
67  case "protected":
68  method.Name = '_' + method.Name;
69  break;
70  }
71 
72  result += method.Name + '(';
73 
74  if (method.Static)
75  {
76  result = "@staticmethod\n" + result;
77  }
78  else
79  {
80  result += "self";
81  if (method.Parameters?.Length > 0)
82  {
83  result += ", ";
84  }
85  }
86 
87  for (var i = 0; i < method.Parameters?.Length; i++)
88  {
89  result += method.Parameters[i].Name;
90  if (!string.IsNullOrWhiteSpace(method.Parameters[i].Default))
91  {
92  result += '=' + method.Parameters[i].Default;
93  }
94  if (i + 1 < method.Parameters.Length)
95  {
96  result += ", ";
97  }
98  }
99 
100  if (body == "pass")
101  {
102  body += '\n';
103  }
104 
105  result += "):\n" + GeneratorConf.ShiftCode(body, 1, Indent);
106  return result;
107  }
108 
109  public string GenerateInit(Class @class)
110  {
111  string result = "", body = "";
112  var init = new Method
113  {
114  Name = "__init__",
115  };
116  var previousIsStatic = false;
117  var paramList = new List<Parameter>();
118 
119  for (var i = 0; i < @class.Fields?.Length; i++)
120  {
121  var field = @class.Fields[i];
122 
123  if (field.Static)
124  {
125  previousIsStatic = true;
126  continue;
127  }
128 
129  string access;
130  switch (field.Access)
131  {
132  case "private":
133  access = "__";
134  break;
135  case "protected":
136  access = "_";
137  break;
138  default:
139  access = "";
140  break;
141  }
142 
143  paramList.Add(new Parameter
144  {
145  Name = field.Name,
146  Default = field.Default
147  });
148  var fieldName = field.Name;
149  field.Name = access + field.Name;
150  body += $"{GenerateField(field)} = {fieldName}";
151 
152  if (i + 1 < @class.Fields.Length && !previousIsStatic)
153  {
154  body += "\n";
155  }
156 
157  previousIsStatic = false;
158 
159  }
160 
161  if (body == "")
162  {
163  body = "pass";
164  }
165 
166  init.Parameters = paramList.ToArray();
167 
168  result += GenerateMethodWithBody(init, body);
169 
170  return result;
171  }
172  }
173 }
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
Generator for Python
Definition: Python.cs:10
override string GenerateClass(Class @class)
Class generator: generates class with fields, methods and subclasses from given class object Class ob...
Definition: Python.cs:16
Parameter[] Parameters
Represents parameters of the method. Type: array of type Parameter
Definition: Models.cs:202
Holds the configuration of generator
override string GenerateField(Field field)
Field generator: generates field from given field object Field objectString of generated code of fiel...
Definition: Python.cs:47
override string GenerateMethod(Method method)
Method generator: generates method from given method object Method objectString of generated code of ...
Definition: Python.cs:53
static string ShiftCode(string code, int num, string indent)
Shifts code using given parameters
string Default
Represents default value of the varibale. Type: string
Definition: Models.cs:114
Interface of language generator
string Access
Represents access level of the method. Type: string
Definition: Models.cs:184
The structure that describes method. Contains name, return type, access level, const and static prope...
Definition: Models.cs:166
The structure that describes parameter. Inherits from Variable
Definition: Models.cs:158
The structure that describes field. Contains access, const and static properties. Inherits from Varia...
Definition: Models.cs:121
string GenerateInit(Class @class)
Definition: Python.cs:109