Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rpkak committed Nov 12, 2023
1 parent d18ed5c commit 0703f10
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/ptlang_main/ptlang_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ typedef enum ptlang_error_type
PTLANG_ERROR_INTEGER_SIZE,
PTLANG_ERROR_UNKNOWN_MEMBER,
PTLANG_ERROR_CAST_ILLEGAL,
PTLANG_ERROR_NON_CONST_GLOBAL_INITIATOR,
} ptlang_error_type;

#define ptlang_error_type_name(error_type) \
Expand All @@ -40,7 +41,8 @@ typedef enum ptlang_error_type
: error_type == PTLANG_ERROR_INTEGER_SIZE ? "Integer Size Error" \
: error_type == PTLANG_ERROR_UNKNOWN_MEMBER ? "Unknown Member Error" \
: error_type == PTLANG_ERROR_UNKNOWN_MEMBER ? "Unknown Member Error" \
: error_type == PTLANG_ERROR_CAST_ILLEGAL ? "Illegal Cast Error" \
: error_type == PTLANG_ERROR_CAST_ILLEGAL ? "Illegal Cast Error" \
: error_type == PTLANG_ERROR_NON_CONST_GLOBAL_INITIATOR ? "Non-Const Global Initiator Error" \
: "Unkown Error"

typedef struct ptlang_error_s
Expand Down
130 changes: 124 additions & 6 deletions src/ptlang_verify/ptlang_verify.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,16 @@ 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,
// ptlang_error **errors)
// {
// ptlang_verify_decl_header()
// }
static void ptlang_verify_decl(ptlang_ast_decl decl, size_t scope_offset, ptlang_context *ctx,
ptlang_error **errors)
{
ptlang_verify_decl_header(decl, scope_offset, ctx, errors);
if (decl->init != NULL)
{
ptlang_verify_exp(decl->init, ctx, errors);
ptlang_verify_check_implicit_cast(decl->init->ast_type, decl->type, decl->pos, ctx, errors);
}
}
// u64 2_HOCH_11 = ${ 2<<11 };
//
// void a(){ ... }
Expand All @@ -211,7 +216,7 @@ static void ptlang_verify_statement(ptlang_ast_stmt statement, uint64_t nesting_
// &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)
ptlang_error **errors)
{

bool correct = ptlang_verify_type(decl->type, ctx, errors);
Expand Down Expand Up @@ -1608,3 +1613,116 @@ static ptlang_error ptlang_verify_generate_type_error(char *before, ptlang_ast_e
ptlang_error error = (ptlang_error){.type = PTLANG_ERROR_TYPE, .pos = *exp->pos, .message = message};
return error;
}

static void ptlang_verify_exp_check_const(ptlang_ast_exp exp, ptlang_context *ctx, ptlang_error **errors)
{
switch (exp->type)
{
case PTLANG_AST_EXP_ASSIGNMENT:
{
arrpfut(*errors, ((ptlang_error){
.type = PTLANG_ERROR_NON_CONST_GLOBAL_INITIATOR,
.pos = *exp->pos,
.message = "Assignments may not be used in global initiators.",
}));
break;
}

case PTLANG_AST_EXP_ADDITION:
case PTLANG_AST_EXP_SUBTRACTION:
case PTLANG_AST_EXP_MULTIPLICATION:
case PTLANG_AST_EXP_DIVISION:
case PTLANG_AST_EXP_MODULO:
case PTLANG_AST_EXP_REMAINDER:
case PTLANG_AST_EXP_EQUAL:
case PTLANG_AST_EXP_NOT_EQUAL:
case PTLANG_AST_EXP_GREATER:
case PTLANG_AST_EXP_GREATER_EQUAL:
case PTLANG_AST_EXP_LESS:
case PTLANG_AST_EXP_LESS_EQUAL:
case PTLANG_AST_EXP_LEFT_SHIFT:
case PTLANG_AST_EXP_RIGHT_SHIFT:
case PTLANG_AST_EXP_AND:
case PTLANG_AST_EXP_OR:
case PTLANG_AST_EXP_BITWISE_AND:
case PTLANG_AST_EXP_BITWISE_OR:
case PTLANG_AST_EXP_BITWISE_XOR:
{
ptlang_verify_exp_check_const(exp->content.binary_operator.left_value, ctx, errors);
ptlang_verify_exp_check_const(exp->content.binary_operator.right_value, ctx, errors);
break;
}
case PTLANG_AST_EXP_NEGATION:
case PTLANG_AST_EXP_NOT:
case PTLANG_AST_EXP_BITWISE_INVERSE:
case PTLANG_AST_EXP_LENGTH:
case PTLANG_AST_EXP_DEREFERENCE:
{
ptlang_verify_exp_check_const(exp->content.unary_operator, ctx, errors);
break;
}
case PTLANG_AST_EXP_FUNCTION_CALL:
{
arrpfut(*errors, ((ptlang_error){
.type = PTLANG_ERROR_NON_CONST_GLOBAL_INITIATOR,
.pos = *exp->pos,
.message = "Function calls may not be used in global initiators.",
}));
break;
}
case PTLANG_AST_EXP_VARIABLE:
{
break;
}
case PTLANG_AST_EXP_INTEGER:
case PTLANG_AST_EXP_FLOAT:
{
break;
}
case PTLANG_AST_EXP_STRUCT:
{
for (size_t i = 0; i < arrlenu(exp->content.struct_.members); i++)
{
ptlang_verify_exp_check_const(exp->content.struct_.members[i].exp, ctx, errors);
}
break;
}
case PTLANG_AST_EXP_ARRAY:
{
for (size_t i = 0; i < arrlenu(exp->content.array.values); i++)
{
ptlang_verify_exp_check_const(exp->content.array.values[i], ctx, errors);
}
break;
}
case PTLANG_AST_EXP_TERNARY:
{

ptlang_verify_exp_check_const(exp->content.ternary_operator.condition, ctx, errors);
ptlang_verify_exp_check_const(exp->content.ternary_operator.if_value, ctx, errors);
ptlang_verify_exp_check_const(exp->content.ternary_operator.else_value, ctx, errors);
break;
}
case PTLANG_AST_EXP_CAST:
{
ptlang_verify_exp_check_const(exp->content.cast.value, ctx, errors);
break;
}
case PTLANG_AST_EXP_STRUCT_MEMBER:
{
ptlang_verify_exp_check_const(exp->content.struct_member.struct_, ctx, errors);
break;
}
case PTLANG_AST_EXP_ARRAY_ELEMENT:
{
ptlang_verify_exp_check_const(exp->content.array_element.array, ctx, errors);
ptlang_verify_exp_check_const(exp->content.array_element.index, ctx, errors);
break;
}
case PTLANG_AST_EXP_REFERENCE:
{
ptlang_verify_exp_check_const(exp->content.reference.value, ctx, errors);
break;
}
}
}
2 changes: 2 additions & 0 deletions src/ptlang_verify/ptlang_verify_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,6 @@ static ptlang_error ptlang_verify_generate_type_error(char *before, ptlang_ast_e
static ptlang_ast_type ptlang_verify_unify_types(ptlang_ast_type type1, ptlang_ast_type type2,
ptlang_context *ctx);

static void ptlang_verify_exp_check_const(ptlang_ast_exp exp, ptlang_context *ctx, ptlang_error **errors);

#endif

0 comments on commit 0703f10

Please sign in to comment.