Statement *ParseForStatement() { Statement *snp; currentFn->UsesPredicate = TRUE; snp = NewStatement(st_for, TRUE); snp->predreg = iflevel; iflevel++; if ((iflevel > maxPn-1) && isThor) error(ERR_OUTOFPREDS); needpunc(openpa,16); if( expression(&(snp->initExpr)) == NULL ) snp->initExpr = (ENODE *)NULL; needpunc(semicolon,32); if( expression(&(snp->exp)) == NULL ) snp->exp = (ENODE *)NULL; needpunc(semicolon,17); if( expression(&(snp->incrExpr)) == NULL ) snp->incrExpr = (ENODE *)NULL; needpunc(closepa,18); snp->s1 = ParseStatement(); // Empty statements return NULL if (snp->s1) snp->s1->outer = snp; iflevel--; return snp; }
SNODE *dostmt(void) /* * dostmt parses the c do-while construct. */ { SNODE *snp; int ogc = goodcode,oswb = switchbreak; goodcode |= GF_INLOOP; snp = xalloc(sizeof(SNODE)); snp->next = 0; snp->stype = st_do; getsym(); goodcode |= GF_CONTINUABLE; snp->s1 = cppblockedstatement(); goodcode = ogc; if( lastst != kw_while ) gensymerror(ERR_IDENTEXPECT,lastid); else getsym(); needpunc(openpa,0); doassign(&snp->exp,TRUE,0); needpunc(closepa,skm_closepa); if( lastst != eof) needpunc( semicolon,0 ); switchbreak = oswb; return snp; }
SNODE *ifstmt(void) /* * ifstmt parses the c if statement and an else clause if * one is present. */ { SNODE *snp; int ogc = goodcode; int temp=0,temp1 = 0; snp = xalloc(sizeof(SNODE)); snp->next = 0; snp->stype = st_if; getsym(); needpunc(openpa,0); doassign(&snp->exp,TRUE,0); needpunc( closepa,skm_closepa ); snp->s1 = cppblockedstatement(); temp1 = goodcode & (GF_RETURN | GF_BREAK | GF_CONTINUE | GF_GOTO); if( lastst == kw_else ) { temp = goodcode & (GF_RETURN | GF_BREAK | GF_CONTINUE | GF_GOTO); goodcode = ogc; getsym(); snp->s2 = cppblockedstatement(); temp = temp & goodcode; } else snp->s2 = 0; goodcode = ogc | (temp & temp1); return snp; }
int InitializeStructure(TYP *tp) { SYM *sp; int nbytes; needpunc(begin,25); nbytes = 0; sp = sp->GetPtr(tp->lst.GetHead()); /* start at top of symbol table */ while(sp != 0) { while(nbytes < sp->value.i) { /* align properly */ // nbytes += GenerateByte(0); GenerateByte(0); // nbytes++; } nbytes += InitializeType(sp->tp); if( lastst == comma) NextToken(); else if(lastst == end) break; else error(ERR_PUNCT); sp = sp->GetNextPtr(); } if( nbytes < tp->size) genstorage( tp->size - nbytes); needpunc(end,26); return tp->size; }
Statement *ParseCatchStatement() { Statement *snp; SYM *sp; TYP *tp,*tp1,*tp2; snp = NewStatement(st_catch, TRUE); if (lastst != openpa) { snp->label = NULL; snp->s2 = 99999; snp->s1 = ParseStatement(); return snp; } needpunc(openpa); tp = head; tp1 = tail; catchdecl = TRUE; ParseAutoDeclarations(); catchdecl = FALSE; tp2 = head; head = tp; tail = tp1; needpunc(closepa); if( (sp = search(declid,&lsyms)) == NULL) sp = makeint(declid); snp->s1 = ParseStatement(); snp->label = (char *)sp; // save off symbol pointer if (sp->tp->typeno >= bt_last) error(ERR_CATCHSTRUCT); snp->s2 = GetTypeHash(sp->tp); return snp; }
Statement *ParseCatchStatement() { Statement *snp; SYM *sp; TYP *tp,*tp1,*tp2; ENODE *node; static char buf[200]; snp = NewStatement(st_catch, TRUE); currentStmt = snp; if (lastst != openpa) { snp->label = (int64_t *)NULL; snp->s2 = (Statement *)99999; snp->s1 = ParseStatement(); // Empty statements return NULL if (snp->s1) snp->s1->outer = snp; return snp; } needpunc(openpa,33); tp = head; tp1 = tail; catchdecl = TRUE; AutoDeclaration::Parse(NULL,&snp->ssyms); cseg(); catchdecl = FALSE; tp2 = head; head = tp; tail = tp1; needpunc(closepa,34); if( (sp = snp->ssyms.Find(*declid,false)) == NULL) sp = makeint((char *)declid->c_str()); node = makenode(sp->storage_class==sc_static ? en_labcon : en_autocon,NULL,NULL); // nameref looks up the symbol using lastid, so we need to back it up and // restore it. strncpy(buf,lastid,199); strncpy(lastid, declid->c_str(),sizeof(lastid)-1); nameref(&node,FALSE); strcpy(lastid,buf); snp->s1 = ParseStatement(); // Empty statements return NULL if (snp->s1) snp->s1->outer = snp; snp->label = (int64_t *)node; // save name reference if (sp->tp->typeno >= bt_last) error(ERR_CATCHSTRUCT); snp->s2 = (Statement *)GetTypeHash(sp->tp); // Empty statements return NULL // if (snp->s2) // snp->s2->outer = snp; return snp; }
Statement *vortex_stmt() { Statement *snp; SYM *sp; TYP *temp1; NextToken(); if( lastst != id ) { error(ERR_IDEXPECT); return 0; } temp1 = TYP::Make(bt_long,0); temp1->val_flag = 1; sp = allocSYM(); sp->SetName(*(new std::string(lastid))); sp->value.i = nextlabel++; sp->storage_class = sc_global; sp->SetType(temp1); currentFn->lsyms.insert(sp); NextToken(); /* get past label name */ needpunc( colon,30 ); snp = (struct snode *)xalloc(sizeof(struct snode)); snp->stype = st_vortex; snp->label = (int64_t *)my_strdup((char *)sp->name->c_str()); snp->next = 0; snp->s1 = ParseStatement(); 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 *casestmt(SNODE *lst) /* * cases are returned as seperate statements. for normal * cases label is the case value and s2 is zero. for the * default case s2 is nonzero. */ { SNODE *snp; SNODE *head=0, *tail; snp = xalloc(sizeof(SNODE)); snp->next = 0; if( lastst == kw_case ) { getsym(); snp->s2 = 0; snp->stype = st_case; snp->label = (SNODE *)intexpr(0); } else if( lastst == kw_default) { goodcode |= GF_DEF; getsym(); snp->stype = st_case; snp->s2 = (SNODE *)1; } else { generror(ERR_NOCASE,0,0); return 0; } needpunc(colon,0); head = 0; if (lst) { head = tail = lst; lst->next = 0; } while( lastst != end && lastst != eof && lastst != kw_case && lastst != kw_default ) { 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 (goodcode & GF_BREAK) goodcode &= ~GF_UNREACH; snp->s1 = head; return snp; }
Statement *ParseSwitchStatement() { Statement *snp; Statement *head, *tail; snp = NewStatement(st_switch, TRUE); iflevel++; if( expression(&(snp->exp)) == NULL ) error(ERR_EXPREXPECT); needpunc(begin,36); head = 0; while( lastst != end ) { if( head == (Statement *)NULL ) { head = tail = ParseCaseStatement(); if (head) head->outer = snp; } else { tail->next = ParseCaseStatement(); if( tail->next != (Statement *)NULL ) { tail->next->outer = snp; tail = tail->next; } } if (tail==(Statement *)NULL) break; // end of file in switch tail->next = (Statement *)NULL; } snp->s1 = head; NextToken(); if( CheckForDuplicateCases(head) ) error(ERR_DUPCASE); iflevel--; return snp; }
void ParseEnumerationList(TABLE *table) { int evalue; SYM *sp; evalue = 0; while(lastst == id) { sp = allocSYM(); sp->name = litlate(lastid); sp->storage_class = sc_const; sp->tp = &stdconst; insert(sp,table); NextToken(); if (lastst==assign) { NextToken(); sp->value.i = GetIntegerExpression((ENODE **)NULL); evalue = sp->value.i+1; } else sp->value.i = evalue++; if( lastst == comma) NextToken(); else if(lastst != end) break; } needpunc(end); }
Statement *Statement::ParseWhile() { Statement *snp; currentFn->UsesPredicate = TRUE; snp = NewStatement(st_while, TRUE); snp->predreg = iflevel; iflevel++; looplevel++; if ((iflevel > maxPn-1) && isThor) error(ERR_OUTOFPREDS); if( lastst != openpa ) error(ERR_EXPREXPECT); else { NextToken(); if( expression(&(snp->exp)) == 0 ) error(ERR_EXPREXPECT); needpunc( closepa,13 ); if (lastst==kw_do) NextToken(); snp->s1 = Statement::Parse(); // Empty statements return NULL if (snp->s1) snp->s1->outer = snp; } iflevel--; looplevel--; return (snp); }
Statement *ParseUntilStatement() { Statement *snp; currentFn->UsesPredicate = TRUE; snp = NewStatement(st_until, TRUE); snp->predreg = iflevel; iflevel++; if ((iflevel > maxPn-1) && isThor) error(ERR_OUTOFPREDS); if( lastst != openpa ) error(ERR_EXPREXPECT); else { NextToken(); if( expression(&(snp->exp)) == 0 ) error(ERR_EXPREXPECT); needpunc( closepa,14 ); snp->s1 = ParseStatement(); // Empty statements return NULL if (snp->s1) snp->s1->outer = snp; } iflevel--; return snp; }
Statement *ParseDoStatement() { Statement *snp; currentFn->UsesPredicate = TRUE; snp = NewStatement(st_do, TRUE); snp->predreg = iflevel; iflevel++; if ((iflevel > maxPn-1) && isThor) error(ERR_OUTOFPREDS); snp->s1 = ParseStatement(); // Empty statements return NULL if (snp->s1) snp->s1->outer = snp; if (lastst == kw_until) snp->stype = st_dountil; else if (lastst== kw_loop) snp->stype = st_doloop; if( lastst != kw_while && lastst != kw_until && lastst != kw_loop ) error(ERR_WHILEXPECT); else { NextToken(); if (snp->stype!=st_doloop) { if( expression(&(snp->exp)) == 0 ) error(ERR_EXPREXPECT); } if( lastst != end ) needpunc( semicolon,15 ); } iflevel--; return snp; }
//struct snode *interrupt_stmt() //{ struct snode *snp; // SYM *sp; // TYP *temp1; // // NextToken(); // if( lastst != id ) { // error(ERR_IDEXPECT); // return 0; // } // sp = allocSYM(); // sp->name = litlate(lastid); // sp->value.i = nextlabel++; // sp->storage_class = sc_global; // sp->tp = temp1; // temp1 = maketype(bt_long,0); // temp1->val_flag = 1; // insert(sp,&lsyms); // NextToken(); /* get past label name */ // needpunc( colon ); // snp = (struct snode *)xalloc(sizeof(struct snode)); // snp->stype = st_interrupt; // snp->label = sp->name; // snp->next = 0; // snp->s1 = statement(); // return snp; //} // struct snode *vortex_stmt() { struct snode *snp; SYM *sp; TYP *temp1; NextToken(); if( lastst != id ) { error(ERR_IDEXPECT); return 0; } temp1 = maketype(bt_long,0); temp1->val_flag = 1; sp = allocSYM(); sp->name = litlate(lastid); sp->value.i = nextlabel++; sp->storage_class = sc_global; sp->tp = temp1; insert(sp,&lsyms); NextToken(); /* get past label name */ needpunc( colon ); snp = (struct snode *)xalloc(sizeof(struct snode)); snp->stype = st_vortex; snp->label = sp->name; snp->next = 0; snp->s1 = ParseStatement(); return snp; }
Statement *ParseGotoStatement() { Statement *snp; SYM *sp; NextToken(); if( lastst != id ) { error(ERR_IDEXPECT); return NULL; } snp = NewStatement(st_goto, FALSE); if( (sp = search(lastid,&lsyms)) == NULL ) { sp = allocSYM(); sp->name = litlate(lastid); sp->value.i = nextlabel++; sp->storage_class = sc_ulabel; sp->tp = 0; insert(sp,&lsyms); } NextToken(); /* get past label name */ if( lastst != end ) needpunc( semicolon ); if( sp->storage_class != sc_label && sp->storage_class != sc_ulabel) error( ERR_LABEL ); else { snp->stype = st_goto; snp->label = sp->value.i; snp->next = NULL; return snp; } return NULL; }
void ParseEnumerationList(TABLE *table) { int evalue; SYM *sp; evalue = 0; while(lastst == id) { sp = allocSYM(); sp->SetName(*(new std::string(lastid))); sp->storage_class = sc_const; sp->tp = &stdconst; table->insert(sp); NextToken(); if (lastst==assign) { NextToken(); sp->value.i = GetIntegerExpression((ENODE **)NULL); evalue = (int)sp->value.i+1; } else sp->value.i = evalue++; if( lastst == comma) NextToken(); else if(lastst != end) break; } needpunc(end,48); }
Statement *ParseSwitchStatement() { Statement *snp; Statement *head, *tail; currentFn->UsesPredicate = TRUE; snp = NewStatement(st_switch, TRUE); snp->predreg = iflevel; iflevel++; if( expression(&(snp->exp)) == NULL ) error(ERR_EXPREXPECT); needpunc(begin); head = 0; while( lastst != end ) { if( head == NULL ) head = tail = ParseCaseStatement(); else { tail->next = ParseCaseStatement(); if( tail->next != NULL ) tail = tail->next; } if (tail==NULL) break; // end of file in switch tail->next = NULL; } snp->s1 = head; NextToken(); if( CheckForDuplicateCases(head) ) error(ERR_DUPCASE); iflevel--; return snp; }
Statement *ParseLabelStatement() { Statement *snp; SYM *sp; snp = NewStatement(st_label, FALSE); if( (sp = search(lastid,&lsyms)) == NULL ) { sp = allocSYM(); sp->name = litlate(lastid); sp->storage_class = sc_label; sp->tp = 0; sp->value.i = nextlabel++; insert(sp,&lsyms); } else { if( sp->storage_class != sc_ulabel ) error(ERR_LABEL); else sp->storage_class = sc_label; } NextToken(); /* get past id */ needpunc(colon); if( sp->storage_class == sc_label ) { snp->label = sp->value.i; snp->next = NULL; return snp; } return 0; }
Statement *ParseIfStatement() { Statement *snp; currentFn->UsesPredicate = TRUE; snp = NewStatement(st_if, TRUE); snp->predreg = iflevel; iflevel++; if( lastst != openpa ) error(ERR_EXPREXPECT); else { NextToken(); if( expression(&(snp->exp)) == 0 ) error(ERR_EXPREXPECT); needpunc( closepa ); if (lastst==kw_then) NextToken(); snp->s1 = ParseStatement(); if( lastst == kw_else ) { NextToken(); snp->s2 = ParseStatement(); } else if (lastst == kw_elsif) { snp->s2 = ParseIfStatement(); } else snp->s2 = 0; } iflevel--; return snp; }
Statement *ParseDoStatement() { Statement *snp; currentFn->UsesPredicate = TRUE; snp = NewStatement(st_do, TRUE); snp->predreg = iflevel; iflevel++; snp->s1 = ParseStatement(); if (lastst == kw_until) snp->stype = st_dountil; else if (lastst== kw_loop) snp->stype = st_doloop; if( lastst != kw_while && lastst != kw_until && lastst != kw_loop ) error(ERR_WHILEXPECT); else { NextToken(); if (snp->stype!=st_doloop) { if( expression(&(snp->exp)) == 0 ) error(ERR_EXPREXPECT); } if( lastst != end ) needpunc( semicolon ); } iflevel--; return snp; }
Statement *ParseCaseStatement() { Statement *snp; Statement *head, *tail; int buf[256]; int nn; int *bf; snp = NewStatement(st_case, FALSE); if (lastst == kw_fallthru) // ignore "fallthru" NextToken(); if( lastst == kw_case ) { NextToken(); snp->s2 = 0; nn = 0; do { buf[nn] = GetIntegerExpression((ENODE **)NULL); nn++; if (lastst != comma) break; NextToken(); } while (nn < 256); if (nn==256) error(ERR_TOOMANYCASECONSTANTS); bf = (int *)xalloc(sizeof(int)*(nn+1)); bf[0] = nn; for (; nn > 0; nn--) bf[nn]=buf[nn-1]; snp->label = (int64_t *)bf; } else if( lastst == kw_default) { NextToken(); snp->s2 = (Statement *)1; snp->stype = st_default; } else { error(ERR_NOCASE); return (Statement *)NULL; } needpunc(colon,35); head = (Statement *)NULL; while( lastst != end && lastst != kw_case && lastst != kw_default ) { if( head == NULL ) { head = tail = ParseStatement(); if (head) head->outer = snp; } else { tail->next = ParseStatement(); if( tail->next != NULL ) { tail->next->outer = snp; tail = tail->next; } } tail->next = 0; } snp->s1 = head; return snp; }
void ParseDeclarationPrefix(char isUnion) { TYP *temp1, *temp2, *temp3, *temp4; switch (lastst) { case id: declid = litlate(lastid); if (funcdecl==1) names[nparms++] = declid; NextToken(); if (lastst == colon) { NextToken(); bit_width = GetIntegerExpression(); if (isUnion) bit_offset = 0; else bit_offset = bit_next; if (bit_width < 0 || bit_width > bit_max) { error(ERR_BITFIELD_WIDTH); bit_width = 1; } if (bit_width == 0 || bit_offset + bit_width > bit_max) bit_offset = 0; bit_next = bit_offset + bit_width; break; // no ParseDeclarationSuffix() } ParseDeclarationSuffix(); break; case star: temp1 = maketype(bt_pointer,8); temp1->btp = head; head = temp1; if(tail == NULL) tail = head; NextToken(); ParseDeclarationPrefix(isUnion); break; case openpa: NextToken(); temp1 = head; temp2 = tail; head = tail = NULL; ParseDeclarationPrefix(isUnion); needpunc(closepa); temp3 = head; temp4 = tail; head = temp1; tail = temp2; ParseDeclarationSuffix(); temp4->btp = head; if(temp4->type == bt_pointer && temp4->val_flag != 0 && head != NULL) temp4->size *= head->size; head = temp3; break; default: ParseDeclarationSuffix(); break; } }
Statement *ParseIntoffStatement() { Statement *snp; snp = NewStatement(st_intoff, TRUE); if( lastst != end ) needpunc( semicolon ); return snp; }
Statement *Statement::ParseSwitch() { Statement *snp; Statement *head, *tail; snp = NewStatement(st_switch, TRUE); snp->nkd = false; iflevel++; looplevel++; needpunc(openpa,0); if( expression(&(snp->exp)) == NULL ) error(ERR_EXPREXPECT); if (lastst==semicolon) { NextToken(); if (lastst==kw_naked) { NextToken(); snp->nkd = true; } } needpunc(closepa,0); needpunc(begin,36); head = 0; while( lastst != end ) { if( head == (Statement *)NULL ) { head = tail = ParseCase(); if (head) head->outer = snp; } else { tail->next = ParseCase(); if( tail->next != (Statement *)NULL ) { tail->next->outer = snp; tail = tail->next; } } if (tail==(Statement *)NULL) break; // end of file in switch tail->next = (Statement *)NULL; } snp->s1 = head; NextToken(); if( head->CheckForDuplicateCases() ) error(ERR_DUPCASE); iflevel--; looplevel--; return (snp); }
Statement *ParseStopStatement() { Statement *snp; snp = NewStatement(st_stop, TRUE); if( lastst != end ) needpunc( semicolon ); return snp; }
Statement *ParseContinueStatement() { Statement *snp; snp = NewStatement(st_continue, TRUE); if( lastst != end ) needpunc( semicolon ); return snp; }
Statement *ParseBreakStatement() { Statement *snp; snp = NewStatement(st_break, TRUE); if( lastst != end ) needpunc( semicolon ); return snp; }
Statement *Statement::ParseContinue() { Statement *snp; snp = NewStatement(st_continue, TRUE); if( lastst != end ) needpunc( semicolon,40 ); return (snp); }
Statement *ParseCheckStatement() { Statement *snp; snp = NewStatement(st_check, TRUE); if( expression(&(snp->exp)) == 0 ) error(ERR_EXPREXPECT); needpunc( semicolon,31 ); return snp; }