CodeGen
 All Classes Namespaces Files Functions Variables Properties Pages
VB.cs
Go to the documentation of this file.
1 using System.Linq;
2 using CodeGen.utils;
3 
4 namespace CodeGen.generators
5 {
10  public class VbGenerator : Generator
11  {
12  private const string ClassFormat = "{0}Class {1}\n{2}{3}{4}{5}";
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 (@class.Parent?.Length > 0)
21  {
22  inherits = Indent + "Inherits " + @class.Parent + "\n\n";
23  }
24 
25  fields = @class.Fields?.Aggregate(fields,
26  (current, field) => current + GenerateField(field) + '\n');
27 
28  methods = @class.Methods?.Aggregate(methods,
29  (current, method) => current + '\n' + GeneratorConf.ShiftCode(GenerateMethod(method), 1, Indent) + '\n');
30 
31  classes = @class.Classes?.Aggregate('\n' + classes,
32  (current, cls) => current + GeneratorConf.ShiftCode(GenerateClass(cls), 1, Indent) + '\n');
33 
34  var access = "";
35  if (@class.Access?.Length > 0)
36  {
37  access = @class.Access + ' ';
38  }
39 
40  return string.Format(ClassFormat, access, @class.Name, inherits, fields, methods, classes) + "\nEnd Class";
41  }
42 
44  public override string GenerateField(Field field)
45  {
46  var result = Indent + field.Access + ' ' + field.Name + " As " + field.Type;
47  if (field.Default?.Length > 0)
48  {
49  result += " = " + field.Default;
50  }
51  return result;
52  }
53 
55  public override string GenerateMethod(Method method)
56  {
57  var result = method.Access + ' ';
58  var type = method.Type?.Length > 0 ? "Function" : "Sub";
59  result += type + ' ' + method.Name + '(';
60 
61  for (var i = 0; i < method.Parameters?.Length; i++)
62  {
63  var parameter = method.Parameters[i].Name + " As " + method.Parameters[i].Type;
64  if (method.Parameters[i].Default?.Length > 0)
65  {
66  result += "Optional " + parameter + " = " + method.Parameters[i].Default;
67  }
68  else
69  {
70  result += "ByVal " + parameter;
71  }
72 
73  if (i + 1 < method.Parameters.Length)
74  {
75  result += ", ";
76  }
77  }
78 
79  result += ')';
80  if (type == "Function")
81  {
82  result += " As " + method.Type + '\n' + Indent + "Return 0";
83  }
84  else
85  {
86  result += '\n' + Indent + "Return";
87  }
88 
89  result += "\nEnd " + type;
90  return result;
91  }
92  }
93 
96  public class VbNormalizer : Normalizer
97  {
98  private static Normalizer _singletonInstance;
99 
100  private VbNormalizer()
101  {
102 
103  }
104 
109  public static Normalizer GetNormalizer()
110  {
111  return _singletonInstance ?? (_singletonInstance = new VbNormalizer());
112  }
113 
115  protected override Class NormalizeClass(ref Class @class)
116  {
117  base.NormalizeClass(ref @class);
118  @class.Access = Utils.Title(@class.Access);
119  return @class;
120  }
121 
123  protected override Field NormalizeField(ref Field field)
124  {
125  base.NormalizeField(ref field);
126  field.Access = string.IsNullOrWhiteSpace(field.Access) ? "Public" : Utils.Title(field.Access);
127  return field;
128  }
129 
131  protected override Method NormalizeMethod(ref Method method)
132  {
133  base.NormalizeMethod(ref method);
134  method.Access = string.IsNullOrWhiteSpace(method.Access) ? "Public" : Utils.Title(method.Access);
135  return method;
136  }
137 
139  protected override string NormalizeType(string type)
140  {
141  switch (type)
142  {
143  case "int":
144  type = "Integer";
145  break;
146  case "void":
147  type = "";
148  break;
149  default:
150  type = Utils.Title(type);
151  break;
152  }
153  return type;
154  }
155  }
156 }
static Normalizer GetNormalizer()
Method for getting a singleton
Definition: VB.cs:109
override string GenerateField(Field field)
Field generator: generates field from given field object Field objectString of generated code of fiel...
Definition: VB.cs:44
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
Parameter[] Parameters
Represents parameters of the method. Type: array of type Parameter
Definition: Models.cs:202
Holds the configuration of generator
Normalizer for Visual Basic
Definition: VB.cs:96
override Method NormalizeMethod(ref Method method)
Method normalizer: normalizes method Method objectNormalized method object
Definition: VB.cs:131
override string GenerateMethod(Method method)
Method generator: generates method from given method object Method objectString of generated code of ...
Definition: VB.cs:55
Interface of language normalizer: normalizes package data according to specified language ...
override Class NormalizeClass(ref Class @class)
Class normalizer: normalizes class with fields, methods and subclasses Class objectNormalized class o...
Definition: VB.cs:115
override string GenerateClass(Class @class)
Class generator: generates class with fields, methods and subclasses from given class object Class ob...
Definition: VB.cs:16
override string NormalizeType(string type)
Type normalizer: fixes the type to language's built in
Definition: VB.cs:139
Generator for Visual Basic
Definition: VB.cs:10
override Field NormalizeField(ref Field field)
Field normalizer: normalizes field Field objectNormalized field object
Definition: VB.cs:123
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
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