Compare commits
1 Commits
main
...
lremes/eva
| Author | SHA1 | Date |
|---|---|---|
|
|
38d2c7dc75 |
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
|
## 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
13
main.c
|
|
@ -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);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue