CodeGen
 All Classes Namespaces Files Functions Variables Properties Pages
Ruby.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 RubyGenerator : Generator
11  {
12  private const string ClassFormat = "class {0}{1}\n{2}{3}{4}end";
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 + "\n" + GeneratorConf.ShiftCode(GenerateMethod(method), 1, Indent) + "\n");
32 
33  classes = @class.Classes?.Aggregate(classes,
34  (current, cls) => current + "\n" + GeneratorConf.ShiftCode(GenerateClass(cls), 1, Indent));
35 
36  if (!string.IsNullOrWhiteSpace(classes)) classes += "\n";
37 
38  return string.Format(ClassFormat, @class.Name, inherits, fields, methods, classes);
39  }
40 
42  public override string GenerateField(Field field)
43  {
44  var result = Indent;
45 
46 // if (field.Access == "public")
47 // {
48 // field.Name = field.Name?.First().ToString().ToUpper() + field.Name?.Substring(1);
49 // }
50 
51 // result += field.Name + ' ' + field.Type;
52 
53  return result;
54  }
55 
57  public override string GenerateMethod(Method method)
58  {
59  return GenerateMethodWithBody(method, "");
60  }
61 
62  private string GenerateMethodWithBody(Method method, string body)
63  {
64  var result = "def ";
65 
66  if (method.Access == "private") result = "private " + result;
67 
68  if (method.Static) result += "self.";
69 
70 
71  result += method.Name;
72  if (method.Parameters?.Length > 0)
73  {
74  result += '(';
75 
76  for (var i = 0; i < method.Parameters?.Length; i++)
77  {
78  result += method.Parameters[i].Name;
79  if (!string.IsNullOrWhiteSpace(method.Parameters[i].Default))
80  {
81  result += '=' + method.Parameters[i].Default;
82  }
83 
84  if (i + 1 < method.Parameters.Length)
85  {
86  result += ", ";
87  }
88  }
89 
90  result += ")";
91  }
92 
93  if (!string.IsNullOrWhiteSpace(body))
94  result += "\n" + GeneratorConf.ShiftCode(body, 1, Indent);
95 
96 
97  result += "\nend";
98  return result;
99  }
100 
101  private string GenerateInit(Class @class)
102  {
103  string result = "", body = "";
104  var init = new Method
105  {
106  Name = "initialize",
107  };
108  var previousIsStatic = false;
109  var paramList = new List<Parameter>();
110 
111  for (var i = 0; i < @class.Fields.Length; i++)
112  {
113  var field = @class.Fields[i];
114 
115  if (field.Static)
116  {
117  previousIsStatic = true;
118  continue;
119  }
120 
121  paramList.Add(new Parameter
122  {
123  Name = field.Name,
124  Default = field.Default
125  });
126 
127  body += "@" + field.Name + " = " + field.Name;
128 
129  if (i + 1 < @class.Fields.Length && !previousIsStatic)
130  {
131  body += "\n";
132  }
133 
134  previousIsStatic = false;
135  }
136 
137  init.Parameters = paramList.ToArray();
138 
139  result += GenerateMethodWithBody(init, body);
140 
141  return result;
142  }
143  }
144 }
Generator for Ruby
Definition: Ruby.cs:10
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
override string GenerateClass(Class @class)
Class generator: generates class with fields, methods and subclasses from given class object Class ob...
Definition: Ruby.cs:16
bool Static
Denotes if the mathod is static or not. Type: boolean
Definition: Models.cs:196
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: Ruby.cs:42
override string GenerateMethod(Method method)
Method generator: generates method from given method object Method objectString of generated code of ...
Definition: Ruby.cs:57
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 field. Contains access, const and static properties. Inherits from Varia...
Definition: Models.cs:121