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.
124 lines
3.6 KiB
124 lines
3.6 KiB
#ifndef FUNCTIONS_C
|
|
#define FUNCTIONC_C
|
|
#include "functions.h"
|
|
|
|
static pnode_info create_function(const char* p_name, const pnode_info args, const pnode_info p_stmts)
|
|
{
|
|
if (!p_name || !p_stmts)
|
|
return NULL;
|
|
|
|
pnode_info new_node = malloc(sizeof(node_info));
|
|
new_node->function_body = malloc(sizeof(node_info));
|
|
new_node->operation = FUNCTION_OPERATION;
|
|
new_node->operation_type.func = FUNCTION;
|
|
new_node->name = p_name;
|
|
new_node->next = NULL;
|
|
|
|
new_node->function_body->arguments = args;
|
|
new_node->function_body->scope_map = _var_map;
|
|
new_node->function_body->statement_list = p_stmts;
|
|
|
|
// causes a new scope to be created
|
|
_var_map = malloc(sizeof(map_void_t));
|
|
map_init(_var_map);
|
|
|
|
if (args)
|
|
{
|
|
// clear argument list
|
|
_arg_list = malloc(sizeof(node_info));
|
|
_arg_list->next = NULL;
|
|
}
|
|
return new_node;
|
|
}
|
|
|
|
static pnode_info create_function_call(const char* p_name, const pnode_info arg_list)
|
|
{
|
|
if (!p_name)
|
|
return NULL;
|
|
|
|
const pnode_info new_node = malloc(sizeof(node_info));
|
|
new_node->operation = FUNCTION_OPERATION;
|
|
new_node->operation_type.func = FUNCTION_CALL;
|
|
new_node->name = p_name;
|
|
new_node->arguments = arg_list;
|
|
|
|
// set the current scope for the call
|
|
new_node->scope_map = _var_map;
|
|
new_node->next = NULL;
|
|
|
|
if (arg_list)
|
|
{
|
|
// clear argument list
|
|
_arg_list = malloc(sizeof(node_info));
|
|
_arg_list->next = NULL;
|
|
}
|
|
return new_node;
|
|
}
|
|
|
|
static pnode_info create_compound_statement(const pnode_info new_statement_list, pnode_info statement_head)
|
|
{
|
|
if (!new_statement_list)
|
|
return NULL;
|
|
|
|
if (!statement_head)
|
|
{
|
|
const pnode_info new_node = malloc(sizeof(node_info));
|
|
statement_head = malloc(sizeof(node_info));
|
|
|
|
statement_head->statement_list = malloc(sizeof(node_info));
|
|
statement_head->operation = EXEC_STMT_LIST_OPERATION;
|
|
statement_head->statement_list->operation = EXEC_STMT_LIST_OPERATION;
|
|
statement_head->statement_list->next = new_statement_list;
|
|
statement_head->next = NULL;
|
|
statement_head->statement_list->statement_list = NULL;
|
|
}
|
|
else
|
|
{
|
|
pnode_info p_temp_head = statement_head;
|
|
//TODO update with doubly linked list
|
|
while (p_temp_head->statement_list != NULL)
|
|
p_temp_head = p_temp_head->statement_list;
|
|
|
|
p_temp_head->statement_list = malloc(sizeof(node_info));
|
|
p_temp_head->operation = EXEC_STMT_LIST_OPERATION;
|
|
p_temp_head->statement_list->operation = EXEC_STMT_LIST_OPERATION;
|
|
p_temp_head->statement_list->next = new_statement_list;
|
|
p_temp_head->statement_list->statement_list = NULL;
|
|
}
|
|
return statement_head;
|
|
}
|
|
|
|
static pnode_info add_to_compound_statement(const pnode_info new_statement, const pnode_info statement_head)
|
|
{
|
|
if (!new_statement || !statement_head)
|
|
return NULL;
|
|
|
|
pnode_info p_head_tmp = statement_head;
|
|
while (p_head_tmp->statement_list)
|
|
p_head_tmp = p_head_tmp->statement_list;
|
|
|
|
while (p_head_tmp->next)
|
|
p_head_tmp = p_head_tmp->next;
|
|
|
|
p_head_tmp->next = malloc(sizeof(node_info));
|
|
p_head_tmp->next = new_statement;
|
|
return statement_head;
|
|
}
|
|
|
|
static pnode_info append_argument(const char* var_name)
|
|
{
|
|
if (!var_name)
|
|
return NULL;
|
|
|
|
const pnode_info new_arg = malloc(sizeof(node_info));
|
|
new_arg->name = var_name;
|
|
new_arg->next = NULL;
|
|
pnode_info temp_arg_head = _arg_list;
|
|
|
|
while (temp_arg_head->next)
|
|
temp_arg_head = temp_arg_head->next;
|
|
|
|
temp_arg_head->next = new_arg;
|
|
return _arg_list;
|
|
}
|
|
#endif |