You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
eon/parser/parser.c

265 lines
7.5 KiB

5 years ago
#include <stdio.h>
5 years ago
#include <stdlib.h>
#include "../vars/vars.c"
5 years ago
5 years ago
//hashmaps/link list heads
extern map_void_t *_var_map;
extern node_info *_temp_statement_head;
//struct variable *_var_head = NULL;
extern map_void_t *_function_list;
5 years ago
5 years ago
node_info* create_var_assignment_string(char *pName, char *pString) {
5 years ago
5 years ago
node_info *newNode = malloc(sizeof(node_info));
variable *newVar = make_variable_string(pName, pString);
5 years ago
newNode->type = ASSIGNMENT;
newNode->opperation = 1;
newNode->var = newVar;
return newNode;
}
5 years ago
node_info* create_var_assignment_int(char *pName, int number) {
5 years ago
5 years ago
node_info *newNode = malloc(sizeof(node_info));
variable *newVar = make_variable_int(pName, number);
5 years ago
newNode->type = ASSIGNMENT;
newNode->opperation = 1;
newNode->var = newVar;
return newNode;
}
5 years ago
node_info* create_var_assignment_char(char *pName, char Char) {
5 years ago
5 years ago
node_info *newNode = malloc(sizeof(node_info));
variable *newVar = make_variable_char(pName, Char);
5 years ago
newNode->type = ASSIGNMENT;
newNode->opperation = 1;
newNode->var = newVar;
return newNode;
}
5 years ago
node_info* create_var_assignment_double(char *pName, double Double) {
5 years ago
5 years ago
node_info *newNode = malloc(sizeof(node_info));
variable *newVar = make_variable_double(pName, Double);
5 years ago
//new node stuff
newNode->type = ASSIGNMENT;
newNode->opperation = 1;
newNode->var = newVar;
return newNode;
}
/*
Adds statement to linked list.
@param nodeInfo link to be added.
@return head of linked list.
*/
5 years ago
node_info* add_to_compound_statement(node_info *new_statement, node_info *statement_head) {
5 years ago
5 years ago
if(statement_head == NULL) {
5 years ago
5 years ago
statement_head = malloc(sizeof(node_info));
statement_head->opperation = 5;
statement_head->next = new_statement;
statement_head->next->next = NULL;
statement_head->statement_list = NULL;
5 years ago
} else {
5 years ago
node_info *p = statement_head; //make a temp of list head, so we can traverse it.
//TODO replace with double linked list end
5 years ago
while (p->statement_list != NULL) {
p = p->statement_list;
}
5 years ago
//TODO replace with double linked list end
5 years ago
while (p->next != NULL) {
p = p->next;
}
5 years ago
p->next = malloc(sizeof(node_info));
p->next = new_statement;
5 years ago
p->next->next = NULL;
}
5 years ago
return statement_head;
5 years ago
}
/*
Executes a statment.
@param nodeInfo statement to be executed
@return void.
*/
//TODO return bool or int detailing the status of execution.
5 years ago
void ex(node_info *n) {
5 years ago
switch(n->opperation){
case 1:
5 years ago
add_var(n->var->name, n->var->value, _var_map);
5 years ago
break;
//TODO expression stuff
case 2:
break;
//print
case 3:
switch(n->printExpression.opperation) {
case 1:
printf("%d\n", (int) n->printExpression.value);
break;
case 2:
printf("%s\n", n->printExpression.string);
break;
case 3:
printf("%s%lf\n", n->printExpression.string, n->printExpression.value);
break;
default:
5 years ago
printf("{ERROR} CHECK YACC FILE FOR INCORRECT PRINT\n");
5 years ago
}
break;
//logic (if, while, for, etc)
case 4:
switch(n->logicalExpression.opperation) {
case 1:
if (n->logicalExpression.expr == 1) {
ex(n->logicalExpression.if_true);
}
break;
case 2:
while (n->logicalExpression.expr == 1) {
ex(n->logicalExpression.while_true);
}
break;
default:
5 years ago
printf("{ERROR} SOMETHING WRONG WITH LOGICAL EXPRESSION CHECK YACC\n");
exit(1);
5 years ago
}
break;
case 5:
while(n->next != NULL) {
n = n->next;
ex(n);
}
break;
case 6:
;
5 years ago
variable_values *value = get_value(n->name, _var_map);
5 years ago
switch(value->type) {
5 years ago
case VAR_STRING:
5 years ago
5 years ago
printf("%s\n", value->_string);
5 years ago
break;
5 years ago
case VAR_INT:
5 years ago
5 years ago
printf("%d\n", value->_int);
5 years ago
break;
5 years ago
case VAR_CHAR:
5 years ago
5 years ago
printf("%c\n", value->_char);
5 years ago
break;
5 years ago
case VAR_DOUBLE:
5 years ago
5 years ago
printf("%f\n", value->_double);
break;
case VAR_VOID: //TODO add void variable type
5 years ago
break;
default:
5 years ago
printf("{ERROR} NO SUCH VARIABLE TYPE CHECK YACC!\n");
exit(1);
5 years ago
}
break;
5 years ago
//TODO update default to case 7 for functions
5 years ago
default:
;
5 years ago
switch(n->type) {
5 years ago
5 years ago
case FUNCTION_CALL:
;
//gets the statements of a function with the provided name
node_info *_stmts = *map_get(&*((map_void_t* ) _function_list), n->name);
while (_stmts->next != NULL) { //loop over all the statements and execute them
_stmts = _stmts->next;
ex(_stmts);
}
break;
5 years ago
5 years ago
case FUNCTION:
;
//if the list of functions already defined is null then we malloc space for it.
if (_function_list == NULL) {
_function_list = malloc(sizeof(map_void_t));
n->_var_list = malloc(sizeof(node_info));
n->next = NULL;
n->local_list = NULL;
map_init(&*((map_void_t *) _function_list));
map_init(&*((map_void_t *) n->_var_list));
//put the function in the hashmap
map_set(&*((map_void_t* ) _function_list), n->name, &*n->_function_body);
} else { //else we dont malloc, we just put the function inside of the hashmap.
5 years ago
5 years ago
map_set(&*((map_void_t* ) _function_list), n->name, &*n->_function_body);
}
break;
5 years ago
5 years ago
default:
;
//TODO add error here and close
}
5 years ago
}
5 years ago
}