Browse Source

Implemented exponentiation.

master
Cameron Weinfurt 1 year ago
parent
commit
9366669030
  1. 86
      eval.c
  2. 4
      eval.h
  3. 38
      main.c

86
eval.c

@ -44,9 +44,93 @@ infint_t *evaluate(expr_t *e) {
}
break;
case EXPR_IPOW:
break;// Not supported at all. No-op for now.
if (!iil) {
iil = infint_new(2);
iil->data[0] = e->left.data;
}
if (iir->size == 1 && iir->data[0] == 0) {
memset(iil->data + 1, 0, iil->size - 1);
iil->data[0]= 1;
iil->size = 1;
} else if (iir->sign) {
memset(iil->data, 0, iil->size);
iil->size = 1;
} else if (iil->size == 1 && (iil->data[0] < 2)) {
iil->sign ^= iir->data[0] & 1;
} else if (!infint_abs_less_than(iir, 2)) {
iil->sign ^= iir->data[0] & 1;
infint_t *acci = infint_new(1);
acci->data[0] = 1;
while (iir->size > 1 || iir->data[0] > 0) {
if (infint_div(iir, 2)) infint_mult2(acci, iil);
infint_mult2(iil, iil);
}
free(iir->data);
iir->data = acci->data;
iir->size = acci->size;
iir->capacity = acci->capacity;
free(acci);
}
}
if (iil) infint_free(iil);
return iir;
}
void test_evaluate() {
expr_t *e;
infint_t *ii;
input_str = "-12321";
e = parse(0);
ii = evaluate(e);
assert(ii->size == 3);
assert(ii->sign == 1);
assert(ii->data[0] == 21);
assert(ii->data[1] == 23);
assert(ii->data[2] == 1);
infint_free(ii);
destroy_expr(e);
input_str = "4*5+2*-3-1*6";
e = parse(0);
ii = evaluate(e);
assert(ii->size == 1);
assert(ii->sign == 0);
assert(ii->data[0] == 8);
infint_free(ii);
destroy_expr(e);
input_str = "1000000-200*400";
e = parse(0);
ii = evaluate(e);
assert(ii->size == 3);
assert(ii->sign == 0);
assert(ii->data[0] == 0);
assert(ii->data[1] == 0);
assert(ii->data[2] == 92);
infint_free(ii);
destroy_expr(e);
input_str = "2^8";
e = parse(0);
ii = evaluate(e);
assert(ii->size == 2);
assert(ii->sign == 0);
assert(ii->data[0] == 56);
assert(ii->data[1] == 2);
infint_free(ii);
destroy_expr(e);
char *output_str;
input_str = "2^128";
e = parse(0);
ii = evaluate(e);
assert(ii->sign == 0);
output_str = infint_to_string(ii);
assert(strcmp(output_str, "340282366920938463463374607431768211456") == 0);
free(output_str);
infint_free(ii);
destroy_expr(e);
}

4
eval.h

@ -9,7 +9,9 @@ infint_t *evaluate(expr_t *e);
#ifdef UNIT_TESTS
#include <assert.h>
//void test_evaluate();
#include "parse.h"
void test_evaluate();
#endif

38
main.c

@ -6,34 +6,40 @@
#include "parse.h"
#include "eval.h"
void eval_line(char *line) {
input_str = line;
expr_t *e = parse(0);
#ifdef UNIT_TESTS
print_expr_tree(e, 0);
#endif
infint_t *iie = evaluate(e);
char *iistr = infint_to_string(iie);
printf("%s\n", iistr);
free(iistr);
infint_free(iie);
destroy_expr(e);
}
int main() {
#ifdef UNIT_TESTS
test_infint();
test_expr_and_parse();
test_evaluate();
printf("**********************\n| All tests passed |\n**********************\n");
#endif
char *line = NULL;
size_t line_len = 0;
while(getline(&line,&line_len,stdin) != -1){
input_str = line;
expr_t *e = parse(0);
if (*line != '\n') {
eval_line(line);
}
free(line);
line = NULL;
#ifdef UNIT_TESTS
print_expr_tree(e, 0);
#endif
infint_t *iie = evaluate(e);
char *iistr = infint_to_string(iie);
printf("%s\n", iistr);
free(iistr);
infint_free(iie);
destroy_expr(e);
}
free(line);
}
Loading…
Cancel
Save