Archived
1
0
Fork 0

Fixed up the compiler warning of destructor and reuse one of the Value*'s during operations to conserve memory.

This commit is contained in:
Brandon Rozek 2018-10-01 13:22:33 -04:00
parent efc49584ee
commit 034a97e3ab
6 changed files with 93 additions and 96 deletions

View file

@ -114,7 +114,9 @@ Value* eval_expression(Node* node, Environment* env) {
case LAMBDATAG: return make_expression(node); break;
case CALLFUNC:
check_num_nodes(node, 2, "cannot have more than two nodes for a function call.");
tempNode = get_expression(get_value(find_variable(env, node->children[0]->id)));
tempVal = get_value(find_variable(env, node->children[0]->id));
tempNode = get_expression(tempVal);
delete tempVal;
local_env = new Environment();
add_variable(local_env,
new Variable(tempNode->children[0]->id, // Get the name of the variable needed for the lambda expression
@ -214,12 +216,10 @@ Value* eval_expression(Node* node, Environment* env) {
std::cerr << "Error: Symbol " << node->id << " not found." << std::endl;
return 0;
}
// Change to return copy of value [TODO]
return get_value(var);
break;
//----------
case VALUE:
// Change to return copy of value [TODO]
return new Value(*node->value);
break;
//----------

View file

@ -3,16 +3,15 @@
#include "operators.hpp"
#include "../variables/value.hpp"
// TODO: Now replace every single operation with vector equivalent
Value* add(Value* x, Value* y) {
// NOTE: Value* x is what is going to be returned for operations, so that we avoid the need for allocating more memory
Value* add(Value* x,Value* y) {
if (!x || !y) { std::cerr << "Error, uninitialized values being used in add." << std::endl; }
if (x->type == BOOLEAN || y->type == BOOLEAN) { std::cerr << "Error, cannot add a boolean." << std::endl; }
if ((x->type == STRING || y->type == STRING) && (x->type != STRING || y->type != STRING)) {
std::cerr << "Error, cannot add a string with another data type." << std::endl;
}
Value* ans;
std::vector<long> longResult;
std::vector<double> doubleResult;
@ -23,36 +22,35 @@ Value* add(Value* x, Value* y) {
longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_long.begin(),
std::back_inserter(longResult), std::plus<long>());
ans = make_long(longResult);
set_long(x, longResult);
} else if (x->type == LONG && y->type == DOUBLE) {
std::vector<long> x_long = get_long(x);
std::vector<double> y_double = get_double(y);
doubleResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_double.begin(),
std::back_inserter(doubleResult), std::plus<>());
ans = make_double(doubleResult);
set_double(x, doubleResult);
} else if (x->type == DOUBLE && y->type == LONG) {
std::vector<double> x_double = get_double(x);
std::vector<long> y_long = get_long(y);
doubleResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_long.begin(),
std::back_inserter(doubleResult), std::plus<>());
ans = make_double(doubleResult);
set_double(x, doubleResult);
} else if (x->type == STRING && y->type == STRING) {
ans = make_string(get_string(x) + get_string(y));
set_string(x, get_string(x) + get_string(y));
} else { // Both are DOUBLE
std::vector<double> x_double = get_double(x);
std::vector<double> y_double = get_double(y);
doubleResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_double.begin(),
std::back_inserter(doubleResult), std::plus<double>());
ans = make_double(doubleResult);
set_double(x, doubleResult);
}
delete x;
delete y;
return ans;
return x;
}
Value* subtract(Value* x, Value* y) {
@ -60,7 +58,6 @@ Value* subtract(Value* x, Value* y) {
if (x->type == BOOLEAN || y->type == BOOLEAN) { std::cerr << "Error, cannot subtract a boolean." << std::endl; }
if (x->type == STRING || y->type == STRING) { std::cerr << "Error, cannot subtract a string." << std::endl; }
Value* ans;
std::vector<long> longResult;
std::vector<double> doubleResult;
@ -71,34 +68,33 @@ Value* subtract(Value* x, Value* y) {
longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_long.begin(),
std::back_inserter(longResult), std::minus<long>());
ans = make_long(longResult);
set_long(x, longResult);
} else if (x->type == LONG && y->type == DOUBLE) {
std::vector<long> x_long = get_long(x);
std::vector<double> y_double = get_double(y);
doubleResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_double.begin(),
std::back_inserter(doubleResult), std::minus<>());
ans = make_double(doubleResult);
set_double(x, doubleResult);
} else if (x->type == DOUBLE && y->type == LONG) {
std::vector<double> x_double = get_double(x);
std::vector<long> y_long = get_long(y);
doubleResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_long.begin(),
std::back_inserter(doubleResult), std::minus<>());
ans = make_double(doubleResult);
set_double(x, doubleResult);
} else { // Both are DOUBLE
std::vector<double> x_double = get_double(x);
std::vector<double> y_double = get_double(y);
doubleResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_double.begin(),
std::back_inserter(doubleResult), std::minus<double>());
ans = make_double(doubleResult);
set_double(x, doubleResult);
}
delete x;
delete y;
return ans;
return x;
}
Value* division(Value* x, Value* y) {
@ -106,7 +102,6 @@ Value* division(Value* x, Value* y) {
if (x->type == BOOLEAN || y->type == BOOLEAN) { std::cerr << "Error, cannot divide a boolean." << std::endl; }
if (x->type == STRING || y->type == STRING) { std::cerr << "Error, cannot division a string." << std::endl; }
Value* ans;
std::vector<long> longResult;
std::vector<double> doubleResult;
@ -117,34 +112,33 @@ Value* division(Value* x, Value* y) {
longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_long.begin(),
std::back_inserter(longResult), std::divides<long>());
ans = make_long(longResult);
set_long(x, longResult);
} else if (x->type == LONG && y->type == DOUBLE) {
std::vector<long> x_long = get_long(x);
std::vector<double> y_double = get_double(y);
doubleResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_double.begin(),
std::back_inserter(doubleResult), std::divides<>());
ans = make_double(doubleResult);
set_double(x, doubleResult);
} else if (x->type == DOUBLE && y->type == LONG) {
std::vector<double> x_double = get_double(x);
std::vector<long> y_long = get_long(y);
doubleResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_long.begin(),
std::back_inserter(doubleResult), std::divides<>());
ans = make_double(doubleResult);
set_double(x, doubleResult);
} else { // Both are DOUBLE
std::vector<double> x_double = get_double(x);
std::vector<double> y_double = get_double(y);
doubleResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_double.begin(),
std::back_inserter(doubleResult), std::divides<double>());
ans = make_double(doubleResult);
set_double(x, doubleResult);
}
delete x;
delete y;
return ans;
return x;
}
Value* multiplication(Value* x, Value* y) {
@ -152,7 +146,6 @@ Value* multiplication(Value* x, Value* y) {
if (x->type == BOOLEAN || y->type == BOOLEAN) { std::cerr << "Error, cannot multiply a boolean." << std::endl; }
if (x->type == STRING || y->type == STRING) { std::cerr << "Error, cannot multiply a string." << std::endl; }
Value* ans;
std::vector<long> longResult;
std::vector<double> doubleResult;
@ -163,34 +156,33 @@ Value* multiplication(Value* x, Value* y) {
longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_long.begin(),
std::back_inserter(longResult), std::multiplies<long>());
ans = make_long(longResult);
set_long(x, longResult);
} else if (x->type == LONG && y->type == DOUBLE) {
std::vector<long> x_long = get_long(x);
std::vector<double> y_double = get_double(y);
doubleResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_double.begin(),
std::back_inserter(doubleResult), std::multiplies<>());
ans = make_double(doubleResult);
set_double(x, doubleResult);
} else if (x->type == DOUBLE && y->type == LONG) {
std::vector<double> x_double = get_double(x);
std::vector<long> y_long = get_long(y);
doubleResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_long.begin(),
std::back_inserter(doubleResult), std::multiplies<>());
ans = make_double(doubleResult);
set_double(x, doubleResult);
} else { // Both are DOUBLE
std::vector<double> x_double = get_double(x);
std::vector<double> y_double = get_double(y);
doubleResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_double.begin(),
std::back_inserter(doubleResult), std::multiplies<double>());
ans = make_double(doubleResult);
set_double(x, doubleResult);
}
delete x;
delete y;
return ans;
return x;
}
Value* less(Value* x, Value* y) {
@ -200,7 +192,6 @@ Value* less(Value* x, Value* y) {
std::cerr << "Error, cannot compare a string with another data type." << std::endl;
}
Value* ans;
std::vector<long> longResult;
// Destruct all four cases
@ -210,38 +201,39 @@ Value* less(Value* x, Value* y) {
longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_long.begin(),
std::back_inserter(longResult), std::less<long>());
ans = make_booleans(longResult);
set_booleans(x, longResult);
} else if (x->type == LONG && y->type == DOUBLE) {
std::vector<long> x_long = get_long(x);
std::vector<double> y_double = get_double(y);
longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_double.begin(),
std::back_inserter(longResult), std::less<>());
ans = make_booleans(longResult);
set_booleans(x, longResult);
} else if (x->type == DOUBLE && y->type == LONG) {
std::vector<double> x_double = get_double(x);
std::vector<long> y_long = get_long(y);
longResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_long.begin(),
std::back_inserter(longResult), std::less<>());
ans = make_booleans(longResult);
set_booleans(x, longResult);
} else if (x->type == STRING && y->type == STRING) {
ans = make_boolean(get_string(x).compare(get_string(y)) < 0);
set_boolean(x, get_string(x).compare(get_string(y)) < 0);
} else { // Both are DOUBLE
std::vector<double> x_double = get_double(x);
std::vector<double> y_double = get_double(y);
longResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_double.begin(),
std::back_inserter(longResult), std::less<double>());
ans = make_booleans(longResult);
set_booleans(x, longResult);
}
delete x;
delete y;
return ans;
return x;
}
// CONTINUE REPLACING FROM HERE
Value* greater(Value* x, Value* y) {
if (!x || !y) { std::cerr << "Error, uninitialized values being used in greater." << std::endl; }
if (x->type == BOOLEAN || y->type == BOOLEAN) { std::cerr << "Error, cannot numerically compare a boolean." << std::endl; }
@ -249,7 +241,6 @@ Value* greater(Value* x, Value* y) {
std::cerr << "Error, cannot compare a string with another data type." << std::endl;
}
Value* ans;
std::vector<long> longResult;
// Destruct all four cases
@ -259,36 +250,35 @@ Value* greater(Value* x, Value* y) {
longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_long.begin(),
std::back_inserter(longResult), std::greater<long>());
ans = make_booleans(longResult);
set_booleans(x, longResult);
} else if (x->type == LONG && y->type == DOUBLE) {
std::vector<long> x_long = get_long(x);
std::vector<double> y_double = get_double(y);
longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_double.begin(),
std::back_inserter(longResult), std::greater<>());
ans = make_booleans(longResult);
set_booleans(x, longResult);
} else if (x->type == DOUBLE && y->type == LONG) {
std::vector<double> x_double = get_double(x);
std::vector<long> y_long = get_long(y);
longResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_long.begin(),
std::back_inserter(longResult), std::greater<>());
ans = make_booleans(longResult);
} else if (x->type == STRING && y->type == STRING) {
ans = make_boolean(get_string(x).compare(get_string(y)) > 0);
set_booleans(x, longResult);
} else if (x->type == STRING && y->type == STRING) {
set_boolean(x, get_string(x).compare(get_string(y)) > 0);
} else { // Both are DOUBLE
std::vector<double> x_double = get_double(x);
std::vector<double> y_double = get_double(y);
longResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_double.begin(),
std::back_inserter(longResult), std::greater<double>());
ans = make_booleans(longResult);
set_booleans(x, longResult);
}
delete x;
delete y;
return ans;
return x;
}
Value* less_equal(Value* x, Value* y) {
@ -298,7 +288,6 @@ Value* less_equal(Value* x, Value* y) {
std::cerr << "Error, cannot compare a string with another data type." << std::endl;
}
Value* ans;
std::vector<long> longResult;
// Destruct all four cases
@ -308,36 +297,35 @@ Value* less_equal(Value* x, Value* y) {
longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_long.begin(),
std::back_inserter(longResult), std::less_equal<long>());
ans = make_booleans(longResult);
set_long(x, longResult);
} else if (x->type == LONG && y->type == DOUBLE) {
std::vector<long> x_long = get_long(x);
std::vector<double> y_double = get_double(y);
longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_double.begin(),
std::back_inserter(longResult), std::less_equal<>());
ans = make_booleans(longResult);
set_booleans(x, longResult);
} else if (x->type == DOUBLE && y->type == LONG) {
std::vector<double> x_double = get_double(x);
std::vector<long> y_long = get_long(y);
longResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_long.begin(),
std::back_inserter(longResult), std::less_equal<>());
ans = make_booleans(longResult);
set_booleans(x, longResult);
} else if (x->type == STRING && y->type == STRING) {
ans = make_boolean(get_string(x).compare(get_string(y)) <= 0);
set_boolean(x, get_string(x).compare(get_string(y)) <= 0);
} else { // Both are DOUBLE
std::vector<double> x_double = get_double(x);
std::vector<double> y_double = get_double(y);
longResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_double.begin(),
std::back_inserter(longResult), std::less_equal<double>());
ans = make_booleans(longResult);
set_booleans(x, longResult);
}
delete x;
delete y;
return ans;
return x;
}
Value* greater_equal(Value* x, Value* y) {
@ -347,7 +335,6 @@ Value* greater_equal(Value* x, Value* y) {
std::cerr << "Error, cannot compare a string with another data type." << std::endl;
}
Value* ans;
std::vector<long> longResult;
// Destruct all four cases
@ -357,36 +344,35 @@ Value* greater_equal(Value* x, Value* y) {
longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_long.begin(),
std::back_inserter(longResult), std::greater_equal<long>());
ans = make_booleans(longResult);
set_booleans(x, longResult);
} else if (x->type == LONG && y->type == DOUBLE) {
std::vector<long> x_long = get_long(x);
std::vector<double> y_double = get_double(y);
longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_double.begin(),
std::back_inserter(longResult), std::greater_equal<>());
ans = make_booleans(longResult);
set_booleans(x, longResult);
} else if (x->type == DOUBLE && y->type == LONG) {
std::vector<double> x_double = get_double(x);
std::vector<long> y_long = get_long(y);
longResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_long.begin(),
std::back_inserter(longResult), std::greater_equal<>());
ans = make_booleans(longResult);
set_booleans(x, longResult);
} else if (x->type == STRING && y->type == STRING) {
ans = make_boolean(get_string(x).compare(get_string(y)) >= 0);
set_boolean(x, get_string(x).compare(get_string(y)) >= 0);
} else { // Both are DOUBLE
std::vector<double> x_double = get_double(x);
std::vector<double> y_double = get_double(y);
longResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_double.begin(),
std::back_inserter(longResult), std::greater_equal<double>());
ans = make_booleans(longResult);
set_booleans(x, longResult);
}
delete x;
delete y;
return ans;
return x;
}
Value* equals(Value* x, Value* y) {
@ -395,7 +381,6 @@ Value* equals(Value* x, Value* y) {
std::cerr << "Error, cannot compare a string with another data type." << std::endl;
}
Value* ans = nullptr;
std::vector<long> longResult;
// Destruct all four cases
@ -405,36 +390,35 @@ Value* equals(Value* x, Value* y) {
longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_long.begin(),
std::back_inserter(longResult), std::equal_to<long>());
ans = make_booleans(longResult);
set_booleans(x, longResult);
} else if (x->type == LONG && y->type == DOUBLE) {
std::vector<long> x_long = get_long(x);
std::vector<double> y_double = get_double(y);
longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_double.begin(),
std::back_inserter(longResult), std::equal_to<>());
ans = make_booleans(longResult);
set_booleans(x, longResult);
} else if (x->type == DOUBLE && y->type == LONG) {
std::vector<double> x_double = get_double(x);
std::vector<long> y_long = get_long(y);
longResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_long.begin(),
std::back_inserter(longResult), std::equal_to<>());
ans = make_booleans(longResult);
set_booleans(x, longResult);
} else if (x->type == STRING && y->type == STRING) {
ans = make_boolean(get_string(x).compare(get_string(y)) == 0);
set_boolean(x, get_string(x).compare(get_string(y)) == 0);
} else { // Both are DOUBLE
std::vector<double> x_double = get_double(x);
std::vector<double> y_double = get_double(y);
longResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_double.begin(),
std::back_inserter(longResult), std::equal_to<double>());
ans = make_booleans(longResult);
set_booleans(x, longResult);
}
delete x;
delete y;
return ans;
return x;
}
Value* not_equals(Value* x, Value* y) {
@ -443,7 +427,6 @@ Value* not_equals(Value* x, Value* y) {
std::cerr << "Error, cannot compare a string with another data type." << std::endl;
}
Value* ans= nullptr;
std::vector<long> longResult;
// Destruct all four cases
@ -453,36 +436,35 @@ Value* not_equals(Value* x, Value* y) {
longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_long.begin(),
std::back_inserter(longResult), std::not_equal_to<long>());
ans = make_booleans(longResult);
set_booleans(x, longResult);
} else if (x->type == LONG && y->type == DOUBLE) {
std::vector<long> x_long = get_long(x);
std::vector<double> y_double = get_double(y);
longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_double.begin(),
std::back_inserter(longResult), std::not_equal_to<>());
ans = make_booleans(longResult);
set_booleans(x, longResult);
} else if (x->type == DOUBLE && y->type == LONG) {
std::vector<double> x_double = get_double(x);
std::vector<long> y_long = get_long(y);
longResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_long.begin(),
std::back_inserter(longResult), std::not_equal_to<>());
ans = make_booleans(longResult);
set_booleans(x, longResult);
} else if (x->type == STRING && y->type == STRING) {
ans = make_boolean(get_string(x).compare(get_string(y)) != 0);
set_boolean(x, get_string(x).compare(get_string(y)) != 0);
} else { // Both are DOUBLE
std::vector<double> x_double = get_double(x);
std::vector<double> y_double = get_double(y);
longResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_double.begin(),
std::back_inserter(longResult), std::not_equal_to<double>());
ans = make_booleans(longResult);
set_booleans(x, longResult);
}
delete x;
delete y;
return ans;
return x;
}
Value* and_value(Value* x, Value* y) {
@ -495,10 +477,11 @@ Value* and_value(Value* x, Value* y) {
std::transform(x_long.begin(), x_long.end(), y_long.begin(),
std::back_inserter(longResult), std::logical_and<>());
delete x;
set_booleans(x, longResult);
delete y;
return make_booleans(longResult);
return x;
}
Value* or_value(Value* x, Value* y) {
@ -511,10 +494,11 @@ Value* or_value(Value* x, Value* y) {
std::transform(x_long.begin(), x_long.end(), y_long.begin(),
std::back_inserter(longResult), std::logical_or<>());
delete x;
set_booleans(x, longResult);
delete y;
return make_booleans(longResult);
return x;
}
Value* not_value(Value* x) {
@ -525,7 +509,7 @@ Value* not_value(Value* x) {
std::vector<long> x_long = get_long(x);
std::transform(x_long.begin(), x_long.end(), std::back_inserter(longResult), std::logical_not<>());
delete x;
set_booleans(x, longResult);
return make_booleans(longResult);
return x;
}

View file

@ -30,3 +30,8 @@ void add_variable(Environment* env, Variable* var) {
env->vars.push_back(var);
}
Environment::~Environment() {
for (uint i = 0; i < size(vars); i++) {
delete vars[i];
}
}

View file

@ -11,11 +11,7 @@ class Environment {
public:
std::vector<Variable*> vars;
Environment() { }
~Environment() {
for (uint i = 0; i < size(vars); i++) {
delete vars[i];
}
}
~Environment();
};
// Variable Lookup Functions

View file

@ -71,6 +71,16 @@ void set_long(Value* val, std::vector<long> num) {
val->type = LONG;
val->val = num;
}
void set_boolean(Value* val, int x) {
val->type = BOOLEAN;
std::vector<long> result;
if (x) { result.push_back(1); } else { result.push_back(0); }
val->val = result;
}
void set_booleans(Value* val, std::vector<long> num) {
val->type = BOOLEAN;
val->val = num;
}
void set_double(Value* val, std::vector<double> dec) {
val->type = DOUBLE;
// val->value.dec = dec;
@ -80,7 +90,7 @@ void set_expression(Value* val, Node* expr) {
val->type = LAMBDA;
val->val = expr;
}
void set_sring(Value* val, std::string str) {
void set_string(Value* val, std::string str) {
val->type = STRING;
val->val = str;
}

View file

@ -77,6 +77,8 @@ std::string get_string(const Value* val);
// Setters
void set_long(Value* val, std::vector<long> num);
void set_boolean(Value* val, int x);
void set_booleans(Value* val, std::vector<long> num);
void set_double(Value* val, std::vector<double> dec);
void set_expression(Value* val, Node* node);
void set_string(Value* val, std::string str);