示例#1
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;
        }
示例#2
0
        public int visit(IrCallst s)
        {
            if (s.func.id.Equals("print")) {

            // Print the argument
            if (0 == s.args.size())
            {
                OnSystemOut(new InterpOutEventArgs());
            }
            else
            {
                IrExp arg0 = s.args.elementAt(0);

                if (arg0 is IrString)
                {
                    OnSystemOut(new InterpOutEventArgs(((IrString)arg0).s));
                }
                else
                {
                    int val = arg0.accept(this);
                    OnSystemOut(new InterpOutEventArgs(val.ToString()));
                }
            }

            } else {

            //– find the FUNC node corresponding to the routine from the program’s funcs list, and switch there
            IrFunc func = findFunc(s.func.id);

            //push parameters onto the stack (at stack[sp+1], stack[sp+2], etc.);

            for (int i = 0; i < s.args.size(); ++i) {
                stack[sp + i + 1] = s.args.elementAt(i).accept(this);
            }

            // adjust the frame pointer (save the current fp in stack[sp], and set sp to be the new fp);
            stack[sp] = fp;

            fp = sp;

            //to execute (by invoking accept() on the FUNC node);
            func.accept(this);

            // After control returns, restore the saved frame pointer.
            sp = fp;
            fp = stack[sp];
            }

            return STATUS_DEFAULT; // index of first statement to execute?
        }
示例#3
0
文件: Canon.cs 项目: robin5/DemiTasse
 public IrStmt visit(IrCallst t)
 {
     IrExp args = t.args.accept(this);
     IrStmt s = getStmt(args);
     if (s != null)
         return mergeStmts(s, new IrCallst(t.func, (IrExpList)getExp(args)));
     return t;
 }