Пример #1
0
        public override Node VisitMethodFeature([NotNull] CoolParser.MethodFeatureContext context)
        {
            //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();
            //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(c.Name, c);//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 Atribute(item.id.Text, SymbolTable.Classes[item.t.Text]));
            //    }
            //}
            //this.currentMethod = name;
            //this.currentClass = classname;

            var s = VisitChildren(context);

            return(new MethodNode(context, s));
        }
        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));
        }
        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 :)
            }
        }
        private void chec_typeAdded(Node s, CoolParser.MethodFeatureContext context, string classname)
        {
            foreach (var son in s.Childs)
            {
                if (SymbolTable.Classes[classname].Attributes.ContainsKey(son.ToString()))
                {
                    son.Type = SymbolTable.Classes[classname].Attributes[son.ToString()].Type;
                }


                if (son.Childs.Count() > 0)
                {
                    foreach (var item in son.Childs)
                    {
                        chec_typeAdded(item, context, classname);
                    }
                }
            }
        }
Пример #5
0
        public override object VisitMethodFeature([NotNull] CoolParser.MethodFeatureContext context)
        {
            CoolParser.ClassdefContext p = (CoolParser.ClassdefContext)(context.Parent);
            dotcode += p.t.Text + "->" + context.id.Text + "\n";
            dotcode += context.id.Text + "[label=\"" + context.id.Text;
            if (context.t != null)
            {
                dotcode += ":" + context.t.Text;
            }
            dotcode += "\nmethod\"]\n";
            // TODO fix signature of methods in dotcode?
            // do not make a new node for each formal
            var fs = context.formal();

            foreach (var item in fs)
            {
                dotcode += context.id.Text + "->" + item.id.Text + "\n";
                dotcode += item.id.Text + "[label=\"" + item.id.Text + ":" + item.t.Text + "\nformal\"]\n";
            }
            return(VisitChildren(context));
        }
Пример #6
0
        //public override object VisitFormal([NotNull] CoolParser.FormalContext context)
        //{
        //    return VisitChildren(context);
        //}

        //public override object
        private void VisitExpr(CoolParser.ExpresionContext context)
        {
            try
            {
                CoolParser.MethodFeatureContext p = (CoolParser.MethodFeatureContext)context.Parent;
                dotcode += p.id.Text + "->" + "\"" + context.GetType().Name.Substring(0, context.GetType().Name.Length - 7) + "\"" + "\n";
                return;
            }
            catch (Exception) { }
            try
            {
                CoolParser.AttribFeatureContext p = (CoolParser.AttribFeatureContext)context.Parent;
                dotcode += p.id.Text + "->" + "\"" + context.GetType().Name.Substring(0, context.GetType().Name.Length - 7) + "\"" + "\n";
                return;
            }
            catch (Exception) { }
            try
            {
                CoolParser.ExpresionContext p = (CoolParser.ExpresionContext)context.Parent;
                dotcode += p.GetType().Name.Substring(0, p.GetType().Name.Length - 7) + "->" + "\"" + context.GetType().Name.Substring(0, context.GetType().Name.Length - 7) + "\"" + "\n";
                return;
            }
            catch (Exception) { }

            //switch (context.Parent)
            //{
            //    case CoolParser.MethodFeatureContext p:
            //        dotcode += p.id.Text + "->" + "\"" + context.GetType().Name.Substring(0, context.GetType().Name.Length - 7) + "\"" + "\n";
            //        break;
            //    case CoolParser.AttribFeatureContext p:
            //        dotcode += p.id.Text + "->" + "\"" + context.GetType().Name.Substring(0, context.GetType().Name.Length - 7) + "\"" + "\n";
            //        break;
            //    case CoolParser.ExpresionContext p:
            //        dotcode += p.GetType().Name.Substring(0, p.GetType().Name.Length - 7) + "->" + "\"" + context.GetType().Name.Substring(0, context.GetType().Name.Length - 7) + "\"" + "\n";
            //        break;
            //    default:
            //        break;
            //}
            ////return VisitChildren(context);
        }
Пример #7
0
        public override Node VisitMethodFeature([NotNull] CoolParser.MethodFeatureContext context)
        {
            var s = VisitChildren(context);

            return(new MethodNode(context, s));
        }
Пример #8
0
 /// <summary>
 /// Exit a parse tree produced by the <c>methodFeature</c>
 /// labeled alternative in <see cref="CoolParser.feature"/>.
 /// <para>The default implementation does nothing.</para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 public virtual void ExitMethodFeature([NotNull] CoolParser.MethodFeatureContext context)
 {
 }
 /// <summary>
 /// Visit a parse tree produced by the <c>methodFeature</c>
 /// labeled alternative in <see cref="CoolParser.feature"/>.
 /// <para>
 /// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/>
 /// on <paramref name="context"/>.
 /// </para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 /// <return>The visitor result.</return>
 public virtual Result VisitMethodFeature([NotNull] CoolParser.MethodFeatureContext context)
 {
     return(VisitChildren(context));
 }
Пример #10
0
 public override bool VisitMethodFeature([NotNull] CoolParser.MethodFeatureContext context)
 {
     return(VisitChildren(context));
 }