Esempio n. 1
0
        internal static bool IsStartOfConstant(string s)
        {
            foreach (string constant in EquationFunctions.GetFunctions(0))
            {
                if (constant.StartsWith(s))
                {
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 2
0
 internal static bool IsConstant(string s)
 {
     return(EquationFunctions.GetFunctions(0).Contains(s));
 }
Esempio n. 3
0
        private static IEquation ParseSingleEquation(SegmentData sD, Action <string> errorCallback)
        {
            string s = sD.Data;

            switch (sD.Type)
            {
            case SegmentType.Number:
                return(new Constant(float.Parse(s)));

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

            case SegmentType.EquationVariable:
                return(new EquationVariable(int.Parse(s.Substring(1, s.Length - 2))));

            case SegmentType.Constant:
                return(EquationFunctions.CreateFunction(s, new IEquation[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);
                bool   isOperator   = functionName.StartsWith("op_");
                if (isOperator)
                {
                    functionName = functionName.Substring(3, functionName.Length - 3);
                }

                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;
                //if (isOperator)
                //    functionParameterCount = EquationOperators.GetOperatorParameterCount(functionName);
                //else
                //    functionParameterCount = EquationFunctions.GetFunctionParameterCount(functionName);

                string[] parametersS            = SplitParameters(functionBody);
                int      functionParameterCount = parametersS.Length;

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

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

                IEquation functionEquation;
                if (isOperator)
                {
                    functionEquation = EquationOperators.CreateOperator(functionName, parameterEquations);
                }
                else
                {
                    functionEquation = EquationFunctions.CreateFunction(functionName, parameterEquations);
                }

                if (functionEquation == null)
                {
                    errorCallback($"No function of name '{functionName}' with {functionParameterCount} parameters exists. Error at {sD.Index}.");
                    return(null);
                }

                return(functionEquation);
            }
            }

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