Skip to content

Commit

Permalink
rc
Browse files Browse the repository at this point in the history
this is very very wip
  • Loading branch information
rpkak committed Mar 30, 2024
1 parent 34c55dd commit 6e1dd2a
Show file tree
Hide file tree
Showing 7 changed files with 1,080 additions and 940 deletions.
757 changes: 393 additions & 364 deletions src/ptlang_ast/ptlang_ast.c

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions src/ptlang_ast/ptlang_ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,28 +141,28 @@ ptlang_ast_stmt ptlang_ast_stmt_continue_new(uint64_t nesting_level, ptlang_ast_
// uint64_t ptlang_ast_module_get_type_alias_count(ptlang_ast_module module);
// void ptlang_ast_module_get_type_aliases(ptlang_ast_module module, char **names, ptlang_ast_type *types);

ptlang_ast_type ptlang_ast_type_copy(ptlang_ast_type type);
ptlang_ast_type *ptlang_ast_type_list_copy(ptlang_ast_type *type_list);

void ptlang_ast_type_destroy(ptlang_ast_type type);
void ptlang_ast_stmt_destroy(ptlang_ast_stmt stmt);
void ptlang_ast_module_destroy(ptlang_ast_module module);
void ptlang_ast_func_destroy(ptlang_ast_func func);
void ptlang_ast_exp_destroy(ptlang_ast_exp exp);
void ptlang_ast_decl_destroy(ptlang_ast_decl decl);
void ptlang_ast_struct_def_destroy(ptlang_ast_struct_def struct_def);
// ptlang_ast_type ptlang_ast_type_copy(ptlang_ast_type type);
// ptlang_ast_type *ptlang_ast_type_list_copy(ptlang_ast_type *type_list);

void ptlang_ast_type_destroy(struct ptlang_ast_type_s *type);
void ptlang_ast_stmt_destroy(struct ptlang_ast_stmt_s *stmt);
void ptlang_ast_module_destroy(struct ptlang_ast_module_s *module);
void ptlang_ast_func_destroy(struct ptlang_ast_func_s *func);
void ptlang_ast_exp_destroy(struct ptlang_ast_exp_s *exp);
void ptlang_ast_decl_destroy(struct ptlang_ast_decl_s *decl);
void ptlang_ast_struct_def_destroy(struct ptlang_ast_struct_def_s *struct_def);
void ptlang_ast_decl_list_destroy(ptlang_ast_decl *decl_list);
void ptlang_ast_type_list_destroy(ptlang_ast_type *type_list);
void ptlang_ast_exp_list_destroy(ptlang_ast_exp *exp_list);
void ptlang_ast_struct_member_list_destroy(ptlang_ast_struct_member_list member_list);

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);
// ptlang_ast_code_position ptlang_ast_code_position_copy(ptlang_ast_code_position pos);

ptlang_ast_exp ptlang_ast_exp_copy(ptlang_ast_exp exp);
// ptlang_ast_exp ptlang_ast_exp_copy(ptlang_ast_exp exp);

ptlang_ast_ident ptlang_ast_ident_copy(ptlang_ast_ident ident);
// ptlang_ast_ident ptlang_ast_ident_copy(ptlang_ast_ident ident);

ptlang_utils_str ptlang_ast_exp_to_string(ptlang_ast_exp exp);

Expand Down
27 changes: 19 additions & 8 deletions src/ptlang_ast/ptlang_ast_nodes.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#ifndef PTLANG_AST_NODES_H
#define PTLANG_AST_NODES_H

#include "ptlang_rc.h"
#include <stdbool.h>
#include <stdint.h>

typedef struct ptlang_ast_type_s *ptlang_ast_type;
typedef struct ptlang_ast_stmt_s *ptlang_ast_stmt;
typedef struct ptlang_ast_module_s *ptlang_ast_module;
typedef struct ptlang_ast_func_s *ptlang_ast_func;
typedef struct ptlang_ast_exp_s *ptlang_ast_exp;
typedef struct ptlang_ast_decl_s *ptlang_ast_decl;
typedef struct ptlang_ast_struct_def_s *ptlang_ast_struct_def;
PTLANG_RC_DEFINE_REF_TYPE_ONLY(ptlang_ast_type);
// typedef struct { size_t ref_count; struct ptlang_ast_type_s content; } **ptlang_ast_type;
PTLANG_RC_DEFINE_REF_TYPE_ONLY(ptlang_ast_stmt);
PTLANG_RC_DEFINE_REF_TYPE_ONLY(ptlang_ast_module);
PTLANG_RC_DEFINE_REF_TYPE_ONLY(ptlang_ast_func);
PTLANG_RC_DEFINE_REF_TYPE_ONLY(ptlang_ast_exp);
PTLANG_RC_DEFINE_REF_TYPE_ONLY(ptlang_ast_decl);
PTLANG_RC_DEFINE_REF_TYPE_ONLY(ptlang_ast_struct_def);
typedef struct ptlang_ast_struct_member_s *ptlang_ast_struct_member_list;

typedef struct ptlang_ast_code_position_s
Expand All @@ -21,7 +23,7 @@ typedef struct ptlang_ast_code_position_s
uint64_t to_column;
} ptlang_ast_code_position_s;

typedef struct ptlang_ast_code_position_s *ptlang_ast_code_position;
PTLANG_RC_DEFINE_REF_TYPE_ONLY(ptlang_ast_code_position);

typedef struct ptlang_ast_ident_s
{
Expand Down Expand Up @@ -300,4 +302,13 @@ struct ptlang_ast_func_s
ptlang_ast_code_position pos;
};


PTLANG_RC_DEFINE_REF_STRUCT(struct ptlang_ast_type_s, ptlang_ast_type);
PTLANG_RC_DEFINE_REF_STRUCT(struct ptlang_ast_stmt_s, ptlang_ast_stmt);
PTLANG_RC_DEFINE_REF_STRUCT(struct ptlang_ast_module_s, ptlang_ast_module);
PTLANG_RC_DEFINE_REF_STRUCT(struct ptlang_ast_func_s, ptlang_ast_func);
PTLANG_RC_DEFINE_REF_STRUCT(struct ptlang_ast_exp_s, ptlang_ast_exp);
PTLANG_RC_DEFINE_REF_STRUCT(struct ptlang_ast_decl_s, ptlang_ast_decl);
PTLANG_RC_DEFINE_REF_STRUCT(struct ptlang_ast_struct_def_s, ptlang_ast_struct_def);
PTLANG_RC_DEFINE_REF_STRUCT(ptlang_ast_code_position_s, ptlang_ast_code_position);
#endif
9 changes: 6 additions & 3 deletions src/ptlang_eval/ptlang_eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ ptlang_ast_exp ptlang_eval_const_exp(ptlang_ast_exp exp, ptlang_context *ctx)
// LLVMCreateJITCompilerForModule(&ee, M, )
LLVMCreateInterpreterForModule(&ee, M, NULL);

uint32_t bit_size = exp->ast_type->type == PTLANG_AST_TYPE_INTEGER ? exp->ast_type->content.integer.size
: exp->ast_type->content.float_size;
uint32_t bit_size = ptlang_rc_deref(ptlang_rc_deref(exp).ast_type).type == PTLANG_AST_TYPE_INTEGER
? ptlang_rc_deref(ptlang_rc_deref(exp).ast_type).content.integer.size
: ptlang_rc_deref(ptlang_rc_deref(exp).ast_type).content.float_size;

uint8_t *binary = ptlang_malloc((bit_size - 1) / 8 + 1);

Expand All @@ -56,7 +57,9 @@ uint32_t ptlang_eval_calc_byte_size(ptlang_ast_type type)
{
// uint32_t bit_size =
// type->type == PTLANG_AST_TYPE_INTEGER ? type->content.integer.size : type->content.float_size;
return (((type->type == PTLANG_AST_TYPE_INTEGER ? type->content.integer.size : type->content.float_size) -
return (((ptlang_rc_deref(type).type == PTLANG_AST_TYPE_INTEGER
? ptlang_rc_deref(type).content.integer.size
: ptlang_rc_deref(type).content.float_size) -
1) >>
3) +
1;
Expand Down
118 changes: 66 additions & 52 deletions src/ptlang_main/ptlang_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ void ptlang_context_destory(ptlang_context *ctx)
ptlang_ast_type ptlang_context_unname_type(ptlang_ast_type type, ptlang_context_type_scope *type_scope)
{

while (type != NULL && type->type == PTLANG_AST_TYPE_NAMED)
while (type != NULL && ptlang_rc_deref(type).type == PTLANG_AST_TYPE_NAMED)
{

ptlang_context_type_scope *kv_pair = shgetp_null(type_scope, type->content.name);
ptlang_context_type_scope *kv_pair = shgetp_null(type_scope, ptlang_rc_deref(type).content.name);
if (kv_pair == NULL)
return NULL;
if (kv_pair->value.type == PTLANG_CONTEXT_TYPE_SCOPE_ENTRY_STRUCT)
Expand All @@ -24,55 +24,60 @@ ptlang_ast_type ptlang_context_unname_type(ptlang_ast_type type, ptlang_context_
return type;
}

ptlang_ast_type ptlang_context_copy_unname_type(ptlang_ast_type type, ptlang_context_type_scope *type_scope)
{
return ptlang_ast_type_copy(ptlang_context_unname_type(type, type_scope));
}
// ptlang_ast_type ptlang_context_copy_unname_type(ptlang_ast_type type, ptlang_context_type_scope
// *type_scope)
// {
// return ptlang_ast_type_copy(ptlang_context_unname_type(type, type_scope));
// }

bool ptlang_context_type_equals(ptlang_ast_type type_1, ptlang_ast_type type_2,
ptlang_context_type_scope *type_scope)
{
type_1 = ptlang_context_unname_type(type_1, type_scope);
type_2 = ptlang_context_unname_type(type_2, type_scope);

if (type_1->type != type_2->type)
if (ptlang_rc_deref(type_1).type != ptlang_rc_deref(type_2).type)
return false;
switch (type_1->type)
switch (ptlang_rc_deref(type_1).type)
{
case PTLANG_AST_TYPE_VOID:
return true;
case PTLANG_AST_TYPE_INTEGER:
return type_1->content.integer.is_signed == type_2->content.integer.is_signed &&
type_1->content.integer.size == type_2->content.integer.size;
return ptlang_rc_deref(type_1).content.integer.is_signed ==
ptlang_rc_deref(type_2).content.integer.is_signed &&
ptlang_rc_deref(type_1).content.integer.size == ptlang_rc_deref(type_2).content.integer.size;
case PTLANG_AST_TYPE_FLOAT:
return type_1->content.float_size == type_2->content.float_size;
return ptlang_rc_deref(type_1).content.float_size == ptlang_rc_deref(type_2).content.float_size;
case PTLANG_AST_TYPE_FUNCTION:
if (!ptlang_context_type_equals(type_1->content.function.return_type,
type_2->content.function.return_type, type_scope))
if (!ptlang_context_type_equals(ptlang_rc_deref(type_1).content.function.return_type,
ptlang_rc_deref(type_2).content.function.return_type, type_scope))
return false;
if (arrlenu(type_1->content.function.parameters) != arrlenu(type_1->content.function.parameters))
if (arrlenu(ptlang_rc_deref(type_1).content.function.parameters) !=
arrlenu(ptlang_rc_deref(type_1).content.function.parameters))
return false;
for (size_t i = 0; i < arrlenu(type_1->content.function.parameters); i++)
for (size_t i = 0; i < arrlenu(ptlang_rc_deref(type_1).content.function.parameters); i++)
{
if (!ptlang_context_type_equals(type_1->content.function.parameters[i],
type_2->content.function.parameters[i], type_scope))
if (!ptlang_context_type_equals(ptlang_rc_deref(type_1).content.function.parameters[i],
ptlang_rc_deref(type_2).content.function.parameters[i],
type_scope))

return false;
}
return true;
case PTLANG_AST_TYPE_HEAP_ARRAY:
return ptlang_context_type_equals(type_1->content.heap_array.type, type_2->content.heap_array.type,
type_scope);
return ptlang_context_type_equals(ptlang_rc_deref(type_1).content.heap_array.type,
ptlang_rc_deref(type_2).content.heap_array.type, type_scope);
case PTLANG_AST_TYPE_ARRAY:
return ptlang_context_type_equals(type_1->content.array.type, type_2->content.array.type,
type_scope) &&
type_1->content.array.len == type_2->content.array.len;
return ptlang_context_type_equals(ptlang_rc_deref(type_1).content.array.type,
ptlang_rc_deref(type_2).content.array.type, type_scope) &&
ptlang_rc_deref(type_1).content.array.len == ptlang_rc_deref(type_2).content.array.len;
case PTLANG_AST_TYPE_REFERENCE:
return ptlang_context_type_equals(type_1->content.reference.type, type_2->content.reference.type,
type_scope) &&
type_1->content.reference.writable == type_2->content.reference.writable;
return ptlang_context_type_equals(ptlang_rc_deref(type_1).content.reference.type,
ptlang_rc_deref(type_2).content.reference.type, type_scope) &&
ptlang_rc_deref(type_1).content.reference.writable ==
ptlang_rc_deref(type_2).content.reference.writable;
case PTLANG_AST_TYPE_NAMED:
return 0 == strcmp(type_1->content.name, type_2->content.name);
return 0 == strcmp(ptlang_rc_deref(type_1).content.name, ptlang_rc_deref(type_2).content.name);
}
}

Expand All @@ -89,7 +94,7 @@ size_t ptlang_context_type_to_string(ptlang_ast_type type, char *out, ptlang_con

return size;
}
switch (type->type)
switch (ptlang_rc_deref(type).type)
{
case PTLANG_AST_TYPE_VOID:
size = sizeof("void");
Expand All @@ -106,8 +111,8 @@ size_t ptlang_context_type_to_string(ptlang_ast_type type, char *out, ptlang_con
size = sizeof("?8388607");
if (out != NULL)
{
snprintf(out, size, "%c%" PRIu32, type->content.integer.is_signed ? 's' : 'u',
type->content.integer.size);
snprintf(out, size, "%c%" PRIu32, ptlang_rc_deref(type).content.integer.is_signed ? 's' : 'u',
ptlang_rc_deref(type).content.integer.size);
}
break;
}
Expand All @@ -116,20 +121,22 @@ size_t ptlang_context_type_to_string(ptlang_ast_type type, char *out, ptlang_con
size = sizeof("f128");
if (out != NULL)
{
snprintf(out, size, "f%d", type->content.float_size);
snprintf(out, size, "f%d", ptlang_rc_deref(type).content.float_size);
}
break;
}
case PTLANG_AST_TYPE_FUNCTION:
{
size = sizeof("(): ") - 1 +
ptlang_context_type_to_string(type->content.function.return_type, NULL, type_scope);
for (size_t i = 0; i < arrlenu(type->content.function.parameters); i++)
ptlang_context_type_to_string(ptlang_rc_deref(type).content.function.return_type, NULL,
type_scope);
for (size_t i = 0; i < arrlenu(ptlang_rc_deref(type).content.function.parameters); i++)
{
size += ptlang_context_type_to_string(type->content.function.parameters[i], NULL, type_scope) -
size += ptlang_context_type_to_string(ptlang_rc_deref(type).content.function.parameters[i], NULL,
type_scope) -
1 + sizeof(", ") - 1;
}
if (arrlenu(type->content.function.parameters) != 0)
if (arrlenu(ptlang_rc_deref(type).content.function.parameters) != 0)
{
size -= sizeof(", ") - 1;
}
Expand All @@ -138,11 +145,12 @@ size_t ptlang_context_type_to_string(ptlang_ast_type type, char *out, ptlang_con
{
*out = '(';
out++;
for (size_t i = 0; i < arrlenu(type->content.function.parameters); i++)
for (size_t i = 0; i < arrlenu(ptlang_rc_deref(type).content.function.parameters); i++)
{
out +=
ptlang_context_type_to_string(type->content.function.parameters[i], out, type_scope) - 1;
if (i < arrlenu(type->content.function.parameters) - 1)
out += ptlang_context_type_to_string(ptlang_rc_deref(type).content.function.parameters[i],
out, type_scope) -
1;
if (i < arrlenu(ptlang_rc_deref(type).content.function.parameters) - 1)
{
*out = ',';
out++;
Expand All @@ -153,54 +161,60 @@ size_t ptlang_context_type_to_string(ptlang_ast_type type, char *out, ptlang_con
memcpy(out, "): ", sizeof("): ") - 1);
out += sizeof("): ") - 1;

ptlang_context_type_to_string(type->content.function.return_type, out, type_scope);
ptlang_context_type_to_string(ptlang_rc_deref(type).content.function.return_type, out,
type_scope);
}

break;
}
case PTLANG_AST_TYPE_HEAP_ARRAY:
{

size = ptlang_context_type_to_string(type->content.array.type, out, type_scope) + sizeof("[]") - 1;
size = ptlang_context_type_to_string(ptlang_rc_deref(type).content.array.type, out, type_scope) +
sizeof("[]") - 1;

if (out != NULL)
{
memcpy(out, "[]", sizeof("[]") - 1);
ptlang_context_type_to_string(type->content.array.type, out + sizeof("[]") - 1, type_scope);
ptlang_context_type_to_string(ptlang_rc_deref(type).content.array.type, out + sizeof("[]") - 1,
type_scope);
}
break;
}
case PTLANG_AST_TYPE_ARRAY:
{
size_t element_size = ptlang_context_type_to_string(type->content.array.type, out, type_scope);
size_t element_size =
ptlang_context_type_to_string(ptlang_rc_deref(type).content.array.type, out, type_scope);

size = element_size + sizeof("[18446744073709551615]") - 1;

if (out != NULL)
{
size_t prefix_len = snprintf(out, size - element_size, "[%" PRIu64 "]", type->content.array.len);
ptlang_context_type_to_string(type->content.array.type, out + prefix_len, type_scope);
size_t prefix_len =
snprintf(out, size - element_size, "[%" PRIu64 "]", ptlang_rc_deref(type).content.array.len);
ptlang_context_type_to_string(ptlang_rc_deref(type).content.array.type, out + prefix_len,
type_scope);
}
break;
}
case PTLANG_AST_TYPE_REFERENCE:
{
size =
ptlang_context_type_to_string(type->content.reference.type, NULL, type_scope) + sizeof("&") - 1;
if (!type->content.reference.writable)
size = ptlang_context_type_to_string(ptlang_rc_deref(type).content.reference.type, NULL, type_scope) +
sizeof("&") - 1;
if (!ptlang_rc_deref(type).content.reference.writable)
{
size += sizeof("const ") - 1;
}
if (out != NULL)
{
*out = '&';
out++;
if (!type->content.reference.writable)
if (!ptlang_rc_deref(type).content.reference.writable)
{
memcpy(out + 1, "const ", sizeof("const ") - 1);
out += sizeof("const ") - 1;
}
ptlang_context_type_to_string(type->content.reference.type, out, type_scope);
ptlang_context_type_to_string(ptlang_rc_deref(type).content.reference.type, out, type_scope);
}

break;
Expand All @@ -210,10 +224,10 @@ size_t ptlang_context_type_to_string(ptlang_ast_type type, char *out, ptlang_con
ptlang_ast_type unnamed_type = ptlang_context_unname_type(type, type_scope);
if (unnamed_type == type)
{
size = strlen(type->content.name) + 1;
size = strlen(ptlang_rc_deref(type).content.name) + 1;
if (out != NULL)
{
memcpy(out, type->content.name, size);
memcpy(out, ptlang_rc_deref(type).content.name, size);
}
}
else
Expand All @@ -238,7 +252,7 @@ ptlang_ast_struct_def ptlang_context_get_struct_def(char *name, ptlang_context_t
struct_def = entry.value.struct_def;
break;
}
name = entry.value.ptlang_type->content.name;
name = ptlang_rc_deref(entry.value.ptlang_type).content.name;
}
return struct_def;
}
Loading

0 comments on commit 6e1dd2a

Please sign in to comment.