Exemplo n.º 1
0
        public override Type VisitCall(BRAQParser.CallContext context)
        {
            //TODO
            //get argument list

            if (context.calee == null)
            {
                return(type_dict[context] = context.single.Accept(this));
            }

            foreach (var exprContext in context.expr())
            {
                exprContext.Accept(this);
            }

            Type[] types = context.expr().Select(x => type_dict[x]).ToArray();


            IToken function_token = context.calee;

            if (TryResolveOwnMethod(context, out var ownMethodInfo))
            {
                result_box.token_to_user_function[context.calee] = ownMethodInfo;
                return(type_dict[context] = ownMethodInfo.return_type);
            }

            //MethodInfo predef_function_info = typeof(Predefs).GetMethod(function_token.Text, types);
            MethodInfo predef_function_info = PredefsHelper.Resolve(dot_prefix_type, function_token.Text, types);

            if (predef_function_info != null)
            {
                if (predef_function_info.GetParameters()
                    .Zip(types, (r, w) => new KeyValuePair <Type, Type>(r.ParameterType, w))
                    .Any(p => p.Key != p.Value))
                {
                    Console.WriteLine("could not bind {0}({1}) [Line {2}]",
                                      function_token.Text,
                                      String.Join(" ", types.Select(x => x.ToString())),
                                      function_token.Line
                                      );
                    throw new BindError();
                }

                result_box.token_to_outer_function[function_token] = predef_function_info;
                type_dict[context] = predef_function_info.ReturnType;
                return(predef_function_info.ReturnType);
            }

            Console.WriteLine(function_token.Text);
            Console.WriteLine(String.Join(" ", types.ToList().Select(x => x.ToString())));
            throw new BindError();
        }
Exemplo n.º 2
0
        public override int VisitCall(BRAQParser.CallContext context)
        {
            if (context.calee == null)
            {
                return(context.single.Accept(this));
            }

            if (user_function_table.ContainsKey(context.calee))
            {
                var info = user_function_table[context.calee];

                foreach (var exprContext in context.expr())
                {
                    exprContext.Accept(this);
                }

                il.EmitCall(OpCodes.Call, info.method_builder, null);
                return(0);
            }

            var function_ptr = function_table[context.calee];

            foreach (var exprContext in context.expr())
            {
                exprContext.Accept(this);
            }

            if (function_ptr.IsStatic)
            {
                il.EmitCall(OpCodes.Call, function_ptr, null);
            }
            else
            {
                il.EmitCall(OpCodes.Call, function_ptr, null);
            }

            return(0);
        }
Exemplo n.º 3
0
        private bool TryResolveOwnMethod(BRAQParser.CallContext context, out OwnMethodInfo ownMethodInfo)
        {
            IToken      name = context.calee;
            List <Type> args = context.expr().Select(x => type_dict[x]).ToList();

            try
            {
                var handle = user_functions
                             .First(x => x.name == name.Text &&
                                    x.arguments.Count == args.Count &&
                                    x.arguments.Select(r => r.b).Zip(args, (r, w) => new Pair <Type, Type>(r, w))
                                    .All(rec => rec.a == rec.b)
                                    );
                ownMethodInfo = handle;

                return(true);
            }
            catch (InvalidOperationException)
            {
                ownMethodInfo = null;
                return(false);
            }
        }