/// <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);

            string CoexistingPrecursorName     = string.Empty;
            string CoexistingPrecursorRootName = ParentFeature.CoexistingPrecursorName;

            if (!string.IsNullOrEmpty(CoexistingPrecursorRootName))
            {
                CoexistingPrecursorName = CSharpNames.ToCSharpIdentifier(CoexistingPrecursorRootName + " " + "Base");
            }

            ICSharpExpressionContext ExpressionContext = new CSharpExpressionContext();
            string ArgumentListString = CSharpArgument.CSharpArgumentList(writer, ExpressionContext, FeatureCall);

            if (CoexistingPrecursorName.Length > 0)
            {
                writer.WriteIndentedLine($"{CoexistingPrecursorName}({ArgumentListString});");
            }
            else
            {
                string ProcedureName = CSharpNames.ToCSharpIdentifier(ParentFeature.Name);
                writer.WriteIndentedLine($"base.{ProcedureName}({ArgumentListString});");
            }
        }
        private void WriteCSharpAgentCall(ICSharpWriter writer, ICSharpExpressionContext expressionContext, int skippedIndex)
        {
            CSharpArgument.CSharpArgumentList(writer, expressionContext, FeatureCall, skippedIndex, true, out string ArgumentListText, out IList <string> OutgoingResultList);
            string QueryText = Query.CSharpText(writer, 0);

            IIdentifier AgentIdentifier     = (IIdentifier)Source.Query.Path[Source.Query.Path.Count - 1];
            string      AgentIdentifierText = CSharpNames.ToCSharpIdentifier(AgentIdentifier.ValidText.Item);

            if (Source.Query.Path.Count > 1)
            {
                QueryText = Query.CSharpText(writer, 1);
            }
            else
            {
                QueryText = "this";
            }

            if (FeatureCall.ArgumentList.Count > 0)
            {
                expressionContext.SetSingleReturnValue($"{AgentIdentifierText}({QueryText}, {ArgumentListText})");
            }
            else
            {
                expressionContext.SetSingleReturnValue($"{AgentIdentifierText}({QueryText})");
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Builds a list of parameters, with and without their type.
        /// </summary>
        /// <param name="usingCollection">The collection of using directives.</param>
        /// <param name="parameterList">The list of parameters.</param>
        /// <param name="parameterListText">The list of parameters with type upon return.</param>
        /// <param name="parameterNameListText">The list of parameters without type upon return.</param>
        public static void BuildParameterList(ICSharpUsingCollection usingCollection, IList <ICSharpParameter> parameterList, out string parameterListText, out string parameterNameListText)
        {
            parameterListText     = string.Empty;
            parameterNameListText = string.Empty;

            foreach (ICSharpParameter Parameter in parameterList)
            {
                if (parameterListText.Length > 0)
                {
                    parameterListText += ", ";
                }
                if (parameterNameListText.Length > 0)
                {
                    parameterNameListText += ", ";
                }

                string      ParameterName = Parameter.Name;
                ICSharpType ParameterType = Parameter.Feature.Type;

                CSharpTypeFormats ParameterFormat = ParameterType.HasInterfaceText ? CSharpTypeFormats.AsInterface : CSharpTypeFormats.Normal;

                string ParameterText     = ParameterType.Type2CSharpString(usingCollection, ParameterFormat, CSharpNamespaceFormats.None);
                string ParameterNameText = CSharpNames.ToCSharpIdentifier(ParameterName);

                parameterListText     += $"{ParameterText} {ParameterNameText}";
                parameterNameListText += ParameterNameText;
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Gets the source code corresponding to the expression.
        /// </summary>
        /// <param name="writer">The stream on which to write.</param>
        /// <param name="expressionContext">The context.</param>
        public void WriteCSharpAsConstant(ICSharpWriter writer, ICSharpExpressionContext expressionContext)
        {
            Debug.Assert(WriteDown);
            Debug.Assert(Class.Source.InitializedObjectList.Contains(Source));

            string ClassNameText = CSharpNames.ToCSharpIdentifier(Class.ValidClassName);

            string AssignmentText = string.Empty;

            foreach (ICSharpAssignmentArgument Assignment in AssignmentList)
            {
                ICSharpExpression SourceExpression = Assignment.SourceExpression;
                string            ExpressionText   = SingleResultExpressionText(writer, SourceExpression);

                foreach (string AssignedField in Assignment.ParameterNameList)
                {
                    if (AssignmentText.Length > 0)
                    {
                        AssignmentText += ", ";
                    }

                    string AssignedFieldText = CSharpNames.ToCSharpIdentifier(AssignedField);
                    AssignmentText += $"{AssignedFieldText} = {ExpressionText}";
                }
            }

            expressionContext.SetSingleReturnValue($"new {ClassNameText}() {{ {AssignmentText} }}");
        }
Ejemplo n.º 5
0
        private static void BuildResultListNoResult(ICSharpUsingCollection usingCollection, IList <ICSharpParameter> resultList, ref string parameterListText, ref string parameterNameListText, out string resultTypeText)
        {
            resultTypeText = "void";

            foreach (ICSharpParameter Result in resultList)
            {
                ICSharpScopeAttributeFeature ResultAttribute = Result.Feature;
                ICSharpType       ParameterType   = ResultAttribute.Type;
                CSharpTypeFormats ParameterFormat = ParameterType.HasInterfaceText ? CSharpTypeFormats.AsInterface : CSharpTypeFormats.Normal;

                string TypeString      = ParameterType.Type2CSharpString(usingCollection, ParameterFormat, CSharpNamespaceFormats.None);
                string AttributeString = CSharpNames.ToCSharpIdentifier(Result.Name);

                if (parameterListText.Length > 0)
                {
                    parameterListText += ", ";
                }
                if (parameterNameListText.Length > 0)
                {
                    parameterNameListText += ", ";
                }

                parameterListText     += $"out {TypeString} {AttributeString}";
                parameterNameListText += $"out {AttributeString}";
            }
        }
        /// <summary>
        /// Get the name of a type.
        /// </summary>
        /// <param name="usingCollection">The collection of using directives.</param>
        /// <param name="cSharpTypeFormat">The type format.</param>
        /// <param name="cSharpNamespaceFormat">The namespace format.</param>
        public override string Type2CSharpString(ICSharpUsingCollection usingCollection, CSharpTypeFormats cSharpTypeFormat, CSharpNamespaceFormats cSharpNamespaceFormat)
        {
            SetUsedInCode();

            string Result;

            if (OriginatingTypedef != null)
            {
                // TODO
                string DelegateName = CSharpNames.ToCSharpIdentifier(OriginatingTypedef.Name);

                Result = PropertyType2CSharpString(DelegateName);
            }
            else
            {
                string BaseTypeText   = BaseType.Type2CSharpString(usingCollection, CSharpTypeFormats.AsInterface, CSharpNamespaceFormats.None);
                string EntityTypeText = EntityType.Type2CSharpString(usingCollection, CSharpTypeFormats.AsInterface, CSharpNamespaceFormats.None);

                Result = $"Func<{BaseTypeText}, {EntityTypeText}>";

                usingCollection.AddUsing(nameof(System));
            }

            return(Result);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Writes down the C# feature.
        /// </summary>
        /// <param name="writer">The stream on which to write.</param>
        /// <param name="featureTextType">The write mode.</param>
        /// <param name="exportStatus">The feature export status.</param>
        /// <param name="isLocal">True if the feature is local to the class.</param>
        /// <param name="isFirstFeature">True if the feature is the first in a list.</param>
        /// <param name="isMultiline">True if there is a separating line above.</param>
        public override void WriteCSharp(ICSharpWriter writer, CSharpFeatureTextTypes featureTextType, CSharpExports exportStatus, bool isLocal, ref bool isFirstFeature, ref bool isMultiline)
        {
            if (!WriteDown)
            {
                return;
            }

            writer.WriteDocumentation(Source);

            bool ClassHasSingleConstructor = isLocal && Owner.ClassConstructorType == CSharpConstructorTypes.OneConstructor;

            string NameString;

            if (ClassHasSingleConstructor)
            {
                NameString = Owner.BasicClassName2CSharpClassName(writer, CSharpTypeFormats.Normal, CSharpNamespaceFormats.None);
            }
            else
            {
                NameString = CSharpNames.ToCSharpIdentifier(Name);
            }

            foreach (ICSharpCommandOverload Overload in OverloadList)
            {
                Overload.WriteCSharp(writer, featureTextType, IsOverride, NameString, exportStatus, ClassHasSingleConstructor, ref isFirstFeature, ref isMultiline);
            }
        }
        private void WriteCSharpInterface(ICSharpWriter writer, CSharpFeatureTextTypes featureTextType, CSharpExports exportStatus, bool isLocal, ref bool isFirstFeature, ref bool isMultiline)
        {
            string ResultType   = EntityType.Type2CSharpString(writer, CSharpTypeFormats.AsInterface, CSharpNamespaceFormats.None);
            string PropertyName = CSharpNames.ToCSharpIdentifier(Name);
            string Accessors    = null;

            if (IsForcedReadWrite)
            {
                Accessors = "{ get; set; }";
            }
            else
            {
                switch (Source.PropertyKind)
                {
                case BaseNode.UtilityType.ReadOnly:
                    Accessors = "{ get; }";
                    break;

                case BaseNode.UtilityType.WriteOnly:
                    Accessors = "{ set; }";
                    break;

                case BaseNode.UtilityType.ReadWrite:
                    Accessors = "{ get; set; }";
                    break;
                }
            }

            Debug.Assert(Accessors != null);

            writer.WriteIndentedLine($"{ResultType} {PropertyName} {Accessors}");

            isMultiline = false;
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Gets a temporary name from a source name.
        /// </summary>
        /// <param name="sourceName">The source name.</param>
        public string GetTemporaryName(string sourceName)
        {
            string CSharpIdentifier = CSharpNames.ToCSharpIdentifier(sourceName);
            string TemporaryName    = $"temp_{TemporaryVariableIndex}_{CSharpIdentifier}";

            TemporaryVariableIndex++;

            return(TemporaryName);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Gets the source code corresponding to the expression.
        /// </summary>
        /// <param name="writer">The stream on which to write.</param>
        /// <param name="expressionContext">The context.</param>
        /// <param name="skippedIndex">Index of a destination to skip.</param>
        public override void WriteCSharp(ICSharpWriter writer, ICSharpExpressionContext expressionContext, int skippedIndex)
        {
            Debug.Assert(WriteDown);
            Debug.Assert(Class.Source.InitializedObjectList.Contains(Source));

            string ClassNameText = CSharpNames.ToCSharpIdentifier(Class.ValidClassName);
            int    Index         = Class.Source.InitializedObjectList.IndexOf(Source);

            expressionContext.SetSingleReturnValue($"{ClassNameText}.InitializedObject{Index}");
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Builds a list of parameters, with and without their type.
        /// </summary>
        /// <param name="usingCollection">The collection of using directives.</param>
        /// <param name="parameterList">The list of parameters.</param>
        /// <param name="resultList">The list of results.</param>
        /// <param name="featureTextType">The write mode.</param>
        /// <param name="parameterListText">The list of parameters with type upon return.</param>
        /// <param name="parameterNameListText">The list of parameters without type upon return.</param>
        /// <param name="resultTypeText">The type text upon return.</param>
        public static void BuildParameterList(ICSharpUsingCollection usingCollection, IList <ICSharpParameter> parameterList, IList <ICSharpParameter> resultList, CSharpFeatureTextTypes featureTextType, out string parameterListText, out string parameterNameListText, out string resultTypeText)
        {
            parameterListText     = string.Empty;
            parameterNameListText = string.Empty;

            foreach (ICSharpParameter Parameter in parameterList)
            {
                if (parameterListText.Length > 0)
                {
                    parameterListText += ", ";
                }
                if (parameterNameListText.Length > 0)
                {
                    parameterNameListText += ", ";
                }

                string      ParameterName = Parameter.Name;
                ICSharpType ParameterType = Parameter.Feature.Type;

                CSharpTypeFormats ParameterFormat = ParameterType.HasInterfaceText ? CSharpTypeFormats.AsInterface : CSharpTypeFormats.Normal;

                string ParameterText     = ParameterType.Type2CSharpString(usingCollection, ParameterFormat, CSharpNamespaceFormats.None);
                string ParameterNameText = CSharpNames.ToCSharpIdentifier(ParameterName);

                parameterListText     += $"{ParameterText} {ParameterNameText}";
                parameterNameListText += ParameterNameText;
            }

            if (resultList.Count == 1)
            {
                BuildResultListSingle(usingCollection, resultList, out resultTypeText);
            }
            else
            {
                int ResultIndex = -1;
                for (int i = 0; i < resultList.Count; i++)
                {
                    ICSharpParameter Result = resultList[i];
                    if (Result.Name == nameof(BaseNode.Keyword.Result))
                    {
                        ResultIndex = i;
                        break;
                    }
                }

                if (ResultIndex < 0)
                {
                    BuildResultListNoResult(usingCollection, resultList, ref parameterListText, ref parameterNameListText, out resultTypeText);
                }
                else
                {
                    BuildResultListWithResult(usingCollection, resultList, ResultIndex, ref parameterListText, ref parameterNameListText, out resultTypeText);
                }
            }
        }
Ejemplo n.º 12
0
        /// <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);

            // TODO: declare the event

            IIdentifier QueryIdentifierItem       = (IIdentifier)Source.QueryIdentifier;
            string      QueryIdentifierItemString = CSharpNames.ToCSharpIdentifier(QueryIdentifierItem.ValidText.Item);

            writer.WriteIndentedLine($"{QueryIdentifierItemString}.set();");
        }
Ejemplo n.º 13
0
        private void WriteCSharpImplementation(ICSharpWriter writer, CSharpExports exportStatus, ref bool isFirstFeature, ref bool isMultiline)
        {
            bool IsEvent = false;

            if (Type is ICSharpClassType AsClassType)
            {
                ICSharpClass Class = AsClassType.Class;
                if (Class.InheritFromDotNetEvent)
                {
                    IsEvent = true;
                }
            }

            writer.WriteDocumentation(Source);

            CSharpAssertion.WriteContract(writer, new List <ICSharpAssertion>(), EnsureList, CSharpContractLocations.Other, false, ref isFirstFeature, ref isMultiline);

            string TypeString      = Type.Type2CSharpString(writer, CSharpTypeFormats.AsInterface, CSharpNamespaceFormats.None);
            string AttributeString = CSharpNames.ToCSharpIdentifier(Name);
            string ExportString    = CSharpNames.ComposedExportStatus(false, false, true, exportStatus);

            if (IsEvent)
            {
                writer.WriteIndentedLine($"{ExportString} event {TypeString} {AttributeString};");
            }
            else if (Type.GetSingletonString(writer, CSharpTypeFormats.Normal, CSharpNamespaceFormats.None, out string SingletonString))
            {
                writer.WriteIndentedLine($"{ExportString} {TypeString} {AttributeString} {{ get {{ return {SingletonString}; }} }}");
            }
            else if (EnsureList.Count > 0)
            {
                writer.WriteIndentedLine($"{ExportString} {TypeString} {AttributeString} {{ get; private set; }}");
                writer.WriteIndentedLine($"protected void Set_{AttributeString}({TypeString} value)");
                writer.WriteIndentedLine("{");
                writer.IncreaseIndent();

                writer.WriteIndentedLine($"{AttributeString} = value;");

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

                writer.DecreaseIndent();
                writer.WriteIndentedLine("}");
            }
            else
            {
                writer.WriteIndentedLine($"{ExportString} {TypeString} {AttributeString} {{ get; protected set; }}");
            }
        }
        private void WriteCSharpImplementation(ICSharpWriter writer, CSharpExports exportStatus, bool isLocal, ref bool isFirstFeature, ref bool isMultiline)
        {
            writer.WriteDocumentation(Source);

            string TypeString      = Type.Type2CSharpString(writer, CSharpTypeFormats.AsInterface, CSharpNamespaceFormats.None);
            string AttributeString = CSharpNames.ToCSharpIdentifier(Name);

            ICSharpExpressionAsConstant ExpressionAsConstant = ConstantExpression as ICSharpExpressionAsConstant;

            Debug.Assert(ExpressionAsConstant != null);

            string ValueString;

            if (ExpressionAsConstant.IsDirectConstant)
            {
                ICSharpExpressionContext SourceExpressionContext = new CSharpExpressionContext();
                ConstantExpression.WriteCSharp(writer, SourceExpressionContext, -1);

                ValueString = SourceExpressionContext.ReturnValue;
                Debug.Assert(ValueString != null);
            }
            else
            {
                ICSharpComputableExpression CompilableExpression = ConstantExpression as ICSharpComputableExpression;
                Debug.Assert(CompilableExpression != null);

                CompilableExpression.Compute(writer);
                ValueString = CompilableExpression.ComputedValue;
                Debug.Assert(ValueString != null);

                writer.WriteIndentedLine($"// {ValueString} = {ConstantExpression.Source.ExpressionToString}");
            }

            string ExportStatusText = CSharpNames.ComposedExportStatus(false, false, true, exportStatus);

            bool IsReadOnlyObject;

            if (Type is ICSharpClassType AsClassType)
            {
                Guid ClassGuid = AsClassType.Source.BaseClass.ClassGuid;
                IsReadOnlyObject = ClassGuid != LanguageClasses.Number.Guid && ClassGuid != LanguageClasses.Boolean.Guid && ClassGuid != LanguageClasses.String.Guid && ClassGuid != LanguageClasses.Character.Guid;
            }
            else
            {
                IsReadOnlyObject = true;
            }

            string ConstString = IsReadOnlyObject ? "static readonly" : "const";

            writer.WriteIndentedLine($"{ExportStatusText} {ConstString} {TypeString} {AttributeString} = {ValueString};");
        }
        /// <summary>
        /// Gets the source code corresponding to the qualified name.
        /// </summary>
        /// <param name="usingCollection">The collection of using directives.</param>
        /// <param name="skippedAtEnd">Number of identifiers to skip at the end.</param>
        public string CSharpText(ICSharpUsingCollection usingCollection, int skippedAtEnd)
        {
            string Result;
            int    i = 0;

            /*
             * if (Context.AttachmentAliasTable.ContainsKey(ValidPath.Item[i].ValidText.Item))
             * {
             *  Result = Context.AttachmentAliasTable[ValidPath.Item[i].ValidText.Item];
             *  i++;
             * }
             * else*/
            Result = string.Empty;

            for (; i + skippedAtEnd < Source.ValidPath.Item.Count; i++)
            {
                if (Result.Length > 0)
                {
                    Result += ".";
                }

                IIdentifier  Item      = Source.ValidPath.Item[i];
                ICSharpClass ItemClass = i < ClassPath.Count ? ClassPath[i] : null;
                string       ItemText  = Item.ValidText.Item;

                if (i == 0 && usingCollection.AttachmentMap.ContainsKey(Item.ValidText.Item))
                {
                    ItemText = usingCollection.AttachmentMap[ItemText];
                }
                else
                {
                    ItemText = CSharpNames.ToCSharpIdentifier(ItemText);
                }

                if (ItemClass != null)
                {
                    if (ItemClass.IsUnparameterizedSingleton && ItemClass.ValidSourceName != "Microsoft .NET")
                    {
                        string TypeText = ItemClass.Type.Type2CSharpString(usingCollection, CSharpTypeFormats.Normal, CSharpNamespaceFormats.None);
                        ItemText = $"{TypeText}.Singleton";
                    }
                }

                Result += ItemText;
            }

            return(Result);
        }
        /// <summary>
        /// Writes down the C# feature.
        /// </summary>
        /// <param name="writer">The stream on which to write.</param>
        /// <param name="featureTextType">The write mode.</param>
        /// <param name="exportStatus">The feature export status.</param>
        /// <param name="isLocal">True if the feature is local to the class.</param>
        /// <param name="isFirstFeature">True if the feature is the first in a list.</param>
        /// <param name="isMultiline">True if there is a separating line above.</param>
        public override void WriteCSharp(ICSharpWriter writer, CSharpFeatureTextTypes featureTextType, CSharpExports exportStatus, bool isLocal, ref bool isFirstFeature, ref bool isMultiline)
        {
            if (!WriteDown)
            {
                return;
            }

            writer.WriteDocumentation(Source);

            string NameString = CSharpNames.ToCSharpIdentifier(Name);

            foreach (ICSharpCommandOverload Overload in OverloadList)
            {
                Overload.WriteCSharp(writer, featureTextType, IsOverride, NameString, exportStatus, false, ref isFirstFeature, ref isMultiline);
            }
        }
Ejemplo n.º 17
0
        private string CSharpTextProperty(ICSharpUsingCollection usingCollection, ICSharpPropertyFeature feature)
        {
            string Result;

            string BaseTypeText;

            if (BaseType != null)
            {
                BaseTypeText = EffectiveBaseType.Type2CSharpString(usingCollection, CSharpTypeFormats.AsInterface, CSharpNamespaceFormats.OneWord);
            }
            else
            {
                BaseTypeText = $"I{CSharpNames.ToCSharpIdentifier(Delegated.Owner.ValidClassName)}";
            }

            Result = $"({BaseTypeText} agentBase) => {{ return agentBase.{CSharpNames.ToCSharpIdentifier(Delegated.Name)}; }}";

            return(Result);
        }
        private void WriteCSharpImplementation(ICSharpWriter writer, CSharpFeatureTextTypes featureTextType, CSharpExports exportStatus, bool isLocal, ref bool isFirstFeature, ref bool isMultiline)
        {
            writer.WriteDocumentation(Source);

            string ResultType   = EntityType.Type2CSharpString(writer, CSharpTypeFormats.AsInterface, CSharpNamespaceFormats.None);
            string PropertyName = CSharpNames.ToCSharpIdentifier(Name);
            bool   IsHandled    = false;

            if (IsForcedReadWrite)
            {
                WriteCSharpForcedReadWriteProperty(writer, IsOverride, exportStatus, PropertyName, ResultType, ref isFirstFeature, ref isMultiline);
                IsHandled = true;
            }
            else
            {
                switch (Source.PropertyKind)
                {
                case BaseNode.UtilityType.ReadOnly:
                    WriteCSharpReadOnlyProperty(writer, IsOverride, exportStatus, PropertyName, ResultType, ref isFirstFeature, ref isMultiline);
                    IsHandled = true;
                    break;

                case BaseNode.UtilityType.WriteOnly:
                    WriteCSharpWriteOnlyProperty(writer, IsOverride, exportStatus, PropertyName, ResultType, ref isFirstFeature, ref isMultiline);
                    IsHandled = true;
                    break;

                case BaseNode.UtilityType.ReadWrite:
                    WriteCSharpReadWriteProperty(writer, IsOverride, exportStatus, PropertyName, ResultType, ref isFirstFeature, ref isMultiline);
                    IsHandled = true;
                    break;
                }
            }

            Debug.Assert(IsHandled);

            if (HasSideBySideAttribute && !Instance.InheritBySideAttribute)
            {
                writer.WriteIndentedLine($"protected {ResultType} _{PropertyName};");

                isMultiline = true;
            }
        }
        /// <summary>
        /// Gets the source code corresponding to the qualified name setter.
        /// </summary>
        /// <param name="usingCollection">The collection of using directives.</param>
        public string CSharpSetter(ICSharpUsingCollection usingCollection)
        {
            string StartText;

            if (Source.ValidPath.Item.Count > 1)
            {
                StartText  = CSharpText(usingCollection, 1);
                StartText += ".";
            }
            else
            {
                StartText = string.Empty;
            }

            IIdentifier Item       = Source.ValidPath.Item[0];
            string      ItemText   = CSharpNames.ToCSharpIdentifier(Item.ValidText.Item);
            string      SetterText = $"{StartText}Set_{ItemText}";

            return(SetterText);
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Gets the source code corresponding to the expression.
        /// </summary>
        /// <param name="writer">The stream on which to write.</param>
        /// <param name="expressionContext">The context.</param>
        /// <param name="skippedIndex">Index of a destination to skip.</param>
        public override void WriteCSharp(ICSharpWriter writer, ICSharpExpressionContext expressionContext, int skippedIndex)
        {
            Debug.Assert(WriteDown);

            string TypeText;
            string ClassName;
            string ConstantName;

            if (Feature != null)
            {
                TypeText = string.Empty;

                if (Class.ValidSourceName == "Microsoft .NET")
                {
                    ClassName    = CSharpNames.ToDotNetIdentifier(Class.ValidClassName);
                    ConstantName = CSharpNames.ToDotNetIdentifier(Feature.Name);
                }
                else
                {
                    ClassName    = CSharpNames.ToCSharpIdentifier(Class.ValidClassName);
                    ConstantName = CSharpNames.ToCSharpIdentifier(Feature.Name);
                }
            }
            else
            {
                TypeText = "(int)";

                if (Class.HasDiscreteWithUnkownValue)
                {
                    ClassName = CSharpNames.ToCSharpIdentifier(Class.ValidClassName) + "_Enum";
                }
                else
                {
                    ClassName = CSharpNames.ToCSharpIdentifier(Class.ValidClassName);
                }

                ConstantName = CSharpNames.ToCSharpIdentifier(Discrete.Name);
            }

            expressionContext.SetSingleReturnValue($"{TypeText}{ClassName}.{ConstantName}");
        }
Ejemplo n.º 21
0
        private string CSharpTextFunction(ICSharpUsingCollection usingCollection, ICSharpFunctionFeature feature)
        {
            string Result;

            // TODO handle several overloads.

            Debug.Assert(feature.OverloadList.Count > 0);
            ICSharpQueryOverload Overload = feature.OverloadList[0] as ICSharpQueryOverload;

            Debug.Assert(Overload != null);

            string BaseTypeText;

            if (BaseType != null)
            {
                BaseTypeText = EffectiveBaseType.Type2CSharpString(usingCollection, CSharpTypeFormats.AsInterface, CSharpNamespaceFormats.OneWord);
            }
            else
            {
                BaseTypeText = $"I{CSharpNames.ToCSharpIdentifier(Delegated.Owner.ValidClassName)}";
            }

            string AgentParameters;
            string ParameterNameListText;

            if (Overload.ParameterList.Count > 0)
            {
                CSharpArgument.BuildParameterList(usingCollection, Overload.ParameterList, out string ParameterListText, out ParameterNameListText);
                AgentParameters = $"({BaseTypeText} agentBase, {ParameterListText})";
            }
            else
            {
                AgentParameters       = $"({BaseTypeText} agentBase)";
                ParameterNameListText = string.Empty;
            }

            Result = $"{AgentParameters} => {{ return agentBase.{CSharpNames.ToCSharpIdentifier(Delegated.Name)}({ParameterNameListText}); }}";

            return(Result);
        }
Ejemplo n.º 22
0
        /// <summary>
        /// Writes down the C# feature.
        /// </summary>
        /// <param name="writer">The stream on which to write.</param>
        public void WriteCSharp(ICSharpWriter writer)
        {
            Debug.Assert(WriteDown);

            string NameString       = CSharpNames.ToCSharpIdentifier(Name);
            string TypeString       = Type.Type2CSharpString(writer, CSharpTypeFormats.AsInterface, CSharpNamespaceFormats.None);
            string DefaultValueText = string.Empty;

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

                DefaultValueText = ExpressionContext.ReturnValue;

                if (DefaultValue.IsComplex)
                {
                    DefaultValueText = $"({DefaultValueText})";
                }
            }

            if (DefaultValueText.Length == 0)
            {
                if (Type.GetSingletonString(writer, CSharpTypeFormats.Normal, CSharpNamespaceFormats.None, out string SingletonString))
                {
                    DefaultValueText = SingletonString;
                }
            }

            if (DefaultValueText.Length == 0)
            {
                DefaultValueText = "default";
            }

            DefaultValueText = $" = {DefaultValueText}";

            writer.WriteIndentedLine($"{TypeString} {NameString}{DefaultValueText};");
        }
Ejemplo n.º 23
0
        private void WriteCSharpCustomOperator(ICSharpWriter writer, ICSharpExpressionContext expressionContext, int skippedIndex)
        {
            string OperatorText = CSharpNames.ToCSharpIdentifier(Operator.Name);

            if (LeftExpression.IsSingleResult && RightExpression.IsSingleResult)
            {
                string LeftText = SingleResultExpressionText(writer, LeftExpression);

                ICSharpExpressionContext SourceExpressionContext = new CSharpExpressionContext();
                RightExpression.WriteCSharp(writer, SourceExpressionContext, -1);
                string RightText = SourceExpressionContext.ReturnValue;

                expressionContext.SetSingleReturnValue($"{LeftText}.{OperatorText}({RightText})");
            }
            else
            {
                Operator.GetOutputFormat(SelectedOverloadType, out int OutgoingParameterCount, out int ReturnValueIndex);

                string LeftText = SingleResultExpressionText(writer, LeftExpression);

                CSharpArgument.CSharpArgumentList(writer, expressionContext, FeatureCall, ReturnValueIndex, false, out string ArgumentListText, out IList <string> OutgoingResultList);

                if (ReturnValueIndex >= 0)
                {
                    string TemporaryResultName = writer.GetTemporaryName();
                    writer.WriteIndentedLine($"var {TemporaryResultName} = {LeftText}.{OperatorText}({ArgumentListText});");

                    OutgoingResultList.Insert(ReturnValueIndex, TemporaryResultName);
                }
                else
                {
                    writer.WriteIndentedLine($"{LeftText}.{OperatorText}({ArgumentListText});");
                }

                expressionContext.SetMultipleResult(OutgoingResultList, ReturnValueIndex);
            }
        }
Ejemplo n.º 24
0
        /// <summary>
        /// Get the name of a type.
        /// </summary>
        /// <param name="usingCollection">The collection of using directives.</param>
        /// <param name="cSharpTypeFormat">The type format.</param>
        /// <param name="cSharpNamespaceFormat">The namespace format.</param>
        public override string Type2CSharpString(ICSharpUsingCollection usingCollection, CSharpTypeFormats cSharpTypeFormat, CSharpNamespaceFormats cSharpNamespaceFormat)
        {
            SetUsedInCode();

            string Result;

            // TODO: detect delegate call parameters to select the proper overload

            if (OriginatingTypedef != null)
            {
                string DelegateName = CSharpNames.ToCSharpIdentifier(OriginatingTypedef.Name);

                Result = CommandOverloadType2CSharpString(DelegateName, Source.OverloadList[0]);
            }
            else
            {
                ICSharpCommandOverloadType OverloadType = OverloadTypeList[0];

                string ActionArgumentText = BaseType.Type2CSharpString(usingCollection, CSharpTypeFormats.AsInterface, CSharpNamespaceFormats.None);

                foreach (ICSharpParameter Parameter in OverloadType.ParameterList)
                {
                    ICSharpType       ParameterType   = Parameter.Feature.Type;
                    CSharpTypeFormats ParameterFormat = ParameterType.HasInterfaceText ? CSharpTypeFormats.AsInterface : CSharpTypeFormats.Normal;
                    string            ParameterText   = ParameterType.Type2CSharpString(usingCollection, ParameterFormat, CSharpNamespaceFormats.None);

                    ActionArgumentText += $", {ParameterText}";
                }

                Result = $"Action<{ActionArgumentText}>";

                usingCollection.AddUsing(nameof(System));
            }

            return(Result);
        }
Ejemplo n.º 25
0
        private static void BuildResultListWithResult(ICSharpUsingCollection usingCollection, IList <ICSharpParameter> resultList, int resultIndex, ref string parameterListText, ref string parameterNameListText, out string resultTypeText)
        {
            resultTypeText = null;

            for (int i = 0; i < resultList.Count; i++)
            {
                ICSharpParameter             Result          = resultList[i];
                ICSharpScopeAttributeFeature ResultAttribute = Result.Feature;
                ICSharpType       ParameterType   = ResultAttribute.Type;
                CSharpTypeFormats ParameterFormat = ParameterType.HasInterfaceText ? CSharpTypeFormats.AsInterface : CSharpTypeFormats.Normal;

                string TypeString      = ParameterType.Type2CSharpString(usingCollection, ParameterFormat, CSharpNamespaceFormats.None);
                string AttributeString = CSharpNames.ToCSharpIdentifier(Result.Name);

                if (i == resultIndex)
                {
                    resultTypeText = TypeString;
                }
                else
                {
                    if (parameterListText.Length > 0)
                    {
                        parameterListText += ", ";
                    }
                    if (parameterNameListText.Length > 0)
                    {
                        parameterNameListText += ", ";
                    }

                    parameterListText     += $"out {TypeString} {AttributeString}";
                    parameterNameListText += $"out {AttributeString}";
                }
            }

            Debug.Assert(resultTypeText != null);
        }
Ejemplo n.º 26
0
        /// <summary>
        /// Writes down the C# conditional instructions.
        /// </summary>
        /// <param name="writer">The stream on which to write.</param>
        public virtual void WriteCSharp(ICSharpWriter writer)
        {
            Debug.Assert(WriteDown);

            foreach (ILanguageConstant Constant in ConstantList)
            {
                bool IsHandled = false;

                switch (Constant)
                {
                case INumberLanguageConstant AsManifestConstant:
                    Number AsNumber = AsManifestConstant.Value;
                    if (AsNumber.TryParseInt(out int IntValue))
                    {
                        writer.WriteIndentedLine($"case {IntValue}:");
                        IsHandled = true;
                    }
                    break;

                case IDiscreteLanguageConstant AsDiscreteConstant:
                    IDiscrete    DiscreteItem        = AsDiscreteConstant.Discrete;
                    IName        ClassEntityName     = (IName)DiscreteItem.EmbeddingClass.EntityName;
                    string       ClassName           = CSharpNames.ToCSharpIdentifier(ClassEntityName.ValidText.Item);
                    IFeatureName DiscreteFeatureName = DiscreteItem.ValidDiscreteName.Item;
                    string       DiscreteName        = CSharpNames.ToCSharpIdentifier(DiscreteFeatureName.Name);

                    writer.WriteIndentedLine($"case {ClassName}.{DiscreteName}:");
                    IsHandled = true;
                    break;
                }

                Debug.Assert(IsHandled);
            }

            Instructions.WriteCSharp(writer, CSharpCurlyBracketsInsertions.Indifferent, true);
        }
        /// <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);

            string CommandText;

            if (IsCallingNumberFeature)
            {
                CommandText = Command.CSharpText(writer, 0);
                IList <IIdentifier> ValidPath = ((IQualifiedName)Source.Command).ValidPath.Item;
                IIdentifier         FinalFeatureIdentifier = ValidPath[ValidPath.Count - 1];

                if (FinalFeatureIdentifier.ValidText.Item == "Increment")
                {
                    CommandText = CommandText.Substring(0, CommandText.Length - 10);
                    writer.WriteIndentedLine($"{CommandText}++;");
                    return;
                }

                else if (FinalFeatureIdentifier.ValidText.Item == "Decrement")
                {
                    CommandText = CommandText.Substring(0, CommandText.Length - 10);
                    writer.WriteIndentedLine($"{CommandText}--;");
                    return;
                }
            }

            bool IsAgent = !(FinalFeature is ICSharpProcedureFeature);

            if (IsAgent)
            {
                IIdentifier AgentIdentifier     = (IIdentifier)Source.Command.Path[Source.Command.Path.Count - 1];
                string      AgentIdentifierText = CSharpNames.ToCSharpIdentifier(AgentIdentifier.ValidText.Item);

                if (Source.Command.Path.Count > 1)
                {
                    CommandText = Command.CSharpText(writer, 1);
                }
                else
                {
                    CommandText = "this";
                }

                if (FeatureCall.ArgumentList.Count > 0)
                {
                    ICSharpExpressionContext ExpressionContext = new CSharpExpressionContext();
                    string ArgumentListText = CSharpArgument.CSharpArgumentList(writer, ExpressionContext, FeatureCall);

                    writer.WriteIndentedLine($"{AgentIdentifierText}({CommandText}, {ArgumentListText});");
                }
                else
                {
                    writer.WriteIndentedLine($"{AgentIdentifierText}({CommandText});");
                }
            }
            else
            {
                CommandText = Command.CSharpText(writer, SkipLastInPath ? 1 : 0);
                ICSharpExpressionContext ExpressionContext = new CSharpExpressionContext();
                string ArgumentListText = CSharpArgument.CSharpArgumentList(writer, ExpressionContext, FeatureCall);

                writer.WriteIndentedLine($"{CommandText}({ArgumentListText});");
            }
        }
Ejemplo n.º 28
0
        /// <summary>
        /// Writes down the C# overload of a feature.
        /// </summary>
        /// <param name="writer">The stream on which to write.</param>
        /// <param name="featureTextType">The write mode.</param>
        /// <param name="isOverride">True if the feature is an override.</param>
        /// <param name="nameString">The composed feature name.</param>
        /// <param name="exportStatus">The feature export status.</param>
        /// <param name="isConstructor">True if the feature is a constructor.</param>
        /// <param name="isFirstFeature">True if the feature is the first in a list.</param>
        /// <param name="isMultiline">True if there is a separating line above.</param>
        public void WriteCSharp(ICSharpWriter writer, CSharpFeatureTextTypes featureTextType, bool isOverride, string nameString, CSharpExports exportStatus, bool isConstructor, ref bool isFirstFeature, ref bool isMultiline)
        {
            Debug.Assert(WriteDown);

            IList <ICSharpParameter> SelectedParameterList = ParameterList;
            IList <ICSharpParameter> SelectedResultList    = ResultList;

            if (isOverride && Precursor != null)
            {
                SelectedParameterList = Precursor.ParameterList;
                SelectedResultList    = Precursor.ResultList;
            }

            CSharpArgument.BuildParameterList(writer, SelectedParameterList, SelectedResultList, featureTextType, out string ArgumentEntityList, out string ArgumentNameList, out string ResultType);
            string ExportStatusText;

            if (featureTextType == CSharpFeatureTextTypes.Implementation)
            {
                bool IsHandled = false;

                switch (Body)
                {
                case ICSharpDeferredBody AsDeferredBody:
                    CSharpAssertion.WriteContract(writer, AsDeferredBody.RequireList, AsDeferredBody.EnsureList, CSharpContractLocations.Other, true, ref isFirstFeature, ref isMultiline);
                    ExportStatusText = CSharpNames.ComposedExportStatus(false, true, false, exportStatus);
                    writer.WriteIndentedLine($"{ExportStatusText} {ResultType} {nameString}({ArgumentEntityList});");
                    isMultiline = false;
                    IsHandled   = true;
                    break;

                case ICSharpEffectiveBody AsEffectiveBody:
                    CSharpAssertion.WriteContract(writer, AsEffectiveBody.RequireList, AsEffectiveBody.EnsureList, CSharpContractLocations.Other, true, ref isFirstFeature, ref isMultiline);

                    CSharpBodyFlags Flags                    = CSharpBodyFlags.MandatoryCurlyBrackets;
                    string          ResultString             = string.Empty;
                    List <string>   InitialisationStringList = new List <string>();

                    if (ResultList.Count == 1)
                    {
                        Flags |= CSharpBodyFlags.HasResult;
                        ICSharpParameter Result = ResultList[0];
                        ResultString = Result.Feature.Type.Type2CSharpString(writer, CSharpTypeFormats.AsInterface, CSharpNamespaceFormats.None);
                    }
                    else
                    {
                        if (ResultType != "void")
                        {
                            Flags       |= CSharpBodyFlags.HasResult;
                            ResultString = ResultType;
                        }

                        foreach (ICSharpParameter Item in ResultList)
                        {
                            string InitValueString;

                            ICSharpType ResultEntityType = Item.Feature.Type;

                            if (ResultEntityType is ICSharpClassType AsClassType)
                            {
                                // TODO: when the type inherit from Enumeration
                                if (AsClassType.Class.Source.ClassGuid == LanguageClasses.AnyOptionalReference.Guid)
                                {
                                    InitValueString = "new OptionalReference<>(null)";     // TODO
                                }
                                else if (AsClassType.Class.Source.ClassGuid == LanguageClasses.String.Guid)
                                {
                                    InitValueString = "\"\"";
                                }

                                else if (AsClassType.Class.Source.ClassGuid == LanguageClasses.Boolean.Guid)
                                {
                                    InitValueString = "false";
                                }

                                else if (AsClassType.Class.Source.ClassGuid == LanguageClasses.Character.Guid)
                                {
                                    InitValueString = "'\0'";
                                }

                                else if (AsClassType.Class.Source.ClassGuid == LanguageClasses.Number.Guid)
                                {
                                    InitValueString = "0";
                                }

                                else
                                {
                                    InitValueString = "null";
                                }
                            }

                            else
                            {
                                InitValueString = "null";     // TODO : tuples
                            }
                            string InitNameString = CSharpNames.ToCSharpIdentifier(Item.Name);

                            InitialisationStringList.Add($"{InitNameString} = {InitValueString};");
                        }
                    }

                    ExportStatusText = CSharpNames.ComposedExportStatus(isOverride, false, false, exportStatus);
                    writer.WriteIndentedLine($"{ExportStatusText} {ResultType} {nameString}({ArgumentEntityList})");

                    AsEffectiveBody.WriteCSharp(writer, Flags, ResultString, false, InitialisationStringList);
                    isMultiline = true;
                    IsHandled   = true;
                    break;

                case ICSharpPrecursorBody AsPrecursorBody:
                    if (isMultiline)
                    {
                        writer.WriteEmptyLine();
                    }

                    ExportStatusText = CSharpNames.ComposedExportStatus(true, false, false, exportStatus);
                    writer.WriteIndentedLine($"{ExportStatusText} {ResultType} {nameString}({ArgumentEntityList})");
                    writer.WriteIndentedLine("{");
                    writer.IncreaseIndent();
                    writer.WriteIndentedLine($"return base.{nameString}({ArgumentNameList});");
                    writer.DecreaseIndent();
                    writer.WriteIndentedLine("}");
                    isMultiline = true;
                    IsHandled   = true;
                    break;
                }

                Debug.Assert(IsHandled);
            }
            else
            {
                writer.WriteIndentedLine($"{ResultType} {nameString}({ArgumentEntityList});");
                isMultiline = false;
            }
        }
        /// <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);

            List <ICSharpVariableContext> DestinationNameList           = new List <ICSharpVariableContext>();
            IDictionary <string, ICSharpQualifiedName> DestinationTable = new Dictionary <string, ICSharpQualifiedName>();

            foreach (ICSharpQualifiedName Destination in DestinationList)
            {
                ICSharpVariableContext DestinationContext;

                if (Destination.IsSimple)
                {
                    string DestinationName = CSharpNames.ToCSharpIdentifier(Destination.SimpleName);
                    if (writer.AttachmentMap.ContainsKey(DestinationName))
                    {
                        DestinationName = writer.AttachmentMap[DestinationName];
                    }

                    DestinationContext = new CSharpVariableContext(DestinationName);
                }
                else
                {
                    string DestinationName = writer.GetTemporaryName();
                    DestinationContext = new CSharpVariableContext(DestinationName, isDeclared: false);
                    DestinationTable.Add(DestinationName, Destination);
                }

                DestinationNameList.Add(DestinationContext);
            }

            ICSharpExpressionContext ExpressionContext = new CSharpExpressionContext(DestinationNameList);

            SourceExpression.WriteCSharp(writer, ExpressionContext, -1);

            IDictionary <string, string> FilledDestinationTable = ExpressionContext.FilledDestinationTable;

            for (int i = 0; i < DestinationList.Count; i++)
            {
                ICSharpQualifiedName Destination = DestinationList[i];
                string Name = DestinationNameList[i].Name;

                Debug.Assert(FilledDestinationTable.ContainsKey(Name));
                string ResultText = FilledDestinationTable[Name];

                if (ResultText == null)
                {
                    ResultText = ExpressionContext.ReturnValue;
                }

                if (Destination.IsAttributeWithContract)
                {
                    string SetterText = Destination.CSharpSetter(writer);
                    writer.WriteIndentedLine($"{SetterText}({ResultText});");
                }
                else if (DestinationTable.ContainsKey(Name))
                {
                    string DestinationName = DestinationTable[Name].CSharpText(writer, 0);
                    writer.WriteIndentedLine($"{DestinationName} = {ResultText};");
                }
                else if (ResultText != Name)
                {
                    writer.WriteIndentedLine($"{Name} = {ResultText};");
                }
            }
        }
Ejemplo n.º 30
0
        /// <summary>
        /// Gets the source code corresponding to the expression.
        /// </summary>
        /// <param name="writer">The stream on which to write.</param>
        /// <param name="expressionContext">The context.</param>
        /// <param name="skippedIndex">Index of a destination to skip.</param>
        public override void WriteCSharp(ICSharpWriter writer, ICSharpExpressionContext expressionContext, int skippedIndex)
        {
            Debug.Assert(WriteDown);

            string CoexistingPrecursorName     = string.Empty;
            string CoexistingPrecursorRootName = ParentFeature.CoexistingPrecursorName;

            if (!string.IsNullOrEmpty(CoexistingPrecursorRootName))
            {
                CoexistingPrecursorName = CSharpNames.ToCSharpIdentifier(CoexistingPrecursorRootName + " " + "Base");
            }

            PrecursorFeature.GetOutputFormat(SelectedOverloadType, out int OutgoingParameterCount, out int ReturnValueIndex);

            CSharpArgument.CSharpArgumentList(writer, expressionContext, FeatureCall, ReturnValueIndex, false, out string ArgumentListText, out IList <string> OutgoingResultList);
            Debug.Assert(OutgoingParameterCount > 0);

            bool HasArguments = (ParentFeature is ICSharpFunctionFeature) || FeatureCall.ArgumentList.Count > 0;

            if (HasArguments)
            {
                ArgumentListText = $"({ArgumentListText})";
            }

            if (!string.IsNullOrEmpty(CoexistingPrecursorRootName))
            {
                expressionContext.SetSingleReturnValue($"{CoexistingPrecursorName}{ArgumentListText}");
            }
            else
            {
                string FunctionName = CSharpNames.ToCSharpIdentifier(ParentFeature.Name);

                if (OutgoingParameterCount == 1)
                {
                    if (ArgumentListText.Length > 0)
                    {
                        expressionContext.SetSingleReturnValue($"base.{FunctionName}{ArgumentListText}");
                    }
                    else if (SelectedOverloadType != null)
                    {
                        expressionContext.SetSingleReturnValue($"base.{FunctionName}()");
                    }
                    else
                    {
                        expressionContext.SetSingleReturnValue($"base.{FunctionName}");
                    }
                }
                else
                {
                    if (ReturnValueIndex >= 0)
                    {
                        string TemporaryResultName = writer.GetTemporaryName();
                        writer.WriteIndentedLine($"var {TemporaryResultName} = base.{FunctionName}{ArgumentListText};");

                        OutgoingResultList.Insert(ReturnValueIndex, TemporaryResultName);
                    }
                    else
                    {
                        writer.WriteIndentedLine($"base.{FunctionName}{ArgumentListText};");
                    }

                    expressionContext.SetMultipleResult(OutgoingResultList, ReturnValueIndex);
                }
            }
        }