Ejemplo n.º 1
0
        public static JToken ToJson(VariantExpression ve)
        {
            var obj = (JObject)ToJson((Expression)ve);

            obj["type"] = "VariantExpression";
            obj["ref"]  = ToJson((dynamic)ve.Reference);
            obj["key"]  = ToJson((dynamic)ve.Key);
            return(obj);
        }
        /// <summary>
        /// Sets the <see cref="ICSharpOutputNode.WriteDown"/> flag.
        /// </summary>
        public override void SetWriteDown()
        {
            if (WriteDown)
            {
                return;
            }

            WriteDown = true;

            foreach (ICSharpInstruction Instruction in InitInstructionList)
            {
                Instruction.SetWriteDown();
            }

            WhileCondition.SetWriteDown();

            foreach (ICSharpScopeAttributeFeature Item in EntityDeclarationList)
            {
                Item.SetWriteDown();
            }

            foreach (ICSharpInstruction Instruction in LoopInstructionList)
            {
                Instruction.SetWriteDown();
            }

            foreach (ICSharpInstruction Instruction in IterationInstructionList)
            {
                Instruction.SetWriteDown();
            }

            foreach (ICSharpAssertion Assertion in InvariantList)
            {
                Assertion.SetWriteDown();
            }

            if (VariantExpression != null)
            {
                VariantExpression.SetWriteDown();
            }
        }
        /// <summary>
        /// Writes down the C# instruction.
        /// </summary>
        /// <param name="writer">The stream on which to write.</param>
        public override void WriteCSharp(ICSharpWriter writer)
        {
            Debug.Assert(WriteDown);

            bool UseCurlyBrackets = false;

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

            if (EntityDeclarationList.Count > 0 /* || AttachmentVariableTable.Count > 0*/ || InitInstructionList.Count > 1)
            {
                UseCurlyBrackets = true;
            }

            if (UseCurlyBrackets)
            {
                writer.WriteIndentedLine("{");
                writer.IncreaseIndent();
            }

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

            if (Source.Variant.IsAssigned)
            {
                writer.WriteIndentedLine("double LoopVariant = double.NaN;");
            }

            /*
             * 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 (EntityDeclarationList.Count > 0 /* || AttachmentVariableTable.Count > 0*/)
            {
                writer.WriteEmptyLine();
            }

            foreach (ICSharpInstruction Item in InitInstructionList)
            {
                Item.WriteCSharp(writer);
            }

            WriteCSharpInvariant(writer);

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

            ICSharpExpressionContext SourceExpressionContext = new CSharpExpressionContext();

            WhileCondition.WriteCSharp(writer, SourceExpressionContext, -1);

            string WhileString = SourceExpressionContext.ReturnValue;

            writer.WriteIndentedLine($"while ({WhileString})");
            writer.WriteIndentedLine("{");
            writer.IncreaseIndent();

            foreach (ICSharpInstruction Item in LoopInstructionList)
            {
                Item.WriteCSharp(writer);
            }

            if (LoopInstructionList.Count > 0 && IterationInstructionList.Count > 0)
            {
                writer.WriteEmptyLine();
            }

            foreach (ICSharpInstruction Item in IterationInstructionList)
            {
                Item.WriteCSharp(writer);
            }

            if (VariantExpression != null)
            {
                ICSharpExpressionContext VariantExpressionContext = new CSharpExpressionContext();
                VariantExpression.WriteCSharp(writer, VariantExpressionContext, -1);

                string ExpressionText = VariantExpressionContext.ReturnValue;

                writer.WriteIndentedLine($"double NewVariantResult = {ExpressionText};");
                writer.WriteIndentedLine("if (NewVariantResult >= LoopVariant)// Takes advantage of the fact that 'x >= NaN' is always false");
                writer.IncreaseIndent();
                writer.WriteIndentedLine("throw new InvalidOperationException();");
                writer.DecreaseIndent();
                writer.WriteIndentedLine("LoopVariant = NewVariantResult;");
            }

            WriteCSharpInvariant(writer);

            writer.DecreaseIndent();
            writer.WriteIndentedLine("}");

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

            if (UseCurlyBrackets)
            {
                writer.DecreaseIndent();
                writer.WriteIndentedLine("}");
            }
        }