⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stmt.c

📁 一个c compiler的source code
💻 C
📖 第 1 页 / 共 2 页
字号:
				snp->exp = 0;
        getsym(); 
				if (lastst == end || lastst == semicolon) {
					if (currentfunc->tp->btp->type != bt_void)
						generror(ERR_RETMISMATCH,0,0);
					needpunc(semicolon,0);
				}
				else {
					int ogc = goodcode;
					goodcode |= GF_SUPERAND;
        	tp = expression(&(snp->exp));
					goodcode = ogc;
        	if( lastst != eof)
                needpunc( semicolon, 0 );
					if (tp->type == bt_void) {
						generror(ERR_NOVOIDRET,0,0);
					}
					else
/*						if (tp->type == bt_pointer && tp->val_flag)
							generror(ERR_NOFUNCARRAY,0,0);
						else
*/						if (!checktype(tp,currentfunc->tp->btp))
								if (isscalar(tp) && isscalar(currentfunc->tp->btp))
									promote_type(currentfunc->tp->btp, &(snp->exp));
								else
									if (currentfunc->tp->btp->type != bt_pointer ||
											floatrecurse(snp->exp))
										generror(ERR_RETMISMATCH,0,0);
				}
        return snp; 
} 
  
SNODE    *breakstmt(void) 
/*
 * handle break
 */
{       SNODE    *snp; 
        snp = xalloc(sizeof(SNODE)); 
				snp->next = 0;
        snp->stype = st_break; 
        getsym(); 
        if( lastst != eof)
                needpunc( semicolon,0 );
        return snp; 
} 
  
SNODE    *contstmt(void) 
/*
 * handle continue
 */
{       SNODE    *snp; 
        snp = xalloc(sizeof(SNODE)); 
				snp->next = 0;
        snp->stype = st_continue; 
				if (!(goodcode & GF_CONTINUABLE))
					generror(ERR_NOCONTINUE,0,0);
        getsym();
        if( lastst != eof)
                needpunc( semicolon,0 );
        return snp;
}
SNODE *_genwordstmt(void)
/*
 * Insert data in the code stream
 */
{
				SNODE *snp;
				snp = xalloc(sizeof(SNODE));
				snp->next = 0;
				snp->stype = st__genword;
				snp->exp = 0;
				getsym();
				if (lastst != openpa) {
					generror(ERR_PUNCT,openpa,skm_semi);
					getsym();
					snp = 0;
				}
				else {
					getsym();
					snp->exp = (ENODE *) intexpr(0);
					if (lastst != closepa) {
						generror(ERR_PUNCT,closepa,skm_semi);
						snp = 0;
					}
					getsym();
				}
				if (lastst != eof)
					needpunc(semicolon,0);
				return(snp);
}
SNODE    *exprstmt(void) 
/* 
 *      exprstmt is called whenever a statement does not begin 
 *      with a keyword. the statement should be an expression. 
 */ 
{       SNODE    *snp; 
        snp = xalloc(sizeof(SNODE)); 
				snp->next = 0;
        snp->stype = st_expr; 
				goodcode &= ~(GF_ASSIGN);
  			if( expression(&snp->exp) == 0 )  {
    			generror(ERR_EXPREXPECT,0,skm_semi); 
					snp->exp = 0;
  			}
				if (!(goodcode & GF_ASSIGN))
					generror(ERR_CODENONE,0,0);
        if( lastst != eof)
                needpunc( semicolon,0 );
        return snp; 
} 
  
SNODE *snp_line(void)
/*
 * construct a statement for the beginning of a new line
 */
{
	SNODE *snp3 = 0;
				if (!incldepth && lineno != lastlineno && lastst != semicolon && lastst != begin) {
					int i = 0,j,l=pstrlen(inputline);
					snp3 = xalloc(sizeof(SNODE));
					snp3->next = 0;
					snp3->stype = st_line;
					snp3->exp = (ENODE *)lineno;
					snp3->next = 0;
					for (j=0; j<l; j++)
						i+= installphichar(inputline[j],phibuf,i);
					if (phibuf[i-1] == '\n')
						i--;
					phibuf[i] = 0;
					if ((phibuf[i-1] & 0xf0) == 0x90)
						phibuf[i-1] = 0x90;
					snp3->label = (SNODE *)xalloc(i+1);
					strcpy(snp3->label, phibuf);
					lastlineno = lineno;
				}
	return snp3;
}
#ifdef CPLUSPLUS
void dodefaultinit(SYM *sp)
/*
 * Evalueate a C++ default clause
 */
{
	TYP *tp;
	if (lastst == assign) {
		getsym();
		if ((tp =autoasnop(&(sp->defalt), sp)) == 0) {
	 		generror(ERR_EXPREXPECT,0,0);
			getsym();
		}
		else sp->defalt = sp->defalt->v.p[1];
	}
}
#endif
void doautoinit(SYM *sym)
/*
 * This is here rather than in init because autoinit is a type of 
 * statement
 */
{
		if (lastst == assign) {
			SNODE *snp = snp_line();
			if (snp) 
				if (cbautoinithead == 0)
					cbautoinithead = cbautoinittail = snp;
				else {
					cbautoinittail->next = snp;
					cbautoinittail= snp;
				}
			getsym();
			snp = xalloc(sizeof(SNODE));
			snp->next = 0;
			snp->stype = st_expr;
			if (autoasnop(&(snp->exp), sym) == 0) {
				generror(ERR_EXPREXPECT,0,0);
				getsym();
			}
			else {
				if (cbautoinithead == 0)
					cbautoinithead = cbautoinittail = snp;
				else {
					cbautoinittail->next = snp;
					cbautoinittail= snp;
				}
			}
		}
}
		
SNODE    *compound(void) 
/* 
 * Process the body of a compound block.  Declarations are already
 * handled by now.
 * 
 */ 
{       SNODE    *head, *tail; 
        head = cbautoinithead;
				tail = cbautoinittail;
				goodcode &= ~(GF_RETURN | GF_BREAK | GF_CONTINUE | GF_GOTO);
        while( lastst != end  && lastst != eof) { 
								if (goodcode & (GF_RETURN | GF_BREAK | GF_CONTINUE | GF_GOTO)) 
									if (lastst == id) {
                    while( isspace(lastch) ) 
                      getch(); 
										if (lastch != ':')
											generror(ERR_UNREACHABLE,0,0);
											
									}
									else
										generror(ERR_UNREACHABLE,0,0);
								goodcode &= ~(GF_RETURN | GF_BREAK| GF_CONTINUE | GF_GOTO);
                if( head == 0 ) 
                        head = tail = statement(); 
                else    { 
                        tail->next = statement(); 
                        } 
                while( tail->next != 0 ) 
                  tail = tail->next; 
                } 
				if (head)
					tail->next = snp_line();
				if (lastst == eof)
					generror(ERR_PUNCT,end,0);
				else
        	getsym(); 

        return head; 
} 
  
SNODE    *labelstmt(int fetchnext) 
/* 
 *      labelstmt processes a label that appears before a 
 *      statement as a seperate statement. 
 */ 
{       SNODE    *snp; 
        SYM             *sp; 
        snp = xalloc(sizeof(SNODE)); 
				snp->next = 0;
        snp->stype = st_label; 
					goodcode &= ~GF_UNREACH;
        if( (sp = search(lastid,&lsyms)) == 0 ) { 
                sp = xalloc(sizeof(SYM)); 
                sp->name = litlate(lastid); 
                sp->storage_class = sc_label; 
								sp->tp = xalloc(sizeof(TYP));
								sp->tp->type = bt_unsigned;
								sp->tp->uflags = 0;
                sp->value.i = nextlabel++; 
                insert(sp,&lsyms); 
                } 
        else    { 
                if( sp->storage_class != sc_ulabel ) 
                        gensymerror(ERR_DUPLABEL,sp->name); 
                else 
                        sp->storage_class = sc_label; 
                } 
        getsym();       /* get past id */ 
        needpunc(colon,0); 
        if( sp->storage_class == sc_label ) { 
                snp->label = (SNODE *)sp->value.i; 
                snp->next = 0; 
								if (lastst != end && fetchnext)
									snp->next = statement();
                return snp; 
                } 
        return 0; 
} 
  
SNODE    *gotostmt(void) 
/* 
 *      gotostmt processes the goto statement and puts undefined 
 *      labels into the symbol table. 
 */ 
{       SNODE    *snp; 
        SYM             *sp; 
        getsym(); 
        if( lastst != id ) { 
                generror(ERR_IDEXPECT,0,0); 
                return 0; 
                } 
        snp = xalloc(sizeof(SNODE)); 
				snp->next = 0;
        if( (sp = search(lastid,&lsyms)) == 0 ) { 
                sp = xalloc(sizeof(SYM)); 
                sp->name = litlate(lastid); 
                sp->value.i = nextlabel++; 
                sp->storage_class = sc_ulabel; 
								sp->tp = xalloc(sizeof(TYP));
								sp->tp->type = bt_unsigned;
                insert(sp,&lsyms); 
                } 
				sp->tp->uflags = UF_USED;
        getsym();       /* get past label name */ 
        if( lastst != eof)
                needpunc( semicolon,0 );
        if( sp->storage_class != sc_label && sp->storage_class != sc_ulabel) 
                gensymerror( ERR_LABEL,sp->name); 
        else    { 
                snp->stype = st_goto; 
                snp->label = (SNODE *)sp->value.i; 
                snp->next = 0; 
                return snp; 
                } 
        return 0; 
} 
  
SNODE *asm_statement(int shortfin);
SNODE    *statement(void) 
/* 
 *      statement figures out which of the statement processors 
 *      should be called and transfers control to the proper 
 *      routine. 
 */ 
{       SNODE    *snp, *snp2, **psnp; 
				SNODE		 *snp3=snp_line();

        switch( lastst ) { 
								case kw_asm:
												asmline = TRUE;
												getsym();
												if (lastst == begin) {
													getsym();
													snp = snp3;
													if (snp)
														psnp = &snp->next;
													else
														psnp = &snp;
													*psnp = 0;
													while (lastst != end && lastst != eof) {
														*psnp = asm_statement(TRUE);
														while (*psnp)
															psnp=&(*psnp)->next;
														*psnp = snp_line();
														while (*psnp)
															psnp=&(*psnp)->next;
													}
													asmline = FALSE;
													if (lastst == end)
														getsym();
													return snp;
												}
												snp = asm_statement(FALSE);
												asmline = FALSE;
												break;
                case semicolon: 
                        getsym(); 
                        snp = 0; 
                        break; 
                case begin: 
                        getsym(); 
                        snp2 = compoundblock(); 
												snp = xalloc(sizeof(SNODE));
												snp->next = 0;
												snp->exp = snp2;
												snp->stype = st_block;
												break;
                case kw_if: 
                        snp = ifstmt(); 
                        break; 
                case kw_while: 
                        snp = whilestmt(); 
												snp->lst = snp3;
												snp3 = 0;
                        break; 
                case kw_for: 
                        snp = forstmt(); 
												snp->lst = snp3;
												snp3 = 0;
                        break; 
                case kw_return: 
                        snp = retstmt(); 
												goodcode |= GF_RETURN;
                        break; 
                case kw_break: 
												goodcode |= GF_BREAK;
												switchbreak = 1;
                        snp = breakstmt(); 
                        break; 
                case kw_goto: 
												goodcode |= GF_GOTO;
                        snp = gotostmt(); 
                        break; 
                case kw_continue: 
												goodcode |= GF_CONTINUE;
                        snp = contstmt(); 
                        break; 
                case kw_do: 
                        snp = dostmt(); 
                        break; 
                case kw_switch: 
                        snp = switchstmt(); 
                        break; 
								case kw_else:
												generror(ERR_ELSE,0,0);
												getsym();
												break;
								case kw__genword:
												snp = _genwordstmt();
												break;
                case id: 
                        while( isspace(lastch) ) 
                                getch(); 
                        if( lastch == ':') 
                                return labelstmt(TRUE); 
                        /* else fall through to process expression */ 
                default:
#ifdef CPLUSPLUS
												if (castbegin(lastst)) {
													if (prm_cplusplus) {
														cbautoinithead = cbautoinittail = 0;
														dodecl(sc_auto);
														snp = cbautoinithead;
													}
													else {
													  generror(ERR_NODECLARE,0,skm_semi);
													  snp = 0;
													}
												}
												else 
#endif
												{
	                       	snp = exprstmt(); 
												}
                        break; 
                } 
        if( snp != 0 ) {
								if (snp3) {
									snp3->next = snp;
									snp = snp3;
								}
				} 
        return snp; 
}
/* Handling for special C++ situations */
SNODE *cppblockedstatement(void)
{
	SNODE *snp;
	TABLE oldoldlsym;
	long oldlcauto;
#ifdef CPLUSPLUS
	if (prm_cplusplus) {
		oldoldlsym = oldlsym;
		oldlsym = lsyms;
	}
#endif
	snp = statement();
#ifdef CPLUSPLUS
	if (prm_cplusplus) {
		check_funcused(&oldlsym,&lsyms);
		gather_labels(&oldlsym,&lsyms);
		cseg();
		lsyms = oldlsym;
		oldlsym.head = oldoldlsym.head;
	}
#endif
	return(snp);
}
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -