Infinity Calculator implementation for Assignment 2 in Computer Organization.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

52 lines
1.1 KiB

#include "eval.h"
infint_t *evaluate(expr_t *e) {
infint_t *iil, *iir;
iil = NULL;
if (e->flags & EXFLAG_REXP) {
iir = evaluate(e->right.expr);
} else {
iir = infint_new(2);
iir->data[0] = e->right.data;
}
iir->sign ^= !!(e->flags & EXFLAG_RNEG);
if (e->flags & EXFLAG_LEXP) {
iil = evaluate(e->left.expr);
iil->sign ^= !!(e->flags & EXFLAG_LNEG);
}
switch(e->opcode) {
case EXPR_ADD:
if (e->flags & EXFLAG_MOD) { // Concatination
infint_shift_right(iir, 1);
iir->data[0] = e->left.data;
} else { // Addition and Subtraction
if (iil) {
infint_add2(iir, iil);
} else if (e->flags & EXFLAG_LNEG) {
infint_sub(iir, e->left.data);
} else {
infint_add(iir, e->left.data);
}
}
break;
case EXPR_MULT:
if (e->flags & EXFLAG_MOD) { // Division
// Not fully supported yet. No-op for now.
} else {
if (iil) {
infint_mult2(iir, iil);
} else {
infint_mult(iir, e->left.data);
iir->sign ^= !!(e->flags & EXFLAG_LNEG);
}
}
break;
case EXPR_IPOW:
break;// Not supported at all. No-op for now.
}
if (iil) infint_free(iil);
return iir;
}