Archived
1
0
Fork 0

Added constructors/destructors for TypeVal and Value

This commit is contained in:
Brandon Rozek 2018-09-28 09:49:58 -04:00
parent bd66b4ad11
commit f84b631124
2 changed files with 22 additions and 32 deletions

View file

@ -4,47 +4,26 @@
#include <iostream> #include <iostream>
#include "../parser/parser.tab.h" #include "../parser/parser.tab.h"
struct Value* make_value(TypeTag type, long num, double dec, struct Node* expr, std::string str) {
/* allocate space */
struct Value* val = new Value();
/* set properties */
val->type = type;
if (type == LONG || type == BOOLEAN) {
val->value.num = num;
} else if (type == DOUBLE){ // Assume DOUBLE
val->value.dec = dec;
} else if (type == STRING) {
val->value.str = str;
} else { // Assume lambda expression
val->value.expr = expr;
}
/* return new variable */
return val;
}
struct Value* make_long(long num) { struct Value* make_long(long num) {
return make_value(LONG, num, 0, nullptr, ""); return new Value(LONG, num, 0, nullptr, "");
} }
struct Value* make_double(double dec) { struct Value* make_double(double dec) {
return make_value(DOUBLE, 0, dec, nullptr, ""); return new Value(DOUBLE, 0, dec, nullptr, "");
} }
struct Value* make_true() { struct Value* make_true() {
return make_value(BOOLEAN, 1, 0, nullptr, ""); return new Value(BOOLEAN, 1, 0, nullptr, "");
} }
struct Value* make_false() { struct Value* make_false() {
return make_value(BOOLEAN, 0, 0, nullptr, ""); return new Value(BOOLEAN, 0, 0, nullptr, "");
} }
struct Value* make_boolean(int x) { struct Value* make_boolean(int x) {
return (x)? make_true() : make_false(); return (x)? make_true() : make_false();
} }
struct Value* make_expression(struct Node* expr) { struct Value* make_expression(struct Node* expr) {
return make_value(LAMBDA, 0, 0, expr, ""); return new Value(LAMBDA, 0, 0, expr, "");
} }
struct Value* make_string(std::string str) { struct Value* make_string(std::string str) {
return make_value(STRING, 0, 0, nullptr, str); return new Value(STRING, 0, 0, nullptr, str);
} }
void delete_value(struct Value* val) { void delete_value(struct Value* val) {

View file

@ -10,19 +10,30 @@ union TypeVal {
double dec; double dec;
struct Node* expr; struct Node* expr;
std::string str; std::string str;
TypeVal() {} TypeVal() { new(&str) std::string(); new(expr) struct Node*; }
// TypeVal(const std::string& s) : str(s) {} // Construct Point object using initializer list. ~TypeVal() { free(&str); free(expr); }
// TypeVal& operator=(const std::string& s) { new(&str) std::string(s); return *this; } // Assign Point object using placement 'new'.
~TypeVal() {}
}; };
struct Value { struct Value {
enum TypeTag type; enum TypeTag type;
TypeVal value; TypeVal value;
// Broken implemenation of constructor below
Value(TypeTag t, long n, double d, struct Node* e, std::string s) {
/* set properties */
type = t;
if (type == LONG || type == BOOLEAN) {
value.num = n;
} else if (type == DOUBLE){ // Assume DOUBLE
value.dec = d;
} else if (type == STRING) {
value.str = s;
} else { // Assume lambda expression
value.expr = e;
}
}
}; };
// Constructors // Constructors
struct Value* make_value(int type, long num, double dec, struct Node* expr, std::string str);
struct Value* make_long(long num); struct Value* make_long(long num);
struct Value* make_double(double dec); struct Value* make_double(double dec);
struct Value* make_true(); struct Value* make_true();