Esempio n. 1
0
        private static void EmitFunction(TypeBuilder tb, Function func)
        {
            LocalSym = new Dictionary<Variable, LocalBuilder>();

            var actual = func.GetSpecifiedChildren<Variable>()
                             .Where(x => x.VarSemantics == VarType.Parameter)
                             .ToArray();
            var methodBuilder = tb.DefineMethod(func.Name.Text, MethodAttributes.Public, GetType(func.ReturnType),
                                                actual.Select(x => GetType(x.Type)).ToArray());
            for (var i = 0; i < actual.Length; ++i)
                methodBuilder.DefineParameter(i+1, ParameterAttributes.In, actual[i].Name.Text);

            var il = methodBuilder.GetILGenerator();

            foreach (var variable in func.GetSpecifiedChildren<Variable>()
                             .Where(x => x.VarSemantics == VarType.Variable)
                             .ToArray())
            {
                var a = il.DeclareLocal(GetType(variable.Type));
                LocalSym.Add(variable, a);
            }

            EmitBody(methodBuilder, func.GetSpecifiedChildren<Body>().First());

            il.Emit(OpCodes.Ret);
        }