void exec_for() { int cond; char *temp, *temp2; int brace ; get_token(); eval_exp(&cond); if(*token!=';') sntx_err(SEMI_EXPECTED); prog++; temp = prog; for(;;) { eval_exp(&cond); if(*token!=';') sntx_err(SEMI_EXPECTED); prog++; temp2 = prog; brace = 1; while(brace) { get_token(); if(*token=='(') brace++; if(*token==')') brace--; } if(cond) interp_block(); else { find_eob(); return; } prog = temp2; eval_exp(&cond); prog = temp; } }
/* Execute a for loop. */ void exec_for(void) { int cond; char *temp, *temp2; int brace ; get_token(); eval_exp(&cond); /* initialization expression */ if(*token != ';') sntx_err(SEMI_EXPECTED); prog++; /* get past the ; */ temp = prog; for(;;) { eval_exp(&cond); /* check the condition */ if(*token != ';') sntx_err(SEMI_EXPECTED); prog++; /* get past the ; */ temp2 = prog; /* find the start of the for block */ brace = 1; while(brace) { get_token(); if(*token == '(') brace++; if(*token == ')') brace--; } if(cond) interp_block(); /* if true, interpret */ else { /* otherwise, skip around loop */ find_eob(); return; } prog = temp2; eval_exp(&cond); /* do the increment */ prog = temp; /* loop back to top */ } }
/* Interpret a single statement or block of code. When interp_block() returns from its initial call, the final brace (or a return) in main() has been encountered. */ void interp_block(void) { int value; char block = 0; do { token_type = get_token(); /* If interpreting single statement, return on first semicolon. */ /* see what kind of token is up */ if(token_type == IDENTIFIER) { /* Not a keyword, so process expression. */ putback(); /* restore token to input stream for further processing by eval_exp() */ eval_exp(&value); /* process the expression */ if(*token!=';') sntx_err(SEMI_EXPECTED); } else if(token_type==BLOCK) { /* if block delimiter */ if(*token == '{') /* is a block */ block = 1; /* interpreting block, not statement */ else return; /* is a }, so return */ } else /* is keyword */ switch(tok) { case CHAR: case INT: /* declare local variables */ putback(); decl_local(); break; case RETURN: /* return from function call */ func_ret(); return; case IF: /* process an if statement */ exec_if(); break; case ELSE: /* process an else statement */ find_eob(); /* find end of else block and continue execution */ break; case WHILE: /* process a while loop */ exec_while(); break; case DO: /* process a do-while loop */ exec_do(); break; case FOR: /* process a for loop */ exec_for(); break; case END: exit(0); } } while (tok != FINISHED && block); }
void jpeg_encode_block(short *in, short *last_block, uint8_t **out, int *out_length, int quality) { const int bwidth = 8; const int bheight = 8; short zz[64]; dc_shift_short(in, bwidth, bheight, -128); dct_8x8(in); quant_8x8(in, quality); zigzag_8x8(in, zz); int zz_len = find_eob(zz); huffman_enc_64(zz, zz_len, out, out_length, last_block); }
/* Execute a while loop. */ void exec_while(void) { int cond; char *temp; putback(); temp = prog; /* save location of top of while loop */ get_token(); eval_exp(&cond); /* check the conditional expression */ if(cond) interp_block(); /* if true, interpret */ else { /* otherwise, skip around loop */ find_eob(); return; } prog = temp; /* loop back to top */ }
void exec_while() { int cond; char *temp; putback(); temp = prog; get_token(); eval_exp(&cond); if(cond) interp_block(); else { find_eob(); return; } prog = temp; }
void exec_if() { int cond; eval_exp(&cond); if(cond) { interp_block(); } else { find_eob(); get_token(); if(tok!=ELSE) { putback(); return; } interp_block(); } }
void interp_block() { int value; char block = 0; do { token_type = get_token(); if(token_type==IDENTIFIER) { putback(); eval_exp(&value); if(*token!=';') sntx_err(SEMI_EXPECTED); } else if(token_type==BLOCK) { if(*token=='{') block = 1; else return; } else switch(tok) { case CHAR: case INT: putback(); decl_local(); break; case RETURN: func_ret(); return; case IF: exec_if(); break; case ELSE: find_eob(); break; case WHILE: exec_while(); break; case DO: exec_do(); break; case FOR: exec_for(); break; case END: return; } } while (tok != FINISHED && block); }
/* Execute an if statement. */ void exec_if(void) { int cond; eval_exp(&cond); /* get if expression */ if(cond) { /* is true so process target of IF */ interp_block(); } else { /* otherwise skip around IF block and process the ELSE, if present */ find_eob(); /* find start of next line */ get_token(); if(tok != ELSE) { putback(); /* restore token if no ELSE is present */ return; } interp_block(); } }