/* MAIN FUNCTION */ int main(int argc, char* argv[]) { char CharSource[MAX_NUM_CHARS_IN_SOURCE] = ""; Token TokSource[MAX_NUM_TOKENS_IN_SOURCE]; int i; if (argc > 1) { for (i = 1; i < argc; ++i) { strcat(CharSource, argv[i]); } } else { printf(">>"); gets(CharSource); printf("the input expression is %s\n", CharSource); } Lexer(CharSource, TokSource); Token* TokBegin = TokSource; printf("%f\n", Eval_Expression(&TokBegin)); return EXIT_SUCCESS; }
/** * Commmand: Evaluate an expression with CPU reg and symbol parsing. */ static int DebugUI_Evaluate(int nArgc, char *psArgs[]) { const char *errstr, *expression = (const char *)psArgs[1]; Uint32 result; int offset; if (nArgc < 2) { return DebugUI_PrintCmdHelp(psArgs[0]); } errstr = Eval_Expression(expression, &result, &offset, false); if (errstr) fprintf(stderr, "ERROR in the expression:\n'%s'\n%*c-%s\n", expression, offset+3, '^', errstr); else DebugUI_PrintValue(result); return DEBUGGER_CMDDONE; }
double Power_Factor(Token** CurToken) { double result; if ( (*CurToken)->type == NUM ) { result = (*CurToken)->value.number; (*CurToken)++; } else if ( (*CurToken)->type == ADDITIVE_OPER ) { switch ( (*CurToken)->value.oper ) { case '+': (*CurToken)++; result = Power_Factor(CurToken); break; case '-': (*CurToken)++; result = -Power_Factor(CurToken); break; } } else if ( (*CurToken)->type == LPAR ) { (*CurToken)++; result = Eval_Expression(CurToken); if ( (*CurToken)->type != RPAR ) { fprintf(stderr, "A RPAR is expected!!\n"); abort(); } (*CurToken)++; } else { fprintf(stderr, "A number or +/- sign is expected!!\n"); abort(); } return result; }
/** * Evaluate everything include within single or double quotes ("" or '') * and replace them with the result. * Caller needs to free the returned string separately. * * Return new string with expressions (potentially) expanded, or * NULL when there's an error in the expression. */ static char *DebugUI_EvaluateExpressions(const char *initial) { int offset, count, diff, inputlen; char *end, *start, *input; const char *errstr; char valuestr[12]; Uint32 value; bool fordsp; /* input is split later on, need to save len here */ input = strdup(initial); if (!input) { perror("ERROR: Input string alloc failed\n"); return NULL; } fordsp = DebugUI_IsForDsp(input); inputlen = strlen(input); start = input; while ((count = strcspn(start, "\"'")) && *(start+count)) { start += count; end = strchr(start+1, *start); if (!end) { fprintf(stderr, "ERROR: matching '%c' missing from '%s'!\n", *start, start); free(input); return NULL; } if (end == start+1) { /* empty expression */ memmove(start, start+2, strlen(start+2)+1); continue; } *end = '\0'; errstr = Eval_Expression(start+1, &value, &offset, fordsp); if (errstr) { *end = *start; /* restore expression mark */ fprintf(stderr, "Expression ERROR:\n'%s'\n%*c-%s\n", input, (int)(start-input)+offset+3, '^', errstr); free(input); return NULL; } end++; count = sprintf(valuestr, "$%x", value); fprintf(stderr, "- '%s' -> %s\n", start+1, valuestr); diff = end-start; if (count < diff) { memcpy(start, valuestr, count); start += count; memmove(start, end, strlen(end) + 1); } else { /* value won't fit to expression, expand string */ char *tmp; inputlen += count-diff+1; tmp = malloc(inputlen+1); if (!tmp) { perror("ERROR: Input string alloc failed\n"); free(input); return NULL; } memcpy(tmp, input, start-input); start = tmp+(start-input); memcpy(start, valuestr, count); start += count; memcpy(start, end, strlen(end) + 1); free(input); input = tmp; } } /* no (more) expressions to evaluate */ return input; }