示例#1
0
        public IEnumerable <StructuralError> TryImportConstants(ModuleContext context)
        {
            if (alias != null)
            {
                return(Enumerable.Empty <StructuralError>());
            }
            IKontrolModule module = context.FindModule(fromModule);

            if (module == null)
            {
                return(new StructuralError(
                           StructuralError.ErrorType.NoSuchModule,
                           $"Module '{fromModule}' not found",
                           Start,
                           End
                           ).Yield());
            }
            foreach (string name in names ?? module.AllConstantNames)
            {
                IKontrolConstant constant = module.FindConstant(name);

                if (constant != null)
                {
                    context.mappedConstants.Add(name, constant);
                }
            }

            return(Enumerable.Empty <StructuralError>());
        }
示例#2
0
        public IEnumerable <StructuralError> TryImportTypes(ModuleContext context)
        {
            IKontrolModule module = context.FindModule(fromModule);

            if (module == null)
            {
                return(new StructuralError(
                           StructuralError.ErrorType.NoSuchModule,
                           $"Module '{fromModule}' not found",
                           Start,
                           End
                           ).Yield());
            }
            if (alias != null)
            {
                context.moduleAliases.Add(alias, fromModule);
            }
            else
            {
                foreach (string name in (names ?? module.AllTypeNames))
                {
                    TO2Type type = module.FindType(name);

                    if (type != null)
                    {
                        context.mappedTypes.Add(name, type);
                    }
                }
            }

            return(Enumerable.Empty <StructuralError>());
        }
示例#3
0
        public IEnumerable <StructuralError> TryImportFunctions(ModuleContext context)
        {
            if (alias != null)
            {
                return(Enumerable.Empty <StructuralError>());
            }
            IKontrolModule module = context.FindModule(fromModule);

            if (module == null)
            {
                return(new StructuralError(
                           StructuralError.ErrorType.NoSuchModule,
                           $"Module '{fromModule}' not found",
                           Start,
                           End
                           ).Yield());
            }

            List <StructuralError> errors = new List <StructuralError>();

            foreach (string name in names ?? module.AllFunctionNames)
            {
                if (context.mappedConstants.ContainsKey(name))
                {
                    continue;
                }

                IKontrolFunction function = module.FindFunction(name);

                if (function != null)
                {
                    context.mappedFunctions.Add(name, function);
                }
                else if (!context.mappedTypes.ContainsKey(name))
                {
                    errors.Add(new StructuralError(
                                   StructuralError.ErrorType.InvalidImport,
                                   $"Module '{fromModule}' does not have public member '{name}''",
                                   Start,
                                   End
                                   ));
                }
            }

            return(errors);
        }
示例#4
0
        private RealizedType ReferencedType(ModuleContext context)
        {
            RealizedType realizedType = moduleName != null
                ? context.FindModule(moduleName)?.FindType(name)?.UnderlyingType(context)
                : context.mappedTypes.Get(name)?.UnderlyingType(context);

            if (realizedType == null)
            {
                throw new CompilationErrorException(new List <StructuralError> {
                    new StructuralError(
                        StructuralError.ErrorType.InvalidType,
                        $"Unable to lookup type {Name}",
                        start,
                        end
                        )
                });
            }

            string[] typeParameterNames = realizedType.GenericParameters;
            if (typeParameterNames.Length != typeArguments.Count)
            {
                throw new CompilationErrorException(new List <StructuralError> {
                    new StructuralError(
                        StructuralError.ErrorType.InvalidType,
                        $"Type {realizedType.Name} expects {typeParameterNames.Length} type parameters, only {typeArguments.Count} where given",
                        start,
                        end
                        )
                });
            }

            Dictionary <string, RealizedType> namedTypeArguments = new Dictionary <string, RealizedType>();

            for (int i = 0; i < typeArguments.Count; i++)
            {
                namedTypeArguments.Add(typeParameterNames[i], typeArguments[i].UnderlyingType(context));
            }

            return(realizedType.FillGenerics(context, namedTypeArguments));
        }