Exemplo n.º 1
0
 public FmapCompiler(ILGenerator body, string variableName, Type[] genericParameters, IRuntimeContainer runtimeContainer)
 {
     this.body = body;
     this.variableName = variableName;
     this.genericParameters = genericParameters;
     this.runtimeContainer = runtimeContainer;
 }
Exemplo n.º 2
0
 public DataCompiler(ILGenerator body, Type[] genericMethodParameters, IRuntimeContainer runtimeContainer)
 {
     this.body = body;
     this.genericMethodParameters = genericMethodParameters;
     this.runtimeContainer = runtimeContainer;
     this.typeConverter = new TypeConverter(runtimeContainer, genericMethodParameters);
 }
Exemplo n.º 3
0
 public BoxedTypeCompiler(string name, BoxedTypeDeclaration declaration, ModuleBuilder module, 
     string moduleName, IRuntimeContainer runtimeContainer)
 {
     this.name = name;
     this.declaration = declaration;
     this.module = module;
     this.moduleName = moduleName;
     this.runtimeContainer = runtimeContainer;
 }
Exemplo n.º 4
0
 public PurityCompiler(ModuleBuilder module, TypeBuilder dataClass, IMetadataContainer container,
     IRuntimeContainer runtimeContainer)
 {
     this.module = module;
     this.dataClass = dataClass;
     this.container = container;
     this.runtimeContainer = runtimeContainer;
     this.typeChecker = new TypeChecker(container);
     this.moduleImporter = new ModuleImporter(container, runtimeContainer);
 }
Exemplo n.º 5
0
 public GFixCompiler(string name, GFixTypeDeclaration declaration, ModuleBuilder module,
     TypeBuilder utilityClass, MethodBuilder fmap, string moduleName, IRuntimeContainer runtimeContainer)
 {
     this.name = name;
     this.declaration = declaration;
     this.module = module;
     this.utilityClass = utilityClass;
     this.fmap = fmap;
     this.moduleName = moduleName;
     this.runtimeContainer = runtimeContainer;
 }
Exemplo n.º 6
0
        private static void RemoveFirstParameter(string name, TypeBuilder dataClass,
            MethodInfo method, IType[] arguments, IType returnType, string[] typeParameters,
            IRuntimeContainer runtimeContainer)
        {
            var arrow = returnType as ArrowType;

            if (arrow != null)
            {
                var newArguments = arguments.Concat(new[] { arrow.Left }).ToArray();
                var newReturnType = arrow.Right;

                var curried = dataClass.DefineMethod(name,
                    MethodAttributes.Public | MethodAttributes.Static,
                    null, Type.EmptyTypes);

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

                var converter = new TypeConverter(runtimeContainer, method.GetGenericArguments());
                var lastArgument = converter.Convert(arrow.Left);
                var compiledReturnType = converter.Convert(newReturnType);

                curried.SetReturnType(compiledReturnType);
                curried.SetParameters(newArguments.Select(converter.Convert).ToArray());

                var curriedBody = curried.GetILGenerator();

                for (int argIndex = 0; argIndex < newArguments.Length - 1; argIndex++)
                {
                    curriedBody.Emit(OpCodes.Ldarg, argIndex);
                }

                if (typeParameters.Any())
                {
                    curriedBody.Emit(OpCodes.Call, method.MakeGenericMethod(curried.GetGenericArguments()));
                }
                else
                {
                    curriedBody.Emit(OpCodes.Call, method);
                }

                curriedBody.Emit(OpCodes.Ldarg, newArguments.Length - 1);
                curriedBody.Emit(OpCodes.Callvirt, TypeBuilder.GetMethod(
                    typeof(IFunction<,>).MakeGenericType(lastArgument, compiledReturnType),
                    typeof(IFunction<,>).GetMethod(Constants.CallMethodName)));
                curriedBody.Emit(OpCodes.Ret);

                RemoveFirstParameter(name, dataClass, curried, newArguments, newReturnType, typeParameters, runtimeContainer);
            }
        }
Exemplo n.º 7
0
        public static MethodBuilder Compile(IType functorType, string variableName, 
            TypeBuilder utilityClass, string[] genericParameters, IRuntimeContainer runtimeContainer)
        {
            var genericParameterNames = new[]
            {
                Constants.FMapMethodInputParameterName,
                Constants.FMapMethodOutputParameterName
            }.Concat(genericParameters).ToArray();

            var fmap = utilityClass.DefineMethod(Constants.FMapMethodName, MethodAttributes.Public | MethodAttributes.Static);
            var fmapParameters = fmap.DefineGenericParameters(genericParameterNames);

            fmap.SetReturnType(typeof(IFunction<,>).MakeGenericType(fmapParameters.Take(2).Select(t => FunctorTypeMapper.Map(functorType, variableName, t, fmapParameters, runtimeContainer)).ToArray()));

            fmap.SetParameters(typeof(IFunction<,>).MakeGenericType(fmapParameters.Take(2).ToArray()));

            var fmapBody = fmap.GetILGenerator();

            functorType.AcceptVisitor(new FmapCompiler(fmapBody, variableName, fmapParameters, runtimeContainer));

            fmapBody.Emit(OpCodes.Ret);

            return fmap;
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
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);
        }
Exemplo n.º 10
0
 public Evaluator(ModuleBuilder module, IMetadataContainer container, IRuntimeContainer runtimeContainer)
 {
     this.module = module;
     this.container = container;
     this.runtimeContainer = runtimeContainer;
 }
Exemplo n.º 11
0
 public ModuleImporter(IMetadataContainer container, IRuntimeContainer runtimeContainer)
 {
     this.container = container;
     this.runtimeContainer = runtimeContainer;
 }