Archived
1
0
Fork 0

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:
Brandon Rozek 2018-09-20 22:11:33 -04:00
parent 832fdc34f6
commit 7664ee1f80
2 changed files with 64 additions and 54 deletions

View file

@ -6,9 +6,7 @@
#define STATEMENT 200 #define STATEMENT 200
#define MAX_VARIABLES 200 #define MAX_VARIABLES 200
#define DOUBLE 300 enum TypeTag { DOUBLE, LONG, BOOLEAN };
#define LONG 301
#define BOOLEAN 302
// Share the line number between files // Share the line number between files
extern int linenum; extern int linenum;
@ -44,12 +42,14 @@ typedef union typeval {
} TypeVal; } TypeVal;
struct Value { struct Value {
int type; enum TypeTag type;
TypeVal value; TypeVal value;
}; };
// Value functions // Value functions
struct Value* make_value(int type, long num, double dec); 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); void delete_value(struct Value* val);
long get_long(struct Value* val); long get_long(struct Value* val);
double get_double(struct Value* val); double get_double(struct Value* val);

View file

@ -369,6 +369,15 @@ struct Value* make_value(int type, long num, double dec) {
/* return new variable */ /* return new variable */
return val; 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) { void delete_value(struct Value* val) {
free(val); free(val);
} }
@ -386,6 +395,7 @@ void set_double(struct Value* val, double dec) {
val->type = DOUBLE; val->type = DOUBLE;
val->value.dec = dec; val->value.dec = dec;
} }
struct Value* add(struct Value* x, struct Value* y) { struct Value* add(struct Value* x, struct Value* y) {
if (!x || !y) { fprintf(stderr, "Error, uninitialized values being used in add.\n"); } 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 // Destruct all four cases
if (x->type == LONG && y->type == LONG) { 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) { } 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) { } 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 } 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; return ans;
@ -412,13 +422,13 @@ struct Value* subtract(struct Value* x, struct Value* y) {
// Destruct all four cases // Destruct all four cases
if (x->type == LONG && y->type == LONG) { 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) { } 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) { } 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 } 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; return ans;
@ -431,13 +441,13 @@ struct Value* division(struct Value* x, struct Value* y) {
// Destruct all four cases // Destruct all four cases
if (x->type == LONG && y->type == LONG) { 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) { } 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) { } 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 } 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; return ans;
@ -450,13 +460,13 @@ struct Value* multiplication(struct Value* x, struct Value* y) {
// Destruct all four cases // Destruct all four cases
if (x->type == LONG && y->type == LONG) { 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) { } 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) { } 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 } 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; return ans;
@ -469,13 +479,13 @@ struct Value* less(struct Value* x, struct Value* y) {
// Destruct all four cases // Destruct all four cases
if (x->type == LONG && y->type == LONG) { 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) { } 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) { } 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 } 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; return ans;
@ -488,13 +498,13 @@ struct Value* greater(struct Value* x, struct Value* y) {
// Destruct all four cases // Destruct all four cases
if (x->type == LONG && y->type == LONG) { 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) { } 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) { } 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 } 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; return ans;
@ -507,13 +517,13 @@ struct Value* less_equal(struct Value* x, struct Value* y) {
// Destruct all four cases // Destruct all four cases
if (x->type == LONG && y->type == LONG) { 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) { } 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) { } 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 } 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; return ans;
@ -525,13 +535,13 @@ struct Value* greater_equal(struct Value* x, struct Value* y) {
// Destruct all four cases // Destruct all four cases
if (x->type == LONG && y->type == LONG) { 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) { } 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) { } 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 } 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; return ans;
@ -544,13 +554,13 @@ struct Value* equals(struct Value* x, struct Value* y) {
// Destruct all four cases // Destruct all four cases
if (x->type == LONG && y->type == LONG) { 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) { } 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) { } 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 } 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; return ans;
@ -563,13 +573,13 @@ struct Value* not_equals(struct Value* x, struct Value* y) {
// Destruct all four cases // Destruct all four cases
if (x->type == LONG && y->type == LONG) { 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) { } 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) { } 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 } 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; return ans;
@ -582,13 +592,13 @@ struct Value* and(struct Value* x, struct Value* y) {
// Destruct all four cases // Destruct all four cases
if (x->type == LONG && y->type == LONG) { 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) { } 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) { } 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 } 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; return ans;
@ -601,13 +611,13 @@ struct Value* or(struct Value* x, struct Value* y) {
// Destruct all four cases // Destruct all four cases
if (x->type == LONG && y->type == LONG) { 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) { } 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) { } 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 } 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; return ans;
@ -620,9 +630,9 @@ struct Value* not(struct Value* x) {
// Destruct all two cases // Destruct all two cases
if (x->type == LONG) { if (x->type == LONG) {
ans = make_value(LONG, !get_long(x), 0); ans = make_long(!get_long(x));
} else { // Assume it's a double } else { // Assume it's a double
ans = make_value(DOUBLE, 0, !get_double(x)); ans = make_double(!get_double(x));
} }
return ans; return ans;