Initial commit

This commit is contained in:
2025-11-17 10:28:09 +01:00
parent 7bff81691f
commit 6ee36e26be
391 changed files with 110253 additions and 0 deletions

115
source/application.c Normal file
View File

@@ -0,0 +1,115 @@
#include "application.h"
struct application * application_new() {
struct application * applicationInstance = malloc( sizeof( struct application ) );
applicationInstance->classes = array_new();
applicationInstance->functions = array_new();
return applicationInstance;
}
void application_initializeGlobals() {
allClasses = array_new();
allFunctions = array_new();
}
void removeFilesFromPath( char * path ) {
printf("Delete all files from this path: %s\n\n", path);
}
array * application_extractIncludedFilePaths( application * currentApplication ) {
char * path = currentApplication->input;
struct array * unloadedFileNames = array_new();
struct array * loadedFileNames = array_new();
file_loadUnloadedFiles( unloadedFileNames, loadedFileNames, path );
printf("%i\n", array_length( loadedFileNames ) );
return loadedFileNames;
}
struct application * application_parseArguments( int argc, char * * argv ) {
application * applicationInstance = application_new();
for ( int i = 0; i < argc; ++i )
{
char * currentArgument = argv[ i ];
if( fileSystem_textIsFilename( currentArgument ) ) {
applicationInstance->input = currentArgument;
}
if( strcmp( currentArgument, "-o" ) == 0 ) {
i++;
applicationInstance->output = argv[ i ];
}
}
printf("\n\ninput: %s\n", applicationInstance->input);
printf("output: %s\n\n\n", applicationInstance->output);
fileSystem_ensureDirectory( applicationInstance->output );
return applicationInstance;
}
struct class * application_getClassByClassName( array * classesArray, char * className ) {
int count = array_length( classesArray );
for (int i = 0; i < count; ++i)
{
struct class * currentClass = array_get( classesArray, i );
if ( strcmp( currentClass->className, className ) == 0 ) {
return currentClass;
}
}
return NULL;
}
void application_printMemoryUsage() {
struct rusage r_usage;
getrusage( RUSAGE_SELF, &r_usage );
printf("Memory usage: %ld kilobytes\n",r_usage.ru_maxrss);
}

39
source/application.h Normal file
View File

@@ -0,0 +1,39 @@
#ifndef _APPLICATION
#define _APPLICATION
#include <text.h>
#include <fileSystem.h>
#include <class.h>
#include <array.h>
typedef struct application{
char * input;
char * output;
struct array * classes;
struct array * functions;
} application;
struct application * application_new();
void application_initializeGlobals();
array * application_extractIncludedFilePaths( struct application * currentApplication );
struct application * application_parseArguments( int argc, char **argv );
struct class * application_getClassByClassName( struct array * classesArray, char * className );
void application_printMemoryUsage();
#endif

88
source/argument.c Normal file
View File

@@ -0,0 +1,88 @@
#include "argument.h"
int argument_determineDatatypeOfArgument( char * argumentDeclaration, struct array * variables ) {
// int
if( text_isValidNumber( argumentDeclaration ) == 1 ) {
return -1;
}
// single quote
if( strchr( argumentDeclaration, 39) != NULL ) {
return -2;
}
// text
if( strchr( argumentDeclaration, '"') != NULL ) {
return -2;
}
struct variable * variableInstance = variable_getByName( variables, argumentDeclaration );
if( strcmp( variableInstance->datatype, "this_variable_is_missing" ) == 0 ) {
return 0;
}
if( strcmp( variableInstance->datatype, "int" ) == 0 ) {
return -1;
}
if( (strcmp(variableInstance->datatype, "char" ) == 0) && variableInstance->isPointer == 1 ) {
return -2;
}
//printf(" -----------check if this is an variable: %s %s\n\n", argumentDeclaration, variableInstance->datatype);
struct class * classInstance = application_getClassByClassName( allClasses, variableInstance->datatype );
if( classInstance == NULL ) {
printf(ANSI_COLOR_RED "\n\n\n\n ERROR class '%s' does not exist!!, The file containing the class '%s' is not included.\n\n\n\n" ANSI_COLOR_RESET, variableInstance->datatype, variableInstance->datatype );
return 0;
}
return ( int ) classInstance->classIndex;
}
struct array * argument_getArgumentDatatypes( struct array * arguments, struct array * variables ) {
int argumentCount = array_length( arguments );
struct array * argumentDatatypes = array_new();
for (int i = 0; i < argumentCount; ++i)
{
char * currentArgument = text_removeWhiteSpaces( array_get( arguments, i ) );
uintptr_t datatype = argument_determineDatatypeOfArgument( currentArgument, variables );
array_add( argumentDatatypes, (void *) datatype );
}
return argumentDatatypes;
}

27
source/argument.h Normal file
View File

@@ -0,0 +1,27 @@
#ifndef _argument
#define _argument
#include "text.h"
#include "class.h"
#include "array.h"
#include "text.h"
#include "variable.h"
#include "string.h"
#include <stdint.h>
struct array * argument_getArgumentDatatypes( struct array * arguments, struct array * variables );
int argument_determineDatatypeOfArgument( char * argumentDeclaration, struct array * variables );
#endif

184
source/array.c Executable file
View File

@@ -0,0 +1,184 @@
#include <stdio.h>
#include <stdlib.h>
#include "array.h"
array * array_new()
{
struct array * v;
v = (struct array *) malloc( sizeof( struct array ) );
v->capacity = ARRAY_INIT_CAPACITY;
v->total = 0;
v->items = malloc(sizeof(void *) * v->capacity);
return v;
}
int array_length(array *v)
{
return v->total;
}
char * array_join( struct array * parts, char * separator ) {
int count = array_length( parts );
char * fullText = "";
for (int i = 0; i < count; ++i)
{
char * currentPart = array_get( parts, i );
fullText = text_concatenate( fullText, currentPart );
if( i < count-1 ) {
fullText = text_concatenate( fullText, separator );
}
printf("join: %s\n", fullText);
//fullText = text_concatenate( fullText, separator );
}
return fullText;
}
static void array_resize( array *v, int capacity )
{
#ifdef DEBUG_ON
printf("array_resize: %d to %d\n", v->capacity, capacity);
#endif
void **items = realloc( v->items, sizeof( void * ) * capacity );
//if (items) {
v->items = items;
v->capacity = capacity;
//}
}
void array_add( array * v, void * item )
{
if ( v->capacity == v->total ){
array_resize( v, v->capacity * 2 );
}
v->items[v->total++] = item;
}
void array_set( array * v, int index, void * item )
{
if (index >= 0 && index < v->total){
v->items[index] = item;
}
}
void * array_get( array *v, int index )
{
if ( index >= 0 && index < v->total ){
return v->items[index];
}
return NULL;
}
int array_push( array * v, void * item ) {
array_add( v, item );
return v->total;
}
void array_unshift( array * v, void * item ) {
int length = v->total;
v->total++;
if ( v->capacity == v->total ){
array_resize( v, v->capacity * 2 );
}
for ( int i = length - 1; i >= 0; --i ) {
v->items[ i + 1 ] = v->items[ i ];
}
v->items[ 0 ] = item;
}
void array_delete( array * v, int index)
{
if ( index < 0 || index >= v->total ){
return;
}
v->items[index] = NULL;
for ( int i = index; i < v->total - 1; i++ ) {
v->items[i] = v->items[i + 1];
v->items[i + 1] = NULL;
}
v->total--;
if ( v->total > 0 && v->total == v->capacity / 4 ){
array_resize( v, v->capacity / 2 );
}
}
void * array_pop( array * v ) {
int count = array_length( v );
void * item = array_get( v, count - 1 );
array_delete( v, count - 1 );
return item;
}
void array_free(array *v)
{
free( v->items );
}

40
source/array.h Executable file
View File

@@ -0,0 +1,40 @@
#ifndef ARRAY_H
#define ARRAY_H
#define ARRAY_INIT_CAPACITY 1000000
#include "text.h"
typedef struct array {
void * * items;
int capacity;
int total;
} array;
array * array_new();
int array_length( array * );
static void array_resize( array *, int );
void array_add( array *, void * );
void array_set( array *, int, void * );
void * array_get( array *, int );
void array_delete( array *, int );
void array_free( array * );
char * array_join( struct array * parts, char * separator );
void array_unshift( array * v, void * item );
void * array_pop( array * v );
#endif

916
source/class.c Normal file
View File

@@ -0,0 +1,916 @@
#include "class.h"
struct class * class_new() {
struct class * classInstance = malloc( sizeof( struct class ) );
return classInstance;
}
void class_reIndexLineNumbers( struct class * baseClass ) {
int maxLineNumber = 0;
struct array * methods = baseClass->methods;
int methodsCount = array_length( methods );
for (int i = 0; i < methodsCount; ++i)
{
struct method * methodInstance = array_get( methods, i );
if( methodInstance->lineNumber > maxLineNumber ) {
maxLineNumber = methodInstance->lineNumber;
}
}
maxLineNumber += 12;
for (int i = 0; i < methodsCount; ++i)
{
struct method * methodInstance = array_get( methods, i );
if( methodInstance->lineNumber == -1 ) {
methodInstance->lineNumber = maxLineNumber;
maxLineNumber += text_findNumberOfLineBreaks( methodInstance->body );
maxLineNumber += 6;
}
}
}
void class_updateThisArgument( struct array * arguments, char * baseClassName ) {
struct text * newThisArgument = text_new( "" );
text_append( newThisArgument, "struct " );
text_append( newThisArgument, baseClassName );
text_append( newThisArgument, " * this" );
array_set( arguments, 0, newThisArgument->value );
//printf("update this argument: %s %s\n ", firstArgument, methodName);
}
int class_getMethodIndexByName( struct class * classInstance, char * methodName ) {
struct array * methods = classInstance->methods;
int count = array_length( methods );
for (int i = 0; i < count; ++i)
{
struct method * currentMethod = array_get( methods, i );
if( strcmp( currentMethod->methodName, methodName ) == 0 ) {
return i;
}
}
return -1;
}
void addMethodsToBaseClass( struct class * baseClass, struct class * extendedClass ) {
struct array * methods = extendedClass->methods;
int methodsCount = array_length( methods );
for (int i = 0; i < methodsCount; ++i)
{
struct method * methodInstance = array_get( methods, i );
struct method * methodCopy = malloc( sizeof( struct method ) );
methodCopy->methodName = methodInstance->methodName;
methodCopy->body = methodInstance->body;
methodCopy->returnType = methodInstance->returnType;
methodCopy->lineNumber = -1;
methodCopy->arguments = methodInstance->arguments;
int methodIndex = class_getMethodIndexByName( baseClass, methodInstance->methodName );
if( methodIndex == -1 ) {
class_updateThisArgument( methodCopy->arguments, baseClass->className );
array_add( baseClass->methods, methodCopy );
} else {
class_updateThisArgument( methodCopy->arguments, baseClass->className );
//array_set( baseClass->methods, methodIndex, methodCopy );
}
}
}
void addPropertiesToBaseClass( struct class * baseClass, struct class * extendedClass ) {
struct array * properties = extendedClass->properties;
int propertyCount = array_length( properties );
for (int i = 0; i < propertyCount; ++i)
{
struct property * propertyInstance = array_get( properties, i );
int propertyIndex = class_getPropertyIndexByName( baseClass, propertyInstance->propertyName );
if( propertyIndex == -1 ) {
array_add( baseClass->properties, propertyInstance );
} else {
array_set( baseClass->properties, propertyIndex, propertyInstance );
}
}
//printf("processCurrent extend: %s \n\n", extendedClass->className );
}
void class_extendClass( struct class * baseClass ) {
if( baseClass->hasExtended == 1 ) {
return;
}
struct array * extends = baseClass->extends;
int extendCount = array_length( extends );
//printf("extend: %s %i \n\n\n\n\n", baseClass->className, extendCount );
for ( int i = 0; i < extendCount; ++i )
{
char * extendName = text_removeWhiteSpaces( array_get( extends, i ) );
struct class * extendedClass = application_getClassByClassName( allClasses, extendName );
if( extendedClass == NULL ) {
printf("\n\n\n\n ERROR: Class '%s' tries to extend class '%s', Class '%s' does not exist. \n\n\n\n", baseClass->className, extendName, extendName);
exit( 0 );
}
if( extendedClass->hasExtended == -1 ) {
class_extendClass( extendedClass );
}
/*
addPropertiesToBaseClass( baseClass, extendedClass );
addMethodsToBaseClass( baseClass, extendedClass );
*/
}
class_reIndexLineNumbers( baseClass );
baseClass->hasExtended = 1;
}
int class_findClassByClassNameExists( array * classesArray, char * name ) {
int count = array_length( classesArray );
for (int i = 0; i < count; ++i)
{
struct class * currentClass = array_get( classesArray, i );
//printf(" %s\n",currentClass->className );
if ( strcmp( currentClass->className, name ) == 0 ) {
return 1;
}
}
return 0;
}
void class_extractMethod( lexer * currentLexer,
char * source,
int i,
struct class * classInstance,
struct array * classesArray,
struct array * functions ) {
double key = lexer_getKey( currentLexer, i );
array * validPreviousTokens = array_new();
array_add( validPreviousTokens, "{");
array_add( validPreviousTokens, "}");
array_add( validPreviousTokens, ";");
int previousKey = lexer_findPreviouseTokenIndex( currentLexer, i, validPreviousTokens ) + 1;
struct method * methodInstance = malloc( sizeof( struct method ) );
methodInstance->isSetter = false;
methodInstance->isGetter = false;
method_extractMethodNameAndReturnType( previousKey, key, source, methodInstance );
// &(int){ 0 } create inline pointer
methodInstance->bodyOriginal = class_extractBodyFromSource( currentLexer, i, source, 0 );
methodInstance->variables = method_extractArguments( currentLexer, key, source, i, classInstance, methodInstance, classesArray );
methodInstance->lineNumber = text_countLineBreaks( source, (int) key );
array_add( classInstance->methods, methodInstance );
}
char * class_extractBodyFromSource( lexer * currentLexer, int i, char * source, int bodyCloseIndex ) {
int bodyOpenIndex = lexer_findNextTokenIndex( currentLexer, i, "{" );
int bodyOpenKey = lexer_findNextToken( currentLexer, i, "{" );
bodyCloseIndex = lexer_findBodyCloseIndex( currentLexer, bodyOpenIndex );
int bodyCloseKey = lexer_getKey( currentLexer, bodyCloseIndex );
char * bodySource = text_slice( source, bodyOpenKey + 1, bodyCloseKey - 1 );
return bodySource;
}
void class_extractProperty( lexer * currentLexer, int key, int i, char * source, struct class * classInstance ) {
char * previousToken = lexer_getToken( currentLexer, i - 1 );
int hasValue = -1;
char * propertyValue = "";
char * propertySource = "";
struct property * propertyInstance = malloc( sizeof( struct property ) );
propertyInstance->propertyName = NULL;
propertyInstance->type = "default";
// todo this can be done more effectively
//printf("previousToken: %s\n", previousToken);
int shift = 0;
if ( strcmp( previousToken, "=" ) == 0 || strcmp( previousToken, "\"" ) == 0 ) {
int IsTokenKey = lexer_findPreviousTokenIndex( currentLexer, i, "=" );
int IsKey = lexer_getKey( currentLexer, IsTokenKey );
int keyBeforeIS = lexer_getKey( currentLexer, IsTokenKey - 1 );
propertyValue = text_slice( source, IsKey + 1, key - 1 );
propertySource = text_removeWhiteSpaces( text_slice( source, keyBeforeIS + 1, IsKey - 1 ) );
hasValue = 1;
} else if ( strcmp( previousToken, ")" ) == 0 ) {
shift = 3;
int IsTokenKey = lexer_findPreviousTokenIndex( currentLexer, i, "=" );
int previousSemicolon = lexer_findPreviousTokenIndex( currentLexer, i, ";" );
if( previousSemicolon > IsTokenKey ) {
printf("\n\n\n This is an function. \n\n\n\n\n\n\n");
int afterPreviousSemicolon = lexer_getKey( currentLexer, previousSemicolon );
int openArgumentKey = lexer_findNextToken( currentLexer, previousSemicolon, "(" );
int closeArgumentKey = lexer_findNextToken( currentLexer, previousSemicolon, ")" );
char * propertyName = text_removeWhiteSpaces( text_slice( source, openArgumentKey + 1 , closeArgumentKey - 1 ) ) ;
struct array * propertyNameParts = text_split( propertyName, " \t" );
propertyInstance->propertyName = property_extractPropertyName( propertyNameParts );
printf(" function name '%s' %i %i %i \n\n",propertyInstance->propertyName, openArgumentKey, closeArgumentKey, afterPreviousSemicolon);
propertySource = text_removeWhiteSpaces( text_slice( source, (int)afterPreviousSemicolon + 1, key-1 ) );
hasValue = 0;
//propertyInstance->propertyName = "customName";
propertyInstance->type = "function";
printf("A\n\n\n");
} else {
int IsKey = lexer_getKey( currentLexer, IsTokenKey );
int keyBeforeIS = lexer_getKey( currentLexer, IsTokenKey - 1 );
propertyValue = text_slice( source, IsKey + 1, key - 1 );
propertySource = text_removeWhiteSpaces( text_slice( source, keyBeforeIS + 1, IsKey - 1 ) );
hasValue = 1;
}
} else {
int previousKey = lexer_getKey( currentLexer, i - 1 );
propertySource = text_slice( source, previousKey + 1, key - 1 );
shift = 0;
}
char * beforeShiftToken = lexer_getToken( currentLexer, i - 1 - shift );
if( strcmp( beforeShiftToken, ">" ) == 0 ) {
int templateOpenKey = lexer_getKey( currentLexer, i - 2 - shift );
int templateCloseKey = lexer_getKey( currentLexer, i - 1 - shift );
int afterCloseKey = lexer_getKey( currentLexer, i - shift );
char * templateArgument = text_slice( source, templateOpenKey + 1, templateCloseKey - 1 );
int beforeClassName = lexer_getKey( currentLexer, i - 3 - shift );
char * className = text_removeWhiteSpaces( text_slice( source, beforeClassName + 1 , templateOpenKey - 1 ) ) ;
char * afterTemplate = text_slice( source, templateCloseKey + 1, afterCloseKey - 1 );
char * classNameWithoutStruct = className;
struct array * nameParts = text_split( className, " \t" );
if( array_length( nameParts ) > 1 ) {
char * firstPart = array_get( nameParts, 0 );
if( strcmp( firstPart, "struct" ) == NULL ) {
classNameWithoutStruct = array_get( nameParts, 1 );
}
}
struct class * classInstance = application_getClassByClassName( allClasses, classNameWithoutStruct );
if( classInstance == NULL ) {
printf("Error: class with template not found: %s \n\n\n\n\n", classNameWithoutStruct );
}
if( !classInstance->usesTemplate ) {
printf("Error: This class does not have an template: %s \n\n", classInstance->className );
}
template_addArgumentValue( classInstance, templateArgument );
struct text * templateClassName = text_new( "" );
templateArgument = text_replaceAll( templateArgument, "*", "_pointer" );
templateArgument = text_removeAllWhiteSpaces( templateArgument );
text_append( templateClassName, className );
text_append( templateClassName, "_" );
text_append( templateClassName, templateArgument );
struct text * newPropertySource = text_new( "" );
text_append( newPropertySource, templateClassName->value );
text_append( newPropertySource, afterTemplate );
propertySource = newPropertySource->value;
if( hasValue == 1 ) {
char * needle = "\\b"; // "\\bTT\\b"
needle = text_concatenate( needle, className );
needle = text_concatenate( needle, "\\b" );
propertyValue = text_regexReplaceAll( propertyValue, needle, className, templateClassName->value );
}
//printf(" templateArgument: %s %s\n\n", className, templateArgument);
}
char * trimmedPropertySource = text_removeWhiteSpaces( propertySource );
//printf(" trimmedPropertySource: %s\n", propertySource);
struct array * propertyParts = text_split( trimmedPropertySource, " \t" );
int propertyPartsLength = array_length( propertyParts );
propertyInstance->isPointer = variable_isPointer( propertyParts );
propertyInstance->hasValue = hasValue;
propertyInstance->decleration = property_extractPropertyDeclaration( propertyParts, propertyInstance );
if( hasValue == 1 ) {
//printf(" propertyValue: %s\n", propertyValue);
propertyInstance->value = property_composePropertyValue( propertyValue, propertyInstance );
}
//printf("propertyName: %s\n\n\n", propertyInstance->propertyName);
if( propertyInstance->propertyName == NULL ) {
propertyInstance->propertyName = property_extractPropertyName( propertyParts );
}
propertyInstance->declerationParts = propertyParts;
array_add( classInstance->properties, (void*) propertyInstance );
//printf("decleration: %s\n\n\n", propertyInstance->decleration);
}
int class_getPropertyIndexByName( struct class * classInstance, char * propertyName ) {
struct array * properties = classInstance->properties;
int propertyCount = array_length( properties );
for (int i = 0; i < propertyCount; ++i)
{
struct property * propertyInstance = array_get( properties, i );
if( strcmp( propertyInstance->propertyName, propertyName ) == 0 ) {
return i;
}
}
return -1;
}
struct property * class_getPropertyByName( struct class * classInstance, char * propertyName ) {
struct array * properties = classInstance->properties;
int propertyCount = array_length( properties );
for (int i = 0; i < propertyCount; ++i)
{
struct property * propertyInstance = array_get( properties, i );
if( strcmp( propertyInstance->propertyName, propertyName ) == 0 ) {
return propertyInstance;
}
}
return NULL;
}
void class_composeHeader( struct class * classInstance, struct text * classHeader, char * filename ) {
char * className = classInstance->className;
if( strcmp( className, "char" ) != 0 && strcmp( className, "int" ) != 0 && strcmp( className, "void" ) != 0 ) {
class_composeStruct( classInstance, classHeader );
}
}
void class_composeTypedefStruct( struct class * classInstance, struct text * classHeader ) {
char * className = classInstance->className;
struct array * properties = classInstance->properties;
int propertyCount = array_length( properties );
text_append( classHeader, "\n\n" );
text_append( classHeader, "typedef struct " );
text_append( classHeader, className );
text_append( classHeader, " " );
text_append( classHeader, className );
text_append( classHeader, ";\n\n" );
text_append( classHeader, "\n\n" );
}
void class_composeStruct( struct class * classInstance, struct text * classHeader ) {
char * className = classInstance->className;
struct array * properties = classInstance->properties;
int propertyCount = array_length( properties );
text_append( classHeader, "typedef struct " );
text_append( classHeader, className );
text_append( classHeader, "{\n\n" );
if( classInstance->hasReflection ) {
if( strcmp( className, "window" ) != 0 && strcmp( className, "Hints" ) != 0 ) {
text_append( classHeader, " unsigned short __classIndex;\n\n" );
}
}
for ( int i = 0; i < propertyCount; ++i )
{
struct property * currentProperty = array_get( properties, i );
char * decleration = currentProperty->decleration;
text_append( classHeader, " " );
text_append( classHeader, decleration );
text_append( classHeader, ";\n\n" );
}
text_append( classHeader, "\n} " );
text_append( classHeader, className );
text_append( classHeader, ";\n\n" );
}
void class_composeMethods( struct class * classInstance, struct text * classHeader, struct array * sourceArray ) {
char * className = classInstance->className;
struct array * methods = classInstance->methods;
int length = array_length( methods );
for (int i = 0; i < length; ++i)
{
struct method * currentMethod = array_get( classInstance->methods, i );
method_compose( currentMethod, className, sourceArray, classHeader );
}
}
int class_classNameIsPrimitive( char * baseClassName ) {
// dont overload numbers because these are used for pointers
char * primitiveTypes[3] = { "int", "long", "size_t" };
int primitiveTypesCount = 3;
for (int j = 0; j < primitiveTypesCount; ++j)
{
char * primitiveType = primitiveTypes[j];
if( strcmp( baseClassName, primitiveType ) == 0 ) {
return 1;
}
}
return -1;
}
struct property * class_getClassPropertyByPropertyName( struct class * instance, char * propertyName ) {
struct array * properties = instance->properties;
int propertyLength = array_length( properties );
for (int i = 0; i < propertyLength; ++i)
{
struct property * currentProperty = array_get( properties, i );
char * currentPropertyName = currentProperty->propertyName;
if( strcmp( currentPropertyName, propertyName ) == 0 ) {
return currentProperty;
}
}
return NULL;
}
char * class_getDatatypeFromClassProperty( char * declaration, struct array * variables ) {
array * parts = text_split( declaration, "->" );
char * propertyName = array_get( parts, 1 );
char * variableName = array_get( parts, 0 );
struct variable * variableInstance = variable_getByName( variables, variableName );
if( variableInstance == NULL ) {
return "";
}
struct class * currentClass = application_getClassByClassName( allClasses, variableInstance->datatype );
if( currentClass == NULL ) {
return "";
}
struct property * propertyInstance = class_getClassPropertyByPropertyName( currentClass, propertyName );
if( propertyInstance == NULL ) {
return "";
}
return propertyInstance->datatype;
}
char * class_openingDeclarationFromBaseClassName( char * baseClassName, char * currentOperatorSymbol, int leftSideIsPointer ) {
struct class * baseClass = application_getClassByClassName( allClasses, baseClassName );
if( baseClass != NULL ) {
char * openingDeclaration;
char * operatorName = method_getOperatorNameByOperatorSymbol( currentOperatorSymbol );
struct text * fullOperatorName = text_new( "" );
text_append( fullOperatorName, "operator_" );
text_append( fullOperatorName, operatorName );
//printf("operator name: %s %s %s\n\n", baseClassName, fullOperatorName->value, operatorName);
struct method * operatorMethod = method_getMethodByMethodName( baseClass, fullOperatorName->value );
if( operatorMethod != NULL ) {
return method_composeOperatorOverloadOpeningDeclaration( baseClassName, operatorName, leftSideIsPointer );
} else {
// printf(" method not found!\n\n");
}
} else {
}
return NULL;
}
char * class_getBaseClassNameBySymbol( char * leftSideSymbol, struct array * variables, int * leftSideIsPointer ) {
if( strcmp( leftSideSymbol, "" ) == 0 ) {
return NULL;
}
if( strcmp( leftSideSymbol, "char") == 0 ) {
return "char";
}
if( strstr( leftSideSymbol, "->" ) != 0 ) {
return class_getDatatypeFromClassProperty( leftSideSymbol, variables );
}
struct variable * variableInstance = variable_getByName( variables, leftSideSymbol );
if( strcmp( variableInstance->datatype, "this_variable_is_missing" ) != 0 ) {
*leftSideIsPointer = variableInstance->isPointer;
return variableInstance->datatype;
}
struct method * methodInstance = method_extractMethodFromSymbol( leftSideSymbol );
if( methodInstance != NULL ) {
return tools_removePointerSymbolFromDatatype( methodInstance->returnType, leftSideIsPointer );
}
char * returntype = function_getReturnTypeBySymbol( leftSideSymbol, leftSideIsPointer );
if( returntype != NULL ) {
return returntype;
}
return NULL;
}

99
source/class.h Normal file
View File

@@ -0,0 +1,99 @@
#ifndef _class
#define _class
#include "index.h"
#include "array.h"
#include "file.h"
#include "text.h"
#include "lexer.h"
#include "template.h"
#include <string.h>
#include <stdbool.h>
struct class{
int classIndex;
char * className;
int lexerIndex;
struct template * template;
struct array * properties;
struct array * methods;
struct array * extends;
int hasExtended;
int hasReflection;
bool usesTemplate;
};
struct class * class_new();
void class_composeTypedefStruct( struct class * classInstance, struct text * classHeader );
void class_reIndexLineNumbers( struct class * baseClass );
void class_updateThisArgument( struct array * arguments, char * baseClassName );
int class_getMethodIndexByName( struct class * classInstance, char * methodName );
void class_addMethodsToBaseClass( struct class * baseClass, struct class * extendedClass );
void class_addPropertiesToBaseClass( struct class * baseClass, struct class * extendedClass );
void class_extendClass( struct class * baseClass );
int class_findClassByClassNameExists( array * classesArray, char * name );
void class_extractMethod( lexer * currentLexer, char * source, int i, struct class * classInstance, struct array * classesArray, struct array * functions );
void class_extractProperty( lexer * currentLexer, int key, int i, char * source, struct class * classInstance );
char * class_extractBodyFromSource( lexer * currentLexer, int i, char * source, int bodyCloseIndex );
struct property * class_getPropertyByName( struct class * classInstance, char * propertyName );
int class_getPropertyIndexByName( struct class * classInstance, char * propertyName );
void class_composeHeader( struct class * classInstance, struct text * classHeader, char * filename );
void class_composeStruct( struct class * classInstance, struct text * classHeader );
void class_composeMethods( struct class * classInstance, struct text * classHeader, struct array * sourceArray );
struct property * class_getClassPropertyByPropertyName( struct class * instance, char * propertyName ) ;
char * class_openingDeclarationFromBaseClassName( char * baseClassName, char * currentOperatorSymbol, int leftSideIsPointer );
char * class_getDatatypeFromClassProperty( char * declaration, struct array * variables );
int class_classNameIsPrimitive( char * baseClassName );
char * class_getBaseClassNameBySymbol( char * leftSideSymbol, struct array * variables, int * leftSideIsPointer ) ;
#endif

2
source/codeBundle.c Normal file
View File

@@ -0,0 +1,2 @@
#include "codeBundle.h"

14
source/codeBundle.h Normal file
View File

@@ -0,0 +1,14 @@
#ifndef _codeBundle
#define _codeBundle
struct codeBundle{
int lineNumber;
char * sourceCode;
};
#endif

180
source/configuration.c Normal file
View File

@@ -0,0 +1,180 @@
// #include "sqlite.h"
int getPropertyIndexOfClassIndex( int propertyCount, char ** propertyNames ) {
int propertyIdOfIndex = -1;
for ( int i = 0; i < propertyCount; ++i )
{
char * propertyName = propertyNames[i];
//printf("propertyName: %s\n", propertyName);
if( strcmp( propertyName, "id" ) == 0 ) {
propertyIdOfIndex = i;
break;
}
}
return propertyIdOfIndex;
}
/*
void getArrayByClassIndex( int items, void * * voidArray, int * structByteSize, int classIndex ) {
struct user * array;
switch( classIndex ) {
case 8:
array = ( struct user * ) malloc( sizeof( struct user ) * 1000 );
voidArray = ( void ** ) array;
*structByteSize = sizeof( struct user );
break;
default:
array = ( struct user * ) malloc( sizeof( struct user ) * 1000 );
voidArray = ( void ** ) array;
*structByteSize = sizeof( struct user );
}
}*/
char * getClassName( int classIndex ) {
return __ClassNames[ classIndex ];
}
int getClassIndexByClassName( char * className ) {
for (int i = 0; i < TOTAL_CLASS_COUNT; ++i)
{
char * currentClassName = __ClassNames[ i ];
if( strcmp( className, currentClassName ) == 0 ) {
//printf("find classname: %s\n", className);
return i;
}
}
return -1;
}
int getPropertyCountByClassIndex( int classIndex ) {
return __ClassPropertyCount[ classIndex ];
}
char * * getPropertiesByClassIndex( int classIndex ) {
return __ClassPropertyNames[ classIndex ];
}
int * getPropertyOffsetsByClassIndex( int classIndex ) {
return __ClassPropertyOffsets[ classIndex ];
}
int getPropertyOffsetByPropertyIndex( int * propertyOffsets, int propertyIndex ) {
return propertyOffsets[ propertyIndex ];
}
int * getPropertyDatatypeIndexesByClassIndex( int classIndex ) {
return __ClassPropertyDatatypeIndices[ classIndex ];
}
int getPropertyDatatypeIndex( int * propertyDatatypeIndices, int propertyIndex ) {
return propertyDatatypeIndices[ propertyIndex ];
}
int getPropertyIndexByPropertyName( int classID, char * propertyName ) {
int propertyCount = getPropertyCountByClassIndex( classID );
char * * propertyNames = getPropertiesByClassIndex( classID );
for (int i = 0; i < propertyCount; ++i)
{
char * propertyNameCompare = propertyNames[i];
if( strcmp( propertyName, propertyNameCompare ) == 0 ) {
return i;
}
}
return -1;
}
int getMethodCountByClassIndex( int classIndex ) {
return __ClassMethodCount[ classIndex ];
}
char * * getMethodNamesByClassIndex( int classIndex ) {
return __ClassMethodNames[ classIndex ];
}
int getMethodIndexByPropertyName( int classID, char * propertyName ) {
int methodCount = getMethodCountByClassIndex( classID );
char * * methodNames = getMethodNamesByClassIndex( classID );
for (int i = 0; i < methodCount; ++i)
{
char * propertyNameCompare = methodNames[i];
if( strcmp( propertyName, propertyNameCompare ) == 0 ) {
return i;
}
}
return -1;
}

33
source/configuration.h Normal file
View File

@@ -0,0 +1,33 @@
#include <string.h>
int getPropertyIndexOfClassIndex( int propertyCount, char ** propertyNames );
void getArrayByClassIndex( int size, void * * voidArray, int * structByteSize, int classIndex );
int getClassIndexByClassName( char * className );
char * getClassName( int classIndex );
int getPropertyCountByClassIndex( int classIndex );
char * * getPropertiesByClassIndex( int classIndex );
int * getPropertyOffsetsByClassIndex( int classIndex );
int getPropertyOffsetByPropertyIndex( int * propertyOffsets, int propertyIndex );
int getPropertyIndexByPropertyName( int classID, char * propertyName );
int * getPropertyDatatypeIndexesByClassIndex( int classIndex );
int getPropertyDatatypeIndex( int * propertyDatatypeIndices, int propertyIndex );
int getMethodCountByClassIndex( int classIndex );
char * * getMethodNamesByClassIndex( int classIndex );
int getMethodIndexByPropertyName( int classID, char * propertyName );
void callMethodOfClass( int classIndex, int methodIndex, void * object );

16
source/console.c Normal file
View File

@@ -0,0 +1,16 @@
#include "console.h"
void console_logColor( char * message, char * color ) {
if( strcmp( color, "green" ) == 0 ) {
printf( ANSI_COLOR_GREEN "%s" ANSI_COLOR_RESET, message );
} else if( strcmp( color, "yellow" ) == 0 ) {
printf( ANSI_COLOR_BRIGHT_YELLOW "%s" ANSI_COLOR_RESET, message );
}
}

24
source/console.h Normal file
View File

@@ -0,0 +1,24 @@
#ifndef _console
#define _console
#include "string.h"
#include "stdio.h"
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_DIM_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_RESET "\x1b[0m"
#define ANSI_COLOR_BRIGHT_YELLOW "\x1b[93m"
void console_log( char * message );
void console_logColor( char * message, char * color );
void console_important( char * message );
#endif

3416
source/file.c Normal file
View File

File diff suppressed because it is too large Load Diff

165
source/file.h Normal file
View File

@@ -0,0 +1,165 @@
#ifndef _file
#define _file
#include "class.h"
#include "application.h"
#include "lexer.h"
#include "fileSystem.h"
#include "string.h"
#include "text.h"
#include "template.h"
#include "array.h"
#include "stdbool.h"
#include "include.h"
typedef struct application application;
struct file{
char * filename;
char * source;
char * path;
char * dirname;
struct array * classes;
struct array * functions;
struct array * includes;
struct array * macros;
struct array * globals;
};
struct file * file_new();
void file_parseClassBody( lexer * currentLexer,
int fromKey,
int toIndex,
char * source,
struct file * fileInstance,
struct class * classInstance );
char * file_getFileNameFromIncludeFixed( char * include );
void file_parseMacros( struct file * fileInstance, struct array * files );
void file_composeFunctions( struct array * , struct array *, struct text * classHeader );
void file_extendClasses( struct file * fileInstance );
void file_compose( struct file * fileInstance, application * applicationInstance );
void file_instanciateTemplates( struct file * fileInstance );
void file_composeHeaderMacros( struct file * fileInstance, struct text * classHeader );
void file_addIncludesToHeader( struct text * classHeader, struct file * currentFile );
void file_preventDoubleLoadingHeader( struct text * classHeader, char * filename );
struct text * file_composeHeaderIncludes( struct file * currentFile );
void file_extractMethodsAndProperties( struct file * fileInstance );
void file_extractFunctions( struct file * fileInstance );
void file_extractInnerClass( lexer * currentLexer, int i, int key, struct file * fileInstance, char * source, int bodyCloseIndex );
char * file_extractFunctionDecleration( lexer * currentLexer, int key, int i, char * source );
void file_extractFunction( lexer * currentLexer, int i, int key, struct file * fileInstance, char * source, int bodyCloseIndex, struct array * classesArray );
void file_removeComments( struct file * fileInstance );
void file_extractGlobalVariable( lexer * currentLexer, int key, int i, struct file * fileInstance, char * source );
void file_extractClasses( struct file * fileInstance );
void file_extractClass( lexer * currentLexer, int key, int i, struct array * classesArray, int depth, char * source );
void file_composeFunctionArguments( struct functionLayout * currentFunction, struct text * methodSource, struct text * classHeader );
void file_composeFunction( struct functionLayout * currentFunction, struct array * sourceArray, struct text * classHeader );
void file_extractTemplates( struct file * fileInstance );
void file_composeConfigInformation( struct array * files, struct application * applicationInstance );
char * file_composePath( char * path, char * filename, char * extension );
void file_composeNewPointerFunction( struct text * finalSourceCode, int hasConstructor, struct array * constructorArguments, struct class * classInstance );
void file_composeConstructorCall( struct text * finalSourceCode, char * className, struct array * constructorArguments, int usePointer );
void file_composePropertieInitiations( struct text * finalSourceCode, int usePointers, struct class * classInstance );
void file_composeNewFunction( struct text * finalSourceCode, int hasConstructor, struct array * constructorArguments, struct class * classInstance );
void file_composeNewFunctions( struct text * finalSourceCode, struct class * classInstance, int hasConstructor, struct array * constructorArguments );
void file_composeNewHeaderDeclerations( struct text * classHeader, char * className, struct array * contructorArguments );
struct array * file_getConstructorArguments( struct class * classInstance, int * hasConstructor );
void file_composeClass( struct class * classInstance, struct text * classHeader, struct text * finalSourceCode );
void file_addLineBreaks( struct text * finalSourceCode, struct array * sourceArray );
void file_composeSourceHeader( struct text * finalSourceCode, char * filename );
void file_writeAll( struct array * sourceArray, struct text * classHeader, char * filename, array * classesArray, array * globals, char * relativePath, struct application * applicationInstance );
void file_parseFunctions( struct file * fileInstance, array * files );
void file_parseMethods( struct file * fileInstance, array * files );
void file_addVariablesFromFile( struct file * fileInstance, struct array * variables );
struct file * file_getFileByFileName( struct array * files, char * includeFileName );
char * file_getFileNameFromInclude( char * include );
char * file_extractInclude( struct file * fileInstance, char * source, lexer * currentLexer, int key, int i );
char * file_eraseDefine( char * source, int key, int lineBreakIndex );
char * file_extractDefine( struct file * fileInstance, char * source, int key, int i );
void file_extractMacros( struct file * fileInstance );
char * file_getBaseNameFromFileName( char * filename );
char * file_getPathFrom( char * filename );
int file_isLibCFileName( char * baseName );
char * file_removeExtensionFromFileName( char * path );
int file_hasLoadedFileName( char * fileName, struct array * loadedFileNames );
void file_loadUnloadedFiles( struct array * unloadedFileNames, struct array * loadedFileNames, char * path );
char * file_getSource( char * filename );
#endif

430
source/fileManager.c Normal file
View File

@@ -0,0 +1,430 @@
#include "fileManager.h"
array * fileManager_composeFiles( array * includedFilePaths ) {
int FileNameCount = array_length( includedFilePaths );
struct array * files = array_new();
for ( int i = 0; i < FileNameCount; ++i )
{
char * filePath = array_get( includedFilePaths, i );
struct file * fileInstance = file_new();
fileInstance->filename = basename( text_copy( filePath ) );
fileInstance->dirname = dirname( text_copy( filePath ) );
if( filePath[0] == 47 ) {
memmove( filePath, filePath + 1, strlen( filePath ) );
}
fileInstance->path = filePath;
printf("basename: %s\n\n", fileInstance->filename);
fileInstance->source = file_getSource( text_copy( fileInstance->path ) );
array_add( files, fileInstance );
}
return files;
}
void fileManager_removeComments( array * files ) {
int fileCount = array_length( files );
for (int i = 0; i < fileCount; ++i)
{
struct file * fileInstance = array_get( files, i );
file_removeComments( fileInstance );
}
}
struct file * fileManager_getFileByPath( array * files, char * filePath ) {
int fileCount = array_length( files );
for (int i = 0; i < fileCount; ++i)
{
struct file * fileInstance = array_get( files, i );
//printf("compare: %s \n", fileInstance->path);
if( strcmp( fileInstance->path, filePath ) == 0 ) {
return fileInstance;
}
}
return NULL;
}
bool array_includes( array * processedFiles, char * value ) {
int count = array_length( processedFiles );
for (int i = 0; i < count; ++i)
{
char * currentValue = array_get( processedFiles, i );
if ( strcmp( value, currentValue ) == 0 )
{
return true;
}
}
return false;
}
/*
void file_fixIncudes( struct file * currentFile, array * files, array * processedFiles ) {
int includeCount = array_length( currentFile->includes );
printf("entering %s \n\n", currentFile->filename );
for (int i = 0; i < includeCount; ++i)
{
struct include * includeInstance = array_get( currentFile->includes, i );
char * includePath = file_getFileNameFromIncludeFixed( includeInstance->path );
char * includeFileBaseName = file_removeExtensionFromFileName( includePath );
//char * includePath = includeInstance->path;
struct text * relativePath = text_new( currentFile->dirname );
text_append( relativePath, "/" );
text_append( relativePath, includeFileBaseName );
char * resolvedPath = resolve( relativePath->value );
//printf("%s \n", resolvedPath );
struct file * fileInstance = fileManager_getFileByPath( files, resolvedPath );
if( fileInstance != NULL ) {
if( !array_includes( processedFiles, resolvedPath ) ) {
printf(" adding %s to %s \n\n", resolve( relativePath->value ), currentFile->filename );
includeInstance->enabled = true;
array_set( currentFile->includes, i, includeInstance );
array_add( processedFiles, resolvedPath );
file_fixIncudes( fileInstance, files, processedFiles );
}
}
}
}
void fileManager_fixIncludes( array * files ) {
printf("\n\n\n\n\n Fix includes \n\n\n\n");
struct file * rootFile = array_get( files, 0 );
struct array * processedFiles = array_new();
printf(" rootFile: %s\n\n\n", rootFile->filename);
file_fixIncudes( rootFile, files, processedFiles );
printf("\n\n");
//exit( 0 );
}
*/
void fileManager_extractMacros( array * files ) {
int fileCount = array_length( files );
for (int i = 0; i < fileCount; ++i)
{
struct file * fileInstance = array_get( files, i );
file_extractMacros( fileInstance );
}
}
void fileManager_parseMacros( array * files ) {
int fileCount = array_length( files );
for (int i = 0; i < fileCount; ++i)
{
struct file * fileInstance = array_get( files, i );
char * source = fileInstance->source;
//file_parseMacros( fileInstance, files );
}
}
void fileManager_extractClasses( array * files, struct application * applicationInstance ) {
int fileCount = array_length( files );
for (int i = 0; i < fileCount; ++i)
{
struct file * fileInstance = array_get( files, i );
char * source = fileInstance->source;
file_extractClasses( fileInstance );
}
}
void fileManager_validateCurlyBrackets( array * files ) {
int fileCount = array_length( files );
for (int i = 0; i < fileCount; ++i)
{
struct file * fileInstance = array_get( files, i );
char * source = fileInstance->source;
text_validateCurlyBrackets( source );
}
}
void fileManager_extractMethodsAndProperties( array * files ) {
printf( "\n" );
console_logColor( "\n\n\nDone.\n\n\n", "green" );
console_logColor( " 2. Extract Methods and Properties.\n\n", "yellow" );
int fileCount = array_length( files );
for (int i = 0; i < fileCount; ++i)
{
struct file * fileInstance = array_get( files, i );
file_extractMethodsAndProperties( fileInstance );
}
}
void fileManager_extractFunctions( array * files ) {
int fileCount = array_length( files );
for (int i = 0; i < fileCount; ++i)
{
struct file * fileInstance = array_get( files, i );
file_extractFunctions( fileInstance );
}
}
void fileManager_parseMethods( array * files ) {
printf( ANSI_COLOR_GREEN "\n\n\nDone.\n\n\n" ANSI_COLOR_RESET );
printf( ANSI_COLOR_BRIGHT_YELLOW " 3. Parse Method bodies.\n\n" ANSI_COLOR_RESET);
int fileCount = array_length( files );
for (int i = 0; i < fileCount; ++i)
{
struct file * fileInstance = array_get( files, i );
printf("%s.c:\n\n", fileInstance->path);
printf(" Classes:\n\n");
file_parseMethods( fileInstance, files );
//printf(" Functions:\n\n");
}
}
void fileManager_parseFunctions( array * files ) {
printf( ANSI_COLOR_BRIGHT_YELLOW " 4. Parse Function bodies.\n\n" ANSI_COLOR_RESET);
int fileCount = array_length( files );
for (int i = 0; i < fileCount; ++i)
{
struct file * fileInstance = array_get( files, i );
printf("file: %s\n", fileInstance->filename);
file_parseFunctions( fileInstance, files );
}
}
void fileManager_extendClasses( array * files ) {
int fileCount = array_length( files );
printf( "\n" );
for (int i = 0; i < fileCount; ++i)
{
struct file * fileInstance = array_get( files, i );
file_extendClasses( fileInstance );
}
}
void fileManager_extractTemplates( array * files ) {
printf("\n\n\n\n\n\n 5. Extract templates\n\n\n\n\n\n\n");
int fileCount = array_length( files );
printf( "\n" );
for (int i = 0; i < fileCount; ++i)
{
struct file * fileInstance = array_get( files, i );
file_extractTemplates( fileInstance );
}
}
void fileManager_instantiateTemplates( array * files ) {
printf("\n\n\n\n\n\n file_instanciateTemplates\n\n\n\n\n\n\n");
int fileCount = array_length( files );
printf( "\n" );
for (int i = 0; i < fileCount; ++i)
{
struct file * fileInstance = array_get( files, i );
file_instanciateTemplates( fileInstance );
}
}
int unlink_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
{
int rv = remove(fpath);
if (rv)
perror(fpath);
return rv;
}
void fileManager_removeFilesFromOutputDirectory( struct array * files, struct application * currentApplication ) {
char * outputDirectory = currentApplication->output;
printf("Delete directory: %s\n\n\n\n", outputDirectory);
nftw( outputDirectory, unlink_cb, 64, FTW_DEPTH | FTW_PHYS );
fileSystem_ensureDirectory( outputDirectory );
}
void fileManager_composeSourceAndHeaderFiles( struct array * files, struct application * currentApplication ) {
int fileCount = array_length( files );
for (int i = 0; i < fileCount; ++i)
{
struct file * fileInstance = array_get( files, i );
//printf("before crash: %s\n\n\n\n", fileInstance->filename);
file_compose( fileInstance, currentApplication );
}
}

65
source/fileManager.h Normal file
View File

@@ -0,0 +1,65 @@
#ifndef _fileManager
#define _fileManager
#include <stdio.h>
#define __USE_XOPEN_EXTENDED
#include <ftw.h>
#include <unistd.h>
#include "application.h"
#include "text.h"
#include "array.h"
#include "file.h"
#include <string.h>
#include <stdio.h>
array * fileManager_composeFiles( array * includedFilePaths );
void fileManager_removeComments( array * files );
void fileManager_extractMacros( array * files );
void fileManager_fixIncludes( array * files );
void fileManager_parseMacros( array * files );
void fileManager_extractClasses( array * files, struct application * applicationInstance );
void fileManager_validateCurlyBrackets( array * files );
void fileManager_instantiateTemplates( array * files );
void fileManager_extractMethodsAndProperties( array * files );
void fileManager_extractTemplates( array * files );
void fileManager_extractFunctions( array * files );
void fileManager_removeFilesFromOutputDirectory( struct array * files, struct application * currentApplication );
void fileManager_parseMethods( array * files );
void fileManager_parseFunctions( array * files );
void fileManager_extendClasses( array * files );
void fileManager_composeSourceAndHeaderFiles( struct array * files, struct application * currentApplication );
#endif

209
source/fileSystem.c Executable file
View File

@@ -0,0 +1,209 @@
#include <fileSystem.h>
void fileSystem_writeFile( char * filepath, char * data )
{
FILE *fp = fopen( filepath, "wb" );
if (fp != NULL)
{
fputs( data, fp );
fclose( fp );
}
}
int fileSystem_textIsFilename( char * value ) {
array * parts = text_split( text_copy( value ) , "." );
int partsCount = array_length( parts );
if( partsCount > 1 ) {
return 1;
} else {
return 0;
}
}
void fileSystem_ensureDirectory( char * filePath ) {
//printf("fileSystem_ensureDirectory: %s\n\n\n\n", filePath);
struct array * parts = text_split( text_copy( filePath ), "/" );
int count = array_length( parts );
for ( int i = 1; i < count; ++i )
{
struct text * currentPath = text_new( "" );
for (int j = 0; j < i; ++j)
{
char * pathPart = array_get( parts, j );
text_append( currentPath, pathPart );
text_append( currentPath, "/" );
if( fileSystem_exists( currentPath->value ) ) {
} else {
if( strlen( currentPath->value ) > 100 ) {
printf(" ERROR: TempPath is to big. '%s' ", currentPath->value);
exit( 0 );
}
printf("Directory does not exists, Creating Directory%i '%s' \n\n", strlen( currentPath->value ),currentPath->value );
fileSystem_makeDirectory( currentPath->value );
}
}
/*
struct array * tempParts = text_split( text_copy( filePath ), "/" );
for (int j = 0; j < count-i; ++j)
{
array_pop( tempParts );
}
char * tempPath = text_join( tempParts, "/" );
if( fileSystem_exists( tempPath ) ) {
} else {
if( strlen( tempPath ) > 100 ) {
printf(" ERROR: TempPath is to big. '%s' ", filePath);
exit( 0 );
}
printf("Directory does not exists, Creating Directory%i '%s' \n\n", strlen( tempPath ),tempPath );
fileSystem_makeDirectory( tempPath );
}
printf( " tempPath: %s\n", tempPath);
*/
}
}
int fileSystem_makeDirectory( char * path ) {
if ( fileSystem_exists( path ) == NULL ) {
mkdir( path, 0700 );
}
return 1;
}
int fileSystem_exists( char * path ) {
if ( access( path, F_OK ) == 0 ) {
return 1;
} else {
return 0;
}
}
struct array * fileSystem_readDir( char * filePath ) {
DIR * dir;
struct dirent * entry;
struct array * files = array_new();
if ( ( dir = opendir( filePath ) ) == NULL ){
perror("opendir() error");
} else {
while( ( entry = readdir( dir ) ) != NULL ) {
char * filename = (char *)entry->d_name;
if ( strcmp( filename, ".." ) != 0 && strcmp( filename, "." ) != 0 ) {
array_add( files, filename );
//printf("%s\n", filename);
}
}
}
return files;
//closedir(dir);
}
char * fileSystem_readFile( char * name ) {
FILE * file = fopen( name, "r" );
if ( file == NULL ) {
fprintf(stderr, "Error: Can't open file '%s'. \n\n", name);
exit(EXIT_FAILURE);
}
fseek( file, 0, SEEK_END );
long length = ftell(file);
fseek( file, 0, SEEK_SET );
char * buffer = malloc( sizeof( char ) * ( length + 1 ) );
buffer[ length ] = '\0';
fread( buffer, sizeof(char), length, file );
fclose(file);
return buffer;
}

43
source/fileSystem.h Executable file
View File

@@ -0,0 +1,43 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <array.h>
#include <dirent.h> // "DIR *"
#include <text.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef WIN32
#include <io.h>
#define F_OK 0
#define access _access
#endif
int fileSystem_textIsFilename( char * value );
char * fileSystem_readFile( char * );
void fileSystem_writeFile( char *, char * );
struct array * fileSystem_readDir( char * filePath );
int fileSystem_exists( char * path );
int fileSystem_makeDirectory( char * path );
void fileSystem_ensureDirectory( char * filePath );

112
source/function.c Normal file
View File

@@ -0,0 +1,112 @@
#include "function.h"
struct functionLayout * function_getFunctionByName( struct array * functions, char * functionName ) {
int count = array_length( functions );
for (int i = 0; i < count; ++i)
{
struct functionLayout * currentFunction = array_get( functions, i );
if( strcmp( currentFunction->name, functionName ) == 0 ) {
return currentFunction;
}
}
return NULL;
}
char * function_getReturnTypeBySymbol( char * functionName, int * leftSideIsPointer ) {
struct functionLayout * currentFunction = function_getFunctionByName( allFunctions, functionName );
if( currentFunction != NULL ) {
struct array * functionDeclarationParts = text_split( currentFunction->declarationText, " \t" );
*leftSideIsPointer = variable_isPointer( functionDeclarationParts );
return array_get( functionDeclarationParts, 0 );
} else {
return NULL;
}
}
void cloneMethodToFunction( struct method * currentMethod, struct array * functions, char * newName ) {
struct text * newMethodDecleration = text_new( "" );
text_append( newMethodDecleration, currentMethod->returnType );
text_append( newMethodDecleration, " " );
text_append( newMethodDecleration, newName);
struct array * arguments = array_new();
int argumentCount = array_length( currentMethod->arguments );
for ( int k = 1; k < argumentCount; ++k )
{
char * currentArgument = array_get( currentMethod->arguments, k );
array_add( arguments, currentArgument );
}
struct functionLayout * functionInstance = malloc( sizeof( struct functionLayout ) );
functionInstance->name = newName;
functionInstance->declarationText = newMethodDecleration->value;
functionInstance->bodyOriginal = currentMethod->bodyOriginal;
functionInstance->variables = array_new();
functionInstance->body = method_parse( currentMethod->bodyOriginal, functionInstance->variables, functions );//;
functionInstance->arguments = arguments;
functionInstance->lineNumber = -1;
array_add( functions, functionInstance );
}
int functionExists( struct array * functions, char * functionName ) {
int count = array_length( functions );
for (int i = 0; i < count; ++i)
{
struct functionLayout * currentFunction = array_get( functions, i );
if( strcmp( currentFunction->name, functionName ) == 0 ) {
return 1;
}
}
return -1;
}

52
source/function.h Normal file
View File

@@ -0,0 +1,52 @@
#ifndef _function
#define _function
#include "method.h"
#include "array.h"
#include "text.h"
#include "string.h"
#include "index.h"
struct functionLayout{
char * name;
char * body;
char * bodyOriginal;
int lineNumber;
int isVariadic;
struct array * declaration;
struct array * arguments;
struct array * variables;
char * declarationText;
};
struct functionLayout * function_getFunctionByName( struct array * functions, char * functionName );
char * function_getReturnTypeBySymbol( char * functionName, int * leftSideIsPointer );
int functionExists( struct array * functions, char * functionName );
void cloneMethodToFunction( struct method * currentMethod, struct array * functions, char * newName );
#endif

12
source/include.c Normal file
View File

@@ -0,0 +1,12 @@
#include "include.h"
struct include * include_new() {
struct include * includeInstance = malloc( sizeof( struct include ) );
return includeInstance;
}

28
source/include.h Normal file
View File

@@ -0,0 +1,28 @@
#ifndef _include
#define _include
#include "index.h"
#include "array.h"
#include "string.h"
#include "tools.h"
#include <stdio.h>
#include <stdbool.h>
struct include{
char * path;
bool enabled;
};
struct include * include_new();
#endif

83
source/index.c Normal file
View File

@@ -0,0 +1,83 @@
#include "index.h"
struct array * allClasses;
struct array * allFunctions;
void main( int argc, char * * argv ) {
application_initializeGlobals();
application * currentApplication = application_parseArguments( argc, argv );
array * includedFilePaths = application_extractIncludedFilePaths( currentApplication );
//return;
array * files = fileManager_composeFiles( includedFilePaths );
fileManager_removeComments( files );
fileManager_extractMacros( files ); // fileManager_extractMacros andIncludes
fileManager_parseMacros( files );
fileManager_extractClasses( files, currentApplication );
fileManager_validateCurlyBrackets( files );
// cleaned until here.
fileManager_extractMethodsAndProperties( files );
fileManager_extractFunctions( files );
fileManager_extractTemplates( files );
fileManager_instantiateTemplates( files );
fileManager_parseMethods( files );
fileManager_parseFunctions( files );
fileManager_extendClasses( files );
fileManager_removeFilesFromOutputDirectory( files, currentApplication );
fileManager_composeSourceAndHeaderFiles( files, currentApplication );
file_composeConfigInformation( files, currentApplication );
application_printMemoryUsage();
console_logColor( "\n\n\n Preprocessing succeeded\n\n\n", "green" );
}
void abort() {
}

85
source/index.h Executable file
View File

@@ -0,0 +1,85 @@
#ifndef _index
#define _index
#include <math.h>
#include <stdarg.h>
#include <array.h>
#include <sys/resource.h>
#include <stdlib.h> // malloc, exit, EXIT_FAILURE
#include <string.h> // strcpy, strtok, strcmp and memcpy
#include <stdint.h>
#include <ctype.h> // isspace
#include <stdio.h> // File, printf
#include <stdbool.h>
#include <time.h>
#include <unistd.h>
#include <libgen.h>
#include <file.h>
#include <replacement.h>
#include <class.h>
#include <property.h>
#include <fileManager.h>
#include <file.h>
#include <function.h>
#include <method.h>
#include <variable.h>
#include <lexer.h>
#include <text.h>
#include <console.h>
#include <fileSystem.h>
#include <application.h>
#include <dirent.h>
#include <errno.h>
#include <sys/types.h>
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_DIM_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_RESET "\x1b[0m"
#define ANSI_COLOR_BRIGHT_YELLOW "\x1b[93m"
extern struct array * allClasses;
extern struct array * allFunctions;
void main();
#endif

593
source/lexer.c Executable file
View File

@@ -0,0 +1,593 @@
#include <lexer.h>
#include <replacement.h>
#include <ctype.h>
#include <stdio.h>
#include <stdint.h>
#define ARRAY_SIZE( arr ) ( sizeof( ( arr ) ) / sizeof( ( arr )[0] ) )
lexer * lexer_new()
{
struct lexer * v;
v = ( struct lexer * ) malloc( sizeof( struct lexer ) );
v->capacity = ARRAY_INIT_CAPACITY;
v->total = 0;
v->keys = malloc(sizeof( int ) * v->capacity );
v->tokens = malloc(sizeof( char * ) * v->capacity );
return v;
}
int lexer_length( lexer * v )
{
return v->total;
}
void lexer_resize( lexer * currentLexer, int capacity )
{
int * keys = realloc( currentLexer->keys, sizeof( int ) * capacity );
char * * tokens = realloc( currentLexer->keys, sizeof( char * ) * capacity );
currentLexer->keys = keys;
currentLexer->tokens = tokens;
currentLexer->capacity = capacity;
}
void lexer_add( lexer * currentLexer, int key, char * token )
{
if ( currentLexer->capacity == currentLexer->total ){
lexer_resize( currentLexer, currentLexer->capacity * 2 );
}
currentLexer->keys[ currentLexer->total ] = key;
currentLexer->tokens[ currentLexer->total ] = token;
currentLexer->total++;
}
char * lexer_getToken( lexer * currentLexer, int index ) {
return currentLexer->tokens[ index ];
}
int lexer_getKey( lexer * currentLexer, int index ) {
return currentLexer->keys[ index ];
}
void lexer_setIndex( lexer * currentLexer, int index ) {
currentLexer->index = index;
}
int lexer_tokenize( lexer * currentLexer, char * haystack, char * needle )
{
int count = 0;
char * tmp = haystack;
while( tmp = strstr( tmp, needle ) ) {
int key = (int)( tmp - haystack );
lexer_add( currentLexer, key, needle );
tmp++;
++count;
}
return count;
}
int lexer_findPreviousKeyByToken( lexer * currentLexer, int fromKey, char * token ) {
for (int i = fromKey - 1; i >= 0; --i)
{
int key = lexer_getKey( currentLexer, i );
char * currentToken = lexer_getToken( currentLexer, i );
if ( strcmp( currentToken, token ) == 0 ) {
return key;
}
}
return -1;
}
int lexer_findPreviousTokenIndex( lexer * currentLexer, int fromKey, char * token ) {
for (int i = fromKey - 1; i >= 0; --i)
{
int key = lexer_getKey( currentLexer, i );
char * currentToken = lexer_getToken( currentLexer, i );
if ( strcmp( currentToken, token ) == 0 ) {
return i;
}
}
return -1;
}
void lexer_sortKeys( lexer * currentLexer ) {
int count = lexer_length( currentLexer );
int * keys = currentLexer->keys;
char * * tokens = currentLexer->tokens;
int i, j;
int temp;
char * temp2;
for (i = 0; i < (count - 1); ++i)
{
for (j = 0; j < count - 1 - i; ++j )
{
int a = keys[j];
int b = keys[j+1];
if ( a > b )
{
temp = keys[j+1];
temp2 = tokens[j+1];
currentLexer->keys[j+1] = keys[j];
currentLexer->keys[j] = temp;
currentLexer->tokens[j+1] = tokens[j];
currentLexer->tokens[j] = temp2;
}
}
}
}
int lexer_findNextToken( lexer * currentLexer, int fromKey, char * token ) {
int count = lexer_length( currentLexer );
for (int i = fromKey; i < count; ++i)
{
int key = lexer_getKey( currentLexer, i );
char * currentToken = lexer_getToken( currentLexer, i );
if ( strcmp( currentToken, token ) == 0 ) {
return key;
}
}
return -1;
}
int lexer_findNextTokenIndex( lexer * currentLexer, int fromKey, char * token ) {
int count = lexer_length( currentLexer );
for (int i = fromKey; i < count; ++i)
{
int key = lexer_getKey( currentLexer, i );
char * currentToken = lexer_getToken( currentLexer, i );
if ( strcmp( currentToken, token ) == 0 ) {
return i;
}
}
return -1;
}
// change name to something it does.
int lexer_findPreviouseTokenIndex( lexer * currentLexer, int fromIndex, struct array * validPreviousTokens ) {
//int count = array_length( currentLexer );
for (int i = fromIndex - 1; i >= 0; --i)
{
char * currentToken = lexer_getToken( currentLexer, i );
int key = lexer_getKey( currentLexer, i );
int validTokenCount = array_length( validPreviousTokens );
for (int j = 0; j < validTokenCount; ++j)
{
char * currentValidToken = array_get( validPreviousTokens, j );
//printf("find previous token: %s %s \n", currentToken, currentValidToken);
if ( strcmp( currentToken, currentValidToken ) == 0 )
{
//printf("token found!!\n\n");
return key;
}
}
}
return -1;
}
int lexer_tokenizeRegex( lexer * currentLexer, char * haystack, char * needle, char * token ) {
const char * error;
unsigned char * name_table;
unsigned int option_bits;
int erroffset;
int crlf_is_newline;
int ovector[OVECCOUNT];
int subject_length;
int rc;
int utf8;
char * subject = text_copy( haystack );
subject_length = ( int ) strlen( subject );
pcre * re = pcre_compile( needle, /* the needle */
0, /* default options */
&error, /* for error message */
&erroffset, /* for error offset */
NULL); /* use default character tables */
if ( re == NULL )
{
printf( "PCRE compilation failed at offset %d: %s\n", erroffset, error );
return 1;
}
rc = pcre_exec( re, /* the compiled needle */
NULL, /* no extra data - we didn't study the needle */
subject, /* the subject string */
subject_length, /* the length of the subject */
0, /* start at offset 0 in the subject */
0, /* default options */
ovector, /* output vector for substring information */
OVECCOUNT ); /* number of elements in the output vector */
if( rc < 0 ) {
switch( rc )
{
case PCRE_ERROR_NOMATCH:
//printf("No match\n");
break;
default:
printf("Matching error %d\n", rc);
break;
}
pcre_free( re ); /* Release memory used for the compiled needle */
return 1;
}
///printf("\nNew: %d\n", ovector[0] + 1);
//lexer_add( currentLexer, ovector[0] + 1, token );
lexer_add( currentLexer, ovector[0]+1, token );
/* See if CRLF is a valid newline sequence. */
crlf_is_newline = option_bits == PCRE_NEWLINE_ANY ||
option_bits == PCRE_NEWLINE_CRLF ||
option_bits == PCRE_NEWLINE_ANYCRLF;
/* Loop for second and subsequent matches */
for (;;)
{
int options = 0; /* Normally no options */
int start_offset = ovector[1]; /* Start at end of previous match */
/* If the previous match was for an empty string, we are finished if we are
at the end of the subject. Otherwise, arrange to run another match at the
same point to see if a non-empty match can be found. */
if (ovector[0] == ovector[1])
{
if (ovector[0] == subject_length) break;
options = PCRE_NOTEMPTY_ATSTART | PCRE_ANCHORED;
}
/* Run the next matching operation */
rc = pcre_exec(
re, /* the compiled needle */
NULL, /* no extra data - we didn't study the needle */
subject, /* the subject string */
subject_length, /* the length of the subject */
start_offset, /* starting offset in the subject */
options, /* options */
ovector, /* output vector for substring information */
OVECCOUNT); /* number of elements in the output vector */
if (rc == PCRE_ERROR_NOMATCH)
{
if (options == 0)
break; /* All matches found */
ovector[1] = start_offset + 1; /* Advance one byte */
if ( crlf_is_newline && /* If CRLF is newline & */
start_offset < subject_length - 1 && /* we are at CRLF, */
subject[start_offset] == '\r' &&
subject[start_offset + 1] == '\n') {
ovector[1] += 1; /* Advance by one more. */
} else if ( utf8 ) { /* Otherwise, ensure we */
/* advance a whole UTF-8 */
while (ovector[1] < subject_length) /* character. */
{
if ((subject[ovector[1]] & 0xc0) != 0x80)
break;
ovector[1] += 1;
}
}
continue; /* Go round the loop again */
}
/* Other matching errors are not recoverable. */
if (rc < 0)
{
printf("Matching error %d\n", rc);
pcre_free(re); /* Release memory used for the compiled needle */
return 1;
}
/* Match succeeded */
//printf("\nNew: %d\n", ovector[0]+1);
lexer_add( currentLexer, ovector[0]+1, token );
/* The match succeeded, but the output vector wasn't big enough. */
}
printf( "\n" );
pcre_free( re ); /* Release memory used for the compiled needle */
return 1;
}
void lexer_getTokens( lexer * currentLexer, char * source ) {
//printf(" lexer_getTokens\n\n");
lexer_tokenizeRegex( currentLexer, source, "\\sclass\\s", "class" );
lexer_tokenize( currentLexer, source, "{" );
lexer_tokenize( currentLexer, source, "}" );
lexer_tokenize( currentLexer, source, "(" );
lexer_tokenize( currentLexer, source, ")" );
lexer_tokenize( currentLexer, source, ";" );
lexer_tokenize( currentLexer, source, "<" );
lexer_tokenize( currentLexer, source, ">" );
lexer_tokenize( currentLexer, source, "=" );
lexer_tokenize( currentLexer, source, "->" );
lexer_tokenize( currentLexer, source, "." );
lexer_tokenize( currentLexer, source, "\"" );
lexer_tokenize( currentLexer, source, "#include" );
lexer_tokenize( currentLexer, source, "#" );
lexer_tokenize( currentLexer, source, "extends" );
lexer_tokenize( currentLexer, source, "reflect" );
lexer_tokenize( currentLexer, source, "template" );
}
int lexer_findBodyCloseIndex( lexer * currentLexer, int fromKey ) {
int count = lexer_length( currentLexer );
int depth = 0;
for (int i = fromKey; i < count; ++i)
{
int key = lexer_getKey( currentLexer, i );
char * currentToken = lexer_getToken( currentLexer, i );
if ( strcmp( currentToken, "{" ) == 0 ) {
depth++;
}
if ( strcmp( currentToken, "}" ) == 0 ) {
depth--;
}
if( depth == 0 ) {
return i;
}
}
return -1;
}
int lexer_findPreviousToken( lexer * currentLexer, int fromKey, char * token ) {
for (int i = fromKey - 1; i > 0; --i)
{
int key = lexer_getKey( currentLexer, i );
char * currentToken = lexer_getToken( currentLexer, i );
if ( strcmp( currentToken, token ) == 0 ) {
return key;
}
}
return -1;
}
void lexer_sortKeys_separate( struct array * keys, struct array * tokens ) {
int count = array_length( keys );
int i, j;
void * temp;
void * temp2;
for (i = 0; i < (count - 1); ++i)
{
for (j = 0; j < count - 1 - i; ++j )
{
intptr_t a = ( intptr_t ) array_get( keys, j );
intptr_t b = ( intptr_t ) array_get( keys, j+1 );
if (a > b)
{
temp = keys->items[j+1];
temp2 = tokens->items[j+1];
keys->items[j+1] = keys->items[j];
keys->items[j] = temp;
tokens->items[j+1] = tokens->items[j];
tokens->items[j] = temp2;
}
}
}
}

79
source/lexer.h Executable file
View File

@@ -0,0 +1,79 @@
#ifndef _lexer
#define _lexer
#include <array.h>
#include <string.h>
#include <text.h>
#include <lexer.h>
#include <stdio.h>
#include <string.h>
#include <pcre.h>
#define OVECCOUNT 30 /* should be a multiple of 3 */
typedef struct lexer{
int * keys;
char * * tokens;
int capacity;
int total;
int index;
} lexer;
lexer * lexer_new();
int lexer_length( lexer * v );
void lexer_resize( lexer * currentLexer, int capacity );
void lexer_add( lexer * currentLexer, int key, char * token );
char * lexer_getToken( lexer * currentLexer, int index );
int lexer_getKey( lexer * currentLexer, int index );
void lexer_setIndex( lexer * currentLexer, int index );
int lexer_tokenize( lexer * currentLexer, char * haystack, char * needle );
void lexer_sortKeys( lexer * currentLexer );
int lexer_findNextToken( lexer * currentLexer, int fromKey, char * token );
int lexer_findPreviousTokenIndex( lexer * currentLexer, int fromKey, char * token );
int lexer_tokenizeRegex( lexer * currentLexer, char * haystack, char * needle, char * token );
void lexer_getTokens( lexer * currentLexer, char * source );
int lexer_findBodyCloseIndex( lexer * currentLexer, int fromKey );
int lexer_findPreviouseTokenIndex( lexer * currentLexer, int fromIndex, struct array * validPreviousTokens );
int lexer_findNextTokenIndex( lexer * currentLexer, int fromKey, char * token );
int lexer_findPreviousKeyByToken( lexer * currentLexer, int fromKey, char * token );
void lexer_setSource( lexer * currentLexer, char * source );
int lexer_findPreviousToken( lexer * currentLexer, int fromKey, char * token );
void lexer_sortKeys_separate( struct array * keys, struct array * tokens );
#endif

3
source/macro.c Normal file
View File

@@ -0,0 +1,3 @@
#include "macro.h"

17
source/macro.h Normal file
View File

@@ -0,0 +1,17 @@
#ifndef _macro
#define _macro
#include "index.h"
#include "array.h"
#include "string.h"
#include "tools.h"
#include <stdio.h>
#endif

2570
source/method.c Normal file
View File

File diff suppressed because it is too large Load Diff

140
source/method.h Normal file
View File

@@ -0,0 +1,140 @@
#ifndef _method
#define _method
#include "lexer.h"
#include "index.h"
#include "array.h"
#include "function.h"
#include "class.h"
#include "method.h"
#include "argument.h"
#include "string.h"
#include "text.h"
#include "codeBundle.h"
#include "replacement.h"
#include "tools.h"
#include "variable.h"
#include <stdlib.h>
#include <stdbool.h>
struct method{
char * methodName;
char * body;
char * bodyOriginal;
char * returnType;
int lineNumber;
int isVariadic;
int isOperator;
bool isSetter;
bool isGetter;
char * operator;
struct array * arguments;
struct array * variables;
};
void method_sortLineNumbers( struct array * lineNumber );
char * method_getReturnTypeFromDeclerationParts( struct array * methodDeclerationParts, int methodDeclerationCount );
void method_extractMethodNameAndReturnType( int previousKey, int key, char * source, struct method * methodInstance );
void method_extractVariablesFromArguments( struct array * arguments, array * classesArray, struct array * variables );
void method_processVariadicMethodArguments( struct class * classInstance, struct method * methodInstance );
char * method_extractTemplates( char * source );
void method_addThisVariableToVariables( struct class * classInstance, array * variables );
void method_addThisVariableToMethodArguments( struct class * classInstance, struct method * methodInstance );
struct array * method_addArgumentsToMethod( struct class * classInstance, struct method * methodInstance, struct array * classesArray, struct array * argumentsArray );
struct property * method_getArrowRecursive( struct array * arrows, char * datatype );
struct array * method_extractArguments( lexer * currentLexer, int key, char * source, int i, struct class * classInstance, struct method * methodInstance, struct array * classesArray );
struct array * method_getArguments_new( lexer * currentLexer, int key, int i, char * source, struct array * variables );
int method_checkIfSymbolIsOperator( char * functionName );
int method_methodIsVaradic( array * arguments );
char * method_composeOperatorOverloadOpeningDeclaration( char * baseClassName, char * operatorName, int leftSideIsPointer ) ;
char * method_getOperatorNameByOperatorSymbol( char * symbol );
int method_findMethodByName( struct class * classInstance, char * methodName );
struct method * method_getMethodByMethodName( struct class * classInstance, char * methodName );
char * method_replaceMultiMethods( char * body );
char * method_overloadOperators( char * body, struct array * variables );
void method_parseFirstNextMethodInChain( lexer * currentLexer, int i, char * body, int key, struct array * replacements );
void method_parseFirstChainMethodCall( lexer * currentLexer, int i, char * body, int currentKey, char * currentToken, struct array * variables, struct array * replacements, int methodSeparatorLength );
void method_replaceNewSymbol( lexer * currentLexer, int i, char * body, int currentKey, struct array * variables, struct array * replacements );
void method_compose( struct method * currentMethod, char * className, struct array * sourceArray, struct text * classHeader );
char * method_parse( char * body, struct array * variables, array * functions );
struct method * method_extractMethodFromSymbol( char * functionName );
int method_processOperatorSymbols( char * body,
int currentKey,
char * currentToken,
array * replacements,
array * variables );
int method_processOperatorSymbol( char * currentOperatorSymbol,
char * body,
int currentKey,
char * currentToken,
array * replacements,
array * variables );
int method_findRightSideEndIndex( char * body, int currentKey, int symbolLength, struct array * variables );
char * method_findLeftSideSymbol( char * body, int currentKey, int symbolLength, int * beforeVariableNameKey, array * variables );
#endif

107
source/property.c Normal file
View File

@@ -0,0 +1,107 @@
#include "property.h"
char * property_composePropertyValue( char * propertyValue, struct property * propertyInstance ) {
struct array * valueParts = text_split( propertyValue, " \t" );
int valuePartsLength = array_length( valueParts );
for (int i = 0; i < valuePartsLength; ++i)
{
char * valuePart = array_get( valueParts, i );
if( strcmp( valuePart, "new" ) == 0 ) {
char * afterNew = array_get( valueParts, i + 1 );
int argumentOpenIndex = text_findFirstCharacterIndex( propertyValue, "(" );
int newKeywordIndex = text_findFirstCharacterIndex( propertyValue, "new" ) + 3;
char * argumentText = text_removeWhiteSpaces( text_slice( propertyValue, argumentOpenIndex , strlen( propertyValue ) ) );
char * datatype = text_removeWhiteSpaces( text_slice( propertyValue, newKeywordIndex , argumentOpenIndex-1 ) );
struct text * newMethodDecleration = text_new( "" );//datatype
text_append( newMethodDecleration, datatype );
text_append( newMethodDecleration, "_new" );
if( propertyInstance->isPointer == 1 ) {
text_append( newMethodDecleration, "Pointer" );
}
text_append( newMethodDecleration, argumentText );
return newMethodDecleration->value;
}
}
//propertyInstance->value = propertyValue;
return propertyValue;
}
char * property_extractPropertyDeclaration( struct array * propertyParts, struct property * propertyInstance ) {
char * formattedDecleration = "";
int propertyPartsLength = array_length( propertyParts );
for ( int j = 0; j < propertyPartsLength; ++j )
{
char * part = (char *) array_get( propertyParts, j );
if( strcmp( part, "const" ) == 0 ) {
} else if( strcmp( part, "volatile" ) == 0 ) {
} else if( strcmp( part, "*" ) == 0 ) {
} else {
if( j < propertyPartsLength - 1 ) {
propertyInstance->datatype = part;
}
}
if( j != 0 ){
formattedDecleration = text_concatenate( formattedDecleration, " " );
}
formattedDecleration = text_concatenate( formattedDecleration, part );
}
return formattedDecleration;
}
char * property_extractPropertyName( struct array * propertyParts ) {
int propertyPartsLength = array_length( propertyParts );
return text_copy( array_get( propertyParts, propertyPartsLength - 1 ) ) ;
}

44
source/property.h Normal file
View File

@@ -0,0 +1,44 @@
#ifndef _property
#define _property
#include "property.h"
#include "array.h"
#include "text.h"
#include "index.h"
struct property{
int hasValue;
char * propertyName;
char * datatype;
char * decleration;
char * value;
int isPointer;
char * type;
struct array * declerationParts;
};
char * property_composePropertyValue( char * propertyValue, struct property * propertyInstance );
char * property_extractPropertyDeclaration( struct array * propertyParts, struct property * propertyInstance );
char * property_extractPropertyName( struct array * propertyParts );
#endif

49
source/replacement.c Normal file
View File

@@ -0,0 +1,49 @@
#include "replacement.h"
void replacement_add( int fromKey, int toKey, char * content, struct array * replacements ) {
struct replacement * replacementInstance = malloc( sizeof( struct replacement ) );
replacementInstance->fromIndex = fromKey;
replacementInstance->toIndex = toKey;
replacementInstance->content = content;
array_add( replacements, replacementInstance );
}
void replacement_sort( struct array * replacements ) {
int count = array_length( replacements );
int i, j;
void * temp;
for (i = 0; i < (count - 1); ++i)
{
for (j = 0; j < count - 1 - i; ++j )
{
struct replacement * a = array_get( replacements, j );
struct replacement * b = array_get( replacements, j+1 );
if ( a->fromIndex > b->fromIndex )
{
temp = b;
replacements->items[j+1] = a;
replacements->items[j] = temp;
}
}
}
}

24
source/replacement.h Normal file
View File

@@ -0,0 +1,24 @@
#ifndef _replacement
#define _replacement
#include "array.h"
#include <stdlib.h>
struct replacement{
int fromIndex;
int toIndex;
char * content;
};
void replacement_add( int fromKey, int toKey, char * content, struct array * replacements );
void replacement_sort( struct array * replacements );
#endif

140
source/template.c Normal file
View File

@@ -0,0 +1,140 @@
#include "template.h"
struct template * template_new() {
struct template * templateInstance = malloc( sizeof( struct template ) );
templateInstance->instances = array_new();
return templateInstance;
}
void template_add_arguments( struct template * currentTemplate, char * argumentText ) {
printf("add arguments to template\n\n\n");
currentTemplate->arguments = text_split( argumentText, "," );
}
bool template_validateArguments( struct template * currentTemplate, char * argumentText ) {
array * argumentsArray = text_split( argumentText, "," );
if( array_length( argumentsArray ) == array_length( currentTemplate->arguments ) ) {
return true;
} else {
return false;
}
}
bool template_addArgumentValue( struct class * classInstance, char * templateArguments ) {
templateArguments = text_regexReplaceAll( templateArguments, "struct ", "struct ", "" );
if( !template_validateArguments( classInstance->template, templateArguments ) ) {
printf("Error: Invalid template arguments. %s \n\n", templateArguments);
return false;
}
if( !template_instanceExists( classInstance->template, templateArguments ) ) {
printf("Adding template instance. \n\n");
template_addInstance( classInstance->template, templateArguments );
template_instanceExists( classInstance->template, templateArguments );
}
return true;
}
char * template_extractTemplateName( struct template * currentTemplate, char * argumentText ) {
array * argumentsArray = text_split( argumentText, "," );
if( array_length( argumentsArray ) > 0 ) {
return array_get( argumentsArray, 0 );
}
return NULL;
}
bool template_instanceExists( struct template * currentTemplate, char * argumentText ) {
array * argumentsValueArray = text_split( argumentText, "," );
array * intances = currentTemplate->instances;
int count = array_length( intances );
for (int i = 0; i < count; ++i)
{
struct templateInstance * currentTemplateInstance = array_get( intances, i );
array * templateArgumentValues = currentTemplateInstance->argumentValues;
int propertyCount = array_length( templateArgumentValues );
bool allPropertiesMatch = true;
for (int j = 0; j < propertyCount; ++j )
{
char * currentArgumentValue = array_get( templateArgumentValues, j );
char * ArgumentValue = array_get( argumentsValueArray, j );
if( strcmp( currentArgumentValue, ArgumentValue ) != 0 ) {
allPropertiesMatch = false;
}
}
if( allPropertiesMatch ) {
return true;
}
}
return false;
}
void template_addInstance( struct template * currentTemplate, char * argumentText ) {
struct templateInstance * newInstance = templateInstance_new();
newInstance->argumentValues = text_split( argumentText, "," );
array_add( currentTemplate->instances, newInstance );
}
//templateInstance_new

48
source/template.h Normal file
View File

@@ -0,0 +1,48 @@
#ifndef _template
#define _template
#include "stdlib.h"
#include <stdio.h>
#include <array.h>
#include <class.h>
#include "templateInstance.h"
#include "templateInstanceProperty.h"
#include <stdbool.h>
#include "text.h"
#include "string.h"
extern array * allClasses;
struct template{
struct array * arguments;
struct array * instances;
};
struct template * template_new();
bool template_addArgumentValue( struct class * classInstance, char * templateArguments );
char * template_extractTemplateName( struct template * currentTemplate, char * argumentText );
void template_add_arguments( struct template * currentTemplate, char * argumentText );
bool template_validateArguments( struct template * currentTemplate, char * argumentText );
void template_addInstance( struct template * currentTemplate, char * argumentText );
bool template_instanceExists( struct template * currentTemplate, char * argumentText );
#endif

12
source/templateInstance.c Normal file
View File

@@ -0,0 +1,12 @@
#include "templateInstance.h"
struct templateInstance * templateInstance_new() {
struct templateInstance * templateInstance = malloc( sizeof( struct templateInstance ) );
templateInstance->argumentValues = array_new();
return templateInstance;
}

23
source/templateInstance.h Normal file
View File

@@ -0,0 +1,23 @@
#ifndef _templateInstance
#define _templateInstance
#include <stdio.h>
#include <array.h>
#include "stdlib.h"
struct templateInstance{
struct array * argumentValues; // not properties
};
struct templateInstance * templateInstance_new();
#endif

View File

@@ -0,0 +1,3 @@
#include "templateInstanceProperty.h"

View File

@@ -0,0 +1,19 @@
#ifndef _templateInstancePropety
#define _templateInstancePropety
#include <stdio.h>
#include <array.h>
struct templateInstanceProperty{
char * namepace;
char * value;
};
#endif

1761
source/text.c Executable file
View File

File diff suppressed because it is too large Load Diff

133
source/text.h Executable file
View File

@@ -0,0 +1,133 @@
#ifndef _text
#define _text
#include <stdarg.h>
#include <math.h>
#include <stdbool.h>
typedef struct text{
char * value;
char * buffer;
int useBuffer;
int length;
int capacity;
} text;
char * text_removeSpaces( char * s );
bool text_contains( char * s, char * needle );
int text_findArgumentCloseIndex( char * body, int fromKey );
void text_validateCurlyBrackets( char * source );
char * text_regexReplaceAll( char * haystack, char * needle, char * needleText, char * replacement );
char * text_regexReplaceAll2( char * haystack, char * needle, char * needleText, char * replacement );
char * text_replaceAll( char * target, const char * needle, const char * replacement );
char * text_replace( char * haystack, char * needle, char * replaceWith );
int text_isValidNumber( char * a );
int text_findNumberOfLineBreaks( char * source );
char * text_replaceChar( char * text, char needle, char replacement );
int text_findNextLineBreak( char * body, int currentKey );
char * text_replaceAt( int fromKey, int toKey, char * target, char * source );
struct text * text_new( char * value );
char * text_removeAllWhiteSpaces( char * a );
struct text * text_appendObject( struct text * this, struct text * object );
void text_free( struct text * this );
char * text_removeSlashFromStart( char * filePath );
void text_constructor( struct text * this, char * value );
void text_resize( struct text * this, int size );
struct text * text_append( struct text * this, const char * value );
struct text * text_appendDebug( struct text * this, const char * value );
struct array * text_splitArguments( char * argumentsText );
int text_exists( char * phrase, char * word );
int text_extractIndexAtWhiteSpace( char * body, int currentKey );
int text_extractIndexAfterWord( char * body, int currentKey );
int text_getIndexBeforeWord( char * body, int currentKey );
int text_indexOff( char * source, char * token, int fromIndex );
char * text_reverse( char * str );
char * text_copy( char * );
char * text_fromNumber( int number );
int text_findPreviousLineBreak( char * body, int currentKey );
char * text_formatLength( char * a, int newLength );
char * text_slice( char *, int, int);
int text_countLineBreaks( char * text, int fromIndex );
char * text_join( struct array * arrayInstance, char * separator );
int text_findFirstCharacterIndex( char * text, char * character );
int text_findFirstCharacterReverse( char * body, int fromKey );
int text_text_findFirstCharacterIndexbackwards( char * body, int fromKey );
int text_findFirstNextCharacterNotWhiteSpace( char * body, int fromKey );
int text_seekParenthesesLeftToRight( char * body, int currentKey );
int text_findQuoteCloseIndex( char * body, int fromKey );
int text_findFirstNextCharacterNotWhiteSpaceNotParenthesis( char * body, int fromKey );
int text_text_findFirstCharacterIndexFromIndex( char * text, char * character, int fromIndex );
char * text_concatenateMultiple( int, ... );
char * text_concatenate( char * a, char * b );
struct array * text_split( char *, char * ) ;
char * text_removeWhiteSpacesLeft(char * );
char * text_removeWhiteSpacesRight(char * );
char * text_removeWhiteSpaces( char * );
char * text_extractWordBeforeKey( char *, int );
#endif

88
source/tools.c Normal file
View File

@@ -0,0 +1,88 @@
#include "tools.h"
char * tools_findDatatype( struct array * variableDeclarationParts ) {
int partsCount = array_length( variableDeclarationParts );
char * datatype = malloc( 100 );
for (int i = 0; i < partsCount; ++i)
{
char * variablePart = text_removeWhiteSpaces( array_get( variableDeclarationParts, i ) );
if( strcmp( variablePart, "extern") == 0 ) {
continue;
}
if( strcmp( variablePart, "struct") == 0 ) {
continue;
}
datatype = variablePart;
break;
}
return datatype;
}
int tools_findArgumentCloseIndexReverse( char * body, int fromKey ) {
int depth = 0;
int hasStarted = -1;
for (int i = fromKey; i >= 0; --i)
{
// ')'
if ( (int)body[i] == 41 ) {
depth++;
hasStarted = 1;
}
if( hasStarted == 1 && depth == 0 ) {
return i + 1;
}
// '('
if ( (int)body[i] == 40 ) {
depth--;
}
}
return -1;
}
char * tools_removePointerSymbolFromDatatype( char * returnType, int * leftSideIsPointer ) {
struct array * returnTypeParts = text_split( text_copy( returnType ), " " );
*leftSideIsPointer = variable_isPointer( returnTypeParts );
//printf(" removePointerSymbolFromDatatype: %i \n\n\n", *leftSideIsPointer);
return array_get( returnTypeParts, 0 );
}

24
source/tools.h Normal file
View File

@@ -0,0 +1,24 @@
#ifndef _tools
#define _tools
#include <stdlib.h>
#include <array.h>
#include <text.h>
#include <string.h>
#include <variable.h>
char * tools_findDatatype( struct array * variableDeclarationParts );
int tools_findArgumentCloseIndexReverse( char * body, int fromKey );
char * tools_removePointerSymbolFromDatatype( char * returnType, int * leftSideIsPointer );
#endif

220
source/variable.c Normal file
View File

@@ -0,0 +1,220 @@
#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;
}

42
source/variable.h Normal file
View File

@@ -0,0 +1,42 @@
#ifndef _variable
#define _variable
#include "index.h"
#include "array.h"
#include "string.h"
#include "tools.h"
#include "lexer.h"
#include <stdio.h>
struct variable{
char * variableName;
char * datatype;
char * declaration;
int isPointer;
int lineNumber;
};
struct variable * variabe_new();
void variable_getVariablesByArgumentParts( struct array * argumentParts, struct array * variables, struct array * classesArray );
struct variable * variable_getByName( struct array * variables, char * name );
int variable_isPointer( struct array * propertyParts );
void variable_extractFromDeclaration( lexer * currentLexer, int currentKey, int i, char * body, struct array * variables );
#endif