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.
199 lines
5.8 KiB
199 lines
5.8 KiB
#ifndef PARSER_C
|
|
#define PARSER_C
|
|
#include "parser.h"
|
|
|
|
static void exec_stmt(pnode_info exec_node)
|
|
{
|
|
if (!exec_node)
|
|
return;
|
|
|
|
switch (exec_node->operation)
|
|
{
|
|
case ASSIGNMENT_OPERATION:
|
|
switch (exec_node->operation_type.assignment)
|
|
{
|
|
case ASSIGN_FROM_CONST:
|
|
add_var(
|
|
exec_node->assignment->var->name,
|
|
exec_node->assignment->var->value,
|
|
exec_node->scope_map
|
|
);
|
|
break;
|
|
case ASSIGN_FROM_VAR:
|
|
add_var(exec_node->assignment->to,
|
|
get_value(exec_node->assignment->from, exec_node->scope_map),
|
|
exec_node->scope_map
|
|
);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
break;
|
|
case LOGIC_OPERATION:
|
|
{
|
|
switch (exec_node->logical_expr->type)
|
|
{
|
|
case IF_LOGIC:
|
|
{
|
|
const pvariable_values p_var_values = get_value(
|
|
exec_node->logical_expr->condition_var,
|
|
exec_node->scope_map
|
|
);
|
|
|
|
if (p_var_values && *(unsigned char*)p_var_values->data_ptr)
|
|
exec_stmt(exec_node->logical_expr->stmt_list);
|
|
break;
|
|
}
|
|
case WHILE_LOGIC:
|
|
{
|
|
pvariable_values p_var_values = get_value(exec_node->logical_expr->condition_var, exec_node->scope_map);
|
|
if (!p_var_values)
|
|
return;
|
|
|
|
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, exec_node->scope_map);
|
|
}
|
|
break;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
break;
|
|
}
|
|
// execute statement list.
|
|
case EXEC_STMT_LIST_OPERATION:
|
|
{
|
|
if (exec_node->statement_list)
|
|
exec_node = exec_node->statement_list;
|
|
|
|
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:
|
|
{
|
|
// TODO replace with puts to add more modularity
|
|
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:
|
|
break;
|
|
}
|
|
break;
|
|
}
|
|
case PRINT_VARIABLE:
|
|
{
|
|
const pvariable_values p_var = get_value(exec_node->var->name, exec_node->scope_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:
|
|
break;
|
|
}
|
|
break;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
break;
|
|
}
|
|
case FUNCTION_OPERATION:
|
|
{
|
|
switch (exec_node->operation_type.func)
|
|
{
|
|
case FUNCTION_CALL:
|
|
{
|
|
pnode_info _stmts = *map_get(&*((map_void_t*)_function_map), exec_node->name);
|
|
if (_stmts)
|
|
{
|
|
// for each argument set the values in the function to the arguments in the calling function
|
|
pnode_info tmp_arg_list = exec_node->arguments;
|
|
pnode_info tmp_func_arg_list = _stmts->arguments;
|
|
if (tmp_arg_list && tmp_func_arg_list)
|
|
{
|
|
while (tmp_arg_list->next && tmp_func_arg_list->next)
|
|
{
|
|
tmp_func_arg_list = tmp_func_arg_list->next;
|
|
tmp_arg_list = tmp_arg_list->next;
|
|
|
|
const pvariable_values values = get_value(
|
|
tmp_arg_list->name,
|
|
exec_node->scope_map
|
|
);
|
|
|
|
map_set_(
|
|
_stmts->scope_map,
|
|
tmp_func_arg_list->name,
|
|
&values,
|
|
sizeof(variable_values*)
|
|
);
|
|
}
|
|
}
|
|
|
|
_stmts = _stmts->statement_list;
|
|
while (_stmts->statement_list)
|
|
{
|
|
_stmts = _stmts->statement_list;
|
|
exec_stmt(_stmts);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
case FUNCTION:
|
|
{
|
|
if (!_function_map)
|
|
{
|
|
_function_map = malloc(sizeof(map_void_t));
|
|
map_init(_function_map);
|
|
map_set(_function_map, exec_node->name, exec_node->function_body);
|
|
}
|
|
else
|
|
map_set(_function_map, exec_node->name, exec_node->function_body);
|
|
break;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
break;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
#endif |