/// <summary> /// Compiles the source code. /// </summary> /// <param name="stream">The source code to compile.</param> public virtual void Compile(Stream stream) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } ErrorList.ClearErrors(); try { ISerializer Serializer = CreateCompilerSerializer(); LoadedRoot = (IRoot)Serializer.Deserialize(stream); } catch (Exception e) { ErrorList.AddError(new ErrorInputFileInvalid(e)); return; } try { CompileRoot(LoadedRoot); } catch (Exception e) { ErrorList.AddError(new ErrorInternal(e)); } }
/// <summary> /// Compiles the source code. /// </summary> /// <param name="root">The source code to compile.</param> public virtual void Compile(BaseNode.IRoot root) { Root = root ?? throw new ArgumentNullException(nameof(root)); ErrorList.ClearErrors(); using (MemoryStream ms = new MemoryStream()) { ISerializer s = new Serializer(); s.Serialize(ms, root); ms.Seek(0, SeekOrigin.Begin); Compile(ms); } }
/// <summary> /// Compiles the file. The file must contain a serialized Easly Root object. /// </summary> /// <param name="fileName">The file to compile.</param> public virtual void Compile(string fileName) { FileName = fileName ?? throw new ArgumentNullException(nameof(fileName)); ErrorList.ClearErrors(); if (File.Exists(FileName)) { try { using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { Compile(fs); } } catch (Exception e) { ErrorList.AddError(new ErrorInputFileInvalid(e)); } } else { ErrorList.AddError(new ErrorInputFileNotFound(FileName)); } }
/// <summary> /// Translates nodes from the compiler to the target language. /// </summary> public override void Translate() { ErrorList.ClearErrors(); if (!CreateClasses(out IDictionary <IClass, ICSharpClass> ClassTable)) { return; } if (!CreateFeatures(ClassTable, out IDictionary <ICompiledFeature, ICSharpFeature> FeatureTable)) { return; } ICSharpContext Context = new CSharpContext(ClassTable, FeatureTable); foreach (KeyValuePair <IClass, ICSharpClass> ClassEntry in ClassTable) { ICSharpClass Class = ClassEntry.Value; IList <ICSharpFeature> ClassFeatureList = new List <ICSharpFeature>(); IList <ICSharpFeature> InheritedFeatureList = new List <ICSharpFeature>(); foreach (KeyValuePair <ICompiledFeature, ICSharpFeature> FeatureEntry in FeatureTable) { ICSharpFeature Feature = FeatureEntry.Value; IFeatureInstance Instance = Feature.Instance; if (Instance.IsDiscontinued) { continue; } if (FeatureEntry.Value.Owner == Class) { ClassFeatureList.Add(Feature); } else if (!IsDirectOrNotMainParentFeature(Instance, Class)) { InheritedFeatureList.Add(Feature); } } Class.SetFeatureList(Context, ClassFeatureList, InheritedFeatureList); } foreach (KeyValuePair <ICompiledFeature, ICSharpFeature> Entry in FeatureTable) { ICSharpFeature Feature = Entry.Value; Feature.InitOverloadsAndBodies(Context); } foreach (KeyValuePair <ICompiledFeature, ICSharpFeature> Entry in FeatureTable) { ICSharpFeature Feature = Entry.Value; Feature.InitHierarchy(Context); } foreach (KeyValuePair <IClass, ICSharpClass> Entry in ClassTable) { ICSharpClass Class = Entry.Value; CheckSharedName(Class, ClassTable); } foreach (KeyValuePair <IClass, ICSharpClass> Entry in ClassTable) { ICSharpClass Class = Entry.Value; Class.CheckOverrides(); } foreach (KeyValuePair <IClass, ICSharpClass> Entry in ClassTable) { ICSharpClass Class = Entry.Value; Class.CheckOverrides(); } bool Continue; do { Continue = false; foreach (KeyValuePair <IClass, ICSharpClass> Entry in ClassTable) { ICSharpClass Class = Entry.Value; Class.CheckForcedReadWrite(FeatureTable, ref Continue); } }while (Continue); foreach (KeyValuePair <IClass, ICSharpClass> Entry in ClassTable) { ICSharpClass Class = Entry.Value; Class.CheckSideBySideAttributes(); } foreach (KeyValuePair <IClass, ICSharpClass> Entry in ClassTable) { ICSharpClass Class = Entry.Value; Class.CheckInheritSideBySideAttributes(FeatureTable); } foreach (KeyValuePair <IClass, ICSharpClass> Entry in ClassTable) { ICSharpClass Class = Entry.Value; Class.CreateDelegates(); } ICSharpFeature SingledClassFeature = null; if (SingledGuid != Guid.Empty || SingledGuid == Guid.Empty) { foreach (KeyValuePair <IClass, ICSharpClass> Entry in ClassTable) { ICSharpClass Class = Entry.Value; if (Class.Source.ClassGuid == SingledGuid || SingledGuid == Guid.Empty || SingledGuid != Guid.Empty) { foreach (ICSharpFeature Feature in Class.FeatureList) { if (Feature is ICSharpFeatureWithName AsWithName && AsWithName.Name == SingledName) { SingledClassFeature = Feature; break; } } } if (SingledClassFeature != null) { break; } } } if (SingledClassFeature == null) { foreach (KeyValuePair <IClass, ICSharpClass> Entry in ClassTable) { ICSharpClass Class = Entry.Value; Class.SetWriteDown(); foreach (ICSharpFeature Feature in Class.FeatureList) { Feature.SetWriteDown(); } foreach (ICSharpAssertion Invariant in Class.InvariantList) { Invariant.SetWriteDown(); } } } else { SingledClassFeature.SetWriteDown(); } if (!Directory.Exists(OutputRootFolder)) { Directory.CreateDirectory(OutputRootFolder); } foreach (KeyValuePair <IClass, ICSharpClass> Entry in ClassTable) { ICSharpClass Class = Entry.Value; if (!CSharpClass.IsLanguageClass(Class.Source) && !IsClassFromLibrary(Class.Source)) { if (Class.WriteDown) { Class.Write(OutputRootFolder, Namespace, SourceFileName, SingledClassFeature); } } } }