Browse Source

Finalizing for turning in.

master
Cameron Weinfurt 1 year ago
parent
commit
0298905f55
  1. 1
      .gitignore
  2. 12
      Makefile
  3. 22
      README.txt
  4. 12
      eval.c
  5. 1
      main.c
  6. 8
      parse.c

1
.gitignore

@ -1,5 +1,6 @@
as2-s20.html
in.txt
out.txt_sample
out.txt
*.o
infcalc

12
Makefile

@ -1,19 +1,21 @@
objs = main.o token.o expr.o parse.o infint.o eval.o
CC = clang
dev: FLAGS= -g -D UNIT_TESTS
dev: build
valgrind ./infcalc
CC = gcc
release: FLAGS= -O2
release: clean build
rm *.o
dev: FLAGS= -g -D UNIT_TESTS
dev: build
valgrind ./infcalc
build: $(objs)
$(CC) $(FLAGS) -o infcalc $(objs)
clean:
touch foo.o
touch infcalc
rm *.o infcalc
main.o: main.c expr.h

22
README.txt

@ -0,0 +1,22 @@
Infinity Calculator Implementation by Cameron Weinfurt
Intro:
This calculator implementation supports the required addition, subtraction and
multiplication, but also exponentiation. The calculator will recognize
division, but will ignore the operation. Each line is evaluated as a single
math expression. The executable is called "infcalc".
Building:
The provided makefile is set up to build the infinity calculator using gcc
using -O2 optimizations and cleaning up object files made as an
intermediate. Running make with no rule specified will build the calculator.
To perform a build that runs the unit tests and displays debug information,
run "make dev". As part of the build process for development builds, the
calculator is run within valgrind to ensure that the there are no memory leaks
within the calculator.
Additional testing:
The provided "in.txt" file contains a collection of test cases whose output
is in "out.txt". Run the following command to run the calculator on each test:
$ cat in.txt | /.infcalc

12
eval.c

@ -78,11 +78,12 @@ infint_t *evaluate(expr_t *e) {
return iir;
}
#ifdef UNIT_TESTS
void test_evaluate() {
expr_t *e;
infint_t *ii;
input_str = "-12321";
input_str = "-12321\0";
e = parse(0);
ii = evaluate(e);
assert(ii->size == 3);
@ -93,7 +94,7 @@ void test_evaluate() {
infint_free(ii);
destroy_expr(e);
input_str = "4*5+2*-3-1*6";
input_str = "4*5+2*-3-1*6\0";
e = parse(0);
ii = evaluate(e);
assert(ii->size == 1);
@ -102,7 +103,7 @@ void test_evaluate() {
infint_free(ii);
destroy_expr(e);
input_str = "1000000-200*400";
input_str = "1000000-200*400\0";
e = parse(0);
ii = evaluate(e);
assert(ii->size == 3);
@ -113,7 +114,7 @@ void test_evaluate() {
infint_free(ii);
destroy_expr(e);
input_str = "2^8";
input_str = "2^8\0";
e = parse(0);
ii = evaluate(e);
assert(ii->size == 2);
@ -124,7 +125,7 @@ void test_evaluate() {
destroy_expr(e);
char *output_str;
input_str = "2^128";
input_str = "2^128\0";
e = parse(0);
ii = evaluate(e);
assert(ii->sign == 0);
@ -134,3 +135,4 @@ void test_evaluate() {
infint_free(ii);
destroy_expr(e);
}
#endif

1
main.c

@ -10,6 +10,7 @@ void eval_line(char *line) {
input_str = line;
expr_t *e = parse(0);
if (!e) return;
#ifdef UNIT_TESTS
print_expr_tree(e, 0);
#endif

8
parse.c

@ -80,7 +80,7 @@ expr_t *parse_operation(int opcode) {
fprintf(stderr, "Expected expression, found end of data.\n");
return NULL;
case TOK_ERR:
fprintf(stderr, "Invalid token \"%c\".", *token_str);
fprintf(stderr, "Invalid token \"%c\".\n", *token_str);
return NULL;
case TOK_NUMDEC:
num_expr = parse_decimal_int();
@ -143,7 +143,7 @@ expr_t *parse(char expect_rparen) {
fprintf(stderr, "Expected expression, found end of data.\n");
return NULL;
case TOK_ERR:
fprintf(stderr, "Invalid token \"%c\".", *token_str);
fprintf(stderr, "Invalid token \"%c\".\n", *token_str);
return NULL;
case TOK_NUMDEC:
expr_tree = parse_decimal_int();
@ -194,7 +194,7 @@ expr_t *parse(char expect_rparen) {
return expr_tree;
}
case TOK_ERR:
fprintf(stderr, "Invalid token \"%c\".", *token_str);
fprintf(stderr, "Invalid token \"%c\".\n", *token_str);
destroy_expr(expr_tree);
return NULL;
case TOK_NUMDEC:
@ -274,7 +274,7 @@ expr_t *parse(char expect_rparen) {
#ifdef UNIT_TESTS
void test_expr_and_parse() {
const char *test_input = "15^2*6000+20-400/2";
const char *test_input = "15^2*6000+20-400/2\0";
input_str = test_input;
expr_t *e = parse(0);

Loading…
Cancel
Save