Ejemplo n.º 1
0
        /// <summary>
        /// Creates a definition for the given parsed function
        /// </summary>
        /// <param name="function">The function</param>
        private FunctionDefinition CreateFunctionDefinition(Data.Function function)
        {
            var parameters = function.Parameters.Select(parameterType => LoaderHelpers.FindType(this.typeProvider, parameterType)).ToList();
            var returnType = LoaderHelpers.FindType(this.typeProvider, function.ReturnType);

            if (function.ClassType == null)
            {
                return(new FunctionDefinition(
                           function.Name,
                           parameters,
                           returnType));
            }
            else
            {
                var classType = LoaderHelpers.FindType(this.typeProvider, TypeSystem.ClassTypeName(function.ClassType));
                if (!classType.IsClass)
                {
                    throw new LoaderException($"'{function.ClassType}' is not a class type.");
                }

                return(new FunctionDefinition(
                           function.Name,
                           parameters,
                           returnType,
                           (ClassType)classType,
                           function.IsConstructor));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Loads the given managed function
        /// </summary>
        /// <param name="function">The function</param>
        /// <param name="functionDefinition">The function definition</param>
        public ManagedFunction LoadManagedFunction(Data.Function function, FunctionDefinition functionDefinition)
        {
            var locals       = function.Locals.Select(local => LoaderHelpers.FindType(this.typeProvider, local)).ToList();
            var instructions = function.Instructions.Select(instruction =>
            {
                switch (instruction.Format)
                {
                case Data.InstructionFormat.OpCodeOnly:
                    return(new Instruction(instruction.OpCode));

                case Data.InstructionFormat.IntValue:
                    return(new Instruction(instruction.OpCode, instruction.IntValue));

                case Data.InstructionFormat.FloatValue:
                    return(new Instruction(instruction.OpCode, instruction.FloatValue));

                case Data.InstructionFormat.StringValue:
                    return(new Instruction(instruction.OpCode, instruction.StringValue));

                case Data.InstructionFormat.Call:
                    return(new Instruction(
                               instruction.OpCode,
                               instruction.StringValue,
                               instruction.Parameters.Select(parameter => LoaderHelpers.FindType(this.typeProvider, parameter)).ToList()));

                case Data.InstructionFormat.CallInstance:
                    {
                        var classType = LoaderHelpers.FindType(this.typeProvider, TypeSystem.ClassTypeName(instruction.ClassType));
                        if (!classType.IsClass)
                        {
                            throw new LoaderException($"'{instruction.ClassType}' is not a class type.");
                        }

                        return(new Instruction(
                                   instruction.OpCode,
                                   instruction.StringValue,
                                   (ClassType)classType,
                                   instruction.Parameters.Select(parameter => LoaderHelpers.FindType(this.typeProvider, parameter)).ToList()));
                    }

                default:
                    return(new Instruction());
                }
            }).ToList();

            return(new ManagedFunction(functionDefinition, locals, instructions));
        }