Replaced int type in Value with an enum. Also wrote abstractions for make_value and cleaned up some operational code
This commit is contained in:
parent
832fdc34f6
commit
7664ee1f80
2 changed files with 64 additions and 54 deletions
|
@ -6,9 +6,7 @@
|
|||
#define STATEMENT 200
|
||||
#define MAX_VARIABLES 200
|
||||
|
||||
#define DOUBLE 300
|
||||
#define LONG 301
|
||||
#define BOOLEAN 302
|
||||
enum TypeTag { DOUBLE, LONG, BOOLEAN };
|
||||
|
||||
// Share the line number between files
|
||||
extern int linenum;
|
||||
|
@ -44,12 +42,14 @@ typedef union typeval {
|
|||
} TypeVal;
|
||||
|
||||
struct Value {
|
||||
int type;
|
||||
enum TypeTag type;
|
||||
TypeVal value;
|
||||
};
|
||||
|
||||
// Value functions
|
||||
struct Value* make_value(int type, long num, double dec);
|
||||
struct Value* make_long(long num);
|
||||
struct Value* make_double(double dec);
|
||||
void delete_value(struct Value* val);
|
||||
long get_long(struct Value* val);
|
||||
double get_double(struct Value* val);
|
||||
|
|
110
src/parser.y
110
src/parser.y
|
@ -369,6 +369,15 @@ struct Value* make_value(int type, long num, double dec) {
|
|||
/* return new variable */
|
||||
return val;
|
||||
}
|
||||
|
||||
struct Value* make_long(long num) {
|
||||
return make_value(LONG, num, 0);
|
||||
}
|
||||
|
||||
struct Value* make_double(double dec) {
|
||||
return make_value(DOUBLE, 0, dec);
|
||||
}
|
||||
|
||||
void delete_value(struct Value* val) {
|
||||
free(val);
|
||||
}
|
||||
|
@ -386,6 +395,7 @@ void set_double(struct Value* val, double dec) {
|
|||
val->type = DOUBLE;
|
||||
val->value.dec = dec;
|
||||
}
|
||||
|
||||
struct Value* add(struct Value* x, struct Value* y) {
|
||||
if (!x || !y) { fprintf(stderr, "Error, uninitialized values being used in add.\n"); }
|
||||
|
||||
|
@ -393,13 +403,13 @@ struct Value* add(struct Value* x, struct Value* y) {
|
|||
|
||||
// Destruct all four cases
|
||||
if (x->type == LONG && y->type == LONG) {
|
||||
ans = make_value(LONG, get_long(x) + get_long(y), 0);
|
||||
ans = make_long(get_long(x) + get_long(y));
|
||||
} else if (x->type == LONG && y->type == DOUBLE) {
|
||||
ans = make_value(DOUBLE, 0, get_long(x) + get_double(y));
|
||||
ans = make_double(get_long(x) + get_double(y));
|
||||
} else if (x->type == DOUBLE && y->type == LONG) {
|
||||
ans = make_value(DOUBLE, 0, get_double(x) + get_long(y));
|
||||
ans = make_double(get_double(x) + get_long(y));
|
||||
} else { // Both are DOUBLE
|
||||
ans = make_value(DOUBLE, 0, get_double(x) + get_double(y));
|
||||
ans = make_double(get_double(x) + get_double(y));
|
||||
}
|
||||
|
||||
return ans;
|
||||
|
@ -412,13 +422,13 @@ struct Value* subtract(struct Value* x, struct Value* y) {
|
|||
|
||||
// Destruct all four cases
|
||||
if (x->type == LONG && y->type == LONG) {
|
||||
ans = make_value(LONG, get_long(x) - get_long(y), 0);
|
||||
ans = make_long(get_long(x) - get_long(y));
|
||||
} else if (x->type == LONG && y->type == DOUBLE) {
|
||||
ans = make_value(DOUBLE, 0, get_long(x) - get_double(y));
|
||||
ans = make_double(get_long(x) - get_double(y));
|
||||
} else if (x->type == DOUBLE && y->type == LONG) {
|
||||
ans = make_value(DOUBLE, 0, get_double(x) - get_long(y));
|
||||
ans = make_double(get_double(x) - get_long(y));
|
||||
} else { // Both are DOUBLE
|
||||
ans = make_value(DOUBLE, 0, get_double(x) - get_double(y));
|
||||
ans = make_double(get_double(x) - get_double(y));
|
||||
}
|
||||
|
||||
return ans;
|
||||
|
@ -431,13 +441,13 @@ struct Value* division(struct Value* x, struct Value* y) {
|
|||
|
||||
// Destruct all four cases
|
||||
if (x->type == LONG && y->type == LONG) {
|
||||
ans = make_value(LONG, get_long(x) / get_long(y), 0);
|
||||
ans = make_long(get_long(x) / get_long(y));
|
||||
} else if (x->type == LONG && y->type == DOUBLE) {
|
||||
ans = make_value(DOUBLE, 0, get_long(x) / get_double(y));
|
||||
ans = make_double(get_long(x) / get_double(y));
|
||||
} else if (x->type == DOUBLE && y->type == LONG) {
|
||||
ans = make_value(DOUBLE, 0, get_double(x) / get_long(y));
|
||||
ans = make_double(get_double(x) / get_long(y));
|
||||
} else { // Both are DOUBLE
|
||||
ans = make_value(DOUBLE, 0, get_double(x) / get_double(y));
|
||||
ans = make_double(get_double(x) / get_double(y));
|
||||
}
|
||||
|
||||
return ans;
|
||||
|
@ -450,13 +460,13 @@ struct Value* multiplication(struct Value* x, struct Value* y) {
|
|||
|
||||
// Destruct all four cases
|
||||
if (x->type == LONG && y->type == LONG) {
|
||||
ans = make_value(LONG, get_long(x) * get_long(y), 0);
|
||||
ans = make_long(get_long(x) * get_long(y));
|
||||
} else if (x->type == LONG && y->type == DOUBLE) {
|
||||
ans = make_value(DOUBLE, 0, get_long(x) * get_double(y));
|
||||
ans = make_double(get_long(x) * get_double(y));
|
||||
} else if (x->type == DOUBLE && y->type == LONG) {
|
||||
ans = make_value(DOUBLE, 0, get_double(x) * get_long(y));
|
||||
ans = make_double(get_double(x) * get_long(y));
|
||||
} else { // Both are DOUBLE
|
||||
ans = make_value(DOUBLE, 0, get_double(x) * get_double(y));
|
||||
ans = make_double(get_double(x) * get_double(y));
|
||||
}
|
||||
|
||||
return ans;
|
||||
|
@ -469,13 +479,13 @@ struct Value* less(struct Value* x, struct Value* y) {
|
|||
|
||||
// Destruct all four cases
|
||||
if (x->type == LONG && y->type == LONG) {
|
||||
ans = make_value(LONG, get_long(x) < get_long(y), 0);
|
||||
ans = make_long(get_long(x) < get_long(y));
|
||||
} else if (x->type == LONG && y->type == DOUBLE) {
|
||||
ans = make_value(DOUBLE, 0, get_long(x) < get_double(y));
|
||||
ans = make_double(get_long(x) < get_double(y));
|
||||
} else if (x->type == DOUBLE && y->type == LONG) {
|
||||
ans = make_value(DOUBLE, 0, get_double(x) < get_long(y));
|
||||
ans = make_double(get_double(x) < get_long(y));
|
||||
} else { // Both are DOUBLE
|
||||
ans = make_value(DOUBLE, 0, get_double(x) < get_double(y));
|
||||
ans = make_double(get_double(x) < get_double(y));
|
||||
}
|
||||
|
||||
return ans;
|
||||
|
@ -488,13 +498,13 @@ struct Value* greater(struct Value* x, struct Value* y) {
|
|||
|
||||
// Destruct all four cases
|
||||
if (x->type == LONG && y->type == LONG) {
|
||||
ans = make_value(LONG, get_long(x) > get_long(y), 0);
|
||||
ans = make_long(get_long(x) > get_long(y));
|
||||
} else if (x->type == LONG && y->type == DOUBLE) {
|
||||
ans = make_value(DOUBLE, 0, get_long(x) > get_double(y));
|
||||
ans = make_double(get_long(x) > get_double(y));
|
||||
} else if (x->type == DOUBLE && y->type == LONG) {
|
||||
ans = make_value(DOUBLE, 0, get_double(x) > get_long(y));
|
||||
ans = make_double(get_double(x) > get_long(y));
|
||||
} else { // Both are DOUBLE
|
||||
ans = make_value(DOUBLE, 0, get_double(x) > get_double(y));
|
||||
ans = make_double(get_double(x) > get_double(y));
|
||||
}
|
||||
|
||||
return ans;
|
||||
|
@ -507,13 +517,13 @@ struct Value* less_equal(struct Value* x, struct Value* y) {
|
|||
|
||||
// Destruct all four cases
|
||||
if (x->type == LONG && y->type == LONG) {
|
||||
ans = make_value(LONG, get_long(x) <= get_long(y), 0);
|
||||
ans = make_long(get_long(x) <= get_long(y));
|
||||
} else if (x->type == LONG && y->type == DOUBLE) {
|
||||
ans = make_value(DOUBLE, 0, get_long(x) <= get_double(y));
|
||||
ans = make_double(get_long(x) <= get_double(y));
|
||||
} else if (x->type == DOUBLE && y->type == LONG) {
|
||||
ans = make_value(DOUBLE, 0, get_double(x) <= get_long(y));
|
||||
ans = make_double(get_double(x) <= get_long(y));
|
||||
} else { // Both are DOUBLE
|
||||
ans = make_value(DOUBLE, 0, get_double(x) <= get_double(y));
|
||||
ans = make_double(get_double(x) <= get_double(y));
|
||||
}
|
||||
|
||||
return ans;
|
||||
|
@ -525,13 +535,13 @@ struct Value* greater_equal(struct Value* x, struct Value* y) {
|
|||
|
||||
// Destruct all four cases
|
||||
if (x->type == LONG && y->type == LONG) {
|
||||
ans = make_value(LONG, get_long(x) >= get_long(y), 0);
|
||||
ans = make_long(get_long(x) >= get_long(y));
|
||||
} else if (x->type == LONG && y->type == DOUBLE) {
|
||||
ans = make_value(DOUBLE, 0, get_long(x) >= get_double(y));
|
||||
ans = make_double(get_long(x) >= get_double(y));
|
||||
} else if (x->type == DOUBLE && y->type == LONG) {
|
||||
ans = make_value(DOUBLE, 0, get_double(x) >= get_long(y));
|
||||
ans = make_double(get_double(x) >= get_long(y));
|
||||
} else { // Both are DOUBLE
|
||||
ans = make_value(DOUBLE, 0, get_double(x) >= get_double(y));
|
||||
ans = make_double(get_double(x) >= get_double(y));
|
||||
}
|
||||
|
||||
return ans;
|
||||
|
@ -544,13 +554,13 @@ struct Value* equals(struct Value* x, struct Value* y) {
|
|||
|
||||
// Destruct all four cases
|
||||
if (x->type == LONG && y->type == LONG) {
|
||||
ans = make_value(LONG, get_long(x) == get_long(y), 0);
|
||||
ans = make_long(get_long(x) == get_long(y));
|
||||
} else if (x->type == LONG && y->type == DOUBLE) {
|
||||
ans = make_value(DOUBLE, 0, get_long(x) == get_double(y));
|
||||
ans = make_double(get_long(x) == get_double(y));
|
||||
} else if (x->type == DOUBLE && y->type == LONG) {
|
||||
ans = make_value(DOUBLE, 0, get_double(x) == get_long(y));
|
||||
ans = make_double(get_double(x) == get_long(y));
|
||||
} else { // Both are DOUBLE
|
||||
ans = make_value(DOUBLE, 0, get_double(x) == get_double(y));
|
||||
ans = make_double(get_double(x) == get_double(y));
|
||||
}
|
||||
|
||||
return ans;
|
||||
|
@ -563,13 +573,13 @@ struct Value* not_equals(struct Value* x, struct Value* y) {
|
|||
|
||||
// Destruct all four cases
|
||||
if (x->type == LONG && y->type == LONG) {
|
||||
ans = make_value(LONG, get_long(x) != get_long(y), 0);
|
||||
ans = make_long(get_long(x) != get_long(y));
|
||||
} else if (x->type == LONG && y->type == DOUBLE) {
|
||||
ans = make_value(DOUBLE, 0, get_long(x) != get_double(y));
|
||||
ans = make_double(get_long(x) != get_double(y));
|
||||
} else if (x->type == DOUBLE && y->type == LONG) {
|
||||
ans = make_value(DOUBLE, 0, get_double(x) != get_long(y));
|
||||
ans = make_double(get_double(x) != get_long(y));
|
||||
} else { // Both are DOUBLE
|
||||
ans = make_value(DOUBLE, 0, get_double(x) != get_double(y));
|
||||
ans = make_double(get_double(x) != get_double(y));
|
||||
}
|
||||
|
||||
return ans;
|
||||
|
@ -582,13 +592,13 @@ struct Value* and(struct Value* x, struct Value* y) {
|
|||
|
||||
// Destruct all four cases
|
||||
if (x->type == LONG && y->type == LONG) {
|
||||
ans = make_value(LONG, get_long(x) && get_long(y), 0);
|
||||
ans = make_long(get_long(x) && get_long(y));
|
||||
} else if (x->type == LONG && y->type == DOUBLE) {
|
||||
ans = make_value(DOUBLE, 0, get_long(x) && get_double(y));
|
||||
ans = make_double(get_long(x) && get_double(y));
|
||||
} else if (x->type == DOUBLE && y->type == LONG) {
|
||||
ans = make_value(DOUBLE, 0, get_double(x) && get_long(y));
|
||||
ans = make_double(get_double(x) && get_long(y));
|
||||
} else { // Both are DOUBLE
|
||||
ans = make_value(DOUBLE, 0, get_double(x) && get_double(y));
|
||||
ans = make_double(get_double(x) && get_double(y));
|
||||
}
|
||||
|
||||
return ans;
|
||||
|
@ -601,13 +611,13 @@ struct Value* or(struct Value* x, struct Value* y) {
|
|||
|
||||
// Destruct all four cases
|
||||
if (x->type == LONG && y->type == LONG) {
|
||||
ans = make_value(LONG, get_long(x) || get_long(y), 0);
|
||||
ans = make_long(get_long(x) || get_long(y));
|
||||
} else if (x->type == LONG && y->type == DOUBLE) {
|
||||
ans = make_value(DOUBLE, 0, get_long(x) || get_double(y));
|
||||
ans = make_double(get_long(x) || get_double(y));
|
||||
} else if (x->type == DOUBLE && y->type == LONG) {
|
||||
ans = make_value(DOUBLE, 0, get_double(x) || get_long(y));
|
||||
ans = make_double(get_double(x) || get_long(y));
|
||||
} else { // Both are DOUBLE
|
||||
ans = make_value(DOUBLE, 0, get_double(x) || get_double(y));
|
||||
ans = make_double(get_double(x) || get_double(y));
|
||||
}
|
||||
|
||||
return ans;
|
||||
|
@ -620,9 +630,9 @@ struct Value* not(struct Value* x) {
|
|||
|
||||
// Destruct all two cases
|
||||
if (x->type == LONG) {
|
||||
ans = make_value(LONG, !get_long(x), 0);
|
||||
ans = make_long(!get_long(x));
|
||||
} else { // Assume it's a double
|
||||
ans = make_value(DOUBLE, 0, !get_double(x));
|
||||
ans = make_double(!get_double(x));
|
||||
}
|
||||
|
||||
return ans;
|
||||
|
|
Reference in a new issue