-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
396 lines (360 loc) · 11.9 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
using System.Text;
using System.Reflection;
using System.Globalization;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
/*
class Program
{
static BBParamAttribute defaultPAttr = new();
static BBFuncAttribute defaultFAttr = new();
static Dictionary<string, Type> instanceVars = new();
static Dictionary<string, Type> localVars = new();
static Dictionary<Type, string> primitiveTypes = new Dictionary<Type, string>
{
{ typeof(bool), "bool" },
{ typeof(byte), "byte" },
{ typeof(char), "char" },
{ typeof(decimal), "decimal" },
{ typeof(double), "double" },
{ typeof(float), "float" },
{ typeof(int), "int" },
{ typeof(long), "long" },
{ typeof(sbyte), "sbyte" },
{ typeof(short), "short" },
{ typeof(string), "string" },
{ typeof(uint), "uint" },
{ typeof(ulong), "ulong" },
{ typeof(ushort), "ushort" },
};
static string TypeToString(Type type)
{
return primitiveTypes.GetValueOrDefault(type) ?? type.Name;
}
static bool IsSummableType(Type type)
{
return type == typeof(int) || type == typeof(float); //TODO:
}
public static void Main()
{
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;
var codeJson = File.ReadAllText("code.json", Encoding.UTF8);
var obj = JsonConvert.DeserializeObject<Dictionary<string, Block[]>>(codeJson);
string output = "";
foreach(var kv1 in obj!)
{
var funcName = kv1.Key;
var blocks = ConvertBlocks(kv1.Value);
if(funcName == "PreLoad")
continue;
output +=
$"public void {funcName}()\n" +
"{\n" +
(ConvertVars(localVars) +
blocks).TrimEnd().Indent() + "\n" +
"}\n";
//instanceVars.Clear();
localVars.Clear();
}
output =
"using System.Numerics;\n" +
"using static Functions;\n" +
"\n" +
"public class Code: Script\n" +
"{\n" +
(ConvertVars(instanceVars) +
output).TrimEnd().Indent() + "\n" +
"}\n";
File.WriteAllText("Code.cs", output);
}
static string PrepareName(string name)
{
name = Regex.Replace(name, @"\W","_");
if(Regex.IsMatch("" + name[0], @"[^a-zA-Z]"))
{
name = "_" + name;
}
return name;
}
static string ConvertVars(Dictionary<string, Type> vars)
{
var output = "";
foreach(var kv2 in vars)
{
var varName = kv2.Key;
var varType = kv2.Value;
output += $"{TypeToString(varType)}? {PrepareName(varName)} = null;\n";
}
return (output == "") ? "" : ("#region VarDecl\n" + output + "#endregion\n");
}
static string ConvertBlocks(Block[] blocks)
{
string output = "";
foreach(var block in blocks)
{
output += ConvertBlock(block) + ";\n";
}
return output;
}
static string ConvertBlock(Block block)
{
var name = block.Function.Substring(3, block.Function.Length - 3 - 1);
var flags = BindingFlags.Public | BindingFlags.Static;
var mInfo = typeof(Functions).GetMethod(name, flags);
if(mInfo == null)
{
//throw new Exception($"{name} is undefiend");
//Console.WriteLine(name);
return "";
}
var parameters = new List<object>();
foreach(var pInfo in mInfo.GetParameters())
{
var sAttr = pInfo.GetCustomAttribute<BBSubBlocks>();
if(sAttr != null)
{
var sbArgNames = new List<string>();
foreach(var paramNameName in sAttr.ParamNames)
{
var paramName = (string)block.Params[paramNameName + "Var"];
sbArgNames.Add(paramName);
}
parameters.Add(new SubBlocks(block.SubBlocks ?? new Block[0], sbArgNames.ToArray()));
}
else
{
var value = Resolve(pInfo, block.Params);
parameters.Add(value);
}
}
string output;
Type returnType;
if(name == "SetVarInTable") //HACK:
{
var p0 = (parameters[0] as Composite)!;
output = ObjectToString(p0);
returnType = p0.DeductType() ?? typeof(object);
}
else
{
output = name + "(" + string.Join(", ", parameters.Select(p => ObjectToString(p))) + ")";
returnType = mInfo.ReturnType;
}
if(returnType != typeof(void))
{
var fAttr = mInfo.GetCustomAttribute<BBFuncAttribute>() ?? defaultFAttr;
var ret = ResolveReturn(fAttr.Dest, block.Params);
if(ret != null)
{
output = ret + " = " + output;
AddType(ret, returnType);
}
}
return output;
}
static void AddType(Reference ret, Type type)
{
if(ret.TableName == null)
{
localVars[ret.VarName] = type;
}
if(ret.TableName == "InstanceVars")
{
instanceVars[ret.VarName] = type;
}
}
static string UCFirst(string str)
{
return Char.ToUpperInvariant(str[0]) + str.Substring(1);
}
static Reference? ResolveReturn(string name, Dictionary<string, object> ps)
{
name = UCFirst(name);
var varName = ps.GetValueOrDefault(name + "Var") as string;
var tableName = ps.GetValueOrDefault(name + "VarTable") as string;
if(varName != null && varName != "Nothing")
{
return new Reference(tableName, varName);
}
else
return null;
}
static string ObjectToString(object value)
{
if(value is string s)
{
if(s.StartsWith("$") && s.EndsWith("$"))
{
s = s.Substring(1, s.Length - 1 - 1);
//s = PrepareName(s);
var constant = Constants.Table.GetValueOrDefault(s);
if(constant != null)
{
s = ObjectToString(constant);
}
return s;
}
else
return ("\"" + s + "\"");
}
else if(value is bool b)
return b ? "true" : "false";
else
{
var type = value.GetType();
if(type.IsEnum)
return string.Join(" | ", value.ToString()!.Split(", ").Select(x => type.Name + "." + x));
else if(type == typeof(float) || type == typeof(double) || type == typeof(decimal))
return value.ToString() + "f";
else
return value.ToString()!;
}
}
static Composite Resolve(ParameterInfo pInfo, Dictionary<string, object> ps)
{
var pAttr = pInfo.GetCustomAttribute<BBParamAttribute>() ?? defaultPAttr;
var name = UCFirst(pInfo.Name!);
var value = (pAttr.ValuePostfix != null) ? ps.GetValueOrDefault(name + pAttr.ValuePostfix) : null;
var valueByLevel = (pAttr.ValueByLevelPostfix != null) ? ps.GetValueOrDefault(name + pAttr.ValueByLevelPostfix) : null;
var varName = (pAttr.VarPostfix != null) ? ps.GetValueOrDefault(name + pAttr.VarPostfix) as string : null;
var tableName = (pAttr.VarTablePostfix != null) ? ps.GetValueOrDefault(name + pAttr.VarTablePostfix) as string : null;
return new Composite(
type: pInfo.ParameterType,
value: value,
var: (varName != null && varName != "Nothing") ? new Reference(tableName, varName) : null,
varByLevel: (valueByLevel != null) ? new Reference("InstanceVars", "VALUE_BY_LEVEL") : null
);
}
class Block
{
public string Function;
public Dictionary<string, object> Params;
public Block[]? SubBlocks;
}
class SubBlocks
{
public Block[] Blocks;
public string[] ParamNames;
public SubBlocks(Block[] blocks, string[] paramNames)
{
Blocks = blocks;
ParamNames = paramNames;
}
public override string ToString()
{
return $"({string.Join(", ", ParamNames.Select(p => PrepareName(p)))}) => {{\n{ConvertBlocks(Blocks).TrimEnd().Indent()}\n}}";
}
}
class Reference
{
public string? TableName;
public string VarName;
public Reference(string? tableName, string varName)
{
TableName = tableName;
VarName = varName;
}
public Type? DeductType()
{
if(TableName == null)
{
return localVars.GetValueOrDefault(VarName);
}
else if(TableName == "InstanceVars")
{
return instanceVars.GetValueOrDefault(VarName);
}
else
return null;
}
public override string ToString()
{
if(TableName != null)
{
var tableName = TableName;
if(tableName == "InstanceVars")
tableName = "this";
return PrepareName(tableName) + "." + PrepareName(VarName);
}
else
return PrepareName(VarName);
}
}
class Composite
{
public Type Type;
public object? Value;
public Reference? Var;
public Reference? VarByLevel;
public Composite(Type type, object? value, Reference? var, Reference? varByLevel)
{
Type = type;
Value = value;
Var = var;
VarByLevel = varByLevel;
}
public Type? DeductType()
{
return Value?.GetType() ?? Var?.DeductType() ?? VarByLevel?.DeductType();
}
public override string ToString()
{
int count = Convert.ToInt32(Value != null) +
Convert.ToInt32(Var != null) +
Convert.ToInt32(VarByLevel != null);
if(count > 0)
{
if(count == 1)
{
if(Value != null)
{
return ObjectToString(Value);
}
else
{
var r = (Var ?? VarByLevel)!;
return r.ToString();
}
}
else if(IsSummableType(Type))
{
var output = "0";
if(Value != null)
{
output = ObjectToString(Value);
}
if(Var != null)
{
output += $" + ({Var.ToString()} ?? 0)";
}
if(VarByLevel != null)
{
output += $" + ({VarByLevel.ToString()} ?? 0)";
}
return output;
}
else
{
var output = new List<string>();
if(Var != null)
{
output.Add(Var.ToString());
}
if(VarByLevel != null)
{
output.Add(VarByLevel.ToString());
}
else if(Value != null)
{
output.Add(ObjectToString(Value));
}
return string.Join(" ?? ", output);
}
}
else
return "default";
}
}
}
*/