public static TypeSymbol MakeClassTemplateInstance(Root root, IReadOnlyList<GenericsInstance> inst, TypeSymbol baseType) { var g = baseType as GenericSymbol; if(g != null) { var i = FindGenericIndex(inst, g); if (i == -1) { return baseType; } return inst[i].Type; } else if (baseType.Generics.Count > 0 || baseType.TacitGeneric.Count > 0) { var prm = new List<TypeSymbol>(); foreach (var v in baseType.Generics) { var t = MakeClassTemplateInstance(root, inst, v); prm.Add(t); } var tprm = new List<TypeSymbol>(); foreach (var v in baseType.TacitGeneric) { var t = MakeClassTemplateInstance(root, inst, v); tprm.Add(t); } return root.ClassManager.Issue(baseType, prm, tprm); } else { return baseType; } }
public void Execute(TestData data) { Root root = new Root(); var import = new CilImport(root); import.ImportAssembly(Assembly.Load("mscorlib")); import.ImportAssembly(Assembly.Load("CoreLibrary")); root.Append(CompileText(data.Name, data.Code)); root.SemanticAnalysis(); if (root.MessageManager.MessageCount > 0) { Directory.SetCurrentDirectory(".."); Console.WriteLine(CompileMessageBuilder.Build(root.MessageManager)); Directory.SetCurrentDirectory("output"); } Assert.That(root.MessageManager, Is.EquivalentTo(data.Message).Using<CompileMessage>(new CompileMessageEqualityComparer())); if (root.MessageManager.ErrorCount > 0) { Assert.That(data.ErrorCount, Is.Not.EqualTo(0), "Compile error"); return; } if (data.NoExecute) { return; } var trans = SyntaxTranslator.ToStructure(root, import, data.Name); trans.Save(); //var cd = Directory.GetCurrentDirectory(); //using (var process = MakeProcess(@"peverify.exe", data.Name + ".exe" + " /nologo /unique", cd)) //{ // process.Start(); // if (!process.WaitForExit(500)) // { // process.Kill(); // Assert.Fail("Peverify.exe timeout execution"); // } // Console.WriteLine(process.StandardOutput.ReadToEnd()); //} using (var process = MakeProcess(data.Name + ".exe")) { process.Start(); process.StandardInput.WriteLine(data.Input); if (!process.WaitForExit(data.TimeOut ?? 500)) { process.Kill(); Assert.Fail(data.Name + ".exe" + " timeout execution"); } var error = process.StandardError.ReadToEnd(); if (!string.IsNullOrWhiteSpace(error)) { Assert.Fail(error); } if (data.Output != null) { var output = TestData.CodeNormalize(process.StandardOutput.ReadToEnd(), true); Assert.That(output, Is.EqualTo(data.Output)); } } }
public static IReadOnlyList<ArgumentSymbol> MakeArgumentTemplateInstanceList(Root root, IReadOnlyList<GenericsInstance> inst, IReadOnlyList<ArgumentSymbol> args) { var ret = new List<ArgumentSymbol>(); foreach (var v in args) { var t = MakeClassTemplateInstance(root, inst, v.DataType); var p = new ArgumentSymbol(); p.Initialize(v.Name, v.VariantType, v.Attribute, t); ret.Add(p); } return ret; }
public static void Main(string[] args) { string fileName = args[0]; var root = new Root(); var import = new CilImport(root); import.ImportAssembly(Assembly.Load("mscorlib")); import.ImportAssembly(Assembly.Load("CoreLibrary")); root.Append(CompileFile(fileName)); root.SemanticAnalysis(); Console.WriteLine(CompileMessageBuilder.Build(root.MessageManager)); if (root.MessageManager.ErrorCount > 0) { return; } var trans = SyntaxTranslator.ToStructure(root, import, fileName.Replace(".dr", "")); trans.Save(); }
internal static OverLoadTypeMatch MakeMatch(Root root, TypeSymbol type, IReadOnlyList<GenericSymbol> fg, IReadOnlyList<GenericsInstance> inst, IReadOnlyList<TypeSymbol> ag) { var ig = new List<TypeSymbol>(); var result = new OverLoadTypeMatch() { Type = type, FormalGenerics = fg, ScopeInstance = inst, ActualGenerics = ag, InstanceGenerics = ig, }; if (TypeSymbol.HasAnyErrorType(fg)) { result.Result = TypeMatchResult.Unknown; return result; } if (!ContainGenericCount(fg, ag)) { result.Result = TypeMatchResult.UnmatchGenericCount; return result; } InitInstance(fg, ag, ig); var tgi = InferInstance(root, inst, ag, ig); if (TypeSymbol.HasAnyErrorType(tgi)) { result.Result = TypeMatchResult.UnmatchGenericType; return result; } result.Result = TypeMatchResult.PerfectMatch; result.Type = GenericsInstance.MakeClassTemplateInstance(root, tgi, type); return result; }
private static IReadOnlyList<GenericsInstance> InferInstance(Root root, IReadOnlyList<GenericsInstance> inst, IReadOnlyList<TypeSymbol> ag, List<TypeSymbol> ig) { var tgi = new List<GenericsInstance>(); for (var i = 0; i < ig.Count; ++i) { if (i < ag.Count) { tgi.Add(new GenericsInstance { Generic = (GenericSymbol)ig[i], Type = ag[i] }); } else { tgi.Add(new GenericsInstance { Generic = (GenericSymbol)ig[i], Type = root.Unknown }); } } tgi = tgi.Concat(inst).ToList(); for (var i = 0; i < ig.Count; ++i) { ig[i] = GenericsInstance.MakeClassTemplateInstance(root, tgi, ig[i]); } return tgi; }
public static IReadOnlyList<TypeSymbol> MakeClassTemplateInstanceList(Root root, IReadOnlyList<GenericsInstance> inst, IReadOnlyList<TypeSymbol> baseTypes) { var ret = new List<TypeSymbol>(); foreach(var v in baseTypes) { ret.Add(MakeClassTemplateInstance(root, inst, v)); } return ret; }
public static RoutineSymbol MakeRoutineTemplateInstance(Root root, IReadOnlyList<GenericsInstance> inst, RoutineSymbol baseRoutine) { if (baseRoutine.Generics.Count > 0 || baseRoutine.TacitGeneric.Count > 0) { var prm = new List<TypeSymbol>(); foreach (var v in baseRoutine.Generics) { var t = MakeClassTemplateInstance(root, inst, v); prm.Add(t); } var tprm = new List<TypeSymbol>(); foreach (var v in baseRoutine.TacitGeneric) { var t = MakeClassTemplateInstance(root, inst, v); tprm.Add(t); } return root.RoutineManager.Issue(baseRoutine, prm, tprm); } else { return baseRoutine; } }
public ConversionManager(Root root) { Root = root; ConvList = new List<RoutineSymbol>(); }
public CilImport(Root root) { Root = root; ImportDictionary = new Dictionary<object, Scope>(); ImportDictionary.Add(typeof(void), Root.Void); }
private static IReadOnlyList<GenericsInstance> InferInstance(Root root, IReadOnlyList<GenericsInstance> inst, IReadOnlyList<TypeSymbol> ag, IReadOnlyList<TypeSymbol> aa, List<TypeSymbol> ig, List<TypeSymbol> ia) { var tgi = new List<GenericsInstance>(); for (var i = 0; i < ig.Count; ++i) { if(i < ag.Count) { tgi.Add(new GenericsInstance { Generic = (GenericSymbol)ig[i], Type = ag[i] }); } else { tgi.Add(new GenericsInstance { Generic = (GenericSymbol)ig[i], Type = root.Unknown }); } } tgi = tgi.Concat(inst).ToList(); for (var i = 0; i < ia.Count; ++i) { var g = ia[i] as GenericSymbol; if(g == null) { continue; } var k = GenericsInstance.FindGenericIndex(tgi, g); if(k == -1) { continue; } var gi = tgi[k]; if (tgi[k].Type is UnknownSymbol) { gi.Type = aa[i]; } else { gi.Type = GetCommonSubType(gi.Type, aa[i]); } tgi[k] = gi; } for (var i = 0; i < ig.Count; ++i) { ig[i] = GenericsInstance.MakeClassTemplateInstance(root, tgi, ig[i]); } for (var i = 0; i < ia.Count; ++i) { ia[i] = GenericsInstance.MakeClassTemplateInstance(root, tgi, ia[i]); } return tgi; }
//todo さらに詳しい順位付けをする。 //todo デフォルト引数に対応する。 //todo 型制約に対応する。 internal static OverLoadCallMatch MakeMatch(Root root, RoutineSymbol call, IReadOnlyList<GenericSymbol> fg, IReadOnlyList<ArgumentSymbol> fa, IReadOnlyList<GenericsInstance> inst, IReadOnlyList<TypeSymbol> ag, IReadOnlyList<TypeSymbol> aa) { var ig = new List<TypeSymbol>(); var ia = new List<TypeSymbol>(); var convs = new List<RoutineSymbol>(); var result = new OverLoadCallMatch() { Call = call, FormalGenerics = fg, FormalArguments = fa, ScopeInstance = inst, ActualGenerics = ag, ActualArguments = aa, InstanceGenerics = ig, InstanceArguments = ia, Converters = convs, }; if (TypeSymbol.HasAnyErrorType(fg) || TypeSymbol.HasAnyErrorType(fa.GetDataTypes())) { result.Result = CallMatchResult.Unknown; return result; } if (!OverLoadTypeMatch.ContainGenericCount(fg, ag)) { result.Result = CallMatchResult.UnmatchGenericCount; return result; } if (!ContainArgumentCount(fa, aa) || !ContainTupleCount(fg, fa, ag, aa)) { result.Result = CallMatchResult.UnmatchArgumentCount; return result; } InitInstance(fg, fa, ag, aa, ig, ia); var tgi = InferInstance(root, inst, ag, aa, ig, ia); if (TypeSymbol.HasAnyErrorType(tgi)) { result.Result = CallMatchResult.UnmatchGenericType; return result; } for (int i = 0; i < ia.Count; i++) { var c = root.ConvManager.Find(aa[i], ia[i]); convs.Add(c); } result.Result = CheckConverterResult(convs); if (HasMatch(result.Result)) { result.Call = GenericsInstance.MakeRoutineTemplateInstance(root, tgi, call); } return result; }
public OperationManager(Root root) { Root = root; OpList = new Dictionary<TokenType, List<RoutineSymbol>>(); CreateLists(); }