Compare commits

..

1 Commits

Author SHA1 Message Date
Luciano 38d2c7dc75 add eval flag 2026-03-29 15:49:48 -07:00
2 changed files with 22 additions and 1 deletions

View File

@ -10,6 +10,16 @@ Where `cc` is our C compiler e.g. `gcc`, `clang`, `zig cc`.
## How to Use ## How to Use
### Single Eval
Run the executable with `-e` flag to evaluate a single expression and exit.
```sh
./calc -e "3.14**2"
9.8596
```
### REPL
Run the executable `calc` to be greeted with the following prompt: Run the executable `calc` to be greeted with the following prompt:
`calc>` `calc>`

13
main.c
View File

@ -25,11 +25,22 @@
#define INPUT_MAX 1024 #define INPUT_MAX 1024
int main(void) int main(int argc, char **argv)
{ {
char buf[INPUT_MAX]; char buf[INPUT_MAX];
calc_FormatKind fmt = CALC_FORMAT_DECIMAL; calc_FormatKind fmt = CALC_FORMAT_DECIMAL;
// -e "expr": evaluate a single expression and exit
if (argc == 3 && strcmp(argv[1], "-e") == 0) {
double result = calc_eval(argv[2]);
if (isnan(result)) {
fprintf(stderr, "error: invalid expression\n");
return 1;
}
printf("%s\n", calc_format_result(result, fmt));
return 0;
}
printf("calc> "); printf("calc> ");
fflush(stdout); fflush(stdout);