示例#1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CSharpContinuation"/> class.
        /// </summary>
        /// <param name="context">The creation context.</param>
        /// <param name="parentFeature">The parent feature.</param>
        /// <param name="source">The Easly node from which the C# node is created.</param>
        protected CSharpContinuation(ICSharpContext context, ICSharpFeature parentFeature, IContinuation source)
            : base(source)
        {
            ParentFeature = parentFeature;

            Instructions = CSharpScope.Create(context, parentFeature, (IScope)source.Instructions);

            foreach (IInstruction Instruction in source.CleanupList)
            {
                ICSharpInstruction NewInstruction = CSharpInstruction.Create(context, parentFeature, Instruction);
                CleanupList.Add(NewInstruction);
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="CSharpEffectiveBody"/> class.
        /// </summary>
        /// <param name="context">The creation context.</param>
        /// <param name="parentFeature">The parent feature.</param>
        /// <param name="source">The Easly body from which the C# body is created.</param>
        protected CSharpEffectiveBody(ICSharpContext context, ICSharpFeature parentFeature, IEffectiveBody source)
            : base(context, parentFeature, source)
        {
            foreach (IInstruction Instruction in source.BodyInstructionList)
            {
                ICSharpInstruction NewInstruction = CSharpInstruction.Create(context, parentFeature, Instruction);
                BodyInstructionList.Add(NewInstruction);
            }

            ICSharpClass Owner = parentFeature.Owner;

            foreach (IEntityDeclaration Item in source.EntityDeclarationList)
            {
                ICSharpScopeAttributeFeature NewDeclaration = CSharpScopeAttributeFeature.Create(context, Owner, Item.ValidEntity.Item);
                EntityDeclarationList.Add(NewDeclaration);
            }
        }
示例#3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CSharpScope"/> class.
        /// </summary>
        /// <param name="context">The creation context.</param>
        /// <param name="parentFeature">The parent feature.</param>
        /// <param name="source">The Easly node from which the C# node is created.</param>
        protected CSharpScope(ICSharpContext context, ICSharpFeature parentFeature, IScope source)
            : base(source)
        {
            ParentFeature = parentFeature;

            foreach (IEntityDeclaration Declaration in source.EntityDeclarationList)
            {
                ICSharpScopeAttributeFeature NewDeclaration = CSharpScopeAttributeFeature.Create(context, parentFeature.Owner, Declaration.ValidEntity.Item);
                EntityDeclarationList.Add(NewDeclaration);
            }

            foreach (IInstruction Instruction in source.InstructionList)
            {
                ICSharpInstruction NewInstruction = CSharpInstruction.Create(context, parentFeature, Instruction);
                InstructionList.Add(NewInstruction);
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="CSharpForLoopInstruction"/> class.
        /// </summary>
        /// <param name="context">The creation context.</param>
        /// <param name="parentFeature">The parent feature.</param>
        /// <param name="source">The Easly instruction from which the C# instruction is created.</param>
        protected CSharpForLoopInstruction(ICSharpContext context, ICSharpFeature parentFeature, IForLoopInstruction source)
            : base(context, parentFeature, source)
        {
            foreach (IEntityDeclaration Declaration in source.EntityDeclarationList)
            {
                ICSharpScopeAttributeFeature NewDeclaration = CSharpScopeAttributeFeature.Create(context, parentFeature.Owner, Declaration.ValidEntity.Item);
                EntityDeclarationList.Add(NewDeclaration);
            }

            foreach (IInstruction Instruction in source.InitInstructionList)
            {
                ICSharpInstruction NewInstruction = Create(context, parentFeature, Instruction);
                InitInstructionList.Add(NewInstruction);
            }

            WhileCondition = CSharpExpression.Create(context, (IExpression)source.WhileCondition);

            foreach (IInstruction Instruction in source.LoopInstructionList)
            {
                ICSharpInstruction NewInstruction = Create(context, parentFeature, Instruction);
                LoopInstructionList.Add(NewInstruction);
            }

            foreach (IInstruction Instruction in source.IterationInstructionList)
            {
                ICSharpInstruction NewInstruction = Create(context, parentFeature, Instruction);
                IterationInstructionList.Add(NewInstruction);
            }

            if (source.Variant.IsAssigned)
            {
                VariantExpression = CSharpExpression.Create(context, (IExpression)source.Variant.Item);
            }

            foreach (IAssertion Item in Source.InvariantList)
            {
                ICSharpAssertion NewAssertion = CSharpAssertion.Create(context, Item);
                InvariantList.Add(NewAssertion);
            }
        }
        /// <summary>
        /// Writes down the body source code.
        /// </summary>
        /// <param name="writer">The stream on which to write down.</param>
        /// <param name="flags">Some flags.</param>
        /// <param name="resultType">Type of the result, if any.</param>
        /// <param name="skipFirstInstruction">Skip the first instruction.</param>
        /// <param name="initialisationStringList">List of initializations.</param>
        public virtual void WriteCSharp(ICSharpWriter writer, CSharpBodyFlags flags, string resultType, bool skipFirstInstruction, IList <string> initialisationStringList)
        {
            Debug.Assert(WriteDown);

            writer.WriteIndentedLine("{");
            writer.IncreaseIndent();

            IList <ICSharpAssertion> EffectiveRequireList = RequireList;
            IList <ICSharpAssertion> EffectiveEnsureList  = EnsureList;

            switch (ParentFeature)
            {
            case ICSharpFunctionFeature AsFunctionFeature:
                if (AsFunctionFeature.OriginalPrecursor != null)
                {
                    ICSharpQueryOverload ParentOverload = null;
                    foreach (ICSharpQueryOverload Overload in AsFunctionFeature.OverloadList)
                    {
                        if (Overload.Body == this)
                        {
                            ParentOverload = Overload;
                            break;
                        }
                    }

                    Debug.Assert(ParentOverload != null);

                    ICSharpQueryOverload ParentPrecursorOverload = ParentOverload.Precursor;
                    if (ParentPrecursorOverload != null)
                    {
                        ICSharpBody PrecursorBody = ParentPrecursorOverload.Body;

                        if (RequireList.Count == 0 && PrecursorBody.RequireList.Count > 0)
                        {
                            EffectiveRequireList = PrecursorBody.RequireList;
                        }

                        if (EnsureList.Count == 0 && PrecursorBody.EnsureList.Count > 0)
                        {
                            EffectiveEnsureList = PrecursorBody.EnsureList;
                        }
                    }
                }
                break;

            case ICSharpProcedureFeature AsProcedureFeature:
                if (AsProcedureFeature.OriginalPrecursor != null)
                {
                    ICSharpCommandOverload ParentOverload = null;
                    foreach (ICSharpCommandOverload Overload in AsProcedureFeature.OverloadList)
                    {
                        if (Overload.Body == this)
                        {
                            ParentOverload = Overload;
                            break;
                        }
                    }

                    Debug.Assert(ParentOverload != null);

                    ICSharpCommandOverload ParentPrecursorOverload = ParentOverload.Precursor;
                    if (ParentPrecursorOverload != null)
                    {
                        ICSharpBody PrecursorBody = ParentPrecursorOverload.Body;

                        if (RequireList.Count == 0 && PrecursorBody.RequireList.Count > 0)
                        {
                            EffectiveRequireList = PrecursorBody.RequireList;
                        }

                        if (EnsureList.Count == 0 && PrecursorBody.EnsureList.Count > 0)
                        {
                            EffectiveEnsureList = PrecursorBody.EnsureList;
                        }
                    }
                }
                break;

            case ICSharpPropertyFeature AsPropertyFeature:
                if (AsPropertyFeature.OriginalPrecursor != null)
                {
                    ICSharpBody PrecursorBody = null;

                    if (this == AsPropertyFeature.GetterBody)
                    {
                        PrecursorBody = AsPropertyFeature.OriginalPrecursor.GetterBody;
                    }
                    else if (this == AsPropertyFeature.SetterBody)
                    {
                        PrecursorBody = AsPropertyFeature.OriginalPrecursor.SetterBody;
                    }

                    if (PrecursorBody != null)
                    {
                        if (RequireList.Count == 0 && PrecursorBody.RequireList.Count > 0)
                        {
                            EffectiveRequireList = PrecursorBody.RequireList;
                        }

                        if (EnsureList.Count == 0 && PrecursorBody.EnsureList.Count > 0)
                        {
                            EffectiveEnsureList = PrecursorBody.EnsureList;
                        }
                    }
                }
                break;

            case ICSharpIndexerFeature AsIndexerFeature:
                if (AsIndexerFeature.OriginalPrecursor != null)
                {
                    ICSharpBody PrecursorBody = null;

                    if (this == AsIndexerFeature.GetterBody)
                    {
                        PrecursorBody = AsIndexerFeature.OriginalPrecursor.GetterBody;
                    }
                    else if (this == AsIndexerFeature.SetterBody)
                    {
                        PrecursorBody = AsIndexerFeature.OriginalPrecursor.SetterBody;
                    }

                    if (PrecursorBody != null)
                    {
                        if (RequireList.Count == 0 && PrecursorBody.RequireList.Count > 0)
                        {
                            EffectiveRequireList = PrecursorBody.RequireList;
                        }

                        if (EnsureList.Count == 0 && PrecursorBody.EnsureList.Count > 0)
                        {
                            EffectiveEnsureList = PrecursorBody.EnsureList;
                        }
                    }
                }
                break;
            }

            foreach (ICSharpAssertion Assertion in EffectiveRequireList)
            {
                Assertion.WriteCSharp(writer);
            }

            if (EffectiveRequireList.Count > 0)
            {
                writer.WriteEmptyLine();
            }

            /*TODO
             * List<AttachmentAlias> AttachmentVariableTable = new List<AttachmentAlias>();
             * foreach (IInstruction Item in BodyInstructionList)
             *  Item.AddAttachmentVariables(Context, AttachmentVariableTable);
             */

            if (flags.HasFlag(CSharpBodyFlags.HasResult))
            {
                writer.WriteIndentedLine($"{resultType} Result = default;");
            }

            foreach (ICSharpScopeAttributeFeature Item in EntityDeclarationList)
            {
                Item.WriteCSharp(writer);
            }

            /*TODO
             * foreach (AttachmentAlias AliasItem in AttachmentVariableTable)
             * {
             *  string AttachedVariableName = AliasItem.EntityName;
             *  string AttachmentTypeString = CSharpTypes.Type2CSharpString(AliasItem.EntityType, Context, AliasItem.AttachmentFormat, CSharpNamespaceFormats.None);
             *
             *  writer.WriteIndentedLine(AttachmentTypeString + " " + AttachedVariableName + ";");
             *  Context.AttachmentVariableTable.Add(AliasItem);
             * }
             */

            if (flags.HasFlag(CSharpBodyFlags.HasResult) || EntityDeclarationList.Count > 0 /* || AttachmentVariableTable.Count > 0*/)
            {
                writer.WriteEmptyLine();
            }

            foreach (string s in initialisationStringList)
            {
                writer.WriteIndentedLine(s);
            }

            if (initialisationStringList.Count > 0)
            {
                writer.WriteEmptyLine();
            }

            for (int i = 0; i < BodyInstructionList.Count; i++)
            {
                if (i == 0 && skipFirstInstruction)
                {
                    continue;
                }

                ICSharpInstruction Item = BodyInstructionList[i];
                Item.WriteCSharp(writer);
            }

            if (EffectiveEnsureList.Count > 0)
            {
                writer.WriteEmptyLine();

                foreach (ICSharpAssertion Assertion in EffectiveEnsureList)
                {
                    Assertion.WriteCSharp(writer);
                }

                if (flags.HasFlag(CSharpBodyFlags.HasResult))
                {
                    writer.WriteEmptyLine();
                }
            }

            // TODO: ExceptionHandlerList

            if (ParentFeature.Owner.HasCheckInvariant)
            {
                writer.WriteEmptyLine();
                writer.WriteIndentedLine("CheckInvariant();");
            }

            if (flags.HasFlag(CSharpBodyFlags.HasResult))
            {
                writer.WriteIndentedLine("return Result;");
            }

            /*TODO
             * foreach (AttachmentAlias AliasItem in AttachmentVariableTable)
             *  Context.AttachmentVariableTable.Remove(AliasItem);
             */

            writer.DecreaseIndent();
            writer.WriteIndentedLine("}");
        }
        /// <summary>
        /// Creates a new C# instruction.
        /// </summary>
        /// <param name="context">The creation context.</param>
        /// <param name="parentFeature">The parent feature.</param>
        /// <param name="source">The Easly instruction from which the C# instruction is created.</param>
        public static ICSharpInstruction Create(ICSharpContext context, ICSharpFeature parentFeature, IInstruction source)
        {
            ICSharpInstruction Result = null;

            switch (source)
            {
            case IAsLongAsInstruction AsAsLongAsInstruction:
                Result = CSharpAsLongAsInstruction.Create(context, parentFeature, AsAsLongAsInstruction);
                break;

            case IAssignmentInstruction AsAssignmentInstruction:
                Result = CSharpAssignmentInstruction.Create(context, parentFeature, AsAssignmentInstruction);
                break;

            case IAttachmentInstruction AsAttachmentInstruction:
                Result = CSharpAttachmentInstruction.Create(context, parentFeature, AsAttachmentInstruction);
                break;

            case ICheckInstruction AsCheckInstruction:
                Result = CSharpCheckInstruction.Create(context, parentFeature, AsCheckInstruction);
                break;

            case ICommandInstruction AsCommandInstruction:
                Result = CSharpCommandInstruction.Create(context, parentFeature, AsCommandInstruction);
                break;

            case ICreateInstruction AsCreateInstruction:
                Result = CSharpCreateInstruction.Create(context, parentFeature, AsCreateInstruction);
                break;

            case IDebugInstruction AsDebugInstruction:
                Result = CSharpDebugInstruction.Create(context, parentFeature, AsDebugInstruction);
                break;

            case IForLoopInstruction AsForLoopInstruction:
                Result = CSharpForLoopInstruction.Create(context, parentFeature, AsForLoopInstruction);
                break;

            case IIfThenElseInstruction AsIfThenElseInstruction:
                Result = CSharpIfThenElseInstruction.Create(context, parentFeature, AsIfThenElseInstruction);
                break;

            case IIndexAssignmentInstruction AsIndexAssignmentInstruction:
                Result = CSharpIndexAssignmentInstruction.Create(context, parentFeature, AsIndexAssignmentInstruction);
                break;

            case IInspectInstruction AsInspectInstruction:
                Result = CSharpInspectInstruction.Create(context, parentFeature, AsInspectInstruction);
                break;

            case IKeywordAssignmentInstruction AsKeywordAssignmentInstruction:
                Result = CSharpKeywordAssignmentInstruction.Create(context, parentFeature, AsKeywordAssignmentInstruction);
                break;

            case IOverLoopInstruction AsOverLoopInstruction:
                Result = CSharpOverLoopInstruction.Create(context, parentFeature, AsOverLoopInstruction);
                break;

            case IPrecursorIndexAssignmentInstruction AsPrecursorIndexAssignmentInstruction:
                Result = CSharpPrecursorIndexAssignmentInstruction.Create(context, parentFeature, AsPrecursorIndexAssignmentInstruction);
                break;

            case IPrecursorInstruction AsPrecursorInstruction:
                Result = CSharpPrecursorInstruction.Create(context, parentFeature, AsPrecursorInstruction);
                break;

            case IRaiseEventInstruction AsRaiseEventInstruction:
                Result = CSharpRaiseEventInstruction.Create(context, parentFeature, AsRaiseEventInstruction);
                break;

            case IReleaseInstruction AsReleaseInstruction:
                Result = CSharpReleaseInstruction.Create(context, parentFeature, AsReleaseInstruction);
                break;

            case IThrowInstruction AsThrowInstruction:
                Result = CSharpThrowInstruction.Create(context, parentFeature, AsThrowInstruction);
                break;
            }

            Debug.Assert(Result != null);

            return(Result);
        }