示例#1
0
        static string GetOpCodeFormat(MathOperationsMetaData.OpSignature sig)
        {
            switch (sig.OpType)
            {
            case nameof(MathOperationsMetaData.CustomOps.Negate):
                return("- {0}");

            case nameof(MathOperationsMetaData.CustomOps.Modulo):
                return("{0} % {1}");

            case nameof(MathOperationsMetaData.CustomOps.Add):
                return("{0} + {1}");

            case nameof(MathOperationsMetaData.CustomOps.Subtract):
                return("{0} - {1}");

            case nameof(MathOperationsMetaData.CustomOps.Multiply):
                return("{0} * {1}");

            case nameof(MathOperationsMetaData.CustomOps.Divide):
                return("{0} / {1}");

            case nameof(MathOperationsMetaData.CustomOps.CubicRoot):
                return("math.pow(math.abs({0}), 1f / 3f)");
            }

            var formatParams = Enumerable.Range(0, sig.Params.Length).Select(i => $"{{{i}}}"); // {0}, {1}, ...

            return($"math.{sig.OpType.ToLower()}({string.Join(", ", formatParams)})");         // something like "math.dot({0}, {1}) or math.cos({0})"
        }
        protected override void OnDefineNode()
        {
            m_MethodName        = TypedNode.Type.GetMethodsSignature().OpType;
            m_CompatibleMethods = MathOperationsMetaData.MethodsByName[m_MethodName];
            m_CurrentMethod     = m_CompatibleMethods.Single(o => o.EnumName == TypedNode.Type.ToString());
            if (InputCount == default)
            {
                InputCount = m_CurrentMethod.Params.Length;
            }

            var mathGenericNode = TypedNode;

            mathGenericNode.GenerationVersion = MathGeneratedDelegates.GenerationVersion;
            Node = mathGenericNode;

            base.OnDefineNode();
        }
示例#3
0
        static string GetOpCodeGen(MathOperationsMetaData.OpSignature sig)
        {
            if (!sig.SupportsMultiInputs())
            {
                var indexedValues = sig.Params.Select((p, index) => $"values[{index}].{p}").ToArray();
                return($"values => {string.Format(GetOpCodeFormat(sig), indexedValues)}, \t// {sig.EnumName}");
            }

            var aType = sig.Params[0].ToString();
            var bType = sig.Params[1].ToString();

            return("values => \t// " + sig.EnumName + "\n" +
                   "\t\t\t{\n" +
                   "\t\t\t\tAssert.IsTrue(values.Length >= 2);\n" +
                   "\t\t\t\tvar result = values[0];\n" +
                   "\t\t\t\tfor (int i = 1; i < values.Length; ++i)\n" +
                   $"\t\t\t\t\tresult = {string.Format(GetOpCodeFormat(sig), $"result.{aType}", $"values[i].{bType}")};\n" +
                   "\t\t\t\treturn result;\n" +
                   "\t\t\t},");
        }