diff --git a/src/set/set.c b/src/set/set.c index bad66a8..9b673a0 100644 --- a/src/set/set.c +++ b/src/set/set.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -5,13 +6,30 @@ #include #include + GHashTable *declaredComposites;//pointer to composites with names, GHashTable *declaredBoxes;//pointer to typeboxes -GArray *Scope;//last object is current scope - - +GArray *Scope;//list of hashtables. last Hashtable is current depth of program. hashtable key: ident, value: Variable* to var +const Type ShortShortUnsingedIntType = { + .kind = TypeKindComposite, + .impl = { + .composite = { + .sign = Unsigned, + .scale = 0.25, + .primitive = Int + } + }, + .nodePtr = NULL, +}; +const Type StringLiteralType = { + .kind = TypeKindReference, + .impl = { + .reference = (ReferenceType) &ShortShortUnsingedIntType, + }, + .nodePtr = NULL, +}; Type *findType(AST_NODE_PTR currentNode){ @@ -129,7 +147,7 @@ StorageQualifier Qualifier_from_string(const char *str) { Variable **createDecl(AST_NODE_PTR currentNode){ DEBUG("create declaration"); - Variable **variables = malloc(currentNode->children[currentNode->child_count -1]->child_count * sizeof(Variable)); + Variable **variables = malloc(currentNode->children[currentNode->child_count -1]->child_count * sizeof(Variable*)); VariableDeclaration decl; decl.nodePtr = currentNode; @@ -165,21 +183,22 @@ Variable **createDecl(AST_NODE_PTR currentNode){ return variables; } -int isVariableInScope(const char* name){ - +Variable* getVariableFromScope(const char* name){ for(size_t i = 0; i < Scope->len; i++){ if(g_hash_table_contains(((GHashTable **) Scope->data)[i], name)) { - return 1; + return g_hash_table_lookup(((GHashTable**)Scope->data)[i], name); } } - return 0; + return NULL; } + + int fillTablesWithVars(GHashTable *variableTable,GHashTable *currentScope , Variable** content, size_t amount){ DEBUG("filling vars in scope and table"); for(size_t i = 0; i < amount; i++){ - if(isVariableInScope(content[i]->name)){ + if(!(NULL == getVariableFromScope(content[i]->name))){ DEBUG("this var already exist: ",content[i]->name); return 1; } @@ -218,25 +237,167 @@ TypeValue createTypeValue(AST_NODE_PTR currentNode){ return value; } +TypeValue createString(AST_NODE_PTR currentNode){ + TypeValue value; + Type *type =(Type*) &StringLiteralType; + value.type = type; + value.nodePtr = currentNode; + value.value = currentNode->value; + return value; +} + +Expression *createExpression(AST_NODE_PTR currentNode); + + +Type* createTypeFromOperands(Type* LeftOperandType, Type* RightOperandType, AST_NODE_PTR currentNode){ + Type *result = malloc(sizeof(Type)); + result->nodePtr = currentNode; + + + if(LeftOperandType->kind == TypeKindComposite && RightOperandType->kind == TypeKindComposite) + { + result->kind = TypeKindComposite; + CompositeType resultImpl; + + resultImpl.nodePtr = currentNode; + resultImpl.sign = MAX(LeftOperandType->impl.composite.sign, RightOperandType->impl.composite.sign); + resultImpl.scale = MAX(LeftOperandType->impl.composite.scale, RightOperandType->impl.composite.scale); + resultImpl.primitive = MAX(LeftOperandType->impl.composite.primitive , RightOperandType->impl.composite.primitive); + + result->impl.composite = resultImpl; + + + } else if(LeftOperandType->kind == TypeKindPrimitive && RightOperandType->kind == TypeKindPrimitive){ + result->kind = TypeKindPrimitive; + + result->impl.primitive = MAX(LeftOperandType->impl.primitive , RightOperandType->impl.primitive); + + } else if(LeftOperandType->kind == TypeKindPrimitive && RightOperandType->kind == TypeKindComposite){ + result->kind = TypeKindComposite; + + result->impl.composite.sign = Signed; + result->impl.composite.scale = MAX( 1.0, RightOperandType->impl.composite.scale); + result->impl.composite.primitive = MAX(Int, RightOperandType->impl.composite.primitive); + result->impl.composite.nodePtr = currentNode; + + } else if(LeftOperandType->kind == TypeKindComposite && RightOperandType->kind == TypeKindPrimitive){ + result->kind = TypeKindComposite; + + result->impl.composite.sign = Signed; + result->impl.composite.scale = MAX( 1.0, LeftOperandType->impl.composite.scale); + result->impl.composite.primitive = MAX(Int, LeftOperandType->impl.composite.primitive); + result->impl.composite.nodePtr = currentNode; + }else{ + return NULL; + } + return result; +} + +int createArithOperation(Expression* ParentExpression, AST_NODE_PTR currentNode, size_t expectedChildCount){ + + ParentExpression->impl.operation.kind = Arithmetic; + ParentExpression->impl.operation.nodePtr = currentNode; + if (expectedChildCount > currentNode->child_count){ + PANIC("Operation has to many children"); + } + for (size_t i = 0; i < currentNode->child_count; i++){ + Expression* expression = createExpression(currentNode->children[i]); + if(NULL == expression){ + return 1; + } + g_array_append_val(ParentExpression->impl.operation.operands , expression); + + } + + switch (currentNode->kind){ + case AST_Add: + ParentExpression->impl.operation.impl.arithmetic = Add; + case AST_Sub: + ParentExpression->impl.operation.impl.arithmetic = Sub; + case AST_Mul: + ParentExpression->impl.operation.impl.arithmetic = Mul; + case AST_Div: + ParentExpression->impl.operation.impl.arithmetic = Div; + case AST_Negate: + ParentExpression->impl.operation.impl.arithmetic = Negate; + default: + PANIC("Current node is not an arithmetic operater"); + break; + } + + + if(ParentExpression->impl.operation.impl.arithmetic == Negate){ + Type* result = malloc(sizeof(Type)); + result = ((Expression**) ParentExpression->impl.operation.operands->data)[0]->result; + result->nodePtr = currentNode; + if (result->kind == TypeKindReference || result->kind == TypeKindBox){ + return 1; + }else if(result->kind == TypeKindComposite){ + result->impl.composite.sign = Signed; + } + ParentExpression->result = result; + + }else{ + + Type* LeftOperandType = ((Expression**) ParentExpression->impl.operation.operands->data)[0]->result; + Type* RightOperandType = ((Expression**) ParentExpression->impl.operation.operands->data)[1]->result; + + ParentExpression->result = createTypeFromOperands(LeftOperandType, RightOperandType, currentNode); + } + + + + return 0; +} + + + Expression *createExpression(AST_NODE_PTR currentNode){ Expression *expression = malloc(sizeof(Expression)); - + expression->nodePtr = currentNode; switch(currentNode->kind){ case AST_Int: case AST_Float: expression->kind = ExpressionKindConstant; expression->impl.constant = createTypeValue(currentNode); + expression->result = expression->impl.constant.type; case AST_String: - //TODO + expression->kind = ExpressionKindConstant; + expression->impl.constant = createString(currentNode); + expression->result = expression->impl.constant.type; case AST_Ident: + expression->kind = ExpressionKindVariable; + expression->impl.variable = getVariableFromScope(currentNode->value); + if(NULL == expression->impl.variable){ + DEBUG("Identifier is not in current scope"); + return NULL; + } + switch (expression->impl.variable->kind) { + case VariableKindDeclaration: + expression->result = expression->impl.variable->impl.declaration.type; + case VariableKindDefinition: + expression->result = expression->impl.variable->impl.definiton.declaration.type; + default: + PANIC("current Variable should not be an BoxMember"); + break; + } case AST_Add: case AST_Sub: case AST_Mul: case AST_Div: + expression->kind = ExpressionKindOperation; + if(createArithOperation(expression, currentNode, 2)){ + return NULL; + } case AST_Negate: + expression->kind = ExpressionKindOperation; + if(createArithOperation(expression,currentNode, 1)){ + return NULL; + } + case AST_Eq: case AST_Less: @@ -252,6 +413,13 @@ Expression *createExpression(AST_NODE_PTR currentNode){ case AST_BitXor: case AST_BitNot: + case AST_IdentList: + //Box Accsess + case AST_List: + // Box Self Access + case AST_Typekind: + case AST_Transmute: + default: PANIC("Node is not an expression but from kind: %i", currentNode->kind); diff --git a/src/set/types.h b/src/set/types.h index 286a2aa..d425066 100644 --- a/src/set/types.h +++ b/src/set/types.h @@ -188,6 +188,7 @@ typedef enum ParameterKind_t { */ typedef struct Parameter_t { const char* name; + ParameterKind kind; union ParameterImplementation { ParameterDeclaration declaration; @@ -316,7 +317,8 @@ typedef enum ArithmeticOperator_t { Add, Sub, Mul, - Div + Div, + Negate } ArithmeticOperator; // .------------------------------------------------. @@ -389,7 +391,7 @@ typedef struct Operation_t { LogicalOperator logical; BitwiseOperator bitwise; } impl; - Expression* operands; + GArray* operands; //Expression* AST_NODE_PTR nodePtr; } Operation; @@ -414,7 +416,7 @@ typedef struct Expression_t { TypeCast typecast; Transmute transmute; TypeValue constant; - Variable variable; + Variable* variable; } impl; AST_NODE_PTR nodePtr; } Expression;