示例#1
0
        /// <summary>
        /// Resolves the given type.
        /// </summary>
        /// <param name="sourceCodePosition">The current position of the token in the source code. Used for throwing exceptions.</param>
        /// <param name="typePath">The complete path to the type.</param>
        protected IType ResolveType(long sourceCodePosition, params string[] typePath)
        {
            string serialized = string.Join("::", typePath);

            if (_builtinTypes.Has(serialized))
            {
                return(_builtinTypes.Lookup(serialized));
            }
            else
            {
                throw new CompileException(sourceCodePosition, "Cannot resolve type: " + serialized);
            }
        }
示例#2
0
        /// <summary>
        /// Looks up the type with the given identifier.
        /// </summary>
        /// <param name="identfier">The identfier of the type (without namespace prefix).</param>
        /// <param name="inNamespace">The namespace in which the type shall be looked up. If the namespace itself does not contain the type definition, the parent namespaces are browsed.</param>
        /// <param name="namespaceLengthRestriction">The amount of namespace parts which shall be considered in the lookup process. This is used by the method itself recursively, and should otherwise be set to the amout of namespace parts.</param>
        /// <returns>the type if it was found, or null otherwise.</returns>
        private IType LookupType(string identfier, IEnumerable <string> inNamespace, int namespaceLengthRestriction)
        {
            if (identfier == null)
            {
                throw new ArgumentNullException("identfier");
            }
            if (inNamespace == null)
            {
                throw new ArgumentNullException("inNamespace");
            }

            // Build the full path to the type, assuming it is defined in the given namespace
            StringBuilder typePath = new StringBuilder();
            int           i        = 0;

            foreach (string nsPart in inNamespace)
            {
                if (++i > namespaceLengthRestriction)
                {
                    break;
                }
                typePath.Append(nsPart);
                typePath.Append("::");
            }
            typePath.Append(identfier);
            string typePathString = typePath.ToString();

            // Check if the type is defined in the given namespace
            if (_typeTable.Has(typePathString))
            {
                return(_typeTable.Lookup(typePathString));
            }
            // Otherwise search it in the parent namespace
            else if (namespaceLengthRestriction > 0)
            {
                return(LookupType(identfier, inNamespace, namespaceLengthRestriction - 1));
            }
            // We already checked the global namespace, so there is no such type
            else
            {
                return(null);
            }
        }