// static void AppendArrayDecl(IFunctionScope f, string typeName, int count) { if (count > 0) { f.Append("new ").Append(typeName).Append("[").Append(count).Append("];").NewLine(); } else { f.Append("Array.Empty<").Append(typeName).Append(">();").NewLine(); } }
public CSCodeGenerationResult Implement(IActivityMonitor monitor, MethodInfo m, ICSCodeGenerationContext c, ITypeScope b) { IFunctionScope mB = b.CreateOverride(m); mB.Parent.Should().BeSameAs(b, "The function is ready to be implemented."); if (IsLambda) { mB.Append("=> ").Append(ActualCode).Append(';').NewLine(); } else { mB.Append(ActualCode); } return(CSCodeGenerationResult.Success); }
public CSCodeGenerationResult Implement(IActivityMonitor monitor, MethodInfo m, ICSCodeGenerationContext codeGenContext, ITypeScope typeBuilder) { IFunctionScope mB = typeBuilder.CreateOverride(m); Debug.Assert(mB.Parent == typeBuilder, "The function is ready to be implemented."); if (_attr.IsLambda) { mB.Append("=> ").Append(_attr.ActualCode).Append(';').NewLine(); } else { mB.Append(_attr.ActualCode).NewLine(); } return(CSCodeGenerationResult.Success); }
public void CreateServiceSupportCode(IStObjServiceEngineMap liftedMap) { _infoType.Namespace.Append(_sourceServiceSupport); _rootType.GeneratedByComment().Append(@" readonly Dictionary<Type, IStObjFinalImplementation> _objectServiceMappings; readonly IStObjFinalImplementation[] _objectServiceMappingList; readonly Dictionary<Type, IStObjServiceClassDescriptor> _simpleServiceMappings; readonly IStObjServiceClassDescriptor[] _simpleServiceList; readonly Dictionary<Type, IStObjServiceClassFactory> _manualServiceMappings; readonly IStObjServiceClassFactory[] _manualServiceList; public IStObjServiceMap Services => this; IReadOnlyDictionary<Type, IStObjFinalImplementation> IStObjServiceMap.ObjectMappings => _objectServiceMappings; IReadOnlyList<IStObjFinalImplementation> IStObjServiceMap.ObjectMappingList => _objectServiceMappingList; IReadOnlyDictionary<Type, IStObjServiceClassDescriptor> IStObjServiceMap.SimpleMappings => _simpleServiceMappings; IReadOnlyList<IStObjServiceClassDescriptor> IStObjServiceMap.SimpleMappingList => _simpleServiceList; IReadOnlyDictionary<Type, IStObjServiceClassFactory> IStObjServiceMap.ManualMappings => _manualServiceMappings; IReadOnlyList<IStObjServiceClassFactory> IStObjServiceMap.ManualMappingList => _manualServiceList;") .NewLine(); // Object mappings. _rootCtor.Append($"_objectServiceMappings = new Dictionary<Type, IStObjFinalImplementation>({liftedMap.ObjectMappings.Count});").NewLine(); foreach (var map in liftedMap.ObjectMappings) { _rootCtor.Append("_objectServiceMappings.Add( ") .AppendTypeOf(map.Key) .Append(", _stObjs[").Append(map.Value.IndexOrdered).Append("].FinalImplementation );") .NewLine(); } if (liftedMap.ObjectMappingList.Count > 0) { _rootCtor.Append($"_objectServiceMappingList = new IStObjFinalImplementation[] {{").NewLine(); foreach (var o in liftedMap.ObjectMappingList) { _rootCtor.NewLine().Append("_stObjs[").Append(o.IndexOrdered).Append("].FinalImplementation,"); } _rootCtor.NewLine().Append("};").NewLine(); } else { _rootCtor.Append($"_objectServiceMappingList = Array.Empty<IStObjFinalImplementation>();").NewLine(); }
public void full_run_test() { var f = TestHelper.TestProjectFolder.AppendPart("TestCodeProject"); TestHelper.CleanupFolder(f); ICodeProject project = CodeWorkspace.CreateProject("MyProject"); project.TargetFrameworks.Add("netcoreapp3.1"); project.OutputType = "Exe"; project.Code.Global.EnsureUsing("System"); ITypeScope program = project.Code.Global.CreateType("public static class Program"); IFunctionScope main = program.GeneratedByComment() .CreateFunction("public static int Main()"); main.Append("Console.WriteLine(").AppendSourceString("Hop!").Append(" );").NewLine(); main.Append("return 0;"); var projectFolder = WriteProjectFolder(f, project); Run(projectFolder, "dotnet", "run", out string output); output.Should().Contain("Hop!"); }
public void playing_with_parts() { INamespaceScope g = CodeWorkspace.Create().Global; INamespaceScopePart gSub = g.CreatePart(); INamespaceScopePart gSub2 = gSub.CreatePart(); ITypeScope gSub2Type1 = gSub2.CreateType("class GSub2Type1"); ITypeScope gSub2Type2 = gSub2.CreateType("class GSub2Type2"); ITypeScopePart gSub2Type1Part1 = gSub2Type1.CreatePart(); ITypeScopePart gSub2Type1Part2 = gSub2Type1.CreatePart(); IFunctionScope gSub2Type1Part2F1 = gSub2Type1Part2.CreateFunction("void Action()"); IFunctionScopePart gSub2Type1Part2F1Part1 = gSub2Type1Part2F1.CreatePart(); g.Append("g:"); gSub.Append("gSub:"); gSub2.Append("gSub2:"); gSub2Type1.Append("gSub2Type1:"); gSub2Type2.Append("gSub2Type2:"); gSub2Type1Part1.Append("gSub2Type1Part1:"); gSub2Type1Part2.Append("gSub2Type1Part2:"); gSub2Type1Part2F1.Append("gSub2Type1Part2F1:"); gSub2Type1Part2F1Part1.Append("gSub2Type1Part2F1Part1:"); var s = g.ToString().Trim(); s.Should().Be(@" gSub2: gSub: g: class GSub2Type1 { gSub2Type1Part1: gSub2Type1Part2: gSub2Type1: void Action() { gSub2Type1Part2F1Part1: gSub2Type1Part2F1: } } class GSub2Type2 { gSub2Type2: }".Trim().ReplaceLineEndings()); }