示例#1
0
        private static ILogic CreateFunction(string functionName, IEnumerable <ILogic> parameters)
        {
            FieldInfo functionInfo = LogicFunctions.GetFunctionInfo(functionName);

            if (functionInfo == null)
            {
                return(null);
            }

            int parameterCount = parameters.Count();

            if (LogicFunctions.GetFunctionParameterCount(functionName) != parameterCount)
            {
                return(null);
            }

            switch (parameterCount)
            {
            case 0:
                return(new Constant(((Func <bool>)functionInfo.GetValue(null)).Invoke()));

            case 1:
                return(new Function1P((Func <bool, bool>)functionInfo.GetValue(null), parameters.ElementAt(0)));

            case 2:
                return(new Function2P((Func <bool, bool, bool>)functionInfo.GetValue(null), parameters.ElementAt(0), parameters.ElementAt(1)));

            case 3:
                return(new Function3P((Func <bool, bool, bool, bool>)functionInfo.GetValue(null), parameters.ElementAt(0), parameters.ElementAt(1), parameters.ElementAt(2)));
            }

            return(null);
        }
示例#2
0
        private static ILogic ParseSingleEquation(SegmentData sD, Action <string> errorCallback)
        {
            string s = sD.Data;

            switch (sD.Type)
            {
            case SegmentType.Variable:
                return(new Variable(int.Parse(s.Substring(1, s.Length - 2))));

            case SegmentType.Constant:
                return(CreateFunction(s, new ILogic[0]));

            case SegmentType.Function: {
                int functionBodyStartIndex = s.IndexOf('(');
                int functionBodyEndIndex   = s.LastIndexOf(')');

                if (functionBodyStartIndex == -1)
                {
                    errorCallback($"Function call '{s}' at {sD.Index} has no body.");
                    return(null);
                }

                string functionName = s.Substring(0, functionBodyStartIndex);
                string functionBody = s.Substring(functionBodyStartIndex, s.Length - functionBodyStartIndex);

                if (functionBodyEndIndex == -1)
                {
                    errorCallback($"Function body '{functionBody}' of function '{functionName}' at {sD.Index} has no end to its body.");
                    return(null);
                }

                if (functionBodyEndIndex != s.Length - 1)
                {
                    errorCallback($"Function body '{functionBody}' of function '{functionName}' at {sD.Index} has an invalid body.");
                    return(null);
                }

                bool invalidParPlacement;
                functionBody = RemoveOuterParentheses(functionBody, out invalidParPlacement);

                if (invalidParPlacement)
                {
                    errorCallback($"Invalid parenthesis placement in function body '{functionBody}' of function '{functionName}' at {sD.Index}.");
                    return(null);
                }

                int functionParameterCount = LogicFunctions.GetFunctionParameterCount(functionName);

                string[] parametersS = SplitParameters(functionBody);

                if (functionParameterCount != parametersS.Length)
                {
                    errorCallback($"Cannot parse function '{s}', invalid number of parameters ({parametersS.Length} - expected {functionParameterCount}.");
                    return(null);
                }

                ILogic[] parameterEquations = new ILogic[functionParameterCount];
                for (int fpc = 0; fpc < functionParameterCount; fpc++)
                {
                    parameterEquations[fpc] = ParseEquation(parametersS[fpc], errorCallback);
                }

                return(CreateFunction(functionName, parameterEquations));
            }
            }

            errorCallback($"Cannot parse equation '{s}' at {sD.Index}.");
            return(null);
        }