added guard for empty expressions
This commit is contained in:
12
src/ee.c
12
src/ee.c
@@ -11,8 +11,13 @@ main (const int argc, const char* argv[])
|
||||
const char** endPtr = argv+argc,
|
||||
** arg = argv+1 ;
|
||||
|
||||
/* Guard for empty expressions */
|
||||
if (arg>=endPtr) {
|
||||
error |= ERR_NO_EXPR;
|
||||
}
|
||||
|
||||
char* fmt = "%g\n";
|
||||
if (strcmp(*arg,"-f") == 0) {
|
||||
if (!error && strcmp(*arg,"-f") == 0) {
|
||||
fmt ="%f\n";
|
||||
arg+= 1;
|
||||
}
|
||||
@@ -35,11 +40,14 @@ main (const int argc, const char* argv[])
|
||||
|
||||
}
|
||||
|
||||
/* Detect malformed expressions */
|
||||
if (!error) {
|
||||
if (expr == NULL)
|
||||
error |= ERR_NS_OPERANDS;
|
||||
|
||||
else if ((*expr).tail != NULL)
|
||||
error |= ERR_NS_OPERATORS;
|
||||
}
|
||||
|
||||
report(&expr, fmt);
|
||||
|
||||
@@ -202,6 +210,8 @@ report(STACK* expr, char* fmt)
|
||||
else if (error & ERR_DIV_BY_ZERO)
|
||||
printf("Undefined\n");
|
||||
|
||||
else if (error & ERR_NO_EXPR)
|
||||
printf("No expression provided.\n");
|
||||
else
|
||||
printf(fmt, peek(expr));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user