staticboolmake_token(char *e){ int position = 0; int i; regmatch_t pmatch; nr_token = 0;
while(e[position] != '\0') { /* Try all rules one by one. */ for(i = 0; i < NR_REGEX; i ++) { if(regexec(&re[i], e + position, 1, &pmatch, 0) == 0 && pmatch.rm_so == 0) { //char *substr_start = e + position; int substr_len = pmatch.rm_eo; //Log("match rules[%d] = \"%s\" at position %d with len %d: %.*s", i, rules[i].regex, position, substr_len, substr_len, substr_start); position += substr_len;
/* TODO: Now a new token is recognized with rules[i]. Add codes * to record the token in the array `tokens'. For certain types * of tokens, some extra actions should be performed. */
switch(rules[i].token_type) { case NOTYPE: break; //It's blank! case HEX:case DEC:case REG: strncpy(tokens[nr_token].str, e + position - substr_len, substr_len);//regs or number tokens[nr_token].str[substr_len] = '\0'; //add '\0', it's very important //WARNING: 64 may be a little small... default: if(rules[i].token_type == MINUS) { //solve neg if(nr_token == 0) tokens[nr_token++].type = NEG; elseif(PLUS <= tokens[nr_token - 1].type && tokens[nr_token - 1].type <= LB) { tokens[nr_token++].type = NEG; } else tokens[nr_token++].type = MINUS; } elseif(rules[i].token_type == STAR) { //solve pointer if(nr_token == 0) tokens[nr_token++].type = POINTER; elseif(PLUS <= tokens[nr_token - 1].type && tokens[nr_token - 1].type <= LB) { tokens[nr_token++].type = POINTER; } else tokens[nr_token++].type = STAR; } else { tokens[nr_token++].type = rules[i].token_type; //other } break; //panic("please implement me"); } break; } }
if(i == NR_REGEX) { printf("no match at position %d\n%s\n%*.s^\n", position, e, position, ""); returnfalse; } }
uint32_teval(int l, int r, bool *success){ *success = true; if(l > r) return *success = false;// Bad Expression !! if(l == r){ //It's a number or reg, otherwise bad expression uint32_t tmp; if(tokens[l].type == HEX) { sscanf(tokens[l].str, "%x", &tmp); return tmp; } elseif(tokens[l].type == DEC) { sscanf(tokens[l].str, "%d", &tmp); return tmp; } elseif(tokens[l].type == REG) { //read register if(strcmp(tokens[l].str + 1, "eax") == 0) return cpu.eax; if(strcmp(tokens[l].str + 1, "ecx") == 0) return cpu.ecx; if(strcmp(tokens[l].str + 1, "edx") == 0) return cpu.edx; if(strcmp(tokens[l].str + 1, "ebx") == 0) return cpu.ebx; if(strcmp(tokens[l].str + 1, "esp") == 0) return cpu.esp; if(strcmp(tokens[l].str + 1, "ebp") == 0) return cpu.ebp; if(strcmp(tokens[l].str + 1, "esi") == 0) return cpu.esi; if(strcmp(tokens[l].str + 1, "edi") == 0) return cpu.edi; if(strcmp(tokens[l].str + 1, "eip") == 0) return cpu.eip; return *success = false; } return *success = false; } bool flag = check_parentheses(l, r, success); if(!success) return0; //Bad if(flag) return eval(l + 1, r - 1, success);//OK, remove parentheses //Now we should find the dominant token int now = -1, type = 0x3f3f3f3f, cnt = 0, i; for(i = l; i <= r; i++) { if(tokens[i].type == LB) ++cnt; if(tokens[i].type == RB) --cnt; if(cnt != 0) continue; //In mathched parentheses, pass if(PLUS <= tokens[i].type && tokens[i].type <= POINTER) { if(type >= PRE[tokens[i].type]) type = PRE[tokens[i].type], now = i; } } assert(now != -1); uint32_t a, b; //solve '!' if(tokens[now].type >= NOT) { //if type>=not, which means other token has been solved //so the first token must be NOT or NEG or POINTER b = eval(l + 1, r, success); if(!(*success)) return *success = false; if(tokens[l].type == NOT) return !b; if(tokens[l].type == NEG) return -b; if(tokens[l].type == POINTER) return swaddr_read(b, 1); return *success = false; } a = eval(l, now - 1, success); if(!(*success))return *success = false; b = eval(now + 1, r ,success); if(!(*success))return *success = false; if(tokens[now].type == PLUS) return a + b; if(tokens[now].type == STAR) return a * b; if(tokens[now].type == DIV) return a / b; if(tokens[now].type == MINUS) return a - b; if(tokens[now].type == EQ) return a == b; if(tokens[now].type == NOTEQ) return a != b; if(tokens[now].type == AND) return a && b; if(tokens[now].type == OR) return a || b; return0; }
1 2 3 4 5 6 7 8 9 10
uint32_texpr(char *e, bool *success){ if(!make_token(e)) { *success = false; return0; } /* TODO: Insert codes to evaluate the expression. */ //panic("please implement me"); //Calculate the value return eval(0, nr_token - 1, success);//call eval to calculate the value of expression e }