cleaned dirty code.

master
xerox 5 years ago
parent 929e257a8e
commit 34f846030a

@ -12,14 +12,15 @@
* @param nodeInfo * to statement list * @param nodeInfo * to statement list
* @return nodeInfo * to head * @return nodeInfo * to head
*/ */
node_info *create_function(char *pName, node_info *pStmts) { node_info *create_function(char *p_name, node_info *p_stmts)
node_info *newNode = malloc(sizeof(node_info)); {
newNode->type = FUNCTION; node_info *new_node = malloc(sizeof(node_info));
newNode->name = pName; new_node->type = FUNCTION;
newNode->_function_body = malloc(sizeof(node_info)); new_node->name = p_name;
newNode->_function_body->next = copy_linked_list(pStmts); new_node->_function_body = malloc(sizeof(node_info));
_free_linked_list(pStmts); new_node->_function_body->next = copy_linked_list(p_stmts);
return newNode; _free_linked_list(p_stmts);
return new_node;
} }
/** /**
@ -28,12 +29,13 @@ node_info *create_function(char *pName, node_info *pStmts) {
* @param name of the function * @param name of the function
* @return a node_info pointer to the head of the function * @return a node_info pointer to the head of the function
*/ */
node_info *create_function_call(char *pName) { node_info *create_function_call(char *p_name)
node_info *newNode = malloc(sizeof(node_info)); {
newNode->type = FUNCTION_CALL; node_info *new_node = malloc(sizeof(node_info));
newNode->name = pName; new_node->type = FUNCTION_CALL;
newNode->next = NULL; new_node->name = p_name;
return newNode; new_node->next = NULL;
return new_node;
} }
/** /**
@ -42,16 +44,19 @@ node_info *create_function_call(char *pName) {
* @param new_statement_list to be appended to list * @param new_statement_list to be appended to list
* @param statement_head of linked list to append to * @param statement_head of linked list to append to
*/ */
node_info *create_compound_statement(node_info *new_statement_list, node_info *statement_head) { node_info *create_compound_statement(node_info *new_statement_list, node_info *statement_head)
{
if(statement_head == NULL) { if(!statement_head)
{
statement_head = malloc(sizeof(node_info)); statement_head = malloc(sizeof(node_info));
node_info *newNode = malloc(sizeof(node_info)); node_info *newNode = malloc(sizeof(node_info));
statement_head->opperation = 5; statement_head->opperation = 5;
statement_head->next = new_statement_list; statement_head->next = new_statement_list;
statement_head->next->next = NULL; statement_head->next->next = NULL;
statement_head->statement_list = NULL; statement_head->statement_list = NULL;
} else { }
else
{
node_info *p = statement_head; node_info *p = statement_head;
//TODO update with doubly linked list //TODO update with doubly linked list
while(p->statement_list != NULL) while(p->statement_list != NULL)
@ -60,7 +65,6 @@ node_info *create_compound_statement(node_info *new_statement_list, node_info *s
p->next = malloc(sizeof(node_info)); p->next = malloc(sizeof(node_info));
p->next = new_statement_list; p->next = new_statement_list;
p->next->next = NULL; p->next->next = NULL;
} }
return statement_head; return statement_head;
} }

@ -2,7 +2,7 @@
#define FUNCTIONS #define FUNCTIONS
#include "../includes/include.h" #include "../includes/include.h"
node_info *create_function(char *pName, node_info *pStmts); node_info *create_function(char *p_name, node_info *p_stmts);
node_info *create_function_call(char *pName); node_info *create_function_call(char *p_name);
node_info *create_compound_statement(node_info *new_statement_list, node_info *statement_head); node_info *create_compound_statement(node_info *new_statement_list, node_info *statement_head);
#endif #endif

@ -8,7 +8,8 @@
/** /**
* VariableType is a enum of varible types that will be inside of my language. * VariableType is a enum of varible types that will be inside of my language.
*/ */
typedef enum variable_type { typedef enum variable_type
{
VAR_STRING, VAR_STRING,
VAR_INT, VAR_INT,
VAR_CHAR, VAR_CHAR,
@ -19,7 +20,8 @@ typedef enum variable_type {
/** /**
* VariableValues is a struct that holds all possible variable values * VariableValues is a struct that holds all possible variable values
*/ */
typedef struct variable_values { typedef struct variable_values
{
variable_type type; variable_type type;
char _char; char _char;
char *_string; char *_string;
@ -31,21 +33,22 @@ typedef struct variable_values {
/** /**
* Variables is a struct that has all the information needed for a variables * Variables is a struct that has all the information needed for a variables
*/ */
typedef struct variable{ typedef struct variable
{
char *name; char *name;
variable_values *value; variable_values *value;
struct Variable *next; struct variable *next;
} variable; } variable;
typedef struct print { typedef struct print
{
int opperation; int opperation;
double value; double value;
char *string; char *string;
} print; } print;
typedef struct logic { typedef struct logic
{
int opperation; int opperation;
int expr; int expr;
struct node_info *if_true; struct node_info *if_true;
@ -54,7 +57,8 @@ typedef struct logic {
} logic; } logic;
typedef struct node_info { typedef struct node_info
{
int opperation; int opperation;
enum type{ enum type{

@ -8,7 +8,7 @@
*/ */
void _free_linked_list(node_info *p_list) { void _free_linked_list(node_info *p_list) {
node_info *temp; node_info *temp;
while(p_list->next != NULL) { while(p_list->next) {
temp = p_list; temp = p_list;
p_list = p_list->next; p_list = p_list->next;
free(temp); free(temp);
@ -24,10 +24,45 @@ node_info *copy_linked_list(node_info *p_list) {
node_info *new_list = malloc(sizeof(node_info)); node_info *new_list = malloc(sizeof(node_info));
new_list->next = NULL; new_list->next = NULL;
node_info *copy_list = new_list; node_info *copy_list = new_list;
while(p_list->next != NULL) { while(p_list->next) {
p_list = p_list->next; p_list = p_list->next;
copy_list->next = p_list; copy_list->next = p_list;
copy_list = copy_list->next; copy_list = copy_list->next;
} }
return new_list; return new_list;
}
/*
@return void
@param node_info* (head of linked list)
@param node_info* (node to add)
Adds link to linked list.
*/
void linked_list_add(node_info* p_head, node_info* new_node)
{
node_info* p_head_temp = p_head;
while (p_head->next)
p_head_temp = p_head_temp->next;
p_head_temp->next = p_head;
}
/*
@return bool (removed or not)
@param node_info* (head of linked list)
@param node_info* (node to delete from linked list)
*/
bool linked_list_remove(node_info* p_head, node_info* delete_node)
{
node_info* p_head_temp = p_head;
while (p_head_temp->next)
{
if (p_head_temp->next == delete_node)
{
p_head_temp->next = delete_node->next;
free(delete_node);
return true;
}
}
return false;
} }

@ -2,6 +2,8 @@
#define LINKED_LIST_H #define LINKED_LIST_H
#include "../includes/include.h" #include "../includes/include.h"
void _free_linked_list(node_info *pList); void _free_linked_list(node_info *p_list);
node_info *copy_linked_list(node_info *pList); node_info *copy_linked_list(node_info *p_list);
void linked_list_add(node_info* p_head, node_info* new_node);
bool linked_list_remove(node_info* p_head, node_info* delete_node)
#endif #endif

@ -35,7 +35,6 @@ node_info *create_logic(int opperation, int expr, node_info *statement_list, nod
return n;*/ return n;*/
break; break;
default: default:
printf("check your yacc file for logical statements\n"); printf("check your yacc file for logical statements\n");
} }
} }

@ -1,6 +1,4 @@
#ifndef LOGIC_H #ifndef LOGIC_H
#define LOGIC_H #define LOGIC_H
node_info *createLogic(int opperation, int expr, node_info *statement_list, node_info *else_false); node_info *createLogic(int opperation, int expr, node_info *statement_list, node_info *else_false);
#endif #endif

@ -8,7 +8,8 @@ extern node_info *_temp_statement_head;
//struct variable *_var_head = NULL; //struct variable *_var_head = NULL;
extern map_void_t *_function_list; extern map_void_t *_function_list;
node_info* create_var_assignment_string(char *p_name, char *value) { node_info* create_var_assignment_string(char *p_name, char *value)
{
node_info *new_node = malloc(sizeof(node_info)); node_info *new_node = malloc(sizeof(node_info));
variable *new_var = make_variable_string(p_name, value); variable *new_var = make_variable_string(p_name, value);
newNode->type = ASSIGNMENT; newNode->type = ASSIGNMENT;
@ -17,7 +18,8 @@ node_info* create_var_assignment_string(char *p_name, char *value) {
return new_node; return new_node;
} }
node_info* create_var_assignment_int(char *p_name, int value) { node_info* create_var_assignment_int(char *p_name, int value)
{
node_info *new_node = malloc(sizeof(node_info)); node_info *new_node = malloc(sizeof(node_info));
variable *new_var = make_variable_int(p_name, value); variable *new_var = make_variable_int(p_name, value);
newNode->type = ASSIGNMENT; newNode->type = ASSIGNMENT;
@ -26,7 +28,8 @@ node_info* create_var_assignment_int(char *p_name, int value) {
return new_node; return new_node;
} }
node_info* create_var_assignment_char(char *p_name, char value) { node_info* create_var_assignment_char(char *p_name, char value)
{
node_info *new_node = malloc(sizeof(node_info)); node_info *new_node = malloc(sizeof(node_info));
variable *new_var = make_variable_char(p_name, value); variable *new_var = make_variable_char(p_name, value);
newNode->type = ASSIGNMENT; newNode->type = ASSIGNMENT;
@ -35,7 +38,8 @@ node_info* create_var_assignment_char(char *p_name, char value) {
return new_node; return new_node;
} }
node_info* create_var_assignment_double(char *p_name, double value) { node_info* create_var_assignment_double(char *p_name, double value)
{
node_info *new_node = malloc(sizeof(node_info)); node_info *new_node = malloc(sizeof(node_info));
variable *new_var = make_variable_double(p_name, value); variable *new_var = make_variable_double(p_name, value);
//new node stuff //new node stuff
@ -52,20 +56,19 @@ Adds statement to linked list.
@param nodeInfo link to be added. @param nodeInfo link to be added.
@return head of linked list. @return head of linked list.
*/ */
node_info* add_to_compound_statement(node_info *new_statement, node_info *statement_head) { node_info* add_to_compound_statement(node_info *new_statement, node_info *statement_head)
{
if(statement_head == NULL) { if(!statement_head)
{
statement_head = malloc(sizeof(node_info)); statement_head = malloc(sizeof(node_info));
statement_head->opperation = 5; statement_head->opperation = 5;
statement_head->next = new_statement; statement_head->next = new_statement;
statement_head->next->next = NULL; statement_head->next->next = NULL;
statement_head->statement_list = NULL; statement_head->statement_list = NULL;
}
} else { else
{
node_info *p = statement_head; //make a temp of list head, so we can traverse it. node_info *p = statement_head; //make a temp of list head, so we can traverse it
//TODO replace with double linked list end //TODO replace with double linked list end
while (p->statement_list) while (p->statement_list)
p = p->statement_list; p = p->statement_list;
@ -88,29 +91,30 @@ Executes a statment.
@return void. @return void.
*/ */
//TODO return bool or int detailing the status of execution. //TODO break this switch up into multiple functions
void ex(node_info *n) //TODO return status code possibly.
{ void ex(node_info *exec_node)
switch(n->opperation) {
switch(exec_node->opperation)
{ {
case 1: case 1:
add_var(n->var->name, n->var->value, _var_map); add_var(exec_node->var->name, exec_node->var->value, _var_map);
break; break;
//TODO expression stuff //TODO expression stuff
case 2: case 2:
break; break;
//print //print
case 3: case 3:
switch(n->printExpression.opperation) { switch(exec_node->printExpression.opperation) {
case 1: case 1:
printf("%d\n", (int) n->printExpression.value); printf("%d\n", (int) exec_node->printExpression.value);
break; break;
case 2: case 2:
printf("%s\n", n->printExpression.string); printf("%s\n", exec_node->printExpression.string);
break; break;
case 3: case 3:
printf("%s%lf\n", n->printExpression.string, n->printExpression.value); printf("%s%lf\n", exec_node->printExpression.string, exec_node->printExpression.value);
break; break;
default: default:
printf("{ERROR} CHECK YACC FILE FOR INCORRECT PRINT\n"); printf("{ERROR} CHECK YACC FILE FOR INCORRECT PRINT\n");
@ -118,29 +122,29 @@ void ex(node_info *n)
break; break;
//logic (if, while, for, etc) //logic (if, while, for, etc)
case 4: case 4:
switch(n->logicalExpression.opperation) { switch(exec_node->logicalExpression.opperation) {
case 1: case 1:
if (n->logicalExpression.expr == 1) if (exec_node->logicalExpression.expr == 1)
ex(n->logicalExpression.if_true); ex(exec_node->logicalExpression.if_true);
break; break;
case 2: case 2:
while (n->logicalExpression.expr == 1) while (exec_node->logicalExpression.expr == 1)
ex(n->logicalExpression.while_true); ex(exec_node->logicalExpression.while_true);
break; break;
default: default:
printf("{ERROR} SOMETHING WRONG WITH LOGICAL EXPRESSION CHECK YACC\n"); printf("{ERROR} SOMETHING WRONG WITH LOGICAL EXPRESSION CHECK YACC\n");
} }
break; break;
case 5: case 5:
while(n->next != NULL) while(exec_node->next != NULL)
{ {
n = n->next; exec_node = exec_node->next;
ex(n); ex(exec_node);
} }
break; break;
case 6: case 6:
; ;
variable_values *value = get_value(n->name, _var_map); variable_values *value = get_value(exec_node->name, _var_map);
switch(value->type) switch(value->type)
{ {
case VAR_STRING: case VAR_STRING:
@ -165,12 +169,12 @@ void ex(node_info *n)
//TODO update default to case 7 for functions //TODO update default to case 7 for functions
default: default:
; ;
switch(n->type) switch(exec_node->type)
{ {
case FUNCTION_CALL: case FUNCTION_CALL:
; ;
//gets the statements of a function with the provided name //gets the statements of a function with the provided name
node_info *_stmts = *map_get(&*((map_void_t* ) _function_list), n->name); node_info *_stmts = *map_get(&*((map_void_t* ) _function_list), exec_node->name);
while (_stmts->next != NULL) { //loop over all the statements and execute them while (_stmts->next != NULL) { //loop over all the statements and execute them
_stmts = _stmts->next; _stmts = _stmts->next;
ex(_stmts); ex(_stmts);
@ -182,15 +186,15 @@ void ex(node_info *n)
if (!_function_list) if (!_function_list)
{ {
_function_list = malloc(sizeof(map_void_t)); _function_list = malloc(sizeof(map_void_t));
n->_var_list = malloc(sizeof(node_info)); exec_node->_var_list = malloc(sizeof(node_info));
n->next = NULL; exec_node->next = NULL;
n->local_list = NULL; exec_node->local_list = NULL;
map_init(&*((map_void_t *) _function_list)); map_init(&*((map_void_t *) _function_list));
map_init(&*((map_void_t *) n->_var_list)); map_init(&*((map_void_t *) exec_node->_var_list));
//put the function in the hashmap //put the function in the hashmap
map_set(&*((map_void_t* ) _function_list), n->name, &*n->_function_body); map_set(&*((map_void_t* ) _function_list), exec_node->name, &*exec_node->_function_body);
} else //else we dont malloc, we just put the function inside of the hashmap. } else //else we dont malloc, we just put the function inside of the hashmap.
map_set(&*((map_void_t* ) _function_list), n->name, &*n->_function_body); map_set(&*((map_void_t* ) _function_list), exec_node->name, &*exec_node->_function_body);
break; break;
default: default:
; ;

@ -2,13 +2,10 @@
#define PARSER_H #define PARSER_H
#include "../includes/include.h" #include "../includes/include.h"
node_info* create_var_assignment_string(char *pName, char *pString); node_info* create_var_assignment_string(char *p_name, char *value);
node_info* create_var_assignment_int(char *pName, int number); node_info* create_var_assignment_int(char *p_name, int value);
node_info* create_var_assignment_char(char *pName, char Char); node_info* create_var_assignment_char(char *pName, char value);
node_info* create_var_assignment_double(char *pName, double Double); node_info* create_var_assignment_double(char *pName, double value);
node_info* create_print_var_node(char *pVarname);
node_info* create_print_statement(int opperation, int value, char *string);
node_info* create_logic(int opperation, int expr, node_info *statement_list, node_info *else_false);
node_info* add_to_compound_statement(node_info *new_statement, node_info *statement_head); node_info* add_to_compound_statement(node_info *new_statement, node_info *statement_head);
void ex(node_info *n); void ex(node_info *exec_node);
#endif #endif

@ -16,7 +16,8 @@
void add_var(char *p_name, variable_values *p_values, map_void_t* p_var_map) void add_var(char *p_name, variable_values *p_values, map_void_t* p_var_map)
{ {
//if variable map is null //if variable map is null
if(p_var_map == NULL) { if(!p_var_map)
{
p_var_map = malloc(sizeof(map_void_t)); p_var_map = malloc(sizeof(map_void_t));
map_init(&*((map_void_t *) p_var_map)); map_init(&*((map_void_t *) p_var_map));
} }
@ -32,9 +33,9 @@ void add_var(char *p_name, variable_values *p_values, map_void_t* p_var_map)
* @param [char *string] Variable name. * @param [char *string] Variable name.
* *
*/ */
variable_values *get_value(char *pName, map_void_t* pVarMap) variable_values *get_value(char *p_name, map_void_t* p_var_map)
{ {
return *map_get(&*((map_void_t* ) pVarMap), pName); return *map_get(&*((map_void_t* ) p_var_map), p_name);
} }
/** /**

@ -1,9 +1,9 @@
#ifndef VARS_H #ifndef VARS_H
#define VARS_H #define VARS_H
void add_var(char *name, variable_values *values, map_void_t* pVarMap); void add_var(char *name, variable_values *values, map_void_t* p_var_map);
variable_values *get_value(char *pName, map_void_t* pVarMap); variable_values *get_value(char *p_name, map_void_t* p_var_map);
variable* make_variable_int(char *pName, int value); variable* make_variable_int(char *p_name, int value);
variable* make_variable_double(char *pName, double value); variable* make_variable_double(char *p_name, double value);
variable* make_variable_char(char *pName, char value); variable* make_variable_char(char *p_name, char value);
variable* make_variable_string(char *pName, char *value); variable* make_variable_string(char *p_name, char *value);
#endif #endif
Loading…
Cancel
Save