Ejemplo n.º 1
0
        private void Add_ArrayMethods(CsClass cl)
        {
            /* [DirectCall("array")]
             * public static NdArray2D<int> Array(
             *  IEnumerable<IEnumerable<int>> obj,
             *  bool             copy  = true,
             *  NumpyArrayOrder  order = NumpyArrayOrder.K)
             * {
             *  return NdArray.Make(obj, copy, order);
             * }*/

            var f = CreateFile();

            f.AddImportNamespace("System.Collections.Generic");
            f.AddImportNamespace("System.Linq");
            for (var dimension = 1; dimension <= MaxDim; dimension++)
            {
                NdArrayLevel1Generator.Generate(f, dimension);
                foreach (var wrappedType in NumpyArrayWrappedTypes)
                {
                    var classLevel2 = NdArrayLevel2Generator.Generate(f, dimension, wrappedType);
                    var resultType  = classLevel2.Name;
                    var m           = cl.AddMethod("Array" + dimension, resultType)
                                      .WithDirectCall("array")
                                      .WithStatic()
                                      .WithBodyComment($"Generated by {nameof(NumpyGenerator)}.{nameof(Add_ArrayMethods)}/1")
                                      .WithBody($"return new {resultType}(obj, copy, order);");
                    NdArrayLevel2Generator.WithAddParams(m, wrappedType, dimension);
                }

                // other types
                {
                    const string wrappedType = "T";
                    // var          classLevel2 = NdArrayLevel2Generator.Generate(f, dimension, wrappedType);
                    foreach (var i in new[] { true })
                    {
                        var resultType = $"NdArray{dimension}D<T>";
                        var m          = cl.AddMethod("Array" + (i ? $"{dimension}" : "") + "<T>", resultType)
                                         .WithDirectCall("array")
                                         .WithStatic()
                                         .WithBodyComment($"Generated by {nameof(NumpyGenerator)}.{nameof(Add_ArrayMethods)}/2")
                                         .WithBody($"return new {resultType}(obj, copy, order);");
                        NdArrayLevel2Generator.WithAddParams(m, wrappedType, dimension);
                    }
                }
            }

            var fileName = Path.Combine(BasePath.FullName, "Lang.Python", "+compatibility", "Numpy", "NdArray.Auto.cs");

            f.SaveIfDifferent(fileName);
        }
Ejemplo n.º 2
0
        public static void Add_FromMethods(Type t, XValueTypeName valueTypeName,
                                           TypesGroup types, CsClass target, IRelatedUnitDefinition u)
        {
#if DEBUG
            if (target.Name == "Power")
            {
                System.Diagnostics.Debug.Write("");
            }
#endif
            foreach (var inputType in "decimal,double,int,long".Split(','))
            {
                var arg = "value";
                if (inputType == OtherValuePropertyType)
                {
                    arg = $"({ValuePropertyType}){arg}";
                }

                var args = new CsArguments(arg, types.Container + "." + u.FieldName)
                           .Create(target.Name);
                var valueIn           = $" value in {u.UnitShortCode.EffectiveValue}";
                var methodName        = "From" + u.FromMethodNameSufix.CoalesceNullOrWhiteSpace(u.FieldName);
                var methodDescription = $"creates {types.Value.FirstLower()} from{valueIn}";

                var cw = Ext.Create(t);
                cw.WriteReturn(args);
                var m = target.AddMethod(methodName, target.Name, methodDescription)
                        .WithStatic()
                        .WithBody(cw);
                m.AddParam("value", inputType).Description = string.Format("{0}{1}", valueTypeName, valueIn);
            }
        }
        public void MakeExtensionMethod(CsClass target)
        {
            if (EnumOptions is null)
            {
                return;
            }

            var paramValue = target.GetTypeName(TypeName);

            var writer = CsCodeWriter.Create <ShellEnumOptionsGenerator>();

            writer.Open("switch (value)");
            foreach (var i in EnumOptions.Items)
            {
                var line = $"case {paramValue}.{i.CsValue}: return {i.LinuxValue.CsEncode()};";
                writer.WriteLine(line);
            }

            var notSupportedException = target.GetTypeName <NotSupportedException>();

            writer.WriteLine($"default: throw new {notSupportedException}();");
            writer.Close();

            var parameter = new CsMethodParameter("value", paramValue)
            {
                UseThis = target.IsStatic
            };

            target.AddMethod(extensionMethodName, "string")
            .WithBody(writer)
            .WithStatic()
            .WithParameter(parameter);
        }
Ejemplo n.º 4
0
        public static CsMethod AddOperator(this CsClass cl, string operatorName, CsArguments csArgument, string resultType = null)
        {
            resultType = resultType.CoalesceNullOrWhiteSpace(cl.Name);
            var code = csArgument.Create(resultType);

            return(cl.AddMethod(operatorName, resultType, "implements " + operatorName + " operator")
                   .WithBodyFromExpression(code));
        }
Ejemplo n.º 5
0
        private static void AddToStringMethod(CsClass cl, KeysGeneratorDef def)
        {
            var m = cl.AddMethod(nameof(ToString), "string");

            m.Overriding = OverridingType.Override;
            var e = def.GetToStringExpression();

            m.Body = $"return {e};";
        }
Ejemplo n.º 6
0
 private void AddEqualsMethods(CsClass cl, KeysGeneratorDef def)
 {
     var typeName = def.TypeName;
     var propName = def.ValuePropertyName;
     {
         var m = cl.AddMethod(nameof(Equals), "bool");
         m.Parameters.Add(new CsMethodParameter("other", typeName));
         m.Body = ValueEquals($"other.{propName}", def);
     }
     {
         var m = cl.AddMethod(nameof(Equals), "bool");
         m.Overriding = OverridingType.Override;
         m.Parameters.Add(new CsMethodParameter("obj", "object"));
         if (def.WrappedType == WrappedTypes.String)
         {
             m.Body = $"return obj is {typeName} s && StringComparer.Ordinal.Equals({propName}, s.{propName});";
         }
         else
         {
             m.Body = $"return obj is {typeName} s && {propName}.Equals(s.{propName});";
         }
     }
 }
        private static void AddOperatorMethod(CsClass cl, MulDivDefinition i)
        {
            var l     = Gv(i.Left, "left");
            var r     = Gv(i.Right, "right");
            var value = $"{l} {i.Operator} {r}";

            if (!i.Result.Equals(UnitDefinition.Scalar))
            {
                value = $"new {i.Result.ClassName}({value})";
            }
            var m = cl.AddMethod(i.Operator, i.Result.ClassName)
                    .WithBody($"return {value};");

            m.AddParam("left", i.Left.ClassName);
            m.AddParam("right", i.Right.ClassName);
            m.Description = i.Description;
        }
Ejemplo n.º 8
0
        private static void AddGetHashCodeMethod(CsClass cl, KeysGeneratorDef def)
        {
            var m = cl.AddMethod(nameof(GetHashCode), "int");

            m.Overriding = OverridingType.Override;

            switch (def.WrappedType)
            {
            case WrappedTypes.String:
                // m.Body = "return Value is null ? 0 : StringComparer.Ordinal.GetHashCode(Value);";
                m.Body = $"return {def.ValuePropertyName}.GetHashCode();";
                break;

            case WrappedTypes.Int:
                m.Body = $"return {def.ValuePropertyName};";
                break;

            default:
                throw new NotImplementedException();
            }
        }
Ejemplo n.º 9
0
        private static void AddConstructor(CsClass csStruct, KeysGeneratorDef def)
        {
            var propName = def.ValuePropertyName;
            var argName  = propName.FirstLower();
            var cs       = new CsCodeWriter();

            if (def.WrappedType == WrappedTypes.String)
            {
                var args      = new CsArguments($"nameof({argName})");
                var exception = args.Create(csStruct.GetTypeName <NullReferenceException>());
                cs.SingleLineIf($"{argName} is null", $"throw {exception};");

                exception = args.Create(csStruct.GetTypeName <ArgumentException>());
                cs.SingleLineIf($"{argName}.Length == 0", $"throw {exception};");

                cs.WriteAssign(propName, $"{argName}.Trim()");
            }
            else
            {
                cs.WriteAssign(propName, argName);
            }

            csStruct.AddConstructor()
            .WithParameter(new CsMethodParameter(argName, def.CsWrappedType))
            .WithBody(cs);


            for (var i = 0; i < 2; i++)
            {
                var eq = i == 0;
                var m  = csStruct.AddMethod(eq ? "==" : "!=", "bool", eq ? "Equality operator" : "Inequality operator")
                         .WithBody($"return {(eq ? "" : "!")}left.Equals(right);");

                m.AddParam("left", csStruct.Name, "first value to compare");
                m.AddParam("right", csStruct.Name, "second value to compare");
            }
        }
Ejemplo n.º 10
0
 private static void TwoArgs(CsClass cl, string dotnetName, string pyName, string firstVar,
                             string secondVar,
                             Action <CSCodeFormatter, string, string> code)
 {
     if (pyName == null)
     {
         pyName = dotnetName.ToLower();
     }
     foreach (var typeX in GetListTypes(cl))
     {
         foreach (var typeY in GetListTypes(cl))
         {
             var cf = new CSCodeFormatter();
             code(cf, typeY, typeX);
             var m = cl.AddMethod(dotnetName, "PyList<double>")
                     .WithBody(cf)
                     .WithStatic()
                     .WithBodyComment($"Generated by {nameof(NumpyGenerator)}.{nameof(TwoArgs)}")
                     .WithDirectCall(pyName);
             m.AddParam(firstVar, typeY);
             m.AddParam(secondVar, typeX);
         }
     }
 }
Ejemplo n.º 11
0
        protected static void MakeToString(CsClass cl, string returnValue)
        {
            var m = cl.AddMethod("ToString", "string").WithBody($"return {returnValue};");

            m.Overriding = OverridingType.Override;
        }