Пример #1
0
        //private Node s;

        public LetNode(CoolParser.LetExpContext context, Node s) : base(s.Childs)
        {
            this.context = context;
            IDs          = new List <IToken>();
            Types        = new List <IToken>();
            Exprs        = new List <CoolParser.ExpresionContext>();

            for (int i = 0; i < context.newvar().Length; i++)
            {
                IDs.Add(context.newvar()[i].id);
                Types.Add(context.newvar()[i].t);
                Exprs.Add(context.newvar()[i].e);
            }
            //IDs = new ITerminalNode[context];
            //Types = context.TYPE();
            //Exprs = context.expresion();
            Symbols = new Dictionary <string, ClassNode>();

            for (int i = 0; i < IDs.Count; i++)
            {
                var g = Types[i].Text;
                //SymbolTable.Classes[Types[i].GetText()] = new ClassNode(g, null);
                Symbols[IDs[i].Text] = SymbolTable.Classes[Types[i].Text];
                //SymbolTable.Symbols.Push(Symbols);
            }
        }
        public override object VisitLetExp([NotNull] CoolParser.LetExpContext context)
        {
            Coord c = GetCoord(context);

            List <Identifier> letInits = new List <Identifier>();

            //for (int i = 0; i < context.ID().Length; i++)
            //{
            //	Type idType = new Type(context.TYPE()[i].GetText(), null, null);
            //	if (p.Types.ContainsKey(context.TYPE()[i].GetText()))
            //	{
            //		idType = p.Types[context.TYPE()[i].GetText()];
            //	}
            //	Expression exp = null;
            //	if (i<context.ASSIGN().Length)
            //		 exp = (Expression)Visit(context.expresion()[i]);

            //	Identifier id = new Identifier(context.ID()[i].GetText(), exp, idType, new Coord(context.Depth(), context.getAltNumber()));
            //	letInits.Add(id);
            //	//Shady ...
            //	// ANTLR  stores in context the def in grammar for Let:
            //	//LET ID COLON TYPE(ASSIGN expresion)? (COMMA ID COLON TYPE(ASSIGN expresion) ? )*IN expresion			#letExp
            //	// as:
            //	// an array of IDs,array of COLON, array of ASSIGN, array of expressions
            //	// so im not sure if the lengths of these are equal since you theoretically can skip the assign part of any identifier and just declare it, so in that case i would not know if the ith assign correponds to the ith ID, is that so?, need to check
            //	//Console.WriteLine();
            //	//if(context.expresion[i])
            //}
            foreach (var x in context.newvar())
            {
                if (x.e != null)
                {
                    letInits.Add(new Identifier(x.id.Text, (Expression)Visit(x.e), new Type(x.t.Text, null, null), GetCoord(context)));
                }
                else
                {
                    letInits.Add(new Identifier(x.id.Text, null, new Type(x.t.Text, null, null), GetCoord(context)));
                }
            }
            var new_body = (Expression)Visit(context.body);

            //Expression body = (Expression)Visit(context.expresion()[0]);
            //context.

            return(new Let(letInits, new_body, GetCoord(context)));
        }