Ejemplo n.º 1
0
 // EXPRESSIONS
 public IrExpList visit(AstExpList n)
 {
     IrExpList el = new IrExpList();
     for (int i = 0; i < n.Count(); i++)
         el.add(n[i].accept(this));
     return el;
 }
Ejemplo n.º 2
0
        // *** partial implementation, will be fixed in proj2 ****
        // Call ---
        // Exp obj;
        // Id cid, mid;
        // ExpList args;
        public IrExp visit(AstCall n)
        {
            string label;

            // An Important Detail: The TypeVisitor program has its own copy of
            // currClass and currMethod. These two variables need to be set to the
            // current environment before typechecking can work. Do the following:

            tv.setClass(currClass);
            tv.setMethod(currMethod);

            // 1. Perform typechecking on the obj component of the Call/CallStmt
            // node, which should return an ObjType representing the obj's class.

            AstObjType t = (AstObjType)n.obj.accept(tv);

            // 2. Look up the MethodRec with the class info.
            ClassRec classRec = symTable.GetClass(t.cid);
            MethodRec methodRec = symTable.GetMethod(classRec, n.mid);

            IrExp accessLink = n.obj.accept(this);
            IrExpList el = new IrExpList(accessLink);
            el.addAll(n.args.accept(this));

            if (maxArgCnt <= n.args.Count())
                maxArgCnt = n.args.Count() + 1;

            if (methodRec.Id().s.CompareTo("main") == 0)
                label = "main";
            else
                label = symTable.UniqueMethodName(classRec, methodRec.Id());

            return new IrCall(new IrName(label), el);
        }
Ejemplo n.º 3
0
 public IrCall(IrName func, IrExpList args)
 {
     _func = func;
     _args = args;
 }
Ejemplo n.º 4
0
        // Print ---
        // Exp e;
        public IrStmt visit(AstPrint n)
        {
            IrName func = new IrName("print");

            IrExpList args = new IrExpList();

            if (n.e != null)
                args.add(n.e.accept(this));

            return new IrCallst(func, args);
        }
Ejemplo n.º 5
0
 // Expressions
 public int visit(IrExpList t)
 {
     return STATUS_DEFAULT;
 }
Ejemplo n.º 6
0
        /* throws ParseException */
        public static IrStmt STMT()
        {
            IrToken t;
            IrCJump.OP n;
            IrStmt s;
            IrExp e1 = null, e2, e3;
            IrExpList el=new IrExpList();

            jj_consume_token(RegExpId.kw44);
            switch ((jj_ntk == RegExpId.UNDEFINED) ? jj_ntk_fn() : jj_ntk)
            {
                case RegExpId.kwMOVE:

                    jj_consume_token(RegExpId.kwMOVE);
                    e1 = EXP();
                    e2 = EXP();
                    s = new IrMove(e1,e2);
                    break;

                case RegExpId.kwJUMP:

                    jj_consume_token(RegExpId.kwJUMP);
                    e1 = EXP();
                    s = new IrJump((IrName)e1);
                    break;

                case RegExpId.kwCJUMP:

                    jj_consume_token(RegExpId.kwCJUMP);
                    n = relopCode();
                    e1 = EXP();
                    e2 = EXP();
                    e3 = EXP();
                    s = new IrCJump(n,e1,e2,(IrName)e3);
                    break;

                case RegExpId.kwLABEL:

                    jj_consume_token(RegExpId.kwLABEL);
                    t = jj_consume_token(RegExpId.ID);
                    s = new IrLabel(t.image);
                    break;

                case RegExpId.kwCALLST:

                    jj_consume_token(RegExpId.kwCALLST);
                    e1 = EXP();
                    jj_consume_token(RegExpId.kw32);

                    RegExpId tmp2;
                    //label_3:
                    while (true)
                    {
                        tmp2 = (jj_ntk == RegExpId.UNDEFINED) ? jj_ntk_fn() : jj_ntk;
                        switch (tmp2)
                        {
                            case RegExpId.kw32:

                                break;

                            default:
                                jj_la1[3] = jj_gen;
                                goto label_3a; // break label_3;
                        }
                        e2 = EXP();
                        el.add(e2);
                    }

                    label_3a:

                    jj_consume_token(RegExpId.kw35);
                    s = new IrCallst((IrName)e1,el);
                    break;

                case RegExpId.kwRETURN:

                    jj_consume_token(RegExpId.kwRETURN);
                    switch ((jj_ntk == RegExpId.UNDEFINED) ? jj_ntk_fn() : jj_ntk)
                    {
                        case RegExpId.kw32:
                            e1 = EXP();
                            break;
                        default:
                            jj_la1[4] = jj_gen;
                            break;
                    }
                    s = new IrReturn(e1);
                    break;

                    default:
                        jj_la1[5] = jj_gen;
                        jj_consume_token(RegExpId.UNDEFINED);
                        throw new IrParseException();
                }

            jj_consume_token(RegExpId.kw45);
                return s;

            //throw new Error("Missing return statement in function");
            //return s;
        }
Ejemplo n.º 7
0
        /* throws ParseException */
        public static IrExp EXP()
        {
            IrToken t;
            int n;
            String str;
            IrStmt s;
            IrExp e, e2;
            IrExpList el = new IrExpList();
            jj_consume_token(RegExpId.kw32);

            switch ((jj_ntk == RegExpId.UNDEFINED) ? jj_ntk_fn() : jj_ntk)
            {
                case RegExpId.kwESEQ:

                    jj_consume_token(RegExpId.kwESEQ);
                    s = STMT();
                    e = EXP();
                    e = new IrEseq(s,e);
                    break;

                case RegExpId.kwMEM:

                    jj_consume_token(RegExpId.kwMEM);
                    e = EXP();
                    e = new IrMem(e);
                    break;

                case RegExpId.kwBINOP:

                    jj_consume_token(RegExpId.kwBINOP);
                    IrBinop.OP op = binopCode();
                    e = EXP();
                    e2 = EXP();
                    e = new IrBinop(op,e,e2);
                    break;

                case RegExpId.kwCALL:

                    jj_consume_token(RegExpId.kwCALL);
                    e = EXP();
                    jj_consume_token(RegExpId.kw32);
                    // label_4:
                    while (true)
                    {
                        switch ((jj_ntk == RegExpId.UNDEFINED) ? jj_ntk_fn() : jj_ntk)
                        {
                            case RegExpId.kw32:
                                break;

                            default:
                                jj_la1[7] = jj_gen;
                                goto label_4a; // break label_4;
                        }
                        e2 = EXP();
                        el.add(e2);
                    }
                    label_4a:

                    jj_consume_token(RegExpId.kw35);
                    e = new IrCall((IrName)e,el);
                    break;

                case RegExpId.kwTEMP:

                    jj_consume_token(RegExpId.kwTEMP);
                    n = INT();
                    e = new IrTemp(n);
                    break;

                case RegExpId.kwNAME:

                    jj_consume_token(RegExpId.kwNAME);
                    t = jj_consume_token(RegExpId.ID);
                    e = new IrName(t.image);
                    break;

                case RegExpId.kwFIELD:

                    jj_consume_token(RegExpId.kwFIELD);
                    e = EXP();
                    n = INT();
                    e = new IrField(e,n);
                    break;

                case RegExpId.kwPARAM:

                    jj_consume_token(RegExpId.kwPARAM);
                    n = INT();
                    e = new IrParam(n);
                    break;

                case RegExpId.kwVAR:

                    jj_consume_token(RegExpId.kwVAR);
                    n = INT();
                    e = new IrVar(n);
                    break;

                case RegExpId.kwCONST:

                    jj_consume_token(RegExpId.kwCONST);
                    n = INT();
                    e = new IrConst(n);
                    break;

                case RegExpId.kwSTRING:

                    jj_consume_token(RegExpId.kwSTRING);
                    str = STR();
                    e = new IrString(str);
                    break;

                default:

                    jj_la1[8] = jj_gen;
                    jj_consume_token(RegExpId.UNDEFINED);
                    throw new IrParseException();
            }

            jj_consume_token(RegExpId.kw35);
            {if (true) return e;}
            throw new Error("Missing return statement in function");
        }
Ejemplo n.º 8
0
 public IrExp visit(IrExpList t)
 {
     IrStmt s = null;
     IrExpList args = new IrExpList();
     for (int i = 0; i < t.size(); i++)
     {
         IrExp e = ((IrExp)t.elementAt(i)).accept(this);
         IrStmt s1 = getStmt(e);
         if (s1 != null) s = mergeStmts(s, s1);
         args.add(getExp(e));
     }
     if (s != null)
         return new IrEseq(s, args);
     return t;
 }
Ejemplo n.º 9
0
 public void addAll(IrExpList el)
 {
     list.AddRange(el.list);
 }