Ejemplo n.º 1
0
        // Checks if a variable with type variableType can be set to a variable with type valueType
        // (Is [variable = value] a valid statement?)
        public static bool CheckTypeCompatibility(string variableType, string valueType, Config config)
        {
            //Check if types are basic types (number, logical, text) or schemes

            if (variableType == VariableTypes.Number)
            {
                return(valueType == VariableTypes.Number);
            }
            if (variableType == VariableTypes.Logical)
            {
                return(valueType == VariableTypes.Logical);
            }
            if (variableType == VariableTypes.Text)
            {
                return(valueType == VariableTypes.Text);
            }
            if (variableType == VariableTypes.Unknown || valueType == VariableTypes.Unknown)
            {
                return(false);
            }

            //If variable is 'object', then a value with any object type can be assigned to it
            if (variableType == VariableTypes.Object)
            {
                if (valueType == VariableTypes.Object)
                {
                    return(true);
                }
                //Basic types are not compatible with 'object'
                if (valueType == VariableTypes.Number || valueType == VariableTypes.Logical || valueType == VariableTypes.Text)
                {
                    return(false);
                }
                Scheme valueScheme = config.GetSchemeByName(valueType);
                if (valueScheme == null)
                {
                    throw CreateException($"Unknown type '{valueType}'");
                }
                return(true);
            }

            //if variable is of a scheme type
            Scheme variableScheme = config.GetSchemeByName(variableType);

            if (variableScheme != null)
            {
                //If scheme exists and the two are equal, they must be compatible
                if (variableType == valueType)
                {
                    return(true);
                }

                //Basic types are not compatible with any scheme
                if (valueType == VariableTypes.Number || valueType == VariableTypes.Logical || valueType == VariableTypes.Text)
                {
                    return(false);
                }

                Scheme valueScheme = config.GetSchemeByName(valueType);

                if (valueScheme == null)
                {
                    throw CreateException($"Unknown type '{valueType}'");
                }

                //Check if the two (different) schemes are compatible (ie. variableType is the ancestor of valueType)
                return(valueScheme.HasAncestor(variableType));
            }

            //if variable type is class type (classlist)
            if (config.IsClassType(variableType))
            {
                return(variableType == valueType);
            }

            throw CreateException($"Unknown type '{variableType}'");
        }