void factor(){ int l; int m; symTableEntry* symbol; if(currentToken == identsym){ //First looking for a constant that matches symbol = findSymbol(symTable, CONST, tokenVal.string, scope); if(findConst(tokenVal.string, scope, &m)){ genCode(LIT, 0, m); }else if(findVar(tokenVal.string, scope, &l, &m)){ //If that fails, looking for a variable genCode(LOD, l, m); }else{ throwError(UNDEC_ID); } readToken(); }else if(currentToken == numbersym){ genCode(LIT, 0, tokenVal.numeric); readToken(); }else if(currentToken == lparentsym){ readToken(); expression(); if(currentToken != rparentsym) throwError(RPAREN_MISS); else readToken(); } else throwError(PREC_FACTOR_CANNOT_BEGIN_SYM); return; }
struct in_out scanExpression(const Context &context, struct expression &arg, bool ifContext) { struct in_out result; struct expression *temp = &arg; //temp = &arg; while (temp != NULL) { //cout<<"Before expr"<<endl; concat(result, scanAssignmentExpression(context, temp->head, ifContext)); temp = temp->tail; } findConst(result. in); findConst(result. out); arg. dep = result; return result; }
void statement(){ int l; int m; symTableEntry* symbol; int tempLabels[2]; if(currentToken == identsym){ symbol = findSymbol(symTable, VAR, tokenVal.string, scope); if(!findVar(tokenVal.string, scope, &l, &m)){ if(findConst(tokenVal.string, scope, &m)) throwError(CANNOT_ASSIGN_TO_CONST_OR_PROC); else throwError(UNDEC_ID); } readToken(); if(currentToken != becomessym) throwError(ASSIGN_EXPEC); readToken(); expression(); genCode(STO, l, m); } else if(currentToken == syawsym){ readToken(); if(currentToken != identsym) throwError(ID_FOLLOW_SYAW); if(!findFunc(tokenVal.string, scope, &l, &m)) throwError(UNDEC_ID); genCode(CAL, l, m); readToken(); } else if(currentToken == sngaisym){ // 'beginsym' readToken(); statement(); while(currentToken == semicolonsym){ readToken(); statement(); } if(currentToken != fpesym && (currentToken != identsym && currentToken != sngaisym && currentToken != txosym && currentToken != tengkrrsym)) // 'endsym' //throwError(WRONG_SYM_AFTER_STATE); throwError(SEMICOL_OR_RBRACK_EXPEC); else if(currentToken == identsym || currentToken == sngaisym || currentToken == txosym || currentToken == tengkrrsym) throwError(SEMICOL_BW_STATE_MISS); readToken(); } else if(currentToken == txosym){ // 'ifsym' tempLabels[1] = 0; readToken(); condition(); tempLabels[0] = reserveCode(); //Conditional jump will go here if(currentToken != tsakrrsym) // 'thensym' throwError(TSAKRR_EXPEC); readToken(); statement(); if(currentToken == txokefyawsym){ tempLabels[1] = reserveCode(); readToken(); } backPatch(tempLabels[0], JPC, 0, genLabel()); if(tempLabels[1]){ statement(); backPatch(tempLabels[1], JMP, 0, genLabel()); } } else if(currentToken == tengkrrsym){ // 'whilesym' readToken(); tempLabels[0] = genLabel(); //Jump back up to here at the end of the loop condition(); tempLabels[1] = reserveCode(); //Stick the conditional jump here if(currentToken != sisym) // 'dosym' throwError(SI_EXPEC); readToken(); statement(); genCode(JMP, 0, tempLabels[0]); backPatch(tempLabels[1], JPC, 0, genLabel()); } else if(currentToken == misym){ // Input genCode(SIO, 0, 2); readToken(); if(currentToken == identsym){ if(findVar(tokenVal.string, scope, &l, &m)) genCode(STO, l, m); else if(findConst(tokenVal.string, scope, &m) || findFunc(tokenVal.string, scope, &l, &m)) throwError(CANNOT_STORE_IN_CONST_OR_PROC); else throwError(UNDEC_ID); readToken(); } } else if(currentToken == wrrpasym){ // Output readToken(); if(currentToken == identsym){ if(findVar(tokenVal.string, scope, &l, &m)) genCode(LOD, l, m); else if(findConst(tokenVal.string, scope, &m)) genCode(LIT, 0, m); else throwError(UNDEC_ID); readToken(); } genCode(SOI, 0, 1); } else if(currentToken == fpesym){ return; } else{ if(currentToken == periodsym) throwError(RBRACK_EXPEC_AT_END); else throwError(STATEMENT_EXPEC); } return; }