Beispiel #1
0
        public Tuple<IType, ITypedExpression> CreateTypedExpression(DataDeclaration decl)
        {
            TypeCheckingResult result = ConstraintCreator.W(decl.Data, container);

            IEnumerable<Constraint> constraints = result.Constraints;

            if (decl.Type != null)
            {
                var converted = PartialTypeCreator.Convert(decl.Type, null);
                var substitution = Unification.Unify(result.Type, converted);
                constraints = constraints.Concat(substitution);
            }

            SolutionSet solutionSet = constraints.Solve(result.MaximalIndex);

            if (decl.Type == null)
            {
                int maximalIndex = result.MaximalIndex;

                solutionSet.ReplaceUnknownsWithTypeParameters(maximalIndex);
            }

            IPartialType type = ApplySolutionSet.Apply(solutionSet, result.Type);
            IConstrainedData data = ApplySolutionSet.Apply(solutionSet, result.Data);

            return Tuple.Create(TypeConverter.Convert(type), DataConverter.Convert(data));
        }
Beispiel #2
0
        public static void Compile(string name, TypeBuilder dataClass, ConstructorInfo ctor, IType type, 
            string[] typeParameters, IMetadataContainer container, IRuntimeContainer runtimeContainer)
        {
            var method = dataClass.DefineMethod(name,
                 MethodAttributes.Public | MethodAttributes.Static, null, Type.EmptyTypes);

            if (typeParameters.Any())
            {
                method.DefineGenericParameters(typeParameters.ToArray());
            }

            var converter = new TypeConverter(runtimeContainer, method.GetGenericArguments());
            var converted = converter.Convert(type);
            method.SetReturnType(converted);

            var body = method.GetILGenerator();

            if (typeParameters.Any())
            {
                body.Emit(OpCodes.Newobj, TypeBuilder.GetConstructor(ctor.DeclaringType.MakeGenericType(method.GetGenericArguments()), ctor));
            }
            else
            {
                body.Emit(OpCodes.Newobj, ctor);
            }

            body.Emit(OpCodes.Ret);

            runtimeContainer.Add(name, method);

            var dataDecl = new DataDeclaration(type, null);
            dataDecl.TypeParameters = typeParameters;
            container.Add(name, dataDecl);

            RemoveFirstParameter(name, dataClass, method, new IType[0], type, typeParameters, runtimeContainer);
        }
Beispiel #3
0
        public static void Compile(string name, TypeBuilder dataClass, ITypedExpression typedExpression,
            DataDeclaration data, IMetadataContainer container, IRuntimeContainer runtimeContainer)
        {
            var method = dataClass.DefineMethod(name,
                MethodAttributes.Public | MethodAttributes.Static, null, Type.EmptyTypes);

            if (data.TypeParameters.Any())
            {
                method.DefineGenericParameters(data.TypeParameters.ToArray());
            }

            var converter = new TypeConverter(runtimeContainer, method.GetGenericArguments());
            var converted = converter.Convert(data.Type);
            method.SetReturnType(converted);

            var body = method.GetILGenerator();
            typedExpression.AcceptVisitor(new DataCompiler(body, method.GetGenericArguments(), runtimeContainer));
            body.Emit(OpCodes.Ret);

            container.Add(name, data);
            runtimeContainer.Add(name, method);

            RemoveFirstParameter(name, dataClass, method, new IType[0], data.Type, data.TypeParameters, runtimeContainer);
        }
Beispiel #4
0
 public void Add(string identifier, DataDeclaration value)
 {
     data[identifier] = value;
 }
Beispiel #5
0
        public System.Reflection.Module Import(string moduleName)
        {
            var files = Directory.EnumerateFiles(Directory.GetCurrentDirectory(), "*.dll");

            var modules = from file in files
                          let assembly = Assembly.LoadFile(file)
                          from m in assembly.GetModules()
                          where m.ScopeName.Equals(moduleName)
                          select m;

            System.Reflection.Module module;

            try
            {
                module = modules.Single();
            }
            catch (InvalidOperationException ex)
            {
                throw new ModuleImportException("Unable to locate unique module with name '" + moduleName + "'.", ex);
            }

            foreach (var type in module.Assembly.GetTypes())
            {
                var typeParameters = type.GetGenericArguments().Select(p => p.Name).ToArray();

                if (type.GetCustomAttributes(typeof(FiniteAttribute), false).Any())
                {
                    var cataMethod = type.GetMethod(Constants.CataMethodName);
                    var functor = ReflectionUtilities.InferFunctorFromFiniteType(cataMethod, "_T", container);

                    FiniteAttribute finiteAttribute = (FiniteAttribute) type.GetCustomAttributes(typeof(FiniteAttribute), false).Single();
                    container.Add(type.Name, new LFixTypeDeclaration(functor, "_T", typeParameters,
                        finiteAttribute.ConstructorName,
                        finiteAttribute.DestructorName,
                        finiteAttribute.FoldName));
                    runtimeContainer.Add(type.Name, type);
                    runtimeContainer.AddDestructor(type.Name, finiteAttribute.DestructorClass);
                }

                if (type.GetCustomAttributes(typeof(InfiniteAttribute), false).Any())
                {
                    var applyMethod = type.GetMethod(Constants.ApplyMethodName);
                    var functor = ReflectionUtilities.InferFunctorFromInfiniteType(applyMethod, "_T", container);

                    InfiniteAttribute infiniteAttribute = (InfiniteAttribute) type.GetCustomAttributes(typeof(InfiniteAttribute), false).Single();
                    container.Add(type.Name, new GFixTypeDeclaration(functor, "_T", typeParameters,
                        infiniteAttribute.ConstructorName,
                        infiniteAttribute.DestructorName,
                        infiniteAttribute.UnfoldName));
                    runtimeContainer.Add(type.Name, type);
                    runtimeContainer.AddDestructor(type.Name, infiniteAttribute.DestructorClass);
                }

                if (type.GetCustomAttributes(typeof(SynonymAttribute), false).Any())
                {
                    var field = type.GetField(Constants.BoxedTypeValueFieldName);
                    var innerType = ReflectionUtilities.InferType(field.FieldType, container);

                    SynonymAttribute synonymAttribute = (SynonymAttribute) type.GetCustomAttributes(typeof(SynonymAttribute), false).Single();
                    container.Add(type.Name, new BoxedTypeDeclaration(innerType, typeParameters,
                        synonymAttribute.ConstructorName,
                        synonymAttribute.DestructorName));
                    runtimeContainer.Add(type.Name, type);
                    runtimeContainer.AddDestructor(type.Name, synonymAttribute.DestructorClass);
                }
            }

            var dataClass = module.Assembly.GetTypes().FirstOrDefault(t => t.Name.Equals(Constants.DataClassName));

            foreach (var method in dataClass.GetMethods())
            {
                if (!method.GetParameters().Any())
                {
                    Type returnType = method.ReturnType;

                    IType type = null;

                    try
                    {
                        type = ReflectionUtilities.InferType(returnType, container);
                    }
                    catch (CompilerException)
                    {
                    }

                    if (type != null)
                    {
                        DataDeclaration declaration = new DataDeclaration(type, null);
                        declaration.TypeParameters = method.GetGenericArguments().Select(p => p.Name).ToArray();

                        container.Add(method.Name, declaration);
                        runtimeContainer.Add(method.Name, method);
                    }
                }
            }

            return module;
        }
Beispiel #6
0
        public void Compile(string declarationName, DataDeclaration data)
        {
            var result = typeChecker.CreateTypedExpression(data);

            data.Type = result.Item1;

            var typedExpression = result.Item2;
            typedExpression.AcceptVisitor(new AbstractionElimination(container));

            var collector = new TypeParameterCollector();
            data.Type.AcceptVisitor(collector);
            data.TypeParameters = collector.Parameters.ToArray();

            MethodCompiler.Compile(declarationName, dataClass, typedExpression, data, container, runtimeContainer);
        }