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/vars/vars.c

145 lines
2.9 KiB

5 years ago
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../hashmap/map.h"
/**
* VariableType is a enum of type struct that holds the varible types that will
* be inside of my language.
*/
typedef enum VariableType{
STRINGVAR,
INT,
CHARVAR,
DOUBLE
} VariableType;
/**
* VariableValues is a struct that holds all possible variable values
*/
typedef struct VariableValues{
VariableType type;
char Char;
char *String;
int Int;
double Double;
} VariableValues;
/**
* Variables is a struct that has all the information needed for a variables
*/
typedef struct Variable{
char *name;
VariableValues *value;
struct Variable *next;
} Variable;
//TODO this needs to be handled somewhere else. hashmap of all variables
struct map_void_t *varMap = NULL;
/**
*
* Version: 0.2
*
* adds var to hashmap if its not already set
* else it will update the var with provided values.
* @return null
* @param string for var name
*
*/
void addVar(char *name, VariableValues *values) {
if(varMap == NULL) {
varMap = malloc(sizeof(map_void_t));
map_init(&*((map_void_t *) varMap));
}
map_set(&*((map_void_t* ) varMap), name, &*values);
}
/**
* returns variables values from hashmap
*
* @see [VariableValues]
* @see [char *string]
* @return [VariableValues] containing variables data
* @param [char *string] Variable name.
*
*/
VariableValues *getValue(char *name) {
return *map_get(&*((map_void_t* ) varMap), name);
}
/**
*
* This makes an integer varible and adds it to the linked list of
* vars.
* @return the head of the linked list which just points to all of the vars
*
*/
Variable* makeVariableInt(char *name, int Int) {
Variable *p = malloc(sizeof(Variable));
VariableValues *IntegerValue = malloc(sizeof(VariableValues));
IntegerValue->type = INT;
IntegerValue->Int = Int;
p->name = name;
p->value = IntegerValue;
return p;
}
Variable* makeVariableDouble(char *name, double Double) {
Variable *p = malloc(sizeof(Variable));
VariableValues *DoubleValue= malloc(sizeof(VariableValues));
DoubleValue->type = DOUBLE;
DoubleValue->Double = Double;
p->name = name;
p->value = DoubleValue;
return p;
}
Variable* makeVariableChar(char *name, char Char) {
Variable *p = malloc(sizeof(Variable));
VariableValues *CharacterValue = malloc(sizeof(VariableValues));
CharacterValue->type = CHARVAR;
CharacterValue->Char = Char;
p->name = name;
p->value = CharacterValue;
return p;
}
Variable* makeVariableString(char *name, char *String){
Variable *p = malloc(sizeof(Variable));
VariableValues *StringValue = malloc(sizeof(VariableValues));
StringValue->type = STRINGVAR;
StringValue->String = String;
p->name = name;
p->value = StringValue;
return p;
}