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.

140 lines
3.1 KiB

5 years ago
#ifndef VARS_C
#define VARS_C
#include "vars.h"
/*
Author: xerox
Date: 1/20/2020
add var to hashmap of variables.
*/
static void add_var(char* p_name, variable_values* p_values, map_void_t* p_var_map)
5 years ago
{
if (!p_name || !p_values)
return;
map_set(p_var_map, p_name, p_values);
}
/*
Author: xerox
Date: 1/20/2020
get value given name and hashmap.
*/
static variable_values* get_value(char* p_name, map_void_t* p_var_map)
5 years ago
{
if (!p_name)
return NULL;
return *map_get(&*((map_void_t* ) p_var_map), p_name);
}
/*
Author: xerox
Date: 1/20/2020
makes a variable struct and define it.
*/
static variable* make_variable(char* p_name, void* value, variable_type var_type)
5 years ago
{
if (!p_name || !value)
5 years ago
return NULL;
variable* new_var = malloc(sizeof(variable));
variable_values* new_value = malloc(sizeof(variable_values));
5 years ago
new_value->type = var_type;
new_value->data_ptr = value;
new_var->name = p_name;
new_var->value = new_value;
return new_var;
}
/*
Author: xerox
Date: 1/20/2020
allocate memory for values that are 8 bytes or less.
*/
static void* alloc_value(void* value, variable_type var_type)
{
if (!value)
return NULL;
5 years ago
switch (var_type)
{
case VAR_INT:
{
int* p_int = malloc(sizeof(int));
5 years ago
*p_int = *(int*)value;
return p_int;
}
case VAR_SHORT:
{
short* p_short = malloc(sizeof(short));
5 years ago
*p_short = *(short*)value;
return p_short;
}
case VAR_BYTE:
{
char* p_char = malloc(sizeof(char));
if (!p_char)
return NULL;
memset(p_char, NULL, sizeof(*p_char));
*p_char = *(char*)value;
return p_char;
}
case VAR_DOUBLE:
{
double* p_double = malloc(sizeof(double));
*p_double = *(double*)value;
return p_double;
}
default:
return NULL;
}
}
/*
Author: xerox
Updated: 1/19/2020
create variable given name, value, and type.
*/
static node_info* create_variable(char* p_name, void* value, variable_type var_type)
{
if (!p_name || !value)
return NULL;
node_info* new_node = malloc(sizeof(node_info));
variable* new_var = make_variable(p_name, value, var_type);
new_node->assignment = malloc(sizeof(assignment_operation));
new_node->operation = ASSIGNMENT_OPERATION;
new_node->operation_type.assignment = ASSIGN_FROM_CONST;
new_node->assignment->var = new_var;
new_node->statement_list = NULL;
new_node->next = NULL;
return new_node;
}
/*
Author: xerox
Updated: 1/20/2020
Move value from one variable to another.
*/
static node_info* move_value(char* to, char* from)
{
if (!from || !to)
return NULL;
node_info* new_node = malloc(sizeof(node_info));
new_node->assignment = malloc(sizeof(*new_node->assignment));
new_node->operation = ASSIGNMENT_OPERATION;
new_node->operation_type.assignment = ASSIGN_FROM_VAR;
new_node->assignment->from = from;
new_node->assignment->to = to;
new_node->statement_list = NULL;
new_node->next = NULL;
return new_node;
}
5 years ago
#endif