Browse Source

MAYA obtained!

master
Cameron Weinfurt 1 year ago
parent
commit
d9e39b70d3
  1. 5
      eval.c
  2. 37
      infint_four_func.c
  3. 57
      infint_util.c
  4. 28
      main.c
  5. 36
      parse.c

5
eval.c

@ -19,8 +19,8 @@ infint_t *evaluate(expr_t *e) {
switch(e->opcode) {
case EXPR_ADD:
if (e->flags & EXFLAG_MOD) { // Concatination
infint_grow_to(iir, iir->size + 1);
iir->data[iir->size - 1] = e->left.data;
infint_shift_right(iir, 1);
iir->data[0] = e->left.data;
} else { // Addition and Subtraction
if (iil) {
infint_add2(iir, iil);
@ -47,5 +47,6 @@ infint_t *evaluate(expr_t *e) {
break;// Not supported at all. No-op for now.
}
if (iil) infint_free(iil);
return iir;
}

37
infint_four_func.c

@ -27,13 +27,14 @@ void infint_sub(infint_t *ii, uint8_t n) {
ii->sign = 1;
} else {
size_t old_size = ii->size;
infint_comp(ii);
infint_add(ii, n);
if (ii->size > old_size)
ii->data[ii->size - 1] = 0;
while (ii->data[ii->size - 1] == 0 && ii->size > 1)
ii->size -= 1;
infint_comp(ii);
infint_t *tmp = infint_new(ii->size);
tmp->size = ii->size;
tmp->data[0] = n;
infint_comp(tmp);
infint_add2(ii,tmp);
infint_free(tmp);
if (ii->size > old_size) ii->data[ii->size - 1] = 0;
while (ii->data[ii->size - 1] == 0 && ii->size > 1) ii->size -= 1;
}
}
@ -96,17 +97,29 @@ void infint_sub2(infint_t *ii1, infint_t *ii2) {
infint_add2(ii1, ii2);
ii1->sign = !ii2->sign;
} else if(infint_abs_less_than2(ii1,ii2)) {
infint_comp(ii1);
if (ii2->size < ii1->size) {
infint_grow_to(ii2,ii1->size);
ii2->size = ii1->size;
}
infint_comp(ii2);
infint_add2(ii1,ii2);
ii1->data[ii1->size - 1] = 0;
infint_comp(ii1);
infint_comp(ii2);
while (ii1->data[ii1->size - 1] == 0 && ii1->size > 1) ii1->size -= 1;
while (ii2->data[ii1->size - 1] == 0 && ii2->size > 1) ii2->size -= 1;
ii1->sign ^= 1;
} else {
infint_comp(ii1);
size_t old_size = ii1->size;
if (ii2->size < ii1->size) {
infint_grow_to(ii2,ii1->size);
ii2->size = ii1->size;
}
infint_comp(ii2);
infint_add2(ii1,ii2);
ii1->data[ii1->size - 1] = 0;
infint_comp(ii2);
if (ii1->size > old_size) ii1->data[ii1->size - 1] = 0;
while (ii1->data[ii1->size - 1] == 0 && ii1->size > 1) ii1->size -= 1;
infint_comp(ii1);
while (ii2->data[ii1->size - 1] == 0 && ii2->size > 1) ii2->size -= 1;
}
}

57
infint_util.c

@ -38,25 +38,32 @@ void infint_free(infint_t *ii) {
}
char infint_grow_to(infint_t *ii, size_t size) {
if (size > ii->capacity) {
size_t new_cap = ii->capacity * 2;
while (size > new_cap) new_cap *= 2;
uint8_t *new_data = realloc(ii->data, new_cap * sizeof(uint8_t));
if (new_data != NULL) {
memset(new_data + ii->capacity, 0, new_cap - ii->capacity);
ii->data = new_data;
ii->capacity = new_cap;
} else {
return -1;
if (ii->size < size) {
if (size > ii->capacity) {
size_t new_cap = ii->capacity * 2;
while (size > new_cap) new_cap *= 2;
uint8_t *new_data = realloc(ii->data, new_cap * sizeof(uint8_t));
if (new_data != NULL) {
memset(new_data + ii->capacity, 0, new_cap - ii->capacity);
ii->data = new_data;
ii->capacity = new_cap;
} else {
return -1;
}
}
ii->size = size;
}
ii->size = size;
return 0;
}
void infint_comp(infint_t *ii) {
for (int i = 0; i < ii->size; ++i)
ii->data[i] = 100 - ii->data[i];
char sign = ii->sign;
for (int i = 0; i < ii->size; ++i) {
ii->data[i] = 99 - ii->data[i];
}
ii->sign = 0;
infint_add(ii,1);
ii->sign = sign;
}
char infint_abs_less_than(infint_t *ii, uint8_t n) {
@ -74,31 +81,29 @@ char infint_abs_less_than2(infint_t *ii1, infint_t *ii2) {
}
void infint_shift_right(infint_t *ii, size_t n) {
size_t old_size = ii->size;
if(n > 0) {
infint_grow_to(ii, ii->size + n);
memmove(ii->data + n, ii->data, ii->size);
infint_grow_to(ii, old_size + n);
memmove(ii->data + n, ii->data, old_size);
memset(ii->data, 0, n);
}
}
char *infint_to_string(infint_t *ii) {
char *str = malloc(ii->size * 2 * sizeof(char) + 1);
size_t si;
char *str = malloc(ii->size * 2 * sizeof(char) + 2);
size_t si = 0;
uint8_t *ii_ptr = ii->data + ii->size - 1;
if (ii->sign) str[si++] = '-';
if (*ii_ptr >= 10) {
str[0] = *ii_ptr / 10 + '0';
str[1] = *ii_ptr % 10 + '0';
si = 2;
str[si++] = *ii_ptr / 10 + '0';
str[si++] = *ii_ptr % 10 + '0';
} else {
str[0] = *ii_ptr + '0';
si = 1;
str[si++] = *ii_ptr + '0';
}
--ii_ptr;
while (ii_ptr >= ii->data) {
str[si] = *ii_ptr / 10 + '0';
++si;
str[si] = *ii_ptr % 10 + '0';
++si;
str[si++] = *ii_ptr / 10 + '0';
str[si++] = *ii_ptr % 10 + '0';
--ii_ptr;
}
str[si] = 0;

28
main.c

@ -16,22 +16,24 @@ int main() {
char *line = NULL;
size_t line_len = 0;
getline(&line,&line_len,stdin);
input_str = line;
expr_t *e = parse(0);
free(line);
while(getline(&line,&line_len,stdin) != -1){
input_str = line;
expr_t *e = parse(0);
free(line);
line = NULL;
#ifdef UNIT_TESTS
print_expr_tree(e, 0);
print_expr_tree(e, 0);
#endif
infint_t *iie = evaluate(e);
char *iistr = infint_to_string(iie);
printf("%s\n", iistr);
infint_t *iie = evaluate(e);
char *iistr = infint_to_string(iie);
printf("%s\n", iistr);
free(iistr);
free(iistr);
infint_free(iie);
destroy_expr(e);
infint_free(iie);
destroy_expr(e);
}
}

36
parse.c

@ -44,36 +44,28 @@ expr_t *parse_hex_int() {
}
expr_t *parse_decimal_int() {
const char *ptr = token_str;
const char *ptr = input_str - 1;
expr_t *num = malloc(sizeof(expr_t));
num->opcode = EXPR_NULL;
num->flags = num->left.data = 0;
num->par = NULL;
num->right.data = *ptr - '0';
ptr++;
if (ptr == input_str) {
num->flags |= EXFLAG_RNEG;
num->right.data *= 10;
return num;
}
num->right.data = num->right.data * 10 + *ptr - '0';
ptr++;
ptr--;
if (ptr < token_str) return num;
num->right.data = num->right.data + 10 * (*ptr - '0');
ptr--;
while(ptr != input_str) {
while(ptr >= token_str) {
expr_t *more_num = malloc(sizeof(expr_t));
more_num->opcode = EXPR_CONCAT;
more_num->flags = EXFLAG_MOD;
more_num->left.data = 0;
more_num->right.data = *ptr - '0';
ptr++;
if (ptr == input_str) {
num->flags |= EXFLAG_RNEG;
num->right.data *= 10;
return bubble_expr(num, more_num);
}
more_num->right.data = more_num->right.data * 10 + *ptr - '0';
ptr++;
ptr--;
if (ptr < token_str) return bubble_expr(num, more_num);
more_num->right.data = more_num->right.data + 10 * (*ptr - '0');
ptr--;
num = bubble_expr(num, more_num);
}
@ -292,14 +284,14 @@ void test_expr_and_parse() {
assert(e->left.expr->left.expr->left.data == 15);
assert(e->left.expr->left.expr->right.data == 2);
assert(e->left.expr->right.expr->flags == 0x30);
assert(e->left.expr->right.expr->left.data == 60);
assert(e->left.expr->right.expr->right.data == 0);
assert(e->left.expr->right.expr->left.data == 0);
assert(e->left.expr->right.expr->right.data == 60);
assert(e->right.expr->flags == 0xA);
assert(e->right.expr->left.data == 20);
assert(e->right.expr->right.expr->flags == 0x21);
assert(e->right.expr->right.expr->left.expr->flags == 0x30);
assert(e->right.expr->right.expr->left.expr->left.data == 40);
assert(e->right.expr->right.expr->left.expr->right.data == 0);
assert(e->right.expr->right.expr->left.expr->left.data == 0);
assert(e->right.expr->right.expr->left.expr->right.data == 4);
assert(e->right.expr->right.expr->right.data == 2);
destroy_expr(e);

Loading…
Cancel
Save