Archived
1
0
Fork 0

Took out remaining char* and removed mallocs

This commit is contained in:
Brandon Rozek 2018-09-26 19:16:59 -04:00
parent 65f50a159b
commit e8e4baabb6
6 changed files with 44 additions and 37 deletions

View file

@ -14,7 +14,7 @@ struct Node* make_node(int type, struct Value* value, std::string id) {
int i;
/* allocate space */
struct Node* node = (struct Node*) malloc(sizeof(struct Node));
struct Node* node = new Node();
/* set properties */
node->type = type;

View file

@ -21,7 +21,7 @@ struct Value* add(struct Value* x, struct Value* y) {
} else if (x->type == DOUBLE && y->type == LONG) {
ans = make_double(get_double(x) + get_long(y));
} 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
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) {
ans = make_boolean(get_double(x) < get_long(y));
} 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
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) {
ans = make_boolean(get_double(x) > get_long(y));
} 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
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) {
ans = make_boolean(get_double(x) <= get_long(y));
} 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
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) {
ans = make_boolean(get_double(x) >= get_long(y));
} 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
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) {
ans = make_boolean(get_long(x) == get_long(y));
} 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
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) {
ans = make_boolean(get_long(x) != get_long(y));
} 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
fprintf(stderr, "Error, cannot compare a boolean with another type.\n");
}

View file

@ -5,7 +5,7 @@
#include "variable.h"
struct Environment* create_environment(void) {
struct Environment* env = (struct Environment*) malloc(sizeof(struct Environment));
struct Environment* env = new Environment();
env->num_vars = 0;
for(int i = 0; i < MAX_VARIABLES; i++) {
env->vars[i] = NULL;

View file

@ -1,12 +1,13 @@
#include <stdio.h>
#include <stdlib.h>
#include "value.h"
#include <string>
#include <iostream>
#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 */
struct Value* val = (struct Value*) malloc(sizeof(struct Value));
struct Value* val = new Value();
/* set properties */
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) {
return make_value(LONG, num, 0, NULL, NULL);
return make_value(LONG, num, 0, NULL, "");
}
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() {
return make_value(BOOLEAN, 1, 0, NULL, NULL);
return make_value(BOOLEAN, 1, 0, NULL, "");
}
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) {
return (x)? make_true() : make_false();
}
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);
}
@ -59,7 +60,7 @@ double get_double(struct Value* val) {
struct Node* get_expression(struct Value* val) {
return val->value.expr;
}
char* get_string(struct Value* val) {
std::string get_string(struct Value* val) {
return val->value.str;
}
@ -75,7 +76,7 @@ void set_expression(struct Value* val, struct Node* expr) {
val->type = LAMBDA;
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->value.str = str;
}
@ -83,17 +84,17 @@ void set_sring(struct Value* val, char* str) {
void print_value(struct Value* val) {
if (val->type == BOOLEAN) {
if (get_long(val)) {
printf("true");
std::cout << "true";
} else {
printf("false");
std::cout << "false";
}
} else if (val->type == LONG) {
printf("%li", get_long(val));
std::cout << get_long(val);
} else if (val->type == STRING) {
printf("%s", get_string(val));
std::cout << get_string(val);
} else if (val->type == DOUBLE) {
printf("%lf", get_double(val));
std::cout << get_double(val);
} else { // Assume lambda expression
printf("<LambdaExpression>");
std::cout << "<LambdaExpression>";
}
}

View file

@ -1,14 +1,20 @@
#ifndef VALUE_H
#define VALUE_H
#include <string>
enum TypeTag { DOUBLE, LONG, BOOLEAN, STRING, LAMBDA };
typedef union typeval {
long num;
double dec;
struct Node* expr;
char* str;
} TypeVal;
union TypeVal {
long num;
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() {}
};
struct Value {
enum TypeTag type;
@ -16,14 +22,14 @@ struct Value {
};
// 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_double(double dec);
struct Value* make_true();
struct Value* make_false();
struct Value* make_boolean(int x);
struct Value* make_expression(struct Node* expr);
struct Value* make_string(char* str);
struct Value* make_string(std::string str);
// Destructor
void delete_value(struct Value* val);
@ -32,13 +38,13 @@ void delete_value(struct Value* val);
long get_long(struct Value* val);
double get_double(struct Value* val);
struct Node* get_expression(struct Value* val);
char* get_string(struct Value* val);
std::string get_string(struct Value* val);
// Setters
void set_long(struct Value* val, long num);
void set_double(struct Value* val, double dec);
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);

View file

@ -6,7 +6,7 @@
/* creates a new variable and returns it */
struct Variable* make_variable(char* id, struct Value* value) {
/* allocate space */
struct Variable* var = (struct Variable*) malloc(sizeof(struct Variable));
struct Variable* var = new Variable();
/* set properties */
strcpy(var->id, id);