Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rpkak committed Nov 5, 2023
1 parent f367582 commit d18ed5c
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 49 deletions.
61 changes: 43 additions & 18 deletions src/ptlang_ast/ptlang_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -634,30 +634,32 @@ ptlang_ast_type ptlang_ast_type_copy(ptlang_ast_type type)
switch (type->type)
{
case PTLANG_AST_TYPE_VOID:
return ptlang_ast_type_void(type->pos);
return ptlang_ast_type_void(ptlang_ast_code_position_copy(type->pos));
case PTLANG_AST_TYPE_INTEGER:
return ptlang_ast_type_integer(type->content.integer.is_signed, type->content.integer.size,
type->pos);
ptlang_ast_code_position_copy(type->pos));
case PTLANG_AST_TYPE_FLOAT:
return ptlang_ast_type_float(type->content.float_size, type->pos);
return ptlang_ast_type_float(type->content.float_size, ptlang_ast_code_position_copy(type->pos));
case PTLANG_AST_TYPE_FUNCTION:
return ptlang_ast_type_function(ptlang_ast_type_copy(type->content.function.return_type),
ptlang_ast_type_list_copy(type->content.function.parameters),
type->pos);
ptlang_ast_code_position_copy(type->pos));
case PTLANG_AST_TYPE_HEAP_ARRAY:
return ptlang_ast_type_heap_array(ptlang_ast_type_copy(type->content.heap_array.type), type->pos);
return ptlang_ast_type_heap_array(ptlang_ast_type_copy(type->content.heap_array.type),
ptlang_ast_code_position_copy(type->pos));
case PTLANG_AST_TYPE_ARRAY:
return ptlang_ast_type_array(ptlang_ast_type_copy(type->content.array.type), type->content.array.len,
type->pos);
ptlang_ast_code_position_copy(type->pos));
case PTLANG_AST_TYPE_REFERENCE:
return ptlang_ast_type_reference(ptlang_ast_type_copy(type->content.reference.type),
type->content.reference.writable, type->pos);
type->content.reference.writable,
ptlang_ast_code_position_copy(type->pos));
case PTLANG_AST_TYPE_NAMED:
{
size_t name_len = strlen(type->content.name) + 1;
char *name = ptlang_malloc(name_len);
memcpy(name, type->content.name, name_len);
return ptlang_ast_type_named(name, type->pos);
return ptlang_ast_type_named(name, ptlang_ast_code_position_copy(type->pos));
}
}
abort();
Expand Down Expand Up @@ -695,6 +697,12 @@ ptlang_ast_type ptlang_ast_type_copy(ptlang_ast_type type)
// }
// }

void ptlang_ast_ident_destroy(ptlang_ast_ident ident)
{
ptlang_free(ident.name);
ptlang_free(ident.pos);
}

void ptlang_ast_type_destroy(ptlang_ast_type type)
{
if (type != NULL)
Expand Down Expand Up @@ -780,7 +788,7 @@ void ptlang_ast_module_destroy(ptlang_ast_module module)
arrfree(module->struct_defs);
for (size_t i = 0; i < arrlenu(module->type_aliases); i++)
{
ptlang_free(module->type_aliases[i].name.name);
ptlang_ast_ident_destroy(module->type_aliases[i].name);
ptlang_ast_type_destroy(module->type_aliases[i].type);
}
arrfree(module->type_aliases);
Expand All @@ -790,7 +798,7 @@ void ptlang_ast_module_destroy(ptlang_ast_module module)

void ptlang_ast_func_destroy(ptlang_ast_func func)
{
ptlang_free(func->name.name);
ptlang_ast_ident_destroy(func->name);
ptlang_free(func->pos);
ptlang_ast_type_destroy(func->return_type);
ptlang_ast_decl_list_destroy(func->parameters);
Expand All @@ -802,6 +810,7 @@ void ptlang_ast_func_destroy(ptlang_ast_func func)
void ptlang_ast_exp_destroy(ptlang_ast_exp exp)
{
ptlang_free(exp->pos);
ptlang_ast_type_destroy(exp->ast_type);
switch (exp->type)
{
case PTLANG_AST_EXP_ASSIGNMENT:
Expand Down Expand Up @@ -844,7 +853,7 @@ void ptlang_ast_exp_destroy(ptlang_ast_exp exp)
ptlang_free(exp->content.str_prepresentation);
break;
case PTLANG_AST_EXP_STRUCT:
ptlang_free(exp->content.struct_.type.name);
ptlang_ast_ident_destroy(exp->content.struct_.type);
ptlang_ast_struct_member_list_destroy(exp->content.struct_.members);
break;
case PTLANG_AST_EXP_ARRAY:
Expand All @@ -862,7 +871,7 @@ void ptlang_ast_exp_destroy(ptlang_ast_exp exp)
break;
case PTLANG_AST_EXP_STRUCT_MEMBER:
ptlang_ast_exp_destroy(exp->content.struct_member.struct_);
ptlang_free(exp->content.struct_member.member_name.name);
ptlang_ast_ident_destroy(exp->content.struct_member.member_name);
break;
case PTLANG_AST_EXP_ARRAY_ELEMENT:
ptlang_ast_exp_destroy(exp->content.array_element.array);
Expand All @@ -884,15 +893,15 @@ void ptlang_ast_decl_destroy(ptlang_ast_decl decl)
{
ptlang_ast_exp_destroy(decl->init);
}
ptlang_free(decl->name.name);
ptlang_ast_ident_destroy(decl->name);

ptlang_free(decl);
}

void ptlang_ast_struct_def_destroy(ptlang_ast_struct_def struct_def)
{
ptlang_free(struct_def->pos);
ptlang_free(struct_def->name.name);
ptlang_ast_ident_destroy(struct_def->name);
ptlang_ast_decl_list_destroy(struct_def->members);

ptlang_free(struct_def);
Expand Down Expand Up @@ -930,20 +939,36 @@ void ptlang_ast_struct_member_list_destroy(ptlang_ast_struct_member_list member_
{
for (size_t i = 0; i < arrlenu(member_list); i++)
{
ptlang_free(member_list[i].str.name);
ptlang_ast_ident_destroy(member_list[i].str);
ptlang_ast_exp_destroy(member_list[i].exp);
}
arrfree(member_list);
}

ptlang_ast_decl ptlang_decl_list_find_last(ptlang_ast_decl *decl_list, char *name)
{
for (size_t i = arrlenu(decl_list) - 1; i >= 0; i--)
for (size_t i = arrlenu(decl_list); i > 0; i--)
{
if (0 == strcmp(decl_list[i]->name.name, name)){
return decl_list[i];
if (0 == strcmp(decl_list[i - 1]->name.name, name))
{
return decl_list[i - 1];
}
}
return NULL;
}

ptlang_ast_code_position ptlang_ast_code_position_copy(ptlang_ast_code_position pos)
{
if (pos == NULL)
{
return NULL;
}
ptlang_ast_code_position new_pos = ptlang_malloc(sizeof(*new_pos));
*new_pos = ((ptlang_ast_code_position_s){
.from_line = pos->from_line,
.from_column = pos->from_column,
.to_line = pos->to_line,
.to_column = pos->to_column,
});
return new_pos;
}
3 changes: 3 additions & 0 deletions src/ptlang_ast/ptlang_ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,7 @@ void ptlang_ast_struct_member_list_destroy(ptlang_ast_struct_member_list member_

ptlang_ast_decl ptlang_decl_list_find_last(ptlang_ast_decl *decl_list, char *name);

ptlang_ast_code_position ptlang_ast_code_position_copy(ptlang_ast_code_position pos);


#endif
1 change: 1 addition & 0 deletions src/ptlang_parser/ptlang.y
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ declaration_without_export: decl EQ const_exp { $$ = $1; ptlang_ast_decl_set_ini
| non_const_decl { $$ = $1; }

module_decl: declaration_without_export { $$ = $1; }
| EXPORT declaration_without_export { $$ = $2; ptlang_ast_decl_set_export($$, true); }

struct_def: STRUCT_DEF ident OPEN_CURLY_BRACE struct_members CLOSE_CURLY_BRACE
{ $$ = ptlang_ast_struct_def_new($2, $4, ppcpft(&@2, &@$)); }
Expand Down
80 changes: 51 additions & 29 deletions src/ptlang_verify/ptlang_verify.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ static void ptlang_verify_statement(ptlang_ast_stmt statement, uint64_t nesting_
}));
*is_unreachable = false;
}
bool *child_node_has_return_value = false;
bool child_node_has_return_value = false;
ptlang_verify_statement(statement->content.block.stmts[i], nesting_level, validate_return_type,
wanted_return_type, new_scope_offset, child_node_has_return_value,
wanted_return_type, new_scope_offset, &child_node_has_return_value,
is_unreachable, ctx, errors);
*has_return_value |= *child_node_has_return_value;
*has_return_value |= child_node_has_return_value;
}
arrsetlen(ctx->scope, new_scope_offset);
break;
Expand Down Expand Up @@ -197,7 +197,20 @@ static void ptlang_verify_statement(ptlang_ast_stmt statement, uint64_t nesting_
}
}

static void ptlang_verify_decl(ptlang_ast_decl decl, size_t scope_offset, ptlang_context *ctx,
// static void ptlang_verify_decl(ptlang_ast_decl decl, size_t scope_offset, ptlang_context *ctx,
// ptlang_error **errors)
// {
// ptlang_verify_decl_header()
// }
// u64 2_HOCH_11 = ${ 2<<11 };
//
// void a(){ ... }
// ():void fptr = a;
// u64 b = 1;
// u64 bb = 2;
// &u64 c = ${irgendeinmacrowaszurcompilezeiteinevoonmehrerenmoeglichenvariablenreferenciert()};
// u64 d = *c;
static void ptlang_verify_decl_header(ptlang_ast_decl decl, size_t scope_offset, ptlang_context *ctx,
ptlang_error **errors)
{

Expand Down Expand Up @@ -972,24 +985,7 @@ static void ptlang_verify_exp(ptlang_ast_exp exp, ptlang_context *ctx, ptlang_er
condition, ".", ctx->type_scope));
}

// TODO use ptlang_verify_check_type_unification instead
if (ptlang_verify_implicit_cast(if_value->ast_type, else_value->ast_type, ctx))
{
exp->ast_type = ptlang_context_copy_unname_type(else_value->ast_type, ctx->type_scope);
}
if (ptlang_verify_implicit_cast(else_value->ast_type, if_value->ast_type, ctx))
{
exp->ast_type = ptlang_context_copy_unname_type(if_value->ast_type, ctx->type_scope);
}
else
{
char *message = NULL;
ptlang_utils_build_str(message, CONST_STR("Couldn't unify the types "),
ptlang_verify_type_to_string(if_value->ast_type, ctx->type_scope),
CONST_STR(" and "),
ptlang_verify_type_to_string(else_value->ast_type, ctx->type_scope),
CONST_STR(" in ternary expression."))
}
exp->ast_type = ptlang_verify_unify_types(if_value->ast_type, else_value->ast_type, ctx);

break;
}
Expand Down Expand Up @@ -1484,8 +1480,8 @@ static char **pltang_verify_struct_get_referenced_types_from_struct_def(ptlang_a
// // return type_alias;
// }

static ptlang_ast_type ptlang_verify_check_type_unification(ptlang_ast_type type1, ptlang_ast_type type2,
ptlang_context *ctx, ptlang_error **errors)
static ptlang_ast_type ptlang_verify_unify_types(ptlang_ast_type type1, ptlang_ast_type type2,
ptlang_context *ctx)
{
type1 = ptlang_context_unname_type(type1, ctx->type_scope);
type2 = ptlang_context_unname_type(type2, ctx->type_scope);
Expand All @@ -1495,15 +1491,43 @@ static ptlang_ast_type ptlang_verify_check_type_unification(ptlang_ast_type type
if (type2 == NULL)
return ptlang_ast_type_copy(type1);

if (ptlang_context_type_equals(type1, type2, ctx->type_scope))
return type1;

if (type1->type == PTLANG_AST_TYPE_REFERENCE && type2->type == PTLANG_AST_TYPE_REFERENCE)
{
if (ptlang_context_type_equals(type1->content.reference.type, type2->content.reference.type,
ctx->type_scope))
{
return ptlang_ast_type_reference(type1->content.reference.type, type1->content.reference.writable &&type2->content.reference.writable, NULL);
return ptlang_ast_type_reference(
type1->content.reference.type,
type1->content.reference.writable && type2->content.reference.writable, NULL);
}
}
// TODO implement
else if (type1->type == PTLANG_AST_TYPE_FLOAT && type2->type == PTLANG_AST_TYPE_FLOAT)
{
return ptlang_ast_type_float(type1->content.float_size >= type2->content.float_size
? type1->content.float_size
: type2->content.float_size,
NULL);
}
else if (type1->type == PTLANG_AST_TYPE_FLOAT && type2->type == PTLANG_AST_TYPE_INTEGER)
{
return ptlang_ast_type_copy(type1);
}
else if (type1->type == PTLANG_AST_TYPE_INTEGER && type2->type == PTLANG_AST_TYPE_FLOAT)
{
return ptlang_ast_type_copy(type2);
}
else if (type1->type == PTLANG_AST_TYPE_INTEGER && type2->type == PTLANG_AST_TYPE_INTEGER)
{
return ptlang_ast_type_integer(type1->content.integer.is_signed || type2->content.integer.is_signed,
type1->content.integer.size > type2->content.integer.size
? type1->content.integer.size
: type2->content.integer.size,
NULL);
}
return ptlang_ast_type_void(NULL);
}

static bool ptlang_verify_implicit_cast(ptlang_ast_type from, ptlang_ast_type to, ptlang_context *ctx)
Expand All @@ -1519,9 +1543,7 @@ static bool ptlang_verify_implicit_cast(ptlang_ast_type from, ptlang_ast_type to
(to->type == PTLANG_AST_TYPE_FLOAT || to->type == PTLANG_AST_TYPE_INTEGER)))
{
if (to->type == PTLANG_AST_EXP_INTEGER)
return (to->content.integer.size >= from->content.integer.size &&
to->content.integer.is_signed == from->content.integer.is_signed) ||
(to->content.integer.size > from->content.integer.size && to->content.integer.is_signed);
return (to->content.integer.size >= from->content.integer.size);
return true;
}
if (to->type == PTLANG_AST_TYPE_REFERENCE && from->type == PTLANG_AST_TYPE_REFERENCE)
Expand Down
9 changes: 7 additions & 2 deletions src/ptlang_verify/ptlang_verify_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,13 @@ static char **pltang_verify_struct_get_referenced_types_from_struct_def(ptlang_a
ptlang_context *ctx,
ptlang_error **errors);

static ptlang_utils_str ptlang_verify_type_to_string(ptlang_ast_type type, ptlang_context_type_scope *type_scope);
static ptlang_utils_str ptlang_verify_type_to_string(ptlang_ast_type type,
ptlang_context_type_scope *type_scope);

static ptlang_error ptlang_verify_generate_type_error(char *before, ptlang_ast_exp exp, char *after, ptlang_context_type_scope *type_scope);
static ptlang_error ptlang_verify_generate_type_error(char *before, ptlang_ast_exp exp, char *after,
ptlang_context_type_scope *type_scope);

static ptlang_ast_type ptlang_verify_unify_types(ptlang_ast_type type1, ptlang_ast_type type2,
ptlang_context *ctx);

#endif

0 comments on commit d18ed5c

Please sign in to comment.