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 LAMBDATAG: return make_expression(node); break;
case CALLFUNC: case CALLFUNC:
check_num_nodes(node, 2, "cannot have more than two nodes for a function call."); 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(); local_env = new Environment();
add_variable(local_env, add_variable(local_env,
new Variable(tempNode->children[0]->id, // Get the name of the variable needed for the lambda expression 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; std::cerr << "Error: Symbol " << node->id << " not found." << std::endl;
return 0; return 0;
} }
// Change to return copy of value [TODO]
return get_value(var); return get_value(var);
break; break;
//---------- //----------
case VALUE: case VALUE:
// Change to return copy of value [TODO]
return new Value(*node->value); return new Value(*node->value);
break; break;
//---------- //----------

View file

@ -3,16 +3,15 @@
#include "operators.hpp" #include "operators.hpp"
#include "../variables/value.hpp" #include "../variables/value.hpp"
// TODO: Now replace every single operation with vector equivalent // 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) { Value* add(Value* x,Value* y) {
if (!x || !y) { std::cerr << "Error, uninitialized values being used in add." << std::endl; } 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 == 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)) { 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; std::cerr << "Error, cannot add a string with another data type." << std::endl;
} }
Value* ans;
std::vector<long> longResult; std::vector<long> longResult;
std::vector<double> doubleResult; std::vector<double> doubleResult;
@ -23,36 +22,35 @@ Value* add(Value* x, Value* y) {
longResult.reserve(x_long.size()); longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_long.begin(), std::transform(x_long.begin(), x_long.end(), y_long.begin(),
std::back_inserter(longResult), std::plus<long>()); std::back_inserter(longResult), std::plus<long>());
ans = make_long(longResult); set_long(x, longResult);
} else if (x->type == LONG && y->type == DOUBLE) { } else if (x->type == LONG && y->type == DOUBLE) {
std::vector<long> x_long = get_long(x); std::vector<long> x_long = get_long(x);
std::vector<double> y_double = get_double(y); std::vector<double> y_double = get_double(y);
doubleResult.reserve(x_long.size()); doubleResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_double.begin(), std::transform(x_long.begin(), x_long.end(), y_double.begin(),
std::back_inserter(doubleResult), std::plus<>()); std::back_inserter(doubleResult), std::plus<>());
ans = make_double(doubleResult); set_double(x, doubleResult);
} else if (x->type == DOUBLE && y->type == LONG) { } else if (x->type == DOUBLE && y->type == LONG) {
std::vector<double> x_double = get_double(x); std::vector<double> x_double = get_double(x);
std::vector<long> y_long = get_long(y); std::vector<long> y_long = get_long(y);
doubleResult.reserve(x_double.size()); doubleResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_long.begin(), std::transform(x_double.begin(), x_double.end(), y_long.begin(),
std::back_inserter(doubleResult), std::plus<>()); std::back_inserter(doubleResult), std::plus<>());
ans = make_double(doubleResult); set_double(x, doubleResult);
} else if (x->type == STRING && y->type == STRING) { } 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 } else { // Both are DOUBLE
std::vector<double> x_double = get_double(x); std::vector<double> x_double = get_double(x);
std::vector<double> y_double = get_double(y); std::vector<double> y_double = get_double(y);
doubleResult.reserve(x_double.size()); doubleResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_double.begin(), std::transform(x_double.begin(), x_double.end(), y_double.begin(),
std::back_inserter(doubleResult), std::plus<double>()); std::back_inserter(doubleResult), std::plus<double>());
ans = make_double(doubleResult); set_double(x, doubleResult);
} }
delete x;
delete y; delete y;
return ans; return x;
} }
Value* subtract(Value* x, Value* y) { 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 == 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; } if (x->type == STRING || y->type == STRING) { std::cerr << "Error, cannot subtract a string." << std::endl; }
Value* ans;
std::vector<long> longResult; std::vector<long> longResult;
std::vector<double> doubleResult; std::vector<double> doubleResult;
@ -71,34 +68,33 @@ Value* subtract(Value* x, Value* y) {
longResult.reserve(x_long.size()); longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_long.begin(), std::transform(x_long.begin(), x_long.end(), y_long.begin(),
std::back_inserter(longResult), std::minus<long>()); std::back_inserter(longResult), std::minus<long>());
ans = make_long(longResult); set_long(x, longResult);
} else if (x->type == LONG && y->type == DOUBLE) { } else if (x->type == LONG && y->type == DOUBLE) {
std::vector<long> x_long = get_long(x); std::vector<long> x_long = get_long(x);
std::vector<double> y_double = get_double(y); std::vector<double> y_double = get_double(y);
doubleResult.reserve(x_long.size()); doubleResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_double.begin(), std::transform(x_long.begin(), x_long.end(), y_double.begin(),
std::back_inserter(doubleResult), std::minus<>()); std::back_inserter(doubleResult), std::minus<>());
ans = make_double(doubleResult); set_double(x, doubleResult);
} else if (x->type == DOUBLE && y->type == LONG) { } else if (x->type == DOUBLE && y->type == LONG) {
std::vector<double> x_double = get_double(x); std::vector<double> x_double = get_double(x);
std::vector<long> y_long = get_long(y); std::vector<long> y_long = get_long(y);
doubleResult.reserve(x_double.size()); doubleResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_long.begin(), std::transform(x_double.begin(), x_double.end(), y_long.begin(),
std::back_inserter(doubleResult), std::minus<>()); std::back_inserter(doubleResult), std::minus<>());
ans = make_double(doubleResult); set_double(x, doubleResult);
} else { // Both are DOUBLE } else { // Both are DOUBLE
std::vector<double> x_double = get_double(x); std::vector<double> x_double = get_double(x);
std::vector<double> y_double = get_double(y); std::vector<double> y_double = get_double(y);
doubleResult.reserve(x_double.size()); doubleResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_double.begin(), std::transform(x_double.begin(), x_double.end(), y_double.begin(),
std::back_inserter(doubleResult), std::minus<double>()); std::back_inserter(doubleResult), std::minus<double>());
ans = make_double(doubleResult); set_double(x, doubleResult);
} }
delete x;
delete y; delete y;
return ans; return x;
} }
Value* division(Value* x, Value* y) { 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 == 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; } if (x->type == STRING || y->type == STRING) { std::cerr << "Error, cannot division a string." << std::endl; }
Value* ans;
std::vector<long> longResult; std::vector<long> longResult;
std::vector<double> doubleResult; std::vector<double> doubleResult;
@ -117,34 +112,33 @@ Value* division(Value* x, Value* y) {
longResult.reserve(x_long.size()); longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_long.begin(), std::transform(x_long.begin(), x_long.end(), y_long.begin(),
std::back_inserter(longResult), std::divides<long>()); std::back_inserter(longResult), std::divides<long>());
ans = make_long(longResult); set_long(x, longResult);
} else if (x->type == LONG && y->type == DOUBLE) { } else if (x->type == LONG && y->type == DOUBLE) {
std::vector<long> x_long = get_long(x); std::vector<long> x_long = get_long(x);
std::vector<double> y_double = get_double(y); std::vector<double> y_double = get_double(y);
doubleResult.reserve(x_long.size()); doubleResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_double.begin(), std::transform(x_long.begin(), x_long.end(), y_double.begin(),
std::back_inserter(doubleResult), std::divides<>()); std::back_inserter(doubleResult), std::divides<>());
ans = make_double(doubleResult); set_double(x, doubleResult);
} else if (x->type == DOUBLE && y->type == LONG) { } else if (x->type == DOUBLE && y->type == LONG) {
std::vector<double> x_double = get_double(x); std::vector<double> x_double = get_double(x);
std::vector<long> y_long = get_long(y); std::vector<long> y_long = get_long(y);
doubleResult.reserve(x_double.size()); doubleResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_long.begin(), std::transform(x_double.begin(), x_double.end(), y_long.begin(),
std::back_inserter(doubleResult), std::divides<>()); std::back_inserter(doubleResult), std::divides<>());
ans = make_double(doubleResult); set_double(x, doubleResult);
} else { // Both are DOUBLE } else { // Both are DOUBLE
std::vector<double> x_double = get_double(x); std::vector<double> x_double = get_double(x);
std::vector<double> y_double = get_double(y); std::vector<double> y_double = get_double(y);
doubleResult.reserve(x_double.size()); doubleResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_double.begin(), std::transform(x_double.begin(), x_double.end(), y_double.begin(),
std::back_inserter(doubleResult), std::divides<double>()); std::back_inserter(doubleResult), std::divides<double>());
ans = make_double(doubleResult); set_double(x, doubleResult);
} }
delete x;
delete y; delete y;
return ans; return x;
} }
Value* multiplication(Value* x, Value* y) { 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 == 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; } if (x->type == STRING || y->type == STRING) { std::cerr << "Error, cannot multiply a string." << std::endl; }
Value* ans;
std::vector<long> longResult; std::vector<long> longResult;
std::vector<double> doubleResult; std::vector<double> doubleResult;
@ -163,34 +156,33 @@ Value* multiplication(Value* x, Value* y) {
longResult.reserve(x_long.size()); longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_long.begin(), std::transform(x_long.begin(), x_long.end(), y_long.begin(),
std::back_inserter(longResult), std::multiplies<long>()); std::back_inserter(longResult), std::multiplies<long>());
ans = make_long(longResult); set_long(x, longResult);
} else if (x->type == LONG && y->type == DOUBLE) { } else if (x->type == LONG && y->type == DOUBLE) {
std::vector<long> x_long = get_long(x); std::vector<long> x_long = get_long(x);
std::vector<double> y_double = get_double(y); std::vector<double> y_double = get_double(y);
doubleResult.reserve(x_long.size()); doubleResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_double.begin(), std::transform(x_long.begin(), x_long.end(), y_double.begin(),
std::back_inserter(doubleResult), std::multiplies<>()); std::back_inserter(doubleResult), std::multiplies<>());
ans = make_double(doubleResult); set_double(x, doubleResult);
} else if (x->type == DOUBLE && y->type == LONG) { } else if (x->type == DOUBLE && y->type == LONG) {
std::vector<double> x_double = get_double(x); std::vector<double> x_double = get_double(x);
std::vector<long> y_long = get_long(y); std::vector<long> y_long = get_long(y);
doubleResult.reserve(x_double.size()); doubleResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_long.begin(), std::transform(x_double.begin(), x_double.end(), y_long.begin(),
std::back_inserter(doubleResult), std::multiplies<>()); std::back_inserter(doubleResult), std::multiplies<>());
ans = make_double(doubleResult); set_double(x, doubleResult);
} else { // Both are DOUBLE } else { // Both are DOUBLE
std::vector<double> x_double = get_double(x); std::vector<double> x_double = get_double(x);
std::vector<double> y_double = get_double(y); std::vector<double> y_double = get_double(y);
doubleResult.reserve(x_double.size()); doubleResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_double.begin(), std::transform(x_double.begin(), x_double.end(), y_double.begin(),
std::back_inserter(doubleResult), std::multiplies<double>()); std::back_inserter(doubleResult), std::multiplies<double>());
ans = make_double(doubleResult); set_double(x, doubleResult);
} }
delete x;
delete y; delete y;
return ans; return x;
} }
Value* less(Value* x, Value* y) { 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; std::cerr << "Error, cannot compare a string with another data type." << std::endl;
} }
Value* ans;
std::vector<long> longResult; std::vector<long> longResult;
// Destruct all four cases // Destruct all four cases
@ -210,38 +201,39 @@ Value* less(Value* x, Value* y) {
longResult.reserve(x_long.size()); longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_long.begin(), std::transform(x_long.begin(), x_long.end(), y_long.begin(),
std::back_inserter(longResult), std::less<long>()); std::back_inserter(longResult), std::less<long>());
ans = make_booleans(longResult); set_booleans(x, longResult);
} else if (x->type == LONG && y->type == DOUBLE) { } else if (x->type == LONG && y->type == DOUBLE) {
std::vector<long> x_long = get_long(x); std::vector<long> x_long = get_long(x);
std::vector<double> y_double = get_double(y); std::vector<double> y_double = get_double(y);
longResult.reserve(x_long.size()); longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_double.begin(), std::transform(x_long.begin(), x_long.end(), y_double.begin(),
std::back_inserter(longResult), std::less<>()); std::back_inserter(longResult), std::less<>());
ans = make_booleans(longResult); set_booleans(x, longResult);
} else if (x->type == DOUBLE && y->type == LONG) { } else if (x->type == DOUBLE && y->type == LONG) {
std::vector<double> x_double = get_double(x); std::vector<double> x_double = get_double(x);
std::vector<long> y_long = get_long(y); std::vector<long> y_long = get_long(y);
longResult.reserve(x_double.size()); longResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_long.begin(), std::transform(x_double.begin(), x_double.end(), y_long.begin(),
std::back_inserter(longResult), std::less<>()); std::back_inserter(longResult), std::less<>());
ans = make_booleans(longResult); set_booleans(x, longResult);
} else if (x->type == STRING && y->type == STRING) { } 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 } else { // Both are DOUBLE
std::vector<double> x_double = get_double(x); std::vector<double> x_double = get_double(x);
std::vector<double> y_double = get_double(y); std::vector<double> y_double = get_double(y);
longResult.reserve(x_double.size()); longResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_double.begin(), std::transform(x_double.begin(), x_double.end(), y_double.begin(),
std::back_inserter(longResult), std::less<double>()); std::back_inserter(longResult), std::less<double>());
ans = make_booleans(longResult); set_booleans(x, longResult);
} }
delete x;
delete y; delete y;
return ans; return x;
} }
// CONTINUE REPLACING FROM HERE
Value* greater(Value* x, Value* y) { Value* greater(Value* x, Value* y) {
if (!x || !y) { std::cerr << "Error, uninitialized values being used in greater." << std::endl; } 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; } 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; std::cerr << "Error, cannot compare a string with another data type." << std::endl;
} }
Value* ans;
std::vector<long> longResult; std::vector<long> longResult;
// Destruct all four cases // Destruct all four cases
@ -259,36 +250,35 @@ Value* greater(Value* x, Value* y) {
longResult.reserve(x_long.size()); longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_long.begin(), std::transform(x_long.begin(), x_long.end(), y_long.begin(),
std::back_inserter(longResult), std::greater<long>()); std::back_inserter(longResult), std::greater<long>());
ans = make_booleans(longResult); set_booleans(x, longResult);
} else if (x->type == LONG && y->type == DOUBLE) { } else if (x->type == LONG && y->type == DOUBLE) {
std::vector<long> x_long = get_long(x); std::vector<long> x_long = get_long(x);
std::vector<double> y_double = get_double(y); std::vector<double> y_double = get_double(y);
longResult.reserve(x_long.size()); longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_double.begin(), std::transform(x_long.begin(), x_long.end(), y_double.begin(),
std::back_inserter(longResult), std::greater<>()); std::back_inserter(longResult), std::greater<>());
ans = make_booleans(longResult); set_booleans(x, longResult);
} else if (x->type == DOUBLE && y->type == LONG) { } else if (x->type == DOUBLE && y->type == LONG) {
std::vector<double> x_double = get_double(x); std::vector<double> x_double = get_double(x);
std::vector<long> y_long = get_long(y); std::vector<long> y_long = get_long(y);
longResult.reserve(x_double.size()); longResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_long.begin(), std::transform(x_double.begin(), x_double.end(), y_long.begin(),
std::back_inserter(longResult), std::greater<>()); std::back_inserter(longResult), std::greater<>());
ans = make_booleans(longResult); set_booleans(x, longResult);
} else if (x->type == STRING && y->type == STRING) { } 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 } else { // Both are DOUBLE
std::vector<double> x_double = get_double(x); std::vector<double> x_double = get_double(x);
std::vector<double> y_double = get_double(y); std::vector<double> y_double = get_double(y);
longResult.reserve(x_double.size()); longResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_double.begin(), std::transform(x_double.begin(), x_double.end(), y_double.begin(),
std::back_inserter(longResult), std::greater<double>()); std::back_inserter(longResult), std::greater<double>());
ans = make_booleans(longResult); set_booleans(x, longResult);
} }
delete x;
delete y; delete y;
return ans; return x;
} }
Value* less_equal(Value* x, Value* y) { 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; std::cerr << "Error, cannot compare a string with another data type." << std::endl;
} }
Value* ans;
std::vector<long> longResult; std::vector<long> longResult;
// Destruct all four cases // Destruct all four cases
@ -308,36 +297,35 @@ Value* less_equal(Value* x, Value* y) {
longResult.reserve(x_long.size()); longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_long.begin(), std::transform(x_long.begin(), x_long.end(), y_long.begin(),
std::back_inserter(longResult), std::less_equal<long>()); std::back_inserter(longResult), std::less_equal<long>());
ans = make_booleans(longResult); set_long(x, longResult);
} else if (x->type == LONG && y->type == DOUBLE) { } else if (x->type == LONG && y->type == DOUBLE) {
std::vector<long> x_long = get_long(x); std::vector<long> x_long = get_long(x);
std::vector<double> y_double = get_double(y); std::vector<double> y_double = get_double(y);
longResult.reserve(x_long.size()); longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_double.begin(), std::transform(x_long.begin(), x_long.end(), y_double.begin(),
std::back_inserter(longResult), std::less_equal<>()); std::back_inserter(longResult), std::less_equal<>());
ans = make_booleans(longResult); set_booleans(x, longResult);
} else if (x->type == DOUBLE && y->type == LONG) { } else if (x->type == DOUBLE && y->type == LONG) {
std::vector<double> x_double = get_double(x); std::vector<double> x_double = get_double(x);
std::vector<long> y_long = get_long(y); std::vector<long> y_long = get_long(y);
longResult.reserve(x_double.size()); longResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_long.begin(), std::transform(x_double.begin(), x_double.end(), y_long.begin(),
std::back_inserter(longResult), std::less_equal<>()); std::back_inserter(longResult), std::less_equal<>());
ans = make_booleans(longResult); set_booleans(x, longResult);
} else if (x->type == STRING && y->type == STRING) { } 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 } else { // Both are DOUBLE
std::vector<double> x_double = get_double(x); std::vector<double> x_double = get_double(x);
std::vector<double> y_double = get_double(y); std::vector<double> y_double = get_double(y);
longResult.reserve(x_double.size()); longResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_double.begin(), std::transform(x_double.begin(), x_double.end(), y_double.begin(),
std::back_inserter(longResult), std::less_equal<double>()); std::back_inserter(longResult), std::less_equal<double>());
ans = make_booleans(longResult); set_booleans(x, longResult);
} }
delete x;
delete y; delete y;
return ans; return x;
} }
Value* greater_equal(Value* x, Value* y) { 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; std::cerr << "Error, cannot compare a string with another data type." << std::endl;
} }
Value* ans;
std::vector<long> longResult; std::vector<long> longResult;
// Destruct all four cases // Destruct all four cases
@ -357,36 +344,35 @@ Value* greater_equal(Value* x, Value* y) {
longResult.reserve(x_long.size()); longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_long.begin(), std::transform(x_long.begin(), x_long.end(), y_long.begin(),
std::back_inserter(longResult), std::greater_equal<long>()); std::back_inserter(longResult), std::greater_equal<long>());
ans = make_booleans(longResult); set_booleans(x, longResult);
} else if (x->type == LONG && y->type == DOUBLE) { } else if (x->type == LONG && y->type == DOUBLE) {
std::vector<long> x_long = get_long(x); std::vector<long> x_long = get_long(x);
std::vector<double> y_double = get_double(y); std::vector<double> y_double = get_double(y);
longResult.reserve(x_long.size()); longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_double.begin(), std::transform(x_long.begin(), x_long.end(), y_double.begin(),
std::back_inserter(longResult), std::greater_equal<>()); std::back_inserter(longResult), std::greater_equal<>());
ans = make_booleans(longResult); set_booleans(x, longResult);
} else if (x->type == DOUBLE && y->type == LONG) { } else if (x->type == DOUBLE && y->type == LONG) {
std::vector<double> x_double = get_double(x); std::vector<double> x_double = get_double(x);
std::vector<long> y_long = get_long(y); std::vector<long> y_long = get_long(y);
longResult.reserve(x_double.size()); longResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_long.begin(), std::transform(x_double.begin(), x_double.end(), y_long.begin(),
std::back_inserter(longResult), std::greater_equal<>()); std::back_inserter(longResult), std::greater_equal<>());
ans = make_booleans(longResult); set_booleans(x, longResult);
} else if (x->type == STRING && y->type == STRING) { } 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 } else { // Both are DOUBLE
std::vector<double> x_double = get_double(x); std::vector<double> x_double = get_double(x);
std::vector<double> y_double = get_double(y); std::vector<double> y_double = get_double(y);
longResult.reserve(x_double.size()); longResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_double.begin(), std::transform(x_double.begin(), x_double.end(), y_double.begin(),
std::back_inserter(longResult), std::greater_equal<double>()); std::back_inserter(longResult), std::greater_equal<double>());
ans = make_booleans(longResult); set_booleans(x, longResult);
} }
delete x;
delete y; delete y;
return ans; return x;
} }
Value* equals(Value* x, Value* y) { 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; std::cerr << "Error, cannot compare a string with another data type." << std::endl;
} }
Value* ans = nullptr;
std::vector<long> longResult; std::vector<long> longResult;
// Destruct all four cases // Destruct all four cases
@ -405,36 +390,35 @@ Value* equals(Value* x, Value* y) {
longResult.reserve(x_long.size()); longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_long.begin(), std::transform(x_long.begin(), x_long.end(), y_long.begin(),
std::back_inserter(longResult), std::equal_to<long>()); std::back_inserter(longResult), std::equal_to<long>());
ans = make_booleans(longResult); set_booleans(x, longResult);
} else if (x->type == LONG && y->type == DOUBLE) { } else if (x->type == LONG && y->type == DOUBLE) {
std::vector<long> x_long = get_long(x); std::vector<long> x_long = get_long(x);
std::vector<double> y_double = get_double(y); std::vector<double> y_double = get_double(y);
longResult.reserve(x_long.size()); longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_double.begin(), std::transform(x_long.begin(), x_long.end(), y_double.begin(),
std::back_inserter(longResult), std::equal_to<>()); std::back_inserter(longResult), std::equal_to<>());
ans = make_booleans(longResult); set_booleans(x, longResult);
} else if (x->type == DOUBLE && y->type == LONG) { } else if (x->type == DOUBLE && y->type == LONG) {
std::vector<double> x_double = get_double(x); std::vector<double> x_double = get_double(x);
std::vector<long> y_long = get_long(y); std::vector<long> y_long = get_long(y);
longResult.reserve(x_double.size()); longResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_long.begin(), std::transform(x_double.begin(), x_double.end(), y_long.begin(),
std::back_inserter(longResult), std::equal_to<>()); std::back_inserter(longResult), std::equal_to<>());
ans = make_booleans(longResult); set_booleans(x, longResult);
} else if (x->type == STRING && y->type == STRING) { } 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 } else { // Both are DOUBLE
std::vector<double> x_double = get_double(x); std::vector<double> x_double = get_double(x);
std::vector<double> y_double = get_double(y); std::vector<double> y_double = get_double(y);
longResult.reserve(x_double.size()); longResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_double.begin(), std::transform(x_double.begin(), x_double.end(), y_double.begin(),
std::back_inserter(longResult), std::equal_to<double>()); std::back_inserter(longResult), std::equal_to<double>());
ans = make_booleans(longResult); set_booleans(x, longResult);
} }
delete x;
delete y; delete y;
return ans; return x;
} }
Value* not_equals(Value* x, Value* y) { 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; std::cerr << "Error, cannot compare a string with another data type." << std::endl;
} }
Value* ans= nullptr;
std::vector<long> longResult; std::vector<long> longResult;
// Destruct all four cases // Destruct all four cases
@ -453,36 +436,35 @@ Value* not_equals(Value* x, Value* y) {
longResult.reserve(x_long.size()); longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_long.begin(), std::transform(x_long.begin(), x_long.end(), y_long.begin(),
std::back_inserter(longResult), std::not_equal_to<long>()); 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) { } else if (x->type == LONG && y->type == DOUBLE) {
std::vector<long> x_long = get_long(x); std::vector<long> x_long = get_long(x);
std::vector<double> y_double = get_double(y); std::vector<double> y_double = get_double(y);
longResult.reserve(x_long.size()); longResult.reserve(x_long.size());
std::transform(x_long.begin(), x_long.end(), y_double.begin(), std::transform(x_long.begin(), x_long.end(), y_double.begin(),
std::back_inserter(longResult), std::not_equal_to<>()); std::back_inserter(longResult), std::not_equal_to<>());
ans = make_booleans(longResult); set_booleans(x, longResult);
} else if (x->type == DOUBLE && y->type == LONG) { } else if (x->type == DOUBLE && y->type == LONG) {
std::vector<double> x_double = get_double(x); std::vector<double> x_double = get_double(x);
std::vector<long> y_long = get_long(y); std::vector<long> y_long = get_long(y);
longResult.reserve(x_double.size()); longResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_long.begin(), std::transform(x_double.begin(), x_double.end(), y_long.begin(),
std::back_inserter(longResult), std::not_equal_to<>()); std::back_inserter(longResult), std::not_equal_to<>());
ans = make_booleans(longResult); set_booleans(x, longResult);
} else if (x->type == STRING && y->type == STRING) { } 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 } else { // Both are DOUBLE
std::vector<double> x_double = get_double(x); std::vector<double> x_double = get_double(x);
std::vector<double> y_double = get_double(y); std::vector<double> y_double = get_double(y);
longResult.reserve(x_double.size()); longResult.reserve(x_double.size());
std::transform(x_double.begin(), x_double.end(), y_double.begin(), std::transform(x_double.begin(), x_double.end(), y_double.begin(),
std::back_inserter(longResult), std::not_equal_to<double>()); std::back_inserter(longResult), std::not_equal_to<double>());
ans = make_booleans(longResult); set_booleans(x, longResult);
} }
delete x;
delete y; delete y;
return ans; return x;
} }
Value* and_value(Value* x, Value* y) { 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::transform(x_long.begin(), x_long.end(), y_long.begin(),
std::back_inserter(longResult), std::logical_and<>()); std::back_inserter(longResult), std::logical_and<>());
delete x; set_booleans(x, longResult);
delete y; delete y;
return make_booleans(longResult); return x;
} }
Value* or_value(Value* x, Value* y) { 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::transform(x_long.begin(), x_long.end(), y_long.begin(),
std::back_inserter(longResult), std::logical_or<>()); std::back_inserter(longResult), std::logical_or<>());
delete x; set_booleans(x, longResult);
delete y; delete y;
return make_booleans(longResult); return x;
} }
Value* not_value(Value* x) { Value* not_value(Value* x) {
@ -525,7 +509,7 @@ Value* not_value(Value* x) {
std::vector<long> x_long = get_long(x); std::vector<long> x_long = get_long(x);
std::transform(x_long.begin(), x_long.end(), std::back_inserter(longResult), std::logical_not<>()); 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); 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: public:
std::vector<Variable*> vars; std::vector<Variable*> vars;
Environment() { } Environment() { }
~Environment() { ~Environment();
for (uint i = 0; i < size(vars); i++) {
delete vars[i];
}
}
}; };
// Variable Lookup Functions // Variable Lookup Functions

View file

@ -71,6 +71,16 @@ void set_long(Value* val, std::vector<long> num) {
val->type = LONG; val->type = LONG;
val->val = num; 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) { void set_double(Value* val, std::vector<double> dec) {
val->type = DOUBLE; val->type = DOUBLE;
// val->value.dec = dec; // val->value.dec = dec;
@ -80,7 +90,7 @@ void set_expression(Value* val, Node* expr) {
val->type = LAMBDA; val->type = LAMBDA;
val->val = expr; val->val = expr;
} }
void set_sring(Value* val, std::string str) { void set_string(Value* val, std::string str) {
val->type = STRING; val->type = STRING;
val->val = str; val->val = str;
} }

View file

@ -77,6 +77,8 @@ std::string get_string(const Value* val);
// Setters // Setters
void set_long(Value* val, std::vector<long> num); 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_double(Value* val, std::vector<double> dec);
void set_expression(Value* val, Node* node); void set_expression(Value* val, Node* node);
void set_string(Value* val, std::string str); void set_string(Value* val, std::string str);