Beispiel #1
0
        public override int VisitVar_node(BRAQParser.Var_nodeContext context)
        {
            if (TryResolve(context.id_name, out var var_definition_point))
            {
                if (!assigned[var_definition_point])
                {
                    string msg = $"using unassigned variable {context.id_name.Text}";
                    Console.WriteLine(msg);
                    throw new TypesolvingError();
                }

                declaration[context.id_name] = var_definition_point;
            }
            else if (TryResolveAsArg(context.id_name, out var argumentDefinition))
            {
                token_to_argument[context.id_name] = argumentDefinition;
            }
            else if (PredefsHelper.ResolveType(context.id_name.Text, imported_types) != null)
            {
            }
            else
            {
                Console.WriteLine($"unknown variable {context.id_name.Text} [Line {context.id_name.Line}]");
                throw new UnknownVariableException();
            }



            return(0);
        }
Beispiel #2
0
        public override Type VisitVar_node(BRAQParser.Var_nodeContext context)
        {
            if (var_to_local_def.ContainsKey(context.id_name))
            {
                var  def_point = var_to_local_def[context.id_name];
                Type t         = locals_type[def_point];
                return(type_dict[context] = t);
            }

            try
            {
                return(type_dict[context] = result_box.argtypes[context.id_name.Text]);
            }
            catch (InvalidOperationException)
            {
            }
            catch (KeyNotFoundException)
            {
            }

            try
            {
                Type t = PredefsHelper.ResolveType(context.id_name.Text, imported_names);
                if (t == null)
                {
                    throw new InvalidOperationException();
                }
                return(type_dict[context] = t);
            }
            catch (InvalidOperationException)
            {
                Console.WriteLine($"unknown variable {context.id_name.Text} [Line {context.id_name.Line}]");
                throw new UnknownVariableException();
            }
        }
Beispiel #3
0
        public override Type VisitShort_call(BRAQParser.Short_callContext context)
        {
            //TODO user_defined functions

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

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

            //get argument type
            Type argument_type;

            argument_type = context.arg.Accept(this);



            Type[] types          = { argument_type };
            IToken function_token = context.calee;


            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;
            }
            else
            {
                Console.WriteLine(function_token.Text);
                Console.WriteLine(String.Join(" ", types.ToList().Select(x => x.ToString())));
                throw new BindError();
            }

            return(predef_function_info.ReturnType);
        }
Beispiel #4
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();
        }
Beispiel #5
0
 private static Type string_to_type(string name, List <Type> imported_names)
 {
     return(PredefsHelper.ResolveType(name, imported_names));
 }