forked from SciSharp/Numpy.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeGenerator.cs
More file actions
1100 lines (1049 loc) · 45.3 KB
/
CodeGenerator.cs
File metadata and controls
1100 lines (1049 loc) · 45.3 KB
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using CodeMinion.Core.Attributes;
using CodeMinion.Core.Helpers;
using CodeMinion.Core.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SliceAndDice;
namespace CodeMinion.Core
{
public class CodeGenerator
{
public CodeGenerator()
{
LoadTemplates();
}
public string CopyrightNotice { get; set; }
public List<StaticApi> StaticApis { get; set; } = new List<StaticApi>();
public List<DynamicApi> DynamicApis { get; set; } = new List<DynamicApi>();
public List<ApiClass> ApiClasses { get; set; } = new List<ApiClass>();
public bool PrintModelJson { get; set; } = false;
public string NameSpace { get; set; } = "Numpy";
public string StaticModuleName { get; set; } = "np";
public string PythonModuleName { get; set; } = "numpy";
public List<Action<CodeWriter>> InitializationGenerators { get; set; } = new List<Action<CodeWriter>>();
//public bool UsePythonIncluded { get; set; } = true;
public HashSet<string> Usings { get; set; } = new HashSet<string>()
{
@"using System;",
@"using System.Collections;",
@"using System.Collections.Generic;",
@"using System.IO;",
@"using System.Linq;",
@"using System.Runtime.InteropServices;",
@"using System.Text;",
@"using Python.Runtime;",
};
public string StaticApiFilesPath { get; set; }
public string DynamicApiFilesPath { get; set; }
public string ModelsPath { get; set; }
public string TestFilesPath { get; set; }
public List<TestFile> TestFiles { get; set; } = new List<TestFile>();
protected Dictionary<string, FunctionBodyTemplate> _templates;
protected virtual void LoadTemplates()
{
_templates = Assembly.GetEntryAssembly().GetTypes()
.Where(x => x.GetCustomAttribute<TemplateAttribute>() != null)
.Select(x => (FunctionBodyTemplate)Activator.CreateInstance(x)).ToDictionary(x =>
x.GetType().GetCustomAttribute<TemplateAttribute>().ApiFunction);
}
// generate an entire API function declaration
protected virtual void GenerateApiFunction(Declaration decl, CodeWriter s, bool prefix = false, bool @static=false)
{
if (decl.ManualOverride)
return;
//if (decl.Name=="cholesky")
// Debugger.Break();
decl.Sanitize();
if (decl.CommentOut)
s.Out("/*");
var class_names = (decl.GeneratedClassName ?? decl.ClassName ?? "no_name").Split('.');
int levels = class_names.Length - 1;
if (levels > 0)
{
foreach (var name in class_names.Skip(1))
{
s.Out($"public static partial class {EscapeName(name)} {{");
s.Indent();
}
}
GenerateDocString(decl, s);
var retval = GenerateReturnType(decl);
switch (decl)
{
case Function func:
var arguments = GenerateArguments(func);
//var passed_args = GeneratePassedArgs(func);
var generics = func.Generics == null ? "" : $"<{string.Join(",", func.Generics)}>";
var prefix_str = "";
if (prefix && levels > 0)
prefix_str = string.Join("_", class_names.Skip(1)) + "_";
s.Out($"public {(@static ? "static ":"")}{retval} {EscapeName(prefix_str + decl.Name)}{func.SharpOnlyPostfix}{generics}({arguments})");
s.Block(() =>
{
GenerateFunctionBody(func, s, prefix_str);
});
break;
case Property prop:
s.Out($"public {(@static ? "static " : "")}{prop.Type} {EscapeName(prop.Name)}");
s.Block(() =>
{
s.Out("get", () =>
{
GeneratePropertyGetter(prop, s);
});
s.Out("set", () =>
{
GeneratePropertySetter(prop, s);
});
});
break;
}
if (levels > 0)
{
foreach (var name in class_names.Skip(1))
{
s.Outdent();
s.Out("}");
}
}
if (decl.CommentOut)
s.Out("*/");
if (PrintModelJson)
{
s.Out("// the declaration model:");
s.Out("/*");
s.Out(JObject.FromObject(decl).ToString(Formatting.Indented));
s.Out("*/");
}
s.Break();
}
//protected virtual void GenerateStaticApiRedirection(StaticApi api, Declaration decl, CodeWriter s)
//{
// if (decl.DebuggerBreak)
// Debugger.Break();
// decl.Sanitize();
// if (decl.CommentOut)
// s.Out("/*");
// var class_names = (decl.GeneratedClassName ?? decl.ClassName ?? "no_name").Split('.');
// int levels = class_names.Length - 1;
// if (levels > 0)
// {
// foreach (var name in class_names.Skip(1))
// {
// s.Out($"public static partial class {EscapeName(name)} {{");
// s.Indent();
// }
// }
// GenerateDocString(decl, s);
// var retval = GenerateReturnType(decl);
// switch (decl)
// {
// case Function func:
// var arguments = GenerateArguments(func);
// var passed_args = GeneratePassedArgs(func);
// var generics = func.Generics == null ? "" : $"<{string.Join(",", func.Generics)}>";
// s.Out($"public static {retval} {EscapeName(decl.Name)}{func.SharpOnlyPostfix}{generics}({arguments})");
// var prefix = "";
// if (levels > 0)
// prefix = string.Join("_", class_names.Skip(1)) + "_";
// s.Indent(() => s.Out(
// $"=> {api.ImplName}.Instance.{EscapeName(prefix + decl.Name)}({passed_args});"));
// break;
// case Property prop:
// s.Out($"public static {retval} {EscapeName(decl.Name)}");
// s.Indent(() => s.Out(
// $"=> {api.ImplName}.Instance.{EscapeName(decl.Name)};"));
// break;
// }
// if (levels > 0)
// {
// foreach (var name in class_names.Skip(1))
// {
// s.Outdent();
// s.Out("}");
// }
// }
// if (decl.CommentOut)
// s.Out("*/");
// s.Break();
//}
// generate the argument list between the parentheses of a generated API function
protected virtual string GenerateArguments(Function decl)
{
var s = new StringBuilder();
int i = 0;
foreach (var arg in decl.Arguments)
{
if (arg.Type == "string")
arg.IsValueType = false;
// TODO modifier (if any)
// parameter type
CheckArgument(arg);
s.Append(arg.Type);
if (arg.IsNullable && arg.IsValueType)
s.Append("?");
s.Append(" ");
// parameter name
s.Append(EscapeName(arg.Name));
if (!string.IsNullOrWhiteSpace(arg.DefaultValue))
s.Append($" = {MapDefaultValue(arg)}");
else if (arg.IsNullable)
s.Append($" = null");
i++;
if (i < decl.Arguments.Count)
s.Append(", ");
}
return s.ToString();
}
private string GeneratePassedArgs(Function decl)
{
var s = new StringBuilder();
int i = 0;
foreach (var arg in decl.Arguments)
{
// TODO modifier (if any)
var argname = EscapeName(arg.Name);
if (arg.IsNamedArg)
s.Append($"{argname}:");
s.Append(argname);
i++;
if (i < decl.Arguments.Count)
s.Append(", ");
}
return s.ToString();
}
// maps None to null, etc
protected virtual string MapDefaultValue(Argument arg)
{
switch (arg.DefaultValue)
{
case "None": return "null";
case "True": return "true";
case "False": return "false";
}
return arg.DefaultValue;
}
// list of c# keywords that are not allowed as variable names or parameter names
protected readonly HashSet<string> _disallowed_names = new HashSet<string>()
{
"abstract", "as", "base", "bool", "break",
"byte", "case", "catch", "char", "checked",
"class", "const", "continue", "decimal", "default",
"delegate", "do", "double", "else", "enum",
"event", "explicit", "extern", "false", "finally",
"fixed", "float", "for", "foreach", "goto",
"if", "implicit", "in", "int", "interface",
"internal", "is", "lock", "long", "namespace",
"new", "null", "object", "operator", "out",
"override", "params", "private", "protected", "public",
"readonly", "ref", "return", "sbyte", "sealed",
"short", "sizeof", "stackalloc", "static", "string",
"struct", "switch", "this", "throw", "true",
"try", "typeof", "uint", "ulong", "unchecked",
"unsafe", "ushort", "using", "var", "virtual",
"void", "volatile", "while",
"add", "alias", "async", "await", "dynamic",
"get", "global", "nameof", "partial", "remove",
"set", "value", "when", "where", "yield",
"ascending", "by", "descending", "equals", "from",
"group", "in", "into", "join", "let",
"on", "orderby", "select", "where"
};
// escape a varibale name if it violates C# syntax
protected virtual string EscapeName(string name)
{
if (_disallowed_names.Contains(name))
return "@" + name;
return name;
}
// generates the return type declaration of a generated API function declaration
protected virtual string GenerateReturnType(Declaration decl)
{
if (decl.Returns == null || decl.Returns.Count == 0)
return "void";
else if (decl.Returns.Count == 1)
{
CheckArgument(decl.Returns[0]);
return decl.Returns[0].Type;
}
else
{
return "(" + string.Join(", ", decl.Returns.Select(x => x.Type).ToArray()) + ")";
}
}
// argument type consistency checks
protected virtual void CheckArgument(Argument arg)
{
switch (arg.Type)
{
// basic types
case "bool":
case "int":
case "long":
case "double":
case "float":
arg.IsValueType = true;
break;
case "object":
case "string":
arg.IsValueType = false;
break;
// sequence types
}
}
/// <summary>
/// Generate the xml doc string from the description(s)
/// </summary>
/// <param name="decl"></param>
/// <param name="s"></param>
protected virtual void GenerateDocString(Declaration decl, CodeWriter s)
{
if (string.IsNullOrWhiteSpace(decl.Description))
return;
s.Out("/// <summary>");
var docstring = ProcessDocString(decl.Description);
foreach (var line in Regex.Split(docstring, @"\r?\n"))
s.Out("///\t" + line);
s.Out("/// </summary>");
if (decl is Function)
{
var func = decl as Function;
foreach (var arg in func.Arguments)
{
if (string.IsNullOrWhiteSpace(arg.Description))
continue;
s.Out($"/// <param name=\"{arg.Name}\">"); // note: docstring doesn't want parameters escaped with "@"
docstring = ProcessDocString(arg.Description);
foreach (var line in Regex.Split(docstring, @"\r?\n"))
s.Out("///\t" + line.TrimStart());
s.Out("/// </param>");
}
}
if (decl.Returns.All(rv => string.IsNullOrWhiteSpace(rv.Description)))
return;
s.Out("/// <returns>");
if (decl.Returns.Count == 1)
foreach (var line in Regex.Split(ProcessDocString(decl.Returns[0].Description), @"\r?\n"))
s.Out("///\t" + line);
else
{
s.Out("/// A tuple of:");
foreach (var rv in decl.Returns)
{
s.Out("/// " + rv.Name);
foreach (var line in Regex.Split(rv.Description, @"\r?\n"))
s.Out("///\t" + line);
}
}
s.Out("/// </returns>");
}
protected virtual void GenerateDocString(ApiClass decl, CodeWriter s)
{
if (string.IsNullOrWhiteSpace(decl.DocString))
return;
var docstring= ProcessDocString(decl.DocString);
s.Out("/// <summary>");
foreach (var line in Regex.Split(docstring, @"\r?\n"))
s.Out("///\t" + line);
s.Out("/// </summary>");
}
protected string ProcessDocString(string docstring)
{
if (string.IsNullOrWhiteSpace(docstring))
return docstring;
// insert linebreak after each sentence
var text= Regex.Replace(docstring, @"(?:(?<=\w|\)|\])(?<!\d)|(?<=\s\d))\.(?=(?:\s|$))", ".<br></br>\n", RegexOptions.Multiline);
text = Regex.Replace(text, @"(\r?\n){2,}", "\n\n");
text = Regex.Replace(text.Trim(), "<br></br>$", "");
return text;
}
// generates only the body of the API function declaration
protected virtual void GenerateFunctionBody(Function func, CodeWriter s, string prefix = "")
{
s.Out("//auto-generated code, do not change");
if (_templates.ContainsKey(func.Name))
{
// use generator template instead
_templates[func.Name].GenerateBody(func, s);
return;
}
//if (func.Name=="norm")
// Debugger.Break();
var class_names = (func.ClassName ?? "no_name").Split('.');
int levels = class_names.Length - 1;
if (levels < 1)
{
s.Out("var __self__=self;");
}
else
{
var last = "self";
foreach (var name in class_names.Skip(1))
{
s.Out($"var {EscapeName(name)} = {last}.GetAttr(\"{name}\");");
last = name;
}
s.Out($"var __self__={last};");
}
if (func.Arguments.Any())
{
// first generate the positional args
s.Out($"var pyargs=ToTuple(new object[]");
s.Block(() =>
{
foreach (var arg in func.Arguments.Where(a => a.IsNamedArg == false))
{
var name = EscapeName(arg.Name);
if (!string.IsNullOrWhiteSpace(arg.ConvertToSharpType))
s.Out($"SharpToSharp<{arg.ConvertToSharpType}>({name}),");
else
s.Out($"{name},");
}
}, "{", "});");
// then generate the named args
s.Out($"var kwargs=new PyDict();");
foreach (var arg in func.Arguments.Where(a => a.IsNamedArg == true))
{
var name = EscapeName(arg.Name);
if (!string.IsNullOrWhiteSpace(arg.DefaultValue))
s.Out($"if ({name}!={arg.DefaultValue}) kwargs[\"{arg.Name}\"]=ToPython({name});");
else if (string.IsNullOrWhiteSpace(arg.DefaultValue))
{
if (string.IsNullOrWhiteSpace(arg.DefaultIfNull) || arg.DefaultValue == "null")
s.Out($"if ({name}!=null) kwargs[\"{arg.Name}\"]=ToPython({name});");
else
s.Out($"kwargs[\"{arg.Name}\"]=ToPython({name} ?? {arg.DefaultIfNull});");
}
else //if (arg.IsNullable)
s.Out($"if ({name}!=null) kwargs[\"{arg.Name}\"]=ToPython({name});");
}
// then call the function
s.Out($"dynamic py = __self__.InvokeMethod(\"{func.Name}\", pyargs, kwargs);");
}
else
{
// call function with no arguments
s.Out($"dynamic py = __self__.InvokeMethod(\"{func.Name}\");");
}
if (func.IsConstructor)
{
s.Out("self=py as PyObject;");
return;
}
// return the return value if any
if (func.Returns.Count == 0)
return;
if (func.Returns.Count == 1)
s.Out($"return ToCsharp<{func.Returns[0].Type}>(py);");
else
{
var returns = func.Returns.Select((x, i) => $"ToCsharp<{x.Type}>(py[{i}])").ToArray();
s.Out($"return ({string.Join(", ", returns)});");
}
}
//private void GenerateForwardingBody(Function member_func, CodeWriter s, string prefix = "")
//{
// var func = member_func.Clone<Function>();
// // inserting this at position 0 since this is a forwarding of a member function to a static implementation
// func.Arguments.Insert(0, new Argument() { Name = "this", Type = "irrelevant" });
// var passed_args = GeneratePassedArgs(func);
// s.Out("var @this=this;");
// var return_keyword = member_func.Returns.Count > 0 ? "return " : "";
// s.Out($"{return_keyword}{func.ForwardToStaticImpl}.{EscapeName(prefix + func.Name)}({passed_args});");
//}
private void GeneratePropertyGetter(Property prop, CodeWriter s)
{
s.Out($"dynamic py = self.GetAttr(\"{prop.Name}\");");
if (prop.Returns.Count==1 && prop.Type!=null)
s.Out($"return ToCsharp<{prop.Type}>(py);");
else
{
throw new NotImplementedException("TODO: Property returns a tuple");
}
}
private void GeneratePropertySetter(Property prop, CodeWriter s)
{
s.Out($"self.SetAttr(\"{prop.Name}\", ToPython(value));");
}
public virtual void GenerateStaticApi(StaticApi api, CodeWriter s)
{
GenerateUsings(s);
s.Out($"namespace {NameSpace}");
s.Block(() =>
{
s.Out($"public static partial class {api.StaticName}");
s.Block(() =>
{
s.Break();
foreach (var decl in api.Declarations)
{
try
{
if (decl.Ignore)
continue;
GenerateApiFunction(decl, s, @static:true);
}
catch (Exception e)
{
s.Out("// Error generating delaration: " + decl.Name);
s.Out("// Message: " + e.Message);
s.Out("/*");
s.Out(e.StackTrace);
s.Out("----------------------------");
s.Out("Declaration JSON:");
s.Out(JObject.FromObject(decl).ToString(Formatting.Indented));
s.Out("*/");
}
}
s.Break();
});
});
}
private void GenerateUsings(CodeWriter s)
{
foreach (var @using in Usings)
{
s.AppendLine(@using);
}
s.Out(@"#if PYTHON_INCLUDED");
s.Out(@"using Python.Included;");
s.Out(@"#endif");
s.AppendLine();
}
public virtual void GenerateApiImpl(StaticApi api, CodeWriter s)
{
GenerateUsings(s);
s.AppendLine($"namespace {NameSpace}");
s.Block(() =>
{
s.Out($"public partial class {api.ImplName}");
s.Block(() =>
{
s.Break();
foreach (var decl in api.Declarations)
{
try
{
if (decl.ManualOverride || decl.Ignore)
continue;
GenerateApiFunction(decl, s, prefix: true);
}
catch (Exception e)
{
s.Out("// Error generating delaration: " + decl.Name);
s.Out("// Message: " + e.Message);
s.Out("/*");
s.Out(e.StackTrace);
s.Out("----------------------------");
s.Out("Declaration JSON:");
s.Out(JObject.FromObject(decl).ToString(Formatting.Indented));
s.Out("*/");
}
}
});
});
}
public virtual void GenerateDynamicApi(DynamicApi api, CodeWriter s)
{
GenerateUsings(s);
s.AppendLine($"namespace {NameSpace}");
s.Block(() =>
{
s.Out($"public partial class {api.ClassName}");
s.Block(() =>
{
s.Break();
foreach (var decl in api.Declarations)
{
try
{
if (decl.ManualOverride || decl.Ignore)
continue;
GenerateApiFunction(decl, s);
}
catch (Exception e)
{
s.Out("// Error generating delaration: " + decl.Name);
s.Out("// Message: " + e.Message);
s.Out("/*");
s.Out(e.StackTrace);
s.Out("----------------------------");
s.Out("Declaration JSON:");
s.Out(JObject.FromObject(decl).ToString(Formatting.Indented));
s.Out("*/");
}
}
});
});
}
public virtual void GenerateClass(ApiClass api, CodeWriter s)
{
GenerateUsings(s);
s.AppendLine($"namespace {NameSpace}");
s.Block(() =>
{
var names = new ArraySlice<string>(api.ClassName.Split('.'));
var static_classes = names.GetSlice(new Slice(0, names.Length - 1));
var class_name = names.Last();
int levels = names.Length - 1;
if (levels > 0)
{
foreach (var name in static_classes)
{
s.Out($"public static partial class {EscapeName(name)} {{");
s.Indent();
}
}
GenerateDocString(api, s);
s.Out($"public partial class {EscapeName(class_name)} : {EscapeName(api.BaseClass)}");
s.Block(() =>
{
s.Out($"// auto-generated class");
s.Break();
s.Out($"public {EscapeName(class_name)}(PyObject pyobj) : base(pyobj) {{ }}");
s.Break();
s.Out($"public {EscapeName(class_name)}({EscapeName(api.BaseClass)} other) : base(other.PyObject as PyObject) {{ }}");
s.Break();
// additional constructors
foreach (var func in api.Constructors)
{
try
{
if (func.ManualOverride || func.Ignore)
continue;
func.Sanitize();
func.IsConstructor = true;
func.ClassName = string.Join(".", static_classes);
func.Name = class_name;
var arguments = GenerateArguments(func);
//var passed_args = GeneratePassedArgs(func);
s.Out($"public {EscapeName(class_name)}({arguments})");
s.Block(() =>
{
GenerateFunctionBody(func, s);
});
}
catch (Exception e)
{
s.Out("// Error generating constructor");
s.Out("// Message: " + e.Message);
s.Out("/*");
s.Out(e.StackTrace);
s.Out("----------------------------");
s.Out("Declaration JSON:");
s.Out(JObject.FromObject(func).ToString(Formatting.Indented));
s.Out("*/");
}
}
// functions
s.Break();
foreach (var decl in api.Declarations)
{
try
{
if (decl.ManualOverride || decl.Ignore)
continue;
GenerateApiFunction(decl, s);
}
catch (Exception e)
{
s.Out("// Error generating delaration: " + decl.Name);
s.Out("// Message: " + e.Message);
s.Out("/*");
s.Out(e.StackTrace);
s.Out("----------------------------");
s.Out("Declaration JSON:");
s.Out(JObject.FromObject(decl).ToString(Formatting.Indented));
s.Out("*/");
}
}
});
if (levels > 0)
{
foreach (var name in static_classes)
{
s.Outdent();
s.Out("}");
}
}
//if (decl.CommentOut)
// s.Out("*/");
s.Break();
});
}
protected void WriteFile(string path, Action<CodeWriter> generate_action)
{
var s = new CodeWriter();
try
{
if (!string.IsNullOrWhiteSpace(CopyrightNotice))
s.Out("// " + CopyrightNotice);
s.Out("// Code generated by CodeMinion: https://github.com/SciSharp/CodeMinion");
s.Break();
generate_action(s);
}
catch (Exception e)
{
s.AppendLine("/*");
s.AppendLine("\r\n --------------- generator exception ---------------------");
s.AppendLine(e.Message);
s.AppendLine(e.StackTrace);
s.AppendLine("*/");
}
var dir = Path.GetDirectoryName(path);
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
File.WriteAllText(path, s.ToString());
}
public void Generate()
{
// generate all static apis that have been configured
var generated_implementations = new HashSet<string>();
var conv_file = Path.Combine(StaticApiFilesPath, $"{StaticModuleName}.module.gen.cs");
WriteFile(conv_file, s => { GenerateStaticModuleHead( s); });
foreach (var api in StaticApis)
{
var outpath = api.OutputPath ?? StaticApiFilesPath;
if (string.IsNullOrWhiteSpace(outpath))
throw new InvalidDataException("either set generators StaticApiFilesPath or static_api's OutputPath");
// generate static apis
//if (!generated_implementations.Contains(api.ImplName))
//{
//}
generated_implementations.Add(api.ImplName);
var partial = (api.PartialName == null ? "" : "." + api.PartialName);
var api_file = Path.Combine(outpath, $"{api.StaticName + partial}.gen.cs");
//var impl_file = Path.Combine(outpath, $"{api.ImplName + partial}.gen.cs");
WriteFile(api_file, s => { GenerateStaticApi(api, s); });
//WriteFile(impl_file, s => { GenerateApiImpl(api, s); });
}
// PythonObject functions:
var pyobj_file = Path.Combine(ModelsPath ?? DynamicApiFilesPath, $"PythonObject.gen.cs");
WriteFile(pyobj_file, s => { GeneratePythonObjectConversions(s); });
// Dynamic APIs
foreach (var api in DynamicApis)
{
var outpath = api.OutputPath ?? DynamicApiFilesPath;
// generate dynamic apis
var partial = (api.PartialName == null ? "" : "." + api.PartialName);
var api_file = Path.Combine(outpath, $"{api.ClassName + partial}.gen.cs");
WriteFile(api_file, s => { GenerateDynamicApi(api, s); });
}
// Classes
foreach (var api in ApiClasses)
{
if (api.Ignore)
continue;
var outpath = api.OutputPath ?? DynamicApiFilesPath;
if (api.SubDir != null)
outpath = Path.Combine(outpath, api.SubDir);
// generate dynamic apis
var partial = (api.PartialName == null ? "" : "." + api.PartialName);
var api_file = Path.Combine(outpath, $"{api.ClassName + partial}.gen.cs");
WriteFile(api_file, s => { GenerateClass(api, s); });
}
// generate missing tests
GenerateAllTests();
}
/// <summary>
/// Generate tests that probably have to be manually corrected, syntax wise. For that reason
/// We will not overwrite any existing files!
/// </summary>
public void GenerateAllTests()
{
foreach (var file in TestFiles)
{
if(file.TestCases.Count==0)
continue;
var path = TestFilesPath;
if (!string.IsNullOrWhiteSpace(file.SubDir))
path = Path.Combine(path, file.SubDir);
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
var test_file = Path.Combine(path, $"{file.Name}.tests.cs");
if (File.Exists(test_file))
continue; // never overwrite already generated files!
WriteFile(test_file, s => { GenerateTests(file, s); });
}
}
private void GenerateTests(TestFile file, CodeWriter s)
{
GenerateUsings(s);
s.Out("using Microsoft.VisualStudio.TestTools.UnitTesting;");
s.Out("using Assert = NUnit.Framework.Assert;");
s.Break();
s.Out($"namespace {NameSpace}.UnitTest", () =>
{
s.Out("[TestClass]");
s.Out($"public class {file.Name}Test : BaseTestCase", () =>
{
foreach (var testcase in file.TestCases)
GenerateTestCase(testcase, s);
});
});
}
private void GenerateTestCase(TestCase testcase, CodeWriter s)
{
s.Break();
s.Out("[TestMethod]");
s.Out($"public void {testcase.Name}()", () =>
{
var given_var = false;
var expected_var = false;
foreach (var part in testcase.TestParts)
{
switch (part)
{
case Comment c:
foreach (var ln in Regex.Split(c.Text, @"\r?\n"))
s.Out(@"// " + ln);
s.Break();
break;
case ExampleCode example:
var lines = Regex.Split(example.Text, @"\r?\n");
foreach (var ln in lines)
s.Out(@"// " + ln);
s.Break();
s.Out("#if TODO");
foreach (var line in example.Lines)
{
if (line.Type == "comment")
{
s.Out(line.Text[0]);
continue;
}
if (line.Type == "cmd")
{
var cmd = line.Text[0];
s.Out($"{(given_var ? "" : "var")} given= " + cmd + ";");
given_var = true;
continue;
}
if (line.Type == "output")
{
s.Out($"{(expected_var ? "" : "var")} expected=");
expected_var = true;
s.Indent(() =>
{
int i = 0;
foreach (var output in line.Text)
{
var newline = i < line.Text.Count - 1 ? @"\n" : "";
var delimiter = i < line.Text.Count - 1 ? @" +" : ";";
s.Out($"\"{output}{newline}\"{delimiter}");
i++;
}
});
}
s.Out("Assert.AreEqual(expected, given.repr);");
}
s.Out("#endif");
break;
}
}
});
s.Break();
}
private void GenerateStaticModuleHead(CodeWriter s)
{
GenerateUsings(s);
s.AppendLine($"namespace {NameSpace}");
s.Block(() =>
{
s.Out($"public static partial class {StaticModuleName}", () =>
{
s.Break();
s.Out("public static PyObject self => _lazy_self.Value;");
s.Break();
s.Out($"private static Lazy<PyObject> _lazy_self = new Lazy<PyObject>(() => ", () =>
{
s.Out("try", () =>
{
s.Out("return InstallAndImport();");
});
s.Out("catch (Exception)", () =>
{
s.Out("// retry to fix the installation by forcing a repair, if Python.Included is used.");
s.Out("return InstallAndImport(force: true);");
});
//s.Out("return instance;");
});
s.Out(");");
s.Break();
s.Out("private static PyObject InstallAndImport(bool force = false)", () =>
{
s.Out(@"#if PYTHON_INCLUDED");
s.Out("Installer.SetupPython(force).Wait();");
s.Out(@"#endif");
foreach (var generator in InitializationGenerators)
generator(s);
s.Out("PythonEngine.Initialize();");
s.Out($"var mod = Py.Import(\"{PythonModuleName}\");");
s.Out("return mod;");
});
s.Break();
s.Out("public static dynamic dynamic_self => self;");
s.Out("private static bool IsInitialized => self != null;");
s.Break();
//s.Out($"private {api.ImplName}() {{ }}");
s.Break();
s.Out("public static void Dispose()", () =>
{
s.Out("self?.Dispose();");
});
s.Break();
GenToTuple(s, @static:true);
GenToPython(s, @static: true);
GenToCsharp(s, @static: true);
GenSharpToSharp(s, @static: true);
GenSpecialConversions(s, @static: true);
});
});
}
private void GeneratePythonObjectConversions(CodeWriter s)
{
GenerateUsings(s);
s.Out($"namespace {NameSpace}", () =>
{
s.Out($"public partial class PythonObject", () =>
{
s.Break();
GenToTuple(s);
GenToPython(s);
GenToCsharp(s);
GenSharpToSharp(s);
GenSpecialConversions(s);
});
});
}
private void GenToTuple(CodeWriter s, bool @static=false)
{
s.Break();
s.Out("//auto-generated");
s.Out($"{(@static?"private static":"public")} PyTuple ToTuple(Array input)", () =>
{
s.Out("var array = new PyObject[input.Length];");
s.Out("for (int i = 0; i < input.Length; i++)", () =>
{
s.Out("array[i]=ToPython(input.GetValue(i));");
});
s.Out("return new PyTuple(array);");
});
}
public HashSet<string> ToCsharpConversions { get; set; } = new HashSet<string>();
private void GenToCsharp(CodeWriter s, bool @static = false)
{
s.Break();
s.Out("//auto-generated");
s.Out($"{(@static?"internal static":"public")} T ToCsharp<T>(dynamic pyobj)", () =>
{
s.Out("switch (typeof(T).Name)", () =>
{
s.Out("// types from 'ToCsharpConversions'");
foreach (var @case in ToCsharpConversions)
{
s.Out(@case);
}
s.Out("default:");
s.Out("var pyClass = $\"{pyobj.__class__}\";");