Skip to content

Commit

Permalink
Refactor the parts of Chaos.h related to complex elements and add get…
Browse files Browse the repository at this point in the history
…ListLength(), getDictLength() and getDictKeyByIndex() functions
  • Loading branch information
mertyildiran committed Apr 25, 2020
1 parent e947979 commit 1f1f6ff
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 40 deletions.
75 changes: 35 additions & 40 deletions Chaos.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,92 +46,87 @@ char* getVariableString(char *name) {
return getSymbolValueString(symbol_name);
}

unsigned long getListLength(char *name) {
Symbol* symbol = getSymbol(name);
return symbol->children_count;
}

bool getListElementBool(char *name, long long i) {
char *symbol_name = malloc(strlen(name) + 1);
strcpy(symbol_name, name);
Symbol* symbol = getListElement(getSymbol(symbol_name), i);
Symbol* symbol = getListElement(getSymbol(name), i);
if (symbol->value_type != V_BOOL) {
throw_error(E_UNEXPECTED_VALUE_TYPE, getValueTypeName(symbol->value_type), symbol->name);
}
return symbol->value.b;
return _getSymbolValueBool(symbol);
}

long long getListElementInt(char *name, long long i) {
char *symbol_name = malloc(strlen(name) + 1);
strcpy(symbol_name, name);
Symbol* symbol = getListElement(getSymbol(symbol_name), i);
Symbol* symbol = getListElement(getSymbol(name), i);
if (symbol->value_type != V_INT) {
throw_error(E_UNEXPECTED_VALUE_TYPE, getValueTypeName(symbol->value_type), symbol->name);
}
return symbol->value.i;
return _getSymbolValueInt(symbol);
}

long double getListElementFloat(char *name, long long i) {
char *symbol_name = malloc(strlen(name) + 1);
strcpy(symbol_name, name);
Symbol* symbol = getListElement(getSymbol(symbol_name), i);
free(symbol_name);
Symbol* symbol = getListElement(getSymbol(name), i);
if (symbol->value_type != V_FLOAT) {
throw_error(E_UNEXPECTED_VALUE_TYPE, getValueTypeName(symbol->value_type), symbol->name);
}
return symbol->value.f;
return _getSymbolValueFloat(symbol);
}

char* getListElementString(char *name, long long i) {
char *symbol_name = malloc(strlen(name) + 1);
strcpy(symbol_name, name);
Symbol* symbol = getListElement(getSymbol(symbol_name), i);
Symbol* symbol = getListElement(getSymbol(name), i);
if (symbol->value_type != V_STRING) {
throw_error(E_UNEXPECTED_VALUE_TYPE, getValueTypeName(symbol->value_type), symbol->name);
}
char* value;
value = malloc(1 + strlen(symbol->value.s));
strcpy(value, symbol->value.s);
return value;
return _getSymbolValueString(symbol);
}

unsigned long getDictLength(char *name) {
Symbol* symbol = getSymbol(name);
return symbol->children_count;
}

char* getDictKeyByIndex(char *name, long long i) {
Symbol* symbol = getSymbol(name);
Symbol* child = symbol->children[i];
char* key;
key = malloc(1 + strlen(child->key));
strcpy(key, child->key);
return key;
}

bool getDictElementBool(char *name, char *key) {
char *symbol_name = malloc(strlen(name) + 1);
strcpy(symbol_name, name);
Symbol* symbol = getDictElement(getSymbol(symbol_name), key);
Symbol* symbol = getDictElement(getSymbol(name), key);
if (symbol->value_type != V_BOOL) {
throw_error(E_UNEXPECTED_VALUE_TYPE, getValueTypeName(symbol->value_type), symbol->name);
}
return symbol->value.b;
return _getSymbolValueBool(symbol);
}

long long getDictElementInt(char *name, char *key) {
char *symbol_name = malloc(strlen(name) + 1);
strcpy(symbol_name, name);
Symbol* symbol = getDictElement(getSymbol(symbol_name), key);
Symbol* symbol = getDictElement(getSymbol(name), key);
if (symbol->value_type != V_INT) {
throw_error(E_UNEXPECTED_VALUE_TYPE, getValueTypeName(symbol->value_type), symbol->name);
}
return symbol->value.i;
return _getSymbolValueInt(symbol);
}

long double getDictElementFloat(char *name, char *key) {
char *symbol_name = malloc(strlen(name) + 1);
strcpy(symbol_name, name);
Symbol* symbol = getDictElement(getSymbol(symbol_name), key);
Symbol* symbol = getDictElement(getSymbol(name), key);
if (symbol->value_type != V_FLOAT) {
throw_error(E_UNEXPECTED_VALUE_TYPE, getValueTypeName(symbol->value_type), symbol->name);
}
return symbol->value.f;
return _getSymbolValueFloat(symbol);
}

char* getDictElementString(char *name, char *key) {
char *symbol_name = malloc(strlen(name) + 1);
strcpy(symbol_name, name);
Symbol* symbol = getDictElement(getSymbol(symbol_name), key);
free(symbol_name);
Symbol* symbol = getDictElement(getSymbol(name), key);
if (symbol->value_type != V_STRING) {
throw_error(E_UNEXPECTED_VALUE_TYPE, getValueTypeName(symbol->value_type), symbol->name);
}
char* value;
value = malloc(1 + strlen(symbol->value.s));
strcpy(value, symbol->value.s);
return value;
return _getSymbolValueString(symbol);
}

void returnVariableBool(bool b) {
Expand Down
6 changes: 6 additions & 0 deletions Chaos.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ bool getVariableBool(char *name);
long long getVariableInt(char *name);
long double getVariableFloat(char *name);
char* getVariableString(char *name);
unsigned long getListLength(char *name);
bool getListElementBool(char *name, long long i);
long long getListElementInt(char *name, long long i);
long double getListElementFloat(char *name, long long i);
char* getListElementString(char *name, long long i);
unsigned long getDictLength(char *name);
char* getDictKeyByIndex(char *name, long long i);
bool getDictElementBool(char *name, char *key);
long long getDictElementInt(char *name, char *key);
long double getDictElementFloat(char *name, char *key);
Expand All @@ -44,10 +47,13 @@ struct Kaos {
long long (*getVariableInt)(char *name);
long double (*getVariableFloat)(char *name);
char* (*getVariableString)(char *name);
unsigned long (*getListLength)(char *name);
bool (*getListElementBool)(char *name, long long i);
long long (*getListElementInt)(char *name, long long i);
long double (*getListElementFloat)(char *name, long long i);
char* (*getListElementString)(char *name, long long i);
unsigned long (*getDictLength)(char *name);
char* (*getDictKeyByIndex)(char *name, long long i);
bool (*getDictElementBool)(char *name, char *key);
long long (*getDictElementInt)(char *name, char *key);
long double (*getDictElementFloat)(char *name, char *key);
Expand Down
3 changes: 3 additions & 0 deletions modules/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ void initKaosApi() {
kaos.getVariableInt = getVariableInt;
kaos.getVariableFloat = getVariableFloat;
kaos.getVariableString = getVariableString;
kaos.getListLength = getListLength;
kaos.getListElementBool = getListElementBool;
kaos.getListElementInt = getListElementInt;
kaos.getListElementFloat = getListElementFloat;
kaos.getListElementString = getListElementString;
kaos.getDictLength = getDictLength;
kaos.getDictKeyByIndex = getDictKeyByIndex;
kaos.getDictElementBool = getDictElementBool;
kaos.getDictElementInt = getDictElementInt;
kaos.getDictElementFloat = getDictElementFloat;
Expand Down
16 changes: 16 additions & 0 deletions symbol.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ Symbol* deepCopyComplex(char *name, Symbol* symbol) {
char* getSymbolValueString(char *name) {
Symbol* symbol = getSymbol(name);
free(name);
return _getSymbolValueString(symbol);
}

char* _getSymbolValueString(Symbol* symbol) {
char* value;
if (symbol->value_type == V_STRING) {
value = malloc(1 + strlen(symbol->value.s));
Expand All @@ -222,6 +226,10 @@ char* getSymbolValueString(char *name) {
long double getSymbolValueFloat(char *name) {
Symbol* symbol = getSymbol(name);
free(name);
return _getSymbolValueFloat(symbol);
}

long double _getSymbolValueFloat(Symbol* symbol) {
long double value;
switch (symbol->value_type)
{
Expand All @@ -247,6 +255,10 @@ long double getSymbolValueFloat(char *name) {
bool getSymbolValueBool(char *name) {
Symbol* symbol = getSymbol(name);
free(name);
return _getSymbolValueBool(symbol);
}

bool _getSymbolValueBool(Symbol* symbol) {
bool value;
switch (symbol->value_type)
{
Expand All @@ -273,6 +285,10 @@ bool getSymbolValueBool(char *name) {
long long getSymbolValueInt(char *name) {
Symbol* symbol = getSymbol(name);
free(name);
return _getSymbolValueInt(symbol);
}

long long _getSymbolValueInt(Symbol* symbol) {
long long value;
switch (symbol->value_type)
{
Expand Down
4 changes: 4 additions & 0 deletions symbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,13 @@ Symbol* getSymbolById(unsigned long long id);
Symbol* deepCopySymbol(Symbol* symbol, enum Type type, char *key);
Symbol* deepCopyComplex(char *name, Symbol* symbol);
char* getSymbolValueString(char *name);
char* _getSymbolValueString(Symbol* symbol);
long double getSymbolValueFloat(char *name);
long double _getSymbolValueFloat(Symbol* symbol);
bool getSymbolValueBool(char *name);
bool _getSymbolValueBool(Symbol* symbol);
long long getSymbolValueInt(char *name);
long long _getSymbolValueInt(Symbol* symbol);
char* getSymbolValueString_NullIfNotString(Symbol* symbol);
long long getSymbolValueInt_ZeroIfNotInt(Symbol* symbol);
void printSymbolValue(Symbol* symbol, bool is_complex, bool pretty, bool escaped, unsigned long iter);
Expand Down

0 comments on commit 1f1f6ff

Please sign in to comment.