added expressions and types

This commit is contained in:
Felix Müller 2024-06-02 23:26:51 +02:00
parent 8e1a1664da
commit 7922fbc8b7
2 changed files with 184 additions and 14 deletions

View File

@ -1,3 +1,4 @@
#include <complex.h>
#include <stdio.h> #include <stdio.h>
#include <ast/ast.h> #include <ast/ast.h>
#include <set/types.h> #include <set/types.h>
@ -5,13 +6,30 @@
#include <sys/log.h> #include <sys/log.h>
#include <glib.h> #include <glib.h>
GHashTable *declaredComposites;//pointer to composites with names, GHashTable *declaredComposites;//pointer to composites with names,
GHashTable *declaredBoxes;//pointer to typeboxes 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){ Type *findType(AST_NODE_PTR currentNode){
@ -129,7 +147,7 @@ StorageQualifier Qualifier_from_string(const char *str) {
Variable **createDecl(AST_NODE_PTR currentNode){ Variable **createDecl(AST_NODE_PTR currentNode){
DEBUG("create declaration"); 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; VariableDeclaration decl;
decl.nodePtr = currentNode; decl.nodePtr = currentNode;
@ -165,21 +183,22 @@ Variable **createDecl(AST_NODE_PTR currentNode){
return variables; return variables;
} }
int isVariableInScope(const char* name){ Variable* getVariableFromScope(const char* name){
for(size_t i = 0; i < Scope->len; i++){ for(size_t i = 0; i < Scope->len; i++){
if(g_hash_table_contains(((GHashTable **) Scope->data)[i], name)) 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){ int fillTablesWithVars(GHashTable *variableTable,GHashTable *currentScope , Variable** content, size_t amount){
DEBUG("filling vars in scope and table"); DEBUG("filling vars in scope and table");
for(size_t i = 0; i < amount; i++){ 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); DEBUG("this var already exist: ",content[i]->name);
return 1; return 1;
} }
@ -218,25 +237,167 @@ TypeValue createTypeValue(AST_NODE_PTR currentNode){
return value; 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 *createExpression(AST_NODE_PTR currentNode){
Expression *expression = malloc(sizeof(Expression)); Expression *expression = malloc(sizeof(Expression));
expression->nodePtr = currentNode;
switch(currentNode->kind){ switch(currentNode->kind){
case AST_Int: case AST_Int:
case AST_Float: case AST_Float:
expression->kind = ExpressionKindConstant; expression->kind = ExpressionKindConstant;
expression->impl.constant = createTypeValue(currentNode); expression->impl.constant = createTypeValue(currentNode);
expression->result = expression->impl.constant.type;
case AST_String: case AST_String:
//TODO expression->kind = ExpressionKindConstant;
expression->impl.constant = createString(currentNode);
expression->result = expression->impl.constant.type;
case AST_Ident: 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_Add:
case AST_Sub: case AST_Sub:
case AST_Mul: case AST_Mul:
case AST_Div: case AST_Div:
expression->kind = ExpressionKindOperation;
if(createArithOperation(expression, currentNode, 2)){
return NULL;
}
case AST_Negate: case AST_Negate:
expression->kind = ExpressionKindOperation;
if(createArithOperation(expression,currentNode, 1)){
return NULL;
}
case AST_Eq: case AST_Eq:
case AST_Less: case AST_Less:
@ -252,6 +413,13 @@ Expression *createExpression(AST_NODE_PTR currentNode){
case AST_BitXor: case AST_BitXor:
case AST_BitNot: case AST_BitNot:
case AST_IdentList:
//Box Accsess
case AST_List:
// Box Self Access
case AST_Typekind:
case AST_Transmute:
default: default:
PANIC("Node is not an expression but from kind: %i", currentNode->kind); PANIC("Node is not an expression but from kind: %i", currentNode->kind);

View File

@ -188,6 +188,7 @@ typedef enum ParameterKind_t {
*/ */
typedef struct Parameter_t { typedef struct Parameter_t {
const char* name; const char* name;
ParameterKind kind; ParameterKind kind;
union ParameterImplementation { union ParameterImplementation {
ParameterDeclaration declaration; ParameterDeclaration declaration;
@ -316,7 +317,8 @@ typedef enum ArithmeticOperator_t {
Add, Add,
Sub, Sub,
Mul, Mul,
Div Div,
Negate
} ArithmeticOperator; } ArithmeticOperator;
// .------------------------------------------------. // .------------------------------------------------.
@ -389,7 +391,7 @@ typedef struct Operation_t {
LogicalOperator logical; LogicalOperator logical;
BitwiseOperator bitwise; BitwiseOperator bitwise;
} impl; } impl;
Expression* operands; GArray* operands; //Expression*
AST_NODE_PTR nodePtr; AST_NODE_PTR nodePtr;
} Operation; } Operation;
@ -414,7 +416,7 @@ typedef struct Expression_t {
TypeCast typecast; TypeCast typecast;
Transmute transmute; Transmute transmute;
TypeValue constant; TypeValue constant;
Variable variable; Variable* variable;
} impl; } impl;
AST_NODE_PTR nodePtr; AST_NODE_PTR nodePtr;
} Expression; } Expression;