Took out remaining char* and removed mallocs
This commit is contained in:
parent
65f50a159b
commit
e8e4baabb6
6 changed files with 44 additions and 37 deletions
|
@ -14,7 +14,7 @@ struct Node* make_node(int type, struct Value* value, std::string id) {
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
/* allocate space */
|
/* allocate space */
|
||||||
struct Node* node = (struct Node*) malloc(sizeof(struct Node));
|
struct Node* node = new Node();
|
||||||
|
|
||||||
/* set properties */
|
/* set properties */
|
||||||
node->type = type;
|
node->type = type;
|
||||||
|
|
|
@ -21,7 +21,7 @@ struct Value* add(struct Value* x, struct Value* y) {
|
||||||
} else if (x->type == DOUBLE && y->type == LONG) {
|
} else if (x->type == DOUBLE && y->type == LONG) {
|
||||||
ans = make_double(get_double(x) + get_long(y));
|
ans = make_double(get_double(x) + get_long(y));
|
||||||
} else if (x->type == STRING && y->type == STRING) {
|
} else if (x->type == STRING && y->type == STRING) {
|
||||||
ans = make_string(strcat(get_string(x), get_string(y)));
|
ans = make_string(get_string(x) + get_string(y));
|
||||||
} else { // Both are DOUBLE
|
} else { // Both are DOUBLE
|
||||||
ans = make_double(get_double(x) + get_double(y));
|
ans = make_double(get_double(x) + get_double(y));
|
||||||
}
|
}
|
||||||
|
@ -109,7 +109,7 @@ struct Value* less(struct Value* x, struct Value* y) {
|
||||||
} else if (x->type == DOUBLE && y->type == LONG) {
|
} else if (x->type == DOUBLE && y->type == LONG) {
|
||||||
ans = make_boolean(get_double(x) < get_long(y));
|
ans = make_boolean(get_double(x) < get_long(y));
|
||||||
} else if (x->type == STRING && y->type == STRING) {
|
} else if (x->type == STRING && y->type == STRING) {
|
||||||
ans = make_boolean(strcmp(get_string(x), get_string(y)) < 0);
|
ans = make_boolean(get_string(x).compare(get_string(y)) < 0);
|
||||||
} else { // Both are DOUBLE
|
} else { // Both are DOUBLE
|
||||||
ans = make_boolean(get_double(x) < get_double(y));
|
ans = make_boolean(get_double(x) < get_double(y));
|
||||||
}
|
}
|
||||||
|
@ -134,7 +134,7 @@ struct Value* greater(struct Value* x, struct Value* y) {
|
||||||
} else if (x->type == DOUBLE && y->type == LONG) {
|
} else if (x->type == DOUBLE && y->type == LONG) {
|
||||||
ans = make_boolean(get_double(x) > get_long(y));
|
ans = make_boolean(get_double(x) > get_long(y));
|
||||||
} else if (x->type == STRING && y->type == STRING) {
|
} else if (x->type == STRING && y->type == STRING) {
|
||||||
ans = make_boolean(strcmp(get_string(x), get_string(y)) > 0);
|
ans = make_boolean(get_string(x).compare(get_string(y)) > 0);
|
||||||
} else { // Both are DOUBLE
|
} else { // Both are DOUBLE
|
||||||
ans = make_boolean(get_double(x) > get_double(y));
|
ans = make_boolean(get_double(x) > get_double(y));
|
||||||
}
|
}
|
||||||
|
@ -159,7 +159,7 @@ struct Value* less_equal(struct Value* x, struct Value* y) {
|
||||||
} else if (x->type == DOUBLE && y->type == LONG) {
|
} else if (x->type == DOUBLE && y->type == LONG) {
|
||||||
ans = make_boolean(get_double(x) <= get_long(y));
|
ans = make_boolean(get_double(x) <= get_long(y));
|
||||||
} else if (x->type == STRING && y->type == STRING) {
|
} else if (x->type == STRING && y->type == STRING) {
|
||||||
ans = make_boolean(strcmp(get_string(x), get_string(y)) <= 0);
|
ans = make_boolean(get_string(x).compare(get_string(y)) <= 0);
|
||||||
} else { // Both are DOUBLE
|
} else { // Both are DOUBLE
|
||||||
ans = make_boolean(get_double(x) <= get_double(y));
|
ans = make_boolean(get_double(x) <= get_double(y));
|
||||||
}
|
}
|
||||||
|
@ -184,7 +184,7 @@ struct Value* greater_equal(struct Value* x, struct Value* y) {
|
||||||
} else if (x->type == DOUBLE && y->type == LONG) {
|
} else if (x->type == DOUBLE && y->type == LONG) {
|
||||||
ans = make_boolean(get_double(x) >= get_long(y));
|
ans = make_boolean(get_double(x) >= get_long(y));
|
||||||
} else if (x->type == STRING && y->type == STRING) {
|
} else if (x->type == STRING && y->type == STRING) {
|
||||||
ans = make_boolean(strcmp(get_string(x), get_string(y)) >= 0);
|
ans = make_boolean(get_string(x).compare(get_string(y)) >= 0);
|
||||||
} else { // Both are DOUBLE
|
} else { // Both are DOUBLE
|
||||||
ans = make_boolean(get_double(x) >= get_double(y));
|
ans = make_boolean(get_double(x) >= get_double(y));
|
||||||
}
|
}
|
||||||
|
@ -212,7 +212,7 @@ struct Value* equals(struct Value* x, struct Value* y) {
|
||||||
} else if (x->type == BOOLEAN && y->type == BOOLEAN) {
|
} else if (x->type == BOOLEAN && y->type == BOOLEAN) {
|
||||||
ans = make_boolean(get_long(x) == get_long(y));
|
ans = make_boolean(get_long(x) == get_long(y));
|
||||||
} else if (x->type == STRING && y->type == STRING) {
|
} else if (x->type == STRING && y->type == STRING) {
|
||||||
ans = make_boolean(strcmp(get_string(x), get_string(y)) == 0);
|
ans = make_boolean(get_string(x).compare(get_string(y)) == 0);
|
||||||
} else { // Type is a mix between boolean and another type
|
} else { // Type is a mix between boolean and another type
|
||||||
fprintf(stderr, "Error, cannot compare a boolean with another type.\n");
|
fprintf(stderr, "Error, cannot compare a boolean with another type.\n");
|
||||||
}
|
}
|
||||||
|
@ -240,7 +240,7 @@ struct Value* not_equals(struct Value* x, struct Value* y) {
|
||||||
} else if (x->type == BOOLEAN && y->type == BOOLEAN) {
|
} else if (x->type == BOOLEAN && y->type == BOOLEAN) {
|
||||||
ans = make_boolean(get_long(x) != get_long(y));
|
ans = make_boolean(get_long(x) != get_long(y));
|
||||||
} else if (x->type == STRING && y->type == STRING) {
|
} else if (x->type == STRING && y->type == STRING) {
|
||||||
ans = make_boolean(strcmp(get_string(x), get_string(y)) != 0);
|
ans = make_boolean(get_string(x).compare(get_string(y)) != 0);
|
||||||
} else { // Type is a mix between boolean and another type
|
} else { // Type is a mix between boolean and another type
|
||||||
fprintf(stderr, "Error, cannot compare a boolean with another type.\n");
|
fprintf(stderr, "Error, cannot compare a boolean with another type.\n");
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
#include "variable.h"
|
#include "variable.h"
|
||||||
|
|
||||||
struct Environment* create_environment(void) {
|
struct Environment* create_environment(void) {
|
||||||
struct Environment* env = (struct Environment*) malloc(sizeof(struct Environment));
|
struct Environment* env = new Environment();
|
||||||
env->num_vars = 0;
|
env->num_vars = 0;
|
||||||
for(int i = 0; i < MAX_VARIABLES; i++) {
|
for(int i = 0; i < MAX_VARIABLES; i++) {
|
||||||
env->vars[i] = NULL;
|
env->vars[i] = NULL;
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "value.h"
|
#include "value.h"
|
||||||
|
#include <string>
|
||||||
|
#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, char* str) {
|
struct Value* make_value(TypeTag type, long num, double dec, struct Node* expr, std::string str) {
|
||||||
/* allocate space */
|
/* allocate space */
|
||||||
struct Value* val = (struct Value*) malloc(sizeof(struct Value));
|
struct Value* val = new Value();
|
||||||
|
|
||||||
/* set properties */
|
/* set properties */
|
||||||
val->type = type;
|
val->type = type;
|
||||||
|
@ -25,24 +26,24 @@ struct Value* make_value(TypeTag type, long num, double dec, struct Node* expr,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Value* make_long(long num) {
|
struct Value* make_long(long num) {
|
||||||
return make_value(LONG, num, 0, NULL, NULL);
|
return make_value(LONG, num, 0, NULL, "");
|
||||||
}
|
}
|
||||||
struct Value* make_double(double dec) {
|
struct Value* make_double(double dec) {
|
||||||
return make_value(DOUBLE, 0, dec, NULL, NULL);
|
return make_value(DOUBLE, 0, dec, NULL, "");
|
||||||
}
|
}
|
||||||
struct Value* make_true() {
|
struct Value* make_true() {
|
||||||
return make_value(BOOLEAN, 1, 0, NULL, NULL);
|
return make_value(BOOLEAN, 1, 0, NULL, "");
|
||||||
}
|
}
|
||||||
struct Value* make_false() {
|
struct Value* make_false() {
|
||||||
return make_value(BOOLEAN, 0, 0, NULL, NULL);
|
return make_value(BOOLEAN, 0, 0, NULL, "");
|
||||||
}
|
}
|
||||||
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, NULL);
|
return make_value(LAMBDA, 0, 0, expr, "");
|
||||||
}
|
}
|
||||||
struct Value* make_string(char* str) {
|
struct Value* make_string(std::string str) {
|
||||||
return make_value(STRING, 0, 0, NULL, str);
|
return make_value(STRING, 0, 0, NULL, str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,7 +60,7 @@ double get_double(struct Value* val) {
|
||||||
struct Node* get_expression(struct Value* val) {
|
struct Node* get_expression(struct Value* val) {
|
||||||
return val->value.expr;
|
return val->value.expr;
|
||||||
}
|
}
|
||||||
char* get_string(struct Value* val) {
|
std::string get_string(struct Value* val) {
|
||||||
return val->value.str;
|
return val->value.str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,7 +76,7 @@ void set_expression(struct Value* val, struct Node* expr) {
|
||||||
val->type = LAMBDA;
|
val->type = LAMBDA;
|
||||||
val->value.expr = expr;
|
val->value.expr = expr;
|
||||||
}
|
}
|
||||||
void set_sring(struct Value* val, char* str) {
|
void set_sring(struct Value* val, std::string str) {
|
||||||
val->type = STRING;
|
val->type = STRING;
|
||||||
val->value.str = str;
|
val->value.str = str;
|
||||||
}
|
}
|
||||||
|
@ -83,17 +84,17 @@ void set_sring(struct Value* val, char* str) {
|
||||||
void print_value(struct Value* val) {
|
void print_value(struct Value* val) {
|
||||||
if (val->type == BOOLEAN) {
|
if (val->type == BOOLEAN) {
|
||||||
if (get_long(val)) {
|
if (get_long(val)) {
|
||||||
printf("true");
|
std::cout << "true";
|
||||||
} else {
|
} else {
|
||||||
printf("false");
|
std::cout << "false";
|
||||||
}
|
}
|
||||||
} else if (val->type == LONG) {
|
} else if (val->type == LONG) {
|
||||||
printf("%li", get_long(val));
|
std::cout << get_long(val);
|
||||||
} else if (val->type == STRING) {
|
} else if (val->type == STRING) {
|
||||||
printf("%s", get_string(val));
|
std::cout << get_string(val);
|
||||||
} else if (val->type == DOUBLE) {
|
} else if (val->type == DOUBLE) {
|
||||||
printf("%lf", get_double(val));
|
std::cout << get_double(val);
|
||||||
} else { // Assume lambda expression
|
} else { // Assume lambda expression
|
||||||
printf("<LambdaExpression>");
|
std::cout << "<LambdaExpression>";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,20 @@
|
||||||
#ifndef VALUE_H
|
#ifndef VALUE_H
|
||||||
#define VALUE_H
|
#define VALUE_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
enum TypeTag { DOUBLE, LONG, BOOLEAN, STRING, LAMBDA };
|
enum TypeTag { DOUBLE, LONG, BOOLEAN, STRING, LAMBDA };
|
||||||
|
|
||||||
typedef union typeval {
|
union TypeVal {
|
||||||
long num;
|
long num;
|
||||||
double dec;
|
double dec;
|
||||||
struct Node* expr;
|
struct Node* expr;
|
||||||
char* str;
|
std::string str;
|
||||||
} TypeVal;
|
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() {}
|
||||||
|
};
|
||||||
|
|
||||||
struct Value {
|
struct Value {
|
||||||
enum TypeTag type;
|
enum TypeTag type;
|
||||||
|
@ -16,14 +22,14 @@ struct Value {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Constructors
|
// Constructors
|
||||||
struct Value* make_value(int type, long num, double dec, struct Node* expr, char* str);
|
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();
|
||||||
struct Value* make_false();
|
struct Value* make_false();
|
||||||
struct Value* make_boolean(int x);
|
struct Value* make_boolean(int x);
|
||||||
struct Value* make_expression(struct Node* expr);
|
struct Value* make_expression(struct Node* expr);
|
||||||
struct Value* make_string(char* str);
|
struct Value* make_string(std::string str);
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
void delete_value(struct Value* val);
|
void delete_value(struct Value* val);
|
||||||
|
@ -32,13 +38,13 @@ 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);
|
||||||
struct Node* get_expression(struct Value* val);
|
struct Node* get_expression(struct Value* val);
|
||||||
char* get_string(struct Value* val);
|
std::string get_string(struct Value* val);
|
||||||
|
|
||||||
// Setters
|
// Setters
|
||||||
void set_long(struct Value* val, long num);
|
void set_long(struct Value* val, long num);
|
||||||
void set_double(struct Value* val, double dec);
|
void set_double(struct Value* val, double dec);
|
||||||
void set_expression(struct Value* val, struct Node* node);
|
void set_expression(struct Value* val, struct Node* node);
|
||||||
void set_string(struct Value* val, char* str);
|
void set_string(struct Value* val, std::string str);
|
||||||
|
|
||||||
void print_value(struct Value* val);
|
void print_value(struct Value* val);
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
/* creates a new variable and returns it */
|
/* creates a new variable and returns it */
|
||||||
struct Variable* make_variable(char* id, struct Value* value) {
|
struct Variable* make_variable(char* id, struct Value* value) {
|
||||||
/* allocate space */
|
/* allocate space */
|
||||||
struct Variable* var = (struct Variable*) malloc(sizeof(struct Variable));
|
struct Variable* var = new Variable();
|
||||||
|
|
||||||
/* set properties */
|
/* set properties */
|
||||||
strcpy(var->id, id);
|
strcpy(var->id, id);
|
||||||
|
|
Reference in a new issue