added guard for empty expressions

This commit is contained in:
Vera Lewis
2025-12-25 20:25:58 -06:00
parent bc87dc3e88
commit f4528e64b9
2 changed files with 16 additions and 5 deletions

View File

@@ -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[])
}
if (expr == NULL)
error |= ERR_NS_OPERANDS;
/* Detect malformed expressions */
if (!error) {
if (expr == NULL)
error |= ERR_NS_OPERANDS;
else if ((*expr).tail != NULL)
error |= ERR_NS_OPERATORS;
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));
}