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.
182 lines
4.9 KiB
182 lines
4.9 KiB
5 years ago
|
#ifndef PARSER_C
|
||
|
#define PARSER_C
|
||
|
#include "parser.h"
|
||
|
|
||
|
/*
|
||
|
Author: xerox
|
||
|
Updated: 1/20/2020
|
||
|
|
||
|
Executes a statment.
|
||
|
|
||
|
@param nodeInfo statement to be executed
|
||
|
@return void.
|
||
|
*/
|
||
|
|
||
|
static void exec_stmt(node_info *exec_node)
|
||
|
{
|
||
|
if (!exec_node)
|
||
|
return;
|
||
|
|
||
|
switch (exec_node->operation)
|
||
|
{
|
||
|
case ASSIGNMENT_OPERATION:
|
||
|
switch (exec_node->operation_type.assignment)
|
||
|
{
|
||
|
case ASSIGN_FROM_CONST:
|
||
5 years ago
|
add_var(
|
||
|
exec_node->assignment->var->name,
|
||
|
exec_node->assignment->var->value,
|
||
|
_var_map
|
||
|
);
|
||
5 years ago
|
break;
|
||
|
case ASSIGN_FROM_VAR:
|
||
|
add_var(exec_node->assignment->to,
|
||
5 years ago
|
get_value(exec_node->assignment->from, _var_map),
|
||
|
_var_map
|
||
|
);
|
||
5 years ago
|
break;
|
||
|
default:
|
||
5 years ago
|
break;
|
||
5 years ago
|
}
|
||
|
break;
|
||
|
case LOGIC_OPERATION:
|
||
|
{
|
||
|
switch (exec_node->logical_expr->type)
|
||
|
{
|
||
|
case IF_LOGIC:
|
||
|
{
|
||
5 years ago
|
variable_values* p_var_values = get_value(
|
||
|
exec_node->logical_expr->condition_var,
|
||
|
_var_map
|
||
|
);
|
||
|
|
||
|
if (p_var_values && *(unsigned char*)p_var_values->data_ptr)
|
||
5 years ago
|
exec_stmt(exec_node->logical_expr->stmt_list);
|
||
|
break;
|
||
|
}
|
||
|
case WHILE_LOGIC:
|
||
|
{
|
||
|
variable_values* p_var_values = get_value(exec_node->logical_expr->condition_var, _var_map);
|
||
|
if (!p_var_values)
|
||
|
return;
|
||
5 years ago
|
|
||
5 years ago
|
while (*(unsigned char*)p_var_values->data_ptr)
|
||
|
{
|
||
|
exec_stmt(exec_node->logical_expr->stmt_list);
|
||
|
p_var_values = get_value(exec_node->logical_expr->condition_var, _var_map);
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
default:
|
||
5 years ago
|
break;
|
||
5 years ago
|
}
|
||
5 years ago
|
break;
|
||
5 years ago
|
}
|
||
|
// execute statement list.
|
||
|
case EXEC_STMT_LIST_OPERATION:
|
||
|
{
|
||
5 years ago
|
if (exec_node->statement_list)
|
||
5 years ago
|
exec_node = exec_node->statement_list;
|
||
5 years ago
|
|
||
5 years ago
|
while (exec_node->next)
|
||
|
{
|
||
|
exec_node = exec_node->next;
|
||
|
exec_stmt(exec_node);
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
// print statement
|
||
|
case PRINT_OPERATION:
|
||
|
{
|
||
|
switch (exec_node->operation_type.print)
|
||
|
{
|
||
|
case PRINT_CONSTANT:
|
||
|
{
|
||
5 years ago
|
// TODO replace with puts to add more modularity
|
||
5 years ago
|
switch (exec_node->print_expr->type)
|
||
|
{
|
||
|
case VAR_STRING:
|
||
|
printf("%s\n", (char*)exec_node->print_expr->value);
|
||
|
break;
|
||
|
case VAR_INT:
|
||
|
printf("%d\n", *(int*)exec_node->print_expr->value);
|
||
|
break;
|
||
|
case VAR_BYTE:
|
||
|
printf("%c\n", *(char*)exec_node->print_expr->value);
|
||
|
break;
|
||
|
case VAR_DOUBLE:
|
||
|
printf("%f\n", *(double*)exec_node->print_expr->value);
|
||
|
break;
|
||
|
case VAR_VOID: //TODO add void variable type
|
||
|
break;
|
||
|
default:
|
||
5 years ago
|
break;
|
||
5 years ago
|
}
|
||
|
break;
|
||
|
}
|
||
|
case PRINT_VARIABLE:
|
||
|
{
|
||
|
variable_values* p_var = get_value(exec_node->var->name, _var_map);
|
||
|
switch (p_var->type)
|
||
|
{
|
||
|
case VAR_STRING:
|
||
|
printf("%s\n", (char*)p_var->data_ptr);
|
||
|
break;
|
||
|
case VAR_INT:
|
||
|
printf("%d\n", *(int*)p_var->data_ptr);
|
||
|
break;
|
||
|
case VAR_BYTE:
|
||
|
printf("%c\n", *(char*)p_var->data_ptr);
|
||
|
break;
|
||
|
case VAR_DOUBLE:
|
||
|
printf("%f\n", *(double*)p_var->data_ptr);
|
||
|
break;
|
||
|
case VAR_VOID: //TODO add void variable type
|
||
|
break;
|
||
|
default:
|
||
5 years ago
|
break;
|
||
5 years ago
|
}
|
||
|
break;
|
||
|
}
|
||
|
default:
|
||
5 years ago
|
break;
|
||
5 years ago
|
}
|
||
|
break;
|
||
|
}
|
||
|
case FUNCTION_OPERATION:
|
||
|
{
|
||
|
switch (exec_node->operation_type.func)
|
||
|
{
|
||
|
case FUNCTION_CALL:
|
||
|
{
|
||
|
node_info* _stmts = *map_get(&*((map_void_t*)_function_list), exec_node->name);
|
||
|
_stmts = _stmts->statement_list;
|
||
|
while (_stmts->statement_list)
|
||
|
{
|
||
|
_stmts = _stmts->statement_list;
|
||
|
exec_stmt(_stmts);
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
case FUNCTION:
|
||
|
{
|
||
|
if (!_function_list)
|
||
|
{
|
||
|
_function_list = malloc(sizeof(map_void_t));
|
||
|
map_init(_function_list);
|
||
|
map_set(_function_list, exec_node->name, exec_node->function_body);
|
||
|
}
|
||
|
else //else we dont malloc, we just put the function inside of the hashmap.
|
||
|
map_set(_function_list, exec_node->name, exec_node->function_body);
|
||
|
break;
|
||
|
}
|
||
|
default:
|
||
5 years ago
|
break;
|
||
5 years ago
|
}
|
||
|
break;
|
||
|
}
|
||
|
default:
|
||
5 years ago
|
break;
|
||
5 years ago
|
}
|
||
|
}
|
||
|
#endif
|