add eval flag
parent
5d94a5595f
commit
0dc59b897c
10
README.md
10
README.md
|
|
@ -10,6 +10,16 @@ Where `cc` is our C compiler e.g. `gcc`, `clang`, `zig cc`.
|
|||
|
||||
## 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:
|
||||
|
||||
`calc>`
|
||||
|
|
|
|||
13
main.c
13
main.c
|
|
@ -25,11 +25,22 @@
|
|||
|
||||
#define INPUT_MAX 1024
|
||||
|
||||
int main(void)
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
char buf[INPUT_MAX];
|
||||
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> ");
|
||||
fflush(stdout);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue