#include "variable.h" struct variable * variabe_new() { struct variable * variableInstance = malloc( sizeof( struct variable ) ); return variableInstance; } // split on whitespace and on * // int *name // int* name // int * name; // const int *name // depends on classesArray void variable_getVariablesByArgumentParts( struct array * argumentParts, struct array * variables, struct array * classesArray ) { int count = array_length( argumentParts ); int exists = -1; char * className = ""; for ( int i = 0; i < count; ++i ) { char * symbol = array_get( argumentParts, i ); //printf("add argument to variable. %s\n\n", symbol); int symbolIsClass = class_findClassByClassNameExists( allClasses, symbol ); if( symbolIsClass == 1 ) { exists = 1; className = symbol; } } if( exists == 1 ) { char * argumentName = array_get( argumentParts, array_length( argumentParts ) - 1 ); struct variable * variableInstance = malloc( sizeof( struct variable ) ); variableInstance->variableName = argumentName; variableInstance->datatype = className; //printf("\n\n\n add argument as variable: variable->variableName %s variableInstance->datatype %s\n\n\n", variableInstance->variableName, variableInstance->datatype); variableInstance->isPointer = variable_isPointer( argumentParts ); array_add( variables, variableInstance ); } } struct variable * variable_getByName( struct array * variables, char * name ) { int count = array_length( variables ); //printf("variable count %i %s \n", count, name ); //for (int i = 0; i < count; ++i) //{ for (int i = count - 1; i >= 0; --i) { struct variable * currentVariable = ( struct variable * ) array_get( variables, i ); //printf("get variable by name %s %s\n", currentVariable->variableName, name); if ( strcmp( currentVariable->variableName, name ) == 0 ) { return currentVariable; } } //printf("error: Variable not found;\n"); struct variable * emptyVariable = malloc( sizeof( struct variable ) * 10 ); emptyVariable->datatype = "this_variable_is_missing"; //printf("\n\nvariable not found: %s \n", name); return emptyVariable; } void variable_extractFromDeclaration( lexer * currentLexer, int currentKey, int i, char * body, struct array * variables ) { int previouskey = lexer_getKey( currentLexer, i - 1 ); char * variableDeclaration = text_removeWhiteSpaces( text_slice( body, previouskey + 1, currentKey - 1 ) ); char * previousToken = lexer_getToken( currentLexer, i - 1 ); //printf(" variable: %s\n", variableDeclaration ); if( strcmp( previousToken, ";" ) == 0 || strcmp( previousToken, "{" ) == 0 || strcmp( previousToken, "}" ) == 0 ) { struct array * variableDeclarationParts = text_split( variableDeclaration, " \t" ); if( array_length( variableDeclarationParts ) == 1 ) { // this is an global var without an datatype in the declaration return; } char * datatype = tools_findDatatype( variableDeclarationParts ); //printf(" datatype: %s\n", datatype); char * variableName = array_get( variableDeclarationParts, array_length( variableDeclarationParts ) - 1 ); //printf(" variableName: %s\n", variableName); if( variableDeclaration == NULL ) { printf("Error variable not found: %s\n", text_copy( variableDeclaration ) ); return; } int isPointer = variable_isPointer( variableDeclarationParts ); struct variable * variableInstance = malloc( sizeof( struct variable ) ); variableInstance->variableName = variableName; variableInstance->datatype = datatype; variableInstance->isPointer = isPointer; array_add( variables, variableInstance ); if( variableInstance->variableName == NULL ) { printf("Error, this datatype does not exist."); } printf("\n\n"); } else { //printf(" This is not a variable bacause previousToken = %s\n\n", previousToken); } } int variable_isPointer( struct array * propertyParts ) { int isPointer = -1; int propertyPartsLength = array_length( propertyParts ); for ( int j = 0; j < propertyPartsLength; ++j ) //propertyPartsLength-1 { char * part = (char *) array_get( propertyParts, j ); if( strcmp( part, "const" ) == 0 ) { //printf("qualifier: %s\n", part); } else if( strcmp( part, "volatile" ) == 0 ) { //printf("qualifier: %s\n", part); } else if( strcmp( part, "*" ) == 0 ) { isPointer = 1; } else { //printf("datatype: %s\n", part); //propertyInstance->datatype = part; } } return isPointer; }