public override object VisitMethodFeature([NotNull] CoolParser.MethodFeatureContext context)
        {
            List <Tuple <string, Type> > parameters = new List <Tuple <string, Type> >();

            foreach (var formal in context.formal())
            {
                Tuple <string, Type> t = (Tuple <string, Type>)(Visit(formal));
                parameters.Add(t);
            }
            Type outputType = new Type(context.TYPE().GetText(), null, new Coord(0, 0));

            if (p.Types.ContainsKey(context.TYPE().GetText()))
            {
                outputType = p.Types[context.TYPE().GetText()];
            }
            else
            {
                p.Types[context.TYPE().GetText()] = outputType;
            }
            Expression exp = (Expression)Visit(context.expresion());

            Coord c = new Coord(context.Depth(), context.getAltNumber());

            return(new Method(context.ID().GetText(), outputType, parameters, exp, c));
        }
コード例 #2
0
        public MethodNode(CoolParser.MethodFeatureContext context, Node s) : base(s.Childs)
        {
            Symbols = new Dictionary <string, ClassNode>();
            Params  = new Dictionary <string, AttributeNode>();
            //Variables = new Dictionary<string, AttributeNode>();

            CoolParser.ClassdefContext p = (CoolParser.ClassdefContext)context.Parent;
            var classname = p.t.Text;
            var type      = "Object";

            if (context.TYPE().GetText() != null)
            {
                type = context.TYPE().GetText();
            }

            var name = context.ID().GetText();

            Name = name;
            SetType(SymbolTable.Classes[type]);
            //Method c;
            if (SymbolTable.Classes.ContainsKey(type) && !(SymbolTable.Classes[classname].Methods.ContainsKey(name)))// if you know my type and not myself
            {
                //c = new Method(name, SymbolTable.Classes[type])
                {
                    Expression = context.expresion();// this shouln't be on the ctor
                    Self       = context;
                }
                SymbolTable.Classes[classname].Methods.Add(Name, this);//let me introduce myself
                var formals = context.formal();
                //foreach (var item in formals)// add the parameters of the method
                //{
                //    //SymbolTable.Classes[classname].Methods[name].Params.Add(item.id.Text, new FormalNode(item.id.Text, SymbolTable.Classes[item.t.Text]));
                //}
                foreach (var item in Childs)
                {
                    try{ SymbolTable.Classes[classname].Methods[name].Params.Add(((FormalNode)item).ID, new AttributeNode(((FormalNode)item).ID, ((FormalNode)item).GetType())); }catch (System.Exception) {}
                }
            }
            this.context = context;
            var d = (CoolParser.ClassdefContext)context.Parent;

            this.ClassName = d.TYPE(0).GetText();// + ".";
            //this.Childs = s.Childs;
            this.Name = context.ID().GetText();
            if (ClassName.ToLower() == "main")
            {
                ClassName = "";
            }
            foreach (var item in Params)
            {
                Symbols.Add(item.Key, item.Value.GetType());// maybe here we could make a dict of atributes but i think this will be harder after in other places and since i only need the class :)
            }
        }