From f84b631124f20d41c428135e97d5f6ca64f30fff Mon Sep 17 00:00:00 2001 From: Brandon Rozek Date: Fri, 28 Sep 2018 09:49:58 -0400 Subject: [PATCH] Added constructors/destructors for TypeVal and Value --- src/variables/value.cpp | 33 ++++++--------------------------- src/variables/value.hpp | 21 ++++++++++++++++----- 2 files changed, 22 insertions(+), 32 deletions(-) diff --git a/src/variables/value.cpp b/src/variables/value.cpp index 4fa5651..991cb2d 100644 --- a/src/variables/value.cpp +++ b/src/variables/value.cpp @@ -4,47 +4,26 @@ #include #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) { - return make_value(LONG, num, 0, nullptr, ""); + return new Value(LONG, num, 0, nullptr, ""); } struct Value* make_double(double dec) { - return make_value(DOUBLE, 0, dec, nullptr, ""); + return new Value(DOUBLE, 0, dec, nullptr, ""); } struct Value* make_true() { - return make_value(BOOLEAN, 1, 0, nullptr, ""); + return new Value(BOOLEAN, 1, 0, nullptr, ""); } struct Value* make_false() { - return make_value(BOOLEAN, 0, 0, nullptr, ""); + return new Value(BOOLEAN, 0, 0, nullptr, ""); } struct Value* make_boolean(int x) { return (x)? make_true() : make_false(); } 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) { - return make_value(STRING, 0, 0, nullptr, str); + return new Value(STRING, 0, 0, nullptr, str); } void delete_value(struct Value* val) { diff --git a/src/variables/value.hpp b/src/variables/value.hpp index 00d281d..0aded1b 100644 --- a/src/variables/value.hpp +++ b/src/variables/value.hpp @@ -10,19 +10,30 @@ union TypeVal { double dec; struct Node* expr; std::string str; - TypeVal() {} - // TypeVal(const std::string& s) : str(s) {} // Construct Point object using initializer list. - // TypeVal& operator=(const std::string& s) { new(&str) std::string(s); return *this; } // Assign Point object using placement 'new'. - ~TypeVal() {} + TypeVal() { new(&str) std::string(); new(expr) struct Node*; } + ~TypeVal() { free(&str); free(expr); } }; struct Value { enum TypeTag type; 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 -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_double(double dec); struct Value* make_true();