Exemple #1
0
void block(unsigned long fsys) {
	long tx0;		// initial table index
	long cx0; 		// initial code index
	long tx1;		// save current table index before processing nested procedures
	long dx1;		// save data allocation index

	dx = 3; tx0 = tx; table[tx].addr = cx; gen(jmp, 0, 0);
	if (lev > levmax) {
		error(32);
	}
	do {
		if (sym == constsym) {
			getsym();
			do {
				constdeclaration();
				while (sym == comma) {
					getsym(); constdeclaration();
				}
				if (sym == semicolon) {
					getsym();
				}
				else {
					error(5);
				}
			} while (sym == ident);
		}
		if (sym == varsym) {
			getsym();
			do {
				vardeclaration();
				while (sym == comma) {
					getsym(); vardeclaration();
				}
				if (sym == semicolon) {
					getsym();
				}
				else {
					error(5);
				}
			} while (sym == ident);
		}
		while (sym == procsym) {
			getsym();
			if (sym == ident) {
				enter(proc); getsym();
			}
			else {
				error(4);
			}
			if (sym == semicolon) {
				getsym();
			}
			else {
				error(5);
			}
			lev = lev + 1; tx1 = tx; dx1 = dx;
			block(fsys | semicolon);
			lev = lev - 1; tx = tx1; dx = dx1;
			if (sym == semicolon) {
				getsym();
				//printf("this sym is %d\n",sym);
				test(statbegsys | ident | procsym, fsys, 6);
			}
			else {
				error(5);
			}
		}
		test(statbegsys | ident, declbegsys, 7);
	} while (sym&declbegsys);
	code[table[tx0].addr].a = cx;
	table[tx0].addr = cx;		// start addr of code
	cx0 = cx; gen(Int, 0, dx);
	statement(fsys | semicolon | endsym);
	gen(opr, 0, 0); // return
	test(fsys, 0, 8);
	listcode(cx0);
}
Exemple #2
0
void block(unsigned long fsys)
{
    long tx0;		// 层初始符号表指针
    long cx0; 		// 层初始code指针
//	long tx1;		// 调用前保留现场
    long dx1;
    dx = 3;
    tx0 = tx;
    table[tx].addr = cx;
    gen(jmp, 0, 0);
    if (lev>levmax)
    {
        error(32);
    }
    do
    {
        if (sym == constsym)
        {
            getsym();
            constdeclaration(tx0);
            while (sym == comma)
            {
                getsym();
                constdeclaration(tx0);
            }
            if (sym == semicolon)
            {
                getsym();
            }
            else
            {
                error(5);
            }
        }
        if (sym == varsym)
        {
            getsym();
            vardeclaration(tx0);
            while (sym == comma)
            {
                getsym();
                vardeclaration(tx0);
            }
            if (sym == semicolon)
            {
                getsym();
            }
            else
            {
                error(5);
            }
        }

        while (sym == procsym)
        {
            getsym();
            if (sym == ident)
            {
                enter(proc,tx0);
                getsym();
            }
            else
            {
                error(4);
            }
            if (sym == semicolon)
            {
                getsym();
            }
            else
            {
                error(5);
            }
            lev = lev + 1;

            dx1 = dx;
            block(fsys | semicolon);
            lev = lev - 1;

            dx = dx1;//问题
            if (sym == semicolon)
            {
                getsym();
            }
            else
            {
                error(5);
                if (sym == period)
                {
                    getsym();
                }
            }
        }

        test(beginsym, statbegsys | ident| declbegsys, 7);//TODO 1
    } while (sym&declbegsys);
    code[table[tx0].addr].a = cx;//把前面生成的跳转语句的跳转位置改成当前位置
    table[tx0].addr = cx;
    cx0 = cx;
    gen(Int, 0, dx);
    statement(fsys | semicolon | endsym);
    gen(opr, 0, 0); // return
    test(fsys, 0, 8);
//	listcode(cx0);
}
Exemple #3
0
void block(unsigned long long fsys)
{
    long tx0;       // initial table index
    long cx0;       // initial code index
    long tx1;       // save current table index before processing nested procedures
    long dx1;       // save data allocation index

    dx=3;
    // 地址寄存器给出每层局部量当前已分配到的相对位置
    // 置初始值为 3 的原因是:每一层最开始的位置有三个空间用于存放
    // 静态链 SL、动态链 DL 和 返回地址 RA
    tx0=tx;                      // 记录本层开始时符号表的位置
    table[tx].addr=cx;           // 符号表记下当前层代码的开始地址
    gen(jmp,0,0);                // block开始时首先写下一句跳转指令
                                 // 地址到后面再补

    if(lev>levmax)
    {
        error(32);
    }

    do
    {
        if(sym==constsym)        // 常数定义
        {
            getsym();

            do
            {
                constdeclaration();
                while(sym==comma)
                {
                    getsym(); constdeclaration();
                }

                if(sym==semicolon)
                {
                    getsym();
                }
                else
                {
                    error(5);
                }
            } while(sym==ident);
        }

        if(sym==varsym)
        {
            getsym();
            do
            {
                vardeclaration();
            } while(sym!=beginsym&&sym!=procsym);

        }

        while(sym==procsym)
        {
            getsym();
            if(sym==ident)
            {
                enter(proc); getsym();
            }
            else
            {
                error(4);
            }

            if(sym==semicolon)
            {
                getsym();
            }
            else
            {
                error(5);
            }

            lev=lev+1; tx1=tx; dx1=dx;
            block(fsys|semicolon);
            lev=lev-1; tx=tx1; dx=dx1;

            if(sym==semicolon)
            {
                getsym();
                test(statbegsys|ident|procsym,fsys,6);
            }
            else
            {
                error(5);
            }
        }

        test(statbegsys|ident,declbegsys,7);
    } while(sym&declbegsys);

    if (sym==beginsym)
    {
        code[table[tx0].addr].a=cx;// 把block开头写下的跳转指令的地址补上
        table[tx0].addr=cx;        // tx0的符号表存的是当前block的参数
        cx0=cx;
        gen(Int,0,dx);

        getsym();
        statement(fsys|semicolon|endsym);
        while(sym==semicolon||(sym&statbegsys))
        {
            if(sym==semicolon) getsym();
            else error(10);
            
            statement(fsys|semicolon|endsym);
        }
            
        if(sym==endsym) getsym();
        else error(17);

        gen(opr,0,0);            // block结束,加上一句返回指令
        test(fsys,0,8);
        //listcode(cx0);        
    } else error(37);
}